Asterisk log monitor with email notifications

Hi all. I didn’t see a way to send alerts like this, so I did a little research and wrote an ‘amateur’ script to send out alerts for me. I’m sure this could be much improved, but it serves my purposes right now. Basically, it just sends out info from the log, and could be modified to trigger on anything you can grep from any log. I stuck this in my crontab (run crontab -e) to get it running every minute (without sending mail to root):

* * * * * /usr/bin/unreachable.sh > /dev/null 2>&1

Hopefully my efforts can help someone out, or spur some community development. This could use some betterment; like right now it sends nightly blank emails when the log rolls over, the email itself lacks formatted content so I could modify the ‘mail’ statement and make the output better, etc.

#!/bin/bash
touch /tmp/newunreachable
touch /tmp/oldunreachable
cat /var/log/asterisk/full | grep  "is now"  > /tmp/newunreachable

send=$(diff /tmp/newunreachable /tmp/oldunreachable)
if [ -n "$send" ]
then
        cat /tmp/newunreachable | mail -s "Connected peers changed" -r asterisk [email protected]
        logger "Found connected peer changes."
fi
\cp /tmp/newunreachable /tmp/oldunreachable

MIT License

Copyright © 2016 Michael Cramer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.