}
# check if there is at least one argument
-if [ -z $1 ]
- then echo "Missing arguments"
- echo "try \'$0 --help\' for help"
+if [ -z "$1" ]; then
+ echo "Missing arguments"
+ echo "try '$0 --help' for help"
exit 3
fi
# print help
-if [[ ( $1 = "--help" || $1 = "-h" ) ]]
- then print_help
- exit 3
+if [[ ( $1 = "--help" || $1 = "-h" ) ]]; then
+ print_help
+# exit 3
fi
# assign value to arguments
c ) critical=$OPTARG ;;
p ) proc=$OPTARG ;;
* ) echo "Unknown argument"
- echo "try \'$0 --help\' for help"
+ echo "try '$0 --help' for help"
exit 3 ;;
esac
done
# check if all arguments are present
-if [[ ( -z $warning || -z $critical || -z $proc ) ]]
+if [[ ( -z "$warning" || -z "$critical" || -z "$proc" ) ]]
then echo "Missing argument"
- echo "try \'$0 --help\' for help"
+ echo "try '$0 --help' for help"
exit 3
fi
#calculate number of process
-nbproc=$(ps -A -o args | grep -w "$proc" | grep -v $0 | grep -v grep | wc -l)
-if [ $nbproc -gt 0 ]
+nbproc=$(pgrep --full --list-full "$proc" | grep -c -v "$0")
+if [ "$nbproc" -gt 0 ]
then
#calculate age of oldest process
- ageproc=$(ps -A -o etime,comm,args | grep "$proc" | grep -v $0 | grep -v grep | gawk '{split($1,t,":");split(t[1],td,"-");if (td[2]) {ta=td[1]*86400; t[1]=td[2]} else {ta=0}; if (t[3]) {$1=(t[1]*60+t[2])*60+t[3]+ta} else {$1=t[1]*60+t[2]};if (NR==1) {maxi=$1;} else {if ($1>maxi){maxi=$1;}}};END {print maxi}')
+# shellcheck disable=SC2009
+ ageproc=$(ps -A -o etime,comm,args | grep "$proc" | grep -v "$0" | grep -v grep | gawk '{split($1,t,":");split(t[1],td,"-");if (td[2]) {ta=td[1]*86400; t[1]=td[2]} else {ta=0}; if (t[3]) {$1=(t[1]*60+t[2])*60+t[3]+ta} else {$1=t[1]*60+t[2]};if (NR==1) {maxi=$1;} else {if ($1>maxi){maxi=$1;}}};END {print maxi}')
case $ageproc in
?|[0-5]? ) maxage=$ageproc" Seconds";;
- ??|???|[0-2]???|3[0-5]?? ) maxage=$(($ageproc/60))" Minutes";;
- * ) maxage=$(($ageproc/3600))" Hours "$(($ageproc % 3600 / 60))" minutes";;
+ ??|???|[0-2]???|3[0-5]?? ) maxage=$((ageproc/60))" Minutes";;
+ * ) maxage=$((ageproc/3600))" Hours "$((ageproc % 3600 / 60))" minutes";;
esac
- msg="there are $nbproc process $proc, oldest has got $maxage age"
- perfmaxage=$(($ageproc/60))
- perfdata="Processes=${nbproc:-0} MaxAge=${perfmaxage:-0}Minutes;$(($warning/60));$(($critical/60));0;"
- if [ $ageproc -gt $critical ]
+ msg="there are $nbproc process $proc, oldest is $maxage old"
+ perfmaxage=$((ageproc/60))
+ perfdata="Processes=${nbproc:-0} MaxAge=${perfmaxage:-0}Minutes;$((warning/60));$((critical/60));0;"
+ if [ "$ageproc" -gt "$critical" ]
then echo "CRITICAL: $msg | $perfdata"
exit 2
- elif [ $ageproc -gt $warning ]
+ elif [ "$ageproc" -gt "$warning" ]
then echo "WARNING: $msg | $perfdata"
exit 1
else echo "OK: $msg | $perfdata"