#!/usr/bin/perl use strict; use File::Temp qw/tempfile/; my $lastline = qx(/usr/bin/tail -n 1 /var/log/asterisk/messages); chomp($lastline); if( ( $lastline =~ /chan_iax2.c: Max retries exceeded to host/g ) || ( $lastline =~ /chan_echolink.c: Cannot find DB entry for Callsign/g ) ){ ( my $lts = $lastline ) =~ s/^\[([A-Za-z0-9\s\:]+)\] [NW].*/$1/g; # sadly perl(Date:Time) not on these so we'll do it the hacky way my $now = qx(/usr/bin/date "+%b %d %H:%M:%S"); chomp($now); ( my $logdate = $lts ) =~ s/^([A-Za-z]+\s\s?[0-9]+)\s[0-9].*$/$1/; $logdate =~ s/\s\s/ 0/g; ( my $nowdate = $now ) =~ s/^([A-Za-z]+\s[0-9]+)\s[0-9].*$/$1/; # If the log entries aren't on the same day, bail out if( $logdate ne $nowdate ){ exit 0; } # turn everything into seconds ( my $logtime = $lts ) =~ s/^.*\s(\d\d\:\d\d\:\d\d)$/$1/; ( my $nowtime = $now ) =~ s/^.*\s(\d\d\:\d\d\:\d\d)$/$1/; ( my $lh, my $lm, my $ls ) = split(/:/, $logtime); ( my $nh, my $nm, my $ns ) = split(/:/, $nowtime); $lh *= 3600; $nh *= 3600; $lm *= 60; $nm *= 60; if( ( ($nh + $nm + $ns) - ( $lh + $lm + $ls ) ) < 300 ){ # The init script for asterisk is really weak. Find # all of the pids that are child pids of the # safe_asterisk process which is launched as a nohup # process rooted in /bin/sh. The astdn.sh script # just does not work right at all to kill stuck children my $sapid = qx(/usr/bin/pgrep safe_asterisk); # be maximally safe and don't do anything if there is # no safe_asterisk process if( length($sapid) > 0 ){ my $apids = qx(/usr/bin/pstree -p $sapid); $apids =~ s/[^\d]/ /g; $apids =~ s/\s+/ /g; system("/usr/bin/kill -9 $apids > /dev/null 2>&1"); sleep 1; unlink "/var/run/asterisk.ctl"; unlink "/var/run/asterisk.pid"; exec("/usr/local/sbin/astup.sh > /dev/null 2>&1"); } my $msg; $msg .= "From: Asterisk \n"; $msg .= "To: zabbix\@megalink.network\n"; $msg .= "Subject: Restarted Asterisk on pi-echo39\n"; $msg .= "\n"; $msg .= "This is the iax_monitor program on pi-echo39.\n"; $msg .= "\n"; $msg .= "Asterisk was cycled at " . qx(/usr/bin/date) . " due to the IAX Max Retries bug.\n"; $msg .= "\n"; $msg .= "Last Log:\n"; $msg .= " " . $lastline . "\n"; $msg .= "\n\n"; $msg .= "Be well, John Spartan!\n"; (my $ofh , my $outfile ) = tempfile( UNLINK => 1 ); print $ofh $msg; system( sprintf("/usr/bin/cat %s | /usr/bin/ssmtp -oi -oem %s", $outfile, "zabbix\@megalink.network") ); } } exit 0;