#!/usr/bin/perl -w # Authors : # GRNET Real Time Services Team # 2004: Dimitrios Vardalis (original implementation using simph323) (email: dvardali@ccf.auth.gr) # 2005-2006: Giorgos Kissandrakis (switched to ohphone use) # 2006: Dimitrios Vardalis (minor improvements) #use strict; use Getopt::Std; use vars qw($opt_p $opt_g $opt_u $opt_n $opt_t $opt_h %ERRORS); use vars qw($listenport $username $gatekeeper $number $timeout $output $attempts $pid); $ERRORS{'OK'} = 0; $ERRORS{'WARNING'} = 1; $ERRORS{'CRITICAL'}= 2; $ERRORS{'UNKNOWN'} = 3; use lib '/usr/local/libexec/nagios/' ; use FileHandle; use IPC::Open2; my $PROGNAME = 'check_voip' ; my $lastline; sub print_help (); sub print_usage (); sub help (); sub version (); sub kill_process (); $SIG{"ALRM"} = sub { close(Reader); if (defined(Writer)) { print Writer "X\n"; } close(Writer); waitpid ($pid, 0); print DEBUG "\n\n".localtime()." Timed out calling number [$number].\n". "Output follows.\nSTART\n".$output."\nEND\n"; close DEBUG; $lastline =~ s/\n//g; print "VOIP UNKNOWN - Timed out trying to call after $attempts attempts and $timeout sec. ". " Last line: $lastline\n"; exit $ERRORS{'UNKNOWN'}; }; unless(getopts('g:u:n:t:h:p:')) { print_usage(); exit $ERRORS{'UNKNOWN'}; } if ($opt_h){ print_usage(); exit $ERRORS{'OK'}; } if(!($opt_n )){ print_usage(); exit $ERRORS{'UNKNOWN'}; } if ($opt_g){ $gatekeeper = $opt_g; } else { print_usage(); exit $ERRORS{'UNKNOWN'}; } if ($opt_u){ $username = $opt_u; } else { print_usage(); exit $ERRORS{'UNKNOWN'}; } $timeout = 45; if ($opt_p) { $listenport = " --listenport $opt_p "; } $number = $opt_n; if ($opt_t) { $timeout = $opt_t; } my $test_prog = '/usr/local/bin/ohphone'; my $test_args = " -g $gatekeeper -u $username -s loopback $listenport $number"; my $test_cmd = $test_prog.$test_args; ###print "$test_cmd\n"; my $debug_file = "/var/tmp/check_voip.log"; unless (open DEBUG, ">> $debug_file ") { print "VOIP WARNING - Couldn't open debug file [$debug_file] for append\n"; exit $ERRORS{"WARNING"}; } alarm($timeout); while (1) { $attempts++; my $status = try_check(); if (!$status) { # print "Sleeping\n"; sleep 3; } } sub try_check { $pid = open2(*Reader,*Writer,$test_cmd); Writer->autoflush(); $output = ''; while () { $output .= $_; $lastline = $_ ; if ($_ =~ /(.*error registering with gatekeeper.*)/) { print "VOIP UNKNOWN - Unable to register with gatekeeper and complete test.\n"; print DEBUG "\n\n".localtime()." Unable to register with gatekeeper [$number].\n". "Output follows.\nSTART\n".$output."\nEND\n"; close DEBUG; close(Reader); print Writer "X\n"; close(Writer); waitpid ($pid, 0); exit $ERRORS{"UNKNOWN"}; } if ($_ =~ /Ringing phone for "(.*)"/ || $_ =~ /.*"(.*)" was busy.*/ || $_ =~ /.*Call with "(.*)" established.*/ ) { print "VOIP OK - Successfully dialed number [$number] at ip [$1]\n"; close DEBUG; close(Reader); print Writer "X\n"; close(Writer); waitpid ($pid, 0); exit $ERRORS{"OK"}; } if ($_ =~ /(.*Could not open H.323 listener port.*)/ ) { close(Reader); print Writer "X\n"; close(Writer); waitpid ($pid, 0); return 0; } if ( $_ =~ /.*Call with "(.*)" completed.*/ || $_ =~ /(.*Call with ".*" ended abnormally.*)/ ) { print "VOIP CRITICAL - Unable to make call to $number\n"; print DEBUG "\n\n".localtime()." Could not call number [$number].\n". "Output follows.\nSTART\n".$output."\nEND\n"; close DEBUG; close(Reader); print Writer "X\n"; close(Writer); waitpid ($pid, 0); exit $ERRORS{"CRITICAL"}; } } print "VOIP UNKNOWN - Unable to handle ohphone output.\n"; print DEBUG "\n\n".localtime()." Software error for number [$number].\n". "Output follows.\nSTART\n".$output."\nEND\n"; close DEBUG; close(Reader); print Writer "X\n"; close(Writer); waitpid ($pid, 0); exit $ERRORS{"WARNING"}; } sub print_usage () { print "Usage: $PROGNAME -n -g -u ". " -t -p \n"; } sub print_help () { print_usage(); print ' -n, Phone number to call -g, Gatekeeper to register to -u, Username -t, Timeout '; } sub help () { print_help(); exit $ERRORS{'OK'}; }