Home > OS >  Avoid mass e-mail notification in error analysis bash script
Avoid mass e-mail notification in error analysis bash script

Time:12-30

I am selecting error log details from a docker container and decide within a shell script, how and when to alert about the issue by discord and/or email.

Because I am receiving the email alerts too often with the same information in the email body, I want to implement the following two adjustments:

Fatal error log selection:

FATS="$(docker logs --since 24h $NODENAME 2>&1 | grep 'FATAL' | grep -v 'INFO')"

Email sent, in case FATS has some content:

swaks --from "$MAILFROM" --to "$MAILTO" --server "$MAILSERVER" --auth LOGIN --auth-user "$MAILUSER" --auth-password "$MAILPASS" --h-Subject "FATAL ERRORS FOUND" --body "$FATS" --silent "1"

How can I send the email only in the case, FATS has another content than the previous run of the script? I have thought about a hash about its content, which is stored and read in a text file. If the hash is the same than the previous script run, the email will be skipped.

Another option could be a local, temporary variable in the global user's bash profile, so that there is no file to be stored on the file system (to avoid read / writes).

How can I do that?

CodePudding user response:

When you are writing a script for your monitoring, add functions for additional functionality, like:

  • logging all the alerts that have been send
  • make sure you don't send more than 1 alert each hour
  • consider sending warnings only during working hours
  • escalate a message when it fails N times without intermediate success
  • possible send an alert to different receivers (different email adresses or also to sms or teams)
  • make an interface for an operator so he can look back when something went wrong the first time.

When you have control which messages you send, it is easy to filter duplicate meassages (after changing --since).

  • Related