Print if found non-desired result

I have a result like this

root@server [~]# grep -rl maldet /etc/cron*
/etc/cron.d/maldet_daily
/etc/cron.d/malcron
/etc/cron.d/malcrondaily
/etc/cron.d/malcronweekly

What I need is, I need an if/else condition such that, if there is any output other than /etc/cron.d/maldet_daily in the grep command, an "else" condition would say "critical: more than one maldet cron found: /etc/cron.d/malcron -- /etc/cron.d/malcrondaily -- /etc/cron.d/malcronweekly " If only /etc/cron.d/maldet_daily is there in output, it should be "OK:"

Hello anil510,

Could you please try following and let me know if this helps.(Not tested though)

grep -rl maldet /etc/cron* | awk '{split($0, X, " ");}END{if(length($0)== 1){print "OK"} else {print "CRITICAL"}}'

Thanks,
R. Singh

R singh, Sorry, Its not working

Hello anil510,

Could you please try following and let me know if this helps.

cat check_cron.ksh
A=`grep -rl  maldet /etc/cron*`
echo $A | awk '{split($0, X, " ");}END{if(length(X)== 1){print "OK"} else if(length(X)==0){print "NO results found."} else {print "CRITICAL"}}'

This should work.

Thanks,
R. Singh

1 Like

Almost fine it seems. But not the real result.

root@server [~]#  grep -rl  maldet /etc/cron*
/etc/cron.d/maldetweekly
/etc/cron.d/maldet_daily
/etc/cron.d/malcron
root@server [~]# A=`grep -rl  maldet /etc/cron*` ; echo $A | awk '{split($0, X, " ");}END{if(length(X)== 1){print "OK"} else if(length(X)==0){print "NO results found."} else {print X[1] "CRITICAL"}}'
/etc/cron.d/maldetweeklyCRITICAL

Hello anil510,

Could you please try this and let me know this helps.(Not tested though)

A=`grep -rl  maldet /etc/cron*`
echo $A | awk '{split($0, X, " ");}END{if(length(X)== 1){print "OK"} else if(length(X)==0){print "NO results found."} else {print "CRITICAL\nResults found are:";for(i in X){print X}}}'

I have mentioned as Results found are: you can remove this if it doesn't require.

Thanks,
R. Singh

I have found the solution.

multimalcron=`grep -rl  maldet /etc/cron* | grep -v maldet_daily`

if [ "$(echo $multimalcron)" ]; then
                echo " Multiple maldet cron found: echo $multimalcron"
fi

Hello anil510,

As per your requirement in Post#1, you have asked that you need to print ok when it is only single entry in cron , the code which is provided by you will not satisfy that condition, also I have added a condition if no entry found for maldet then it will show that. Could you please try code which I provided you in Post#6, let me know if that helps you.

Thanks,
R. Singh

The following seems to provide something closer to the spirit of what was requested in the original post in this thread (and, except for the one call to grep , only uses shell built-ins):

list="$(grep -rl 'maldet' /etc/cron*)"
if [ "$list" = "/etc/cron.d/maldet_daily" ]
then	echo 'OK:'
else	if [ "$list" = '' ]
	then	echo 'critical: maldet not found:'
	else	printf 'critical: maldet cron found in the following file(s): '
		last=''
		echo "$list" | while read line
		do	if [ "$last" != '' ]
			then	printf '%s -- ' "$last"
			fi
			last="$line"
		done
		printf '%s\n' "$last"
	fi
fi