summaryrefslogtreecommitdiff
path: root/allstar/allstar-monitor
blob: 27f34d7f6043fe957a1ea871c322ded393751bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/perl

use strict;

use File::Temp qw/tempfile/;
use Sys::Hostname;

my $host = hostname;

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 )
	){

	( 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";
			system("/usr/local/sbin/astup.sh > /dev/null 2>&1");
		}

		my $msg;
		$msg .= "From: Asterisk <root\@megalink.network>\n";
		$msg .= "To: zabbix\@megalink.network\n";
		$msg .= "Subject: Restarted Asterisk on $host\n";
		$msg .= "\n";
		$msg .= "This is the allstar-monitor program on $host.\n";
		$msg .= "\n";
		$msg .= "Asterisk was cycled at " . qx(/usr/bin/date) . " due to a hang condition.\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;