1

I'm working in a Linux-based environment where there are more than 100 servers. I need to get a mail notification and popup notification whenever anyone connects a storage device. How do I configure that?

karel
  • 122,292
  • 133
  • 301
  • 332

1 Answers1

0

The simplest way to do this is to monitor for the word 'mount' in /var/log/syslog on a cronjob. This sample would run every 10 minutes.

#!/bin/bash
# Gets the time and date 1 minute ago (00:00 would return 23:59 previous day)
TIME=`date +"%b %d %H:%M" --date '-1 min'`
# Truncate the minutes
TIME=${TIME::-1}

# Gets the past 10 minutes of logs and looks for 'mount'
MOUNT=`grep "$TIME" /var/log/syslog | grep mount`

if [ {{ '${#MOUNT}' }} -ge 1 ]; then
    echo -e "$MOUNT" | sendemail -t {{ email_address }} -f {{ from_address }} -u "$(hostname) Mount Detected" -s {{ smtp_server }} > /dev/null
fi

Make sure the date format matches what is in your logs. You can use whatever sendmail command you wish. If you wanted once a minute just remove the time truncate line. If you wanted immediate, you'd need to monitor udisksctl monitor for output. Once the email is sent, you can use any email notification for your popup.

rtaft
  • 1,920