Question

I would like to setup an alert that activates and sends an email if one of the numbers on the left side of this output is higher than 25.

The output I current receive from my bash script:

  3 00:05:00:E7:5A:EA
  3 00:0C:85:F2:F8:4E
  3 00:11:92:21:87:14
  3 00:17:C5:69:49:A1
  3 00:1A:E3:8C:E4:1A
  3 00:1D:A2:E7:BC:24
  3 00:26:98:14:91:05
  3 44:03:A7:C0:0D:26
 13 C0:62:6B:AE:6A:5D
 26 E8:B7:48:36:8C:AE

Above are the top 10 mac addresses with the most arp entries (Public IPs Only) from our cable plant.

For example: the bottom mac address is at 26 which should trigger and email alert that sends off to me with a subject of "Alert: Valid Info Here"

The extent of my bash scripting ability is basically groups of commands, so I'll need some real help with this one.

Was it helpful?

Solution

You can try the following script:

#!/bin/bash

msg=
count=0
while read line; do
    read freq mac <<< $line
    if (( $freq > 25 )) ; then 
        (( count ++ ))
        msg="$msg$line"$'\n'
    fi
done 
mail your@email.com -s "Alert: $count mac with more than 25 entries" <<< $msg

Redirect the output from your script into this script's standard input (typically with a pipe |) : your_script.sh | the_above_script.sh

Make sure to change the dummy email address. You will then receive an email with the line count in the subject and the full lines in the body.

Here is an example run where I inserted an echo just before the mail command

$ cat message
  3 00:05:00:E7:5A:EA
  3 00:0C:85:F2:F8:4E
  3 00:11:92:21:87:14
  3 00:17:C5:69:49:A1
  3 00:1A:E3:8C:E4:1A
  3 00:1D:A2:E7:BC:24
  3 00:26:98:14:91:05
  3 44:03:A7:C0:0D:26
 13 C0:62:6B:AE:6A:5D
 26 E8:B7:48:36:8C:AE
$ ./t.sh < message
mail your@email.com -s Alert: 1 mac with more than 25 entries <<< 26 E8:B7:48:36:8C:AE

If you can use awk, here is a shorter solution

< message.txt awk '{ if ($1>25) exit 1 }'  ||  mailx -s "Random Subject" myemail@mydomain.com < message.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top