#!/usr/bin/perl ## check_services version 0.27 ## check services on different hosts ## and mail info ## ## license change 2001 ## Copyright (c) 1999 Jeremy C. Reed. All Rights Reserved. ## This program is free software; you may redistribute it and/or modify ## it as long as you retain this entire copyright and notice. ## This software is provided as is and the author is not liable ## for anything resulting from its use. ## ## updated: Nov. 10, 1999 ## use the --verbose switch to see what's happening # @mail_list = ('firstperson@yahoo.com', 'anotherperson@hotmail.com'); @mail_list = ('your address on another machine'); # ignore is a router to be pinged just to see if I can get out! # if I can't get out, of course, the other stuff won't work. # @ignore = ('this machine's router's or gateway's ip'); @ignore = (); # %routers = ('ip.to.check', 'a machine name but not used except for reporting', # '192.168.1.13, 'bob's router', # '10.1.228.75', '75.bogus.GW1.SEA5.foo.bar'); %routers = ('192.168.1.12', 'some router', '10.1.228.29', 'some other router'); %ts = (); %dns = ('192.168.0.5', 'ns1.foo.bar', '10.1.228.99', 'another dns'); %http = ('192.168.99.100', 'web.foo.bar'); %mail = ('10.1.1.200', 'mail.foo.bar', '10.5.5.5', 'my secondary MX'); %news = (); $lynx = '/usr/bin/lynx'; $telnet = '/usr/bin/telnet'; $sleep = '/bin/sleep'; $ping = '/bin/ping'; $nslookup = '/usr/bin/nslookup'; $sendmail = '/usr/sbin/sendmail -t'; ## main if ($ARGV[0] eq '--verbose') { $verbose = '1'; } if (! &check_ignore) { $all_problems = ''; $all_problems .= &check_routers; $all_problems .= &check_terminal_servers; $all_problems .= &check_dns; $all_problems .= &check_http; $all_problems .= &check_mail; $all_problems .= &check_news; if ($all_problems) { &mail_problem; } } exit; sub check_ignore { foreach $ip (@ignore) { if ($verbose) { print "checking $ip\n"; } $result = `$ping -c 1 $ip 2>&1`; if ($result =~ /0 packets received/) { if ($verbose) { print "problem $ip\n"; } return 1; } } return 0; } sub check_routers { my $problem; foreach $ip (keys %routers) { if ($verbose) { print "ROUTER: $ip (@routers{$ip})\n"; } $result = `$ping -c 1 $ip 2>&1`; if ($result =~ /0 packets received/) { $problem .= "ROUTER: $ip (@routers{$ip}) appears to have a problem.\n"; if ($verbose) { print "problem $ip\n"; } } } return $problem; } sub check_terminal_servers { my $problem; foreach $ip (keys %ts) { if ($verbose) { print "TS: $ip (@ts{$ip})\n"; } $result = `$ping -c 1 $ip 2>&1`; if ($result =~ /0 packets received/) { $problem .= "TERMINAL SERVER: $ip (@ts{$ip}) appears to have a problem.\n"; if ($verbose) { print "problem $ip\n"; } } } return $problem; } sub check_dns { my $problem; foreach $ip (keys %dns) { if ($verbose) { print "DNS: $ip (@dns{$ip})\n"; } $result = `$nslookup $ip $ip 2>&1`; if ($result =~ /Can\'t find server name/) { $problem .= "DNS: $ip (@dns{$ip}) timed out (can\'t find server name).\n"; } elsif ($result =~ /can\'t find $ip: Non-existent host/) { $problem .= "DNS: $ip (@dns{$ip}) appears to have a problem.\n"; if ($verbose) { print "problem $ip\n"; } } } return $problem; } sub check_http { my $problem; foreach $ip (keys %http) { if ($verbose) { print "HTTP: $ip (@http{$ip})\n"; } $results = `$lynx '$ip' -source -term=vt100 2>&1`; if (($results =~ /404 Not Found/) || ($results =~ /Can\'t access startfile/)) { $problem .= "HTTP: $ip (@http{$ip}) appears to have a problem.\n"; if ($verbose) { print "problem $ip\n"; } } } return $problem; } sub check_mail { my $problem; foreach $ip (keys %mail) { if ($verbose) { print "MAIL: $ip (@mail{$ip})\n"; } $results = `(echo quit ; $sleep 3) | $telnet $ip 25 2>&1`; if (($results =~ /Connection refused/) || ($results =~ /Unknown host/) || ($results =~ /Unable to connect/) || ($results =~ /Connection timed out/)) { $problem .= "MAIL: $ip (@mail{$ip}) appears to have a problem.\n"; if ($verbose) { print "problem $ip\n"; } } } return $problem; } sub check_news { my $problem, $bad; foreach $ip (keys %news) { if ($verbose) { print "NEWS: $ip (@news{$ip})\n"; } $bad = ''; $results = `(echo quit ; $sleep 3) | $telnet $ip 119 2>&1`; if (($results =~ /Connection refused/) || ($results =~ /Unknown host/) || ($results =~ /Unable to connect/) || ($results =~ /Connection timed out/)) { $bad = 1; } elsif ($results !~ /205 GoodBye/) { $bad = 1; } if ($bad) { $problem .= "NEWS: $ip (@news{$ip}) appears to have a problem.\n"; if ($verbose) { print "problem $ip\n"; } } } return $problem; } sub mail_problem { foreach $email_address (@mail_list) { if ($verbose) { print "mailing to $email_address\n"; } open (SENDMAIL, "| $sendmail"); print SENDMAIL <