servicecheck.pl
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| servicecheck.pl [2006/11/13 12:27] – 206.161.192.11 | servicecheck.pl [2006/11/13 14:18] (current) – 62.92.124.145 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== servicecheck.pl ====== | ||
| + | |||
| + | This script checks for the availability of services by trying to connect to their according ports. Servers, ports and portdescription are read from a config file given as parameter. | ||
| + | |||
| + | <code perl> | ||
| + | # | ||
| + | use Socket; | ||
| + | |||
| + | if(scalar(@ARGV) < 1){ | ||
| + | help(); | ||
| + | exit; | ||
| + | } | ||
| + | |||
| + | my %CFG=readConfig($ARGV[0]); | ||
| + | my $FULL=0; | ||
| + | | ||
| + | |||
| + | my ($server, $services); | ||
| + | my ($service, | ||
| + | my ($iaddr, | ||
| + | foreach $server (sort(keys(%{$CFG{server}}))){ | ||
| + | $services = ${$CFG{server}}{$server}; | ||
| + | # resolve servernames | ||
| + | $dnsfail = 0; | ||
| + | $iaddr | ||
| + | if ($dnsfail) { | ||
| + | printf (" | ||
| + | next; | ||
| + | } | ||
| + | |||
| + | # check ports | ||
| + | foreach $service (@{$services}){ | ||
| + | ($port, | ||
| + | $name = trim($name); | ||
| + | $port =~ s/ | ||
| + | $prot = trim($1); | ||
| + | $port = trim($port); | ||
| + | $prot = ' | ||
| + | $connfail = 0; | ||
| + | $paddr | ||
| + | if($prot eq ' | ||
| + | socket(SOCK, | ||
| + | }else{ | ||
| + | socket(SOCK, | ||
| + | } | ||
| + | connect(SOCK, | ||
| + | close (SOCK); | ||
| + | |||
| + | if($connfail){ | ||
| + | printf (" | ||
| + | | ||
| + | }elsif($FULL){ | ||
| + | printf (" | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | sub help(){ | ||
| + | print << | ||
| + | Usage: servicecheck.pl configfile [full] | ||
| + | |||
| + | This script checks for the availability of services by trying to | ||
| + | connect to their according ports. | ||
| + | Servers, ports and portdescription are read from a config file. | ||
| + | |||
| + | If optional parameter full is given even successful connections | ||
| + | are displayed else only errors are shown. | ||
| + | | ||
| + | EOF | ||
| + | } | ||
| + | |||
| + | sub readConfig($){ | ||
| + | my $cfgfile = $_[0]; | ||
| + | open (FILE, | ||
| + | my @file = < | ||
| + | close (FILE); | ||
| + | |||
| + | my ($name, | ||
| + | my %config; | ||
| + | my $section=''; | ||
| + | my $sname=''; | ||
| + | |||
| + | foreach my $line (@file){ | ||
| + | $line=~ s/#.*$//; | ||
| + | $line=~ s/^\s*//; | ||
| + | $line=~ s/\s*$//; | ||
| + | next if ($line eq '' | ||
| + | if($line =~ m/< | ||
| + | $section = ''; | ||
| + | $sname = ''; | ||
| + | next; | ||
| + | } | ||
| + | if($line =~ m/< | ||
| + | $section = $1; | ||
| + | $sname = $2; | ||
| + | $sname = trim($sname); | ||
| + | if($sname eq '' | ||
| + | $config{lc($section)} = []; #prepare anonymous arrayref | ||
| + | }else{ | ||
| + | ${$config{lc($section)}}{$sname} = []; | ||
| + | } | ||
| + | next; | ||
| + | } | ||
| + | if($section && $sname eq '' | ||
| + | push(@{$config{lc($section)}}, | ||
| + | }elsif($section && $sname ne '' | ||
| + | push(@{${$config{lc($section)}}{$sname}}, | ||
| + | }else{ | ||
| + | ($name, | ||
| + | $val=~ s/^\s*//; | ||
| + | $name=~ s/\s*$//; | ||
| + | $config{lc($name)}=$val; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | return %config; | ||
| + | } | ||
| + | |||
| + | sub trim($){ | ||
| + | my $string = $_[0]; | ||
| + | $string =~s/^\s//g; | ||
| + | $string =~s/\s$//g; | ||
| + | return $string; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Here is an example configfile | ||
| + | |||
| + | < | ||
| + | <server imap.domain> | ||
| + | 143 = IMAP | ||
| + | 25 = SMTP | ||
| + | </ | ||
| + | |||
| + | <server fs.domain> | ||
| + | 389 = LDAP | ||
| + | 139 = Samba | ||
| + | 80 = Apache | ||
| + | | ||
| + | </ | ||
| + | |||
| + | <server printserver.domain> | ||
| + | 515 = LPD | ||
| + | # 139 = Samba | ||
| + | 6566 = Sane | ||
| + | 631 = Cups 631 | ||
| + | 80 = Cups 80 | ||
| + | </ | ||
| + | |||
| + | <server devel.domain> | ||
| + | 80 = Apache | ||
| + | </ | ||
| + | </ | ||
| + | |||
