[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 #!/usr/bin/perl 2 # 3 # Script takes as argument list of pairs host:service, ie 4 # ./check_service.pl host1:serwis1,host2:serwis2 5 # Returns: 6 # 0, if all pairs host:service has correct status 7 # 2, if any of pair host:service has wrong status (U/W/C) 8 # 3, if any of pair host:service was not found in status file 9 # 10 # Hostname can contain only letters/digits/_/- 11 # Service name can contain only letters/digits/_/-/ /. 12 # 13 # Version 1.0, 04.10.2006, Mariusz Preiss 14 # 15 # command in services.cfg looks like 16 # check_command check_services.pl!host1:PING,host2:FTP 17 # (warning confirmations has to be sent also) 18 # in checkcommand.cfg following section has to be added 19 # define command{ 20 # command_name check_services.pl 21 # command_line $USER1$/check_services.pl $ARG1$ 22 # } 23 # 24 25 26 # path to the status file 27 $stat_filename = "/usr/local/nagios/var/status.dat"; 28 29 # we read file with blocks 30 use English; 31 $INPUT_RECORD_SEPARATOR = "}"; 32 33 # lets check the status file 34 open DANE, "< $stat_filename" or die "Cannot access file $stat_filename\n"; 35 36 # status checking function of service on host, or host itself 37 # Status' (current_state) in status.dat file 38 # Serwisy 39 # 0 - OK 40 # 1 - Warning 41 # 2 - Critical 42 # 3 - Unknown 43 # 4 - Dependent 44 sub check_status($$) { 45 seek(DANE,0,0); 46 my ($host, $service) = @_; 47 48 while(<DANE>) { 49 # we identify block using hostname and service description 50 if($_ =~ /\sservice_description=$service\n/i and 51 $_ =~ /\shost_name=$host}\n/i and 52 $_ =~ /\scurrent_state=(\d+)\n/i) { 53 return $1; 54 } 55 } 56 return -1; 57 } 58 59 60 # if the argument list is empty, we can leave 61 my $args = join(' ',@ARGV); 62 if($args !~ /^([\d\w\-]+:[\d\w\.\-\s]+,?)+$/) { 63 die "Wrong arguments in check_service.pl script"; 64 } 65 66 # we are preparing variable, which will contain exit status 67 my $error = 0; 68 69 # we are preparing variables for loop below 70 my @args = split(',',join(' ',@ARGV)); 71 foreach(@args) { 72 if($_ =~ /([\d\w\-]+):([\d\w\.\-\s]+)/) { 73 my $host_service = "$1}:$2}"; 74 my $status = check_status($1,$2); 75 if($status > 0) { 76 $error = 2; 77 } 78 elsif($status == -1) { 79 # there is no such pair host:service 80 $error = 3; 81 print "Error - pair $host_service not found in status.dat file!\n"; 82 exit $error; 83 } 84 } 85 } 86 87 close DANE; 88 if($error > 0) { 89 print "Critical error!\n"; 90 } 91 else { 92 print "Status OK\n"; 93 } 94 exit $error;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |