[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: filter utility to send cron e-mail only if unexpected output



In <[🔎] 4A01AC7B.4010101@fgm.com>, Barclay, Daniel wrote:
>Does Debian have any utility to address the following situation?

Not that I know of.

>I have some scripts that I run both manually and as cron jobs.  The
> scripts generate stdout/stderr output reporting what they're doing.
>
>I want to see the output when I run the scripts manually.  However, when
> the scripts are run by cron, normally I don't want cron to e-mail me that
> bulky output for each run--I want the output (the full output) only when
> the output is different than expected (e.g., if something has gone wrong
> or has changed, which I want to notice).

For maximum utility, in particular use with cron, your scripts should follow 
two Unix philosophies: (1) If everything is going as expected, no output is 
required. (2) If the task was not completed, exit with a non-zero value.

If you follow the first, your script can run from cron with no problems and 
users can opt-in to extra output with a "-v" option or similar.  You might 
even be *really* nice and give a "-q" option, too.  Then you could even make 
the "-v" the default for interactive calls and "-q" the default for non-
interactive calls.

If you follow the second, you can always use something like so for even 
quieter cron jobs:
output=$(my_script and my_args 2>&1)
if [ $? -ne 0 ]; then
	printf >&2 '%s\n' "$output"
fi

So, my first advice is to fix your scripts so they act like Unix utilities.

>I started to write a wrapper script to take a command to execute and a
> file of regular expressions defining expected output lines (like
> logcheck's ignore files), to run the command and check against those
> regular expressions, and to either suppress the output if everything is
> as expected or emit all output if anything isn't as expected.
>
>Since Debian has logcheck with its exclusions for log-file checking, I
> thought maybe there's something with similar (regarding expected
> patterns) for cron job commands.

If you can't / won't fix your scripts, Here's a start on that wrapper:
#!/bin/bash
# Bashified on purpose, I'd love to see a version that worked well
# in dash or the POSIX shell, but array variables are too useful here.
known_bad_regexs=( 'ERROR' 'WARN' )
known_good_regexs=( 'LOG' 'INFO' )

output=$("$@" 2>&1)
retval=$?

for bre in "${known_bad_regexs[@]}"; do
	if grep -Eq -e "$bre" <<< "$output"; then
		printf '%s\n' >&2 "$output"
		exit $retval
	fi
done

possibly_bad=$output
for gre in "${known_good_regexs[@]"; do
	possibly_bad=$(grep -Ev -e "$gre" <<< "$possibly_bad")
done
if [ -n "$possibly_bad" ]; then
	printf '%s\n' >&2 "$output"
	exit $retval
fi
-- 
Boyd Stephen Smith Jr.           	 ,= ,-_-. =.
bss@iguanasuicide.net            	((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy 	 `-'(. .)`-'
http://iguanasuicide.net/        	     \_/

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: