#!/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 FileHandle; use IPC::Open2; use vars qw($opt_p $opt_g $opt_u $opt_t $opt_h %ERRORS); use vars qw($listenport $username $gatekeeper $timeout $output $attempts $pid); $ERRORS{'OK'} = 0; $ERRORS{'WARNING'} = 1; $ERRORS{'CRITICAL'}= 2; $ERRORS{'UNKNOWN'} = 3; use lib '/usr/local/libexec/nagios/' ; 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); $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'}; } $timeout = 45; if ($opt_p) { $listenport = $opt_p; } else { $listenport = 1720; } if ($opt_u) { $username = $opt_u; } else { print_usage(); exit $ERRORS{'UNKNOWN'}; } if ($opt_g) { $gatekeeper = $opt_g; } else { print_usage(); exit $ERRORS{'UNKNOWN'}; } if ($opt_t) { $timeout = $opt_t; } my $test_prog = '/usr/local/bin/ohphone'; my $test_args = " --gatekeeper $gatekeeper --user $username --listenport $listenport -l"; 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 ($_ =~ /Gatekeeper set: (.*)/) { print "VOIP OK - Successfully registered with gatekeeper $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 ($_ =~ /(.*Error registering with gatekeeper.*)/) { print "VOIP CRITICAL - Unable to register with gatekeeper at IP $gatekeeper.\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"; close DEBUG; close(Reader); print Writer "X\n"; close(Writer); waitpid ($pid, 0); exit $ERRORS{"UNKNOWN"}; } sub print_usage () { print "Usage: $PROGNAME -g -u ". " -t -p \n"; } sub print_help () { print_usage(); print ' -g, Gatekeeper to register to -u, Username -t, Timeout '; } sub help () { print_help(); exit $ERRORS{'OK'}; }