#!/usr/bin/perl # # Writen by Priyantha Kumara, Wightman Telecom # Clifford, ON Canada # This will replace the 'status.cgi' that Nagios uses by default for search hosts # This program searches hosts in the 'objects.cache' file. The search is not case sensitive. # # Version 2.0 # Dec 28, 2007 # All Rights Reserved # use CGI; $script_name = "search2.cgi"; $script_version = "2.0.5"; $query = CGI::new(); $h = $query->param("host"); $opt = $query->param("opt"); # Avoid inputing specail characters that would crash the program if ($h =~ /\`|\~|\@|\#|\$|\%|\^|\&|\*|\(|\)|\:|\=|\+|\"|\'|\;|\<|\>/){ $h = "Illegal Input"; } if ($h ne "Illegal Input") { $cache = '/usr/local/nagios/var/objects.cache'; $i = 0; if (!open (file1,$cache)){ print "Can't open file $cache: $!\n"; } else { SWITCH: { if ($opt eq "Host"){ while () { ($k,$c,$d) = split/\s/,$_; if ($c eq "host_name") { if ($d =~ /($h)/i){ # i indicates case insensitive match push (@list1, $d); } } } last SWITCH; } if ($opt eq "IP"){ while () { ($k,$c,$d) = split/\s/,$_; if ($c eq "host_name"){ $temp = $d; } if ($c eq "address"){ if ($d =~ /($h)/i){ push (@list1, $temp); } } } last SWITCH; } } } close(file1); $j =0; sub compare { my($item, @list) = @_; my ($match) = 0; foreach $list (@list) { if ($item eq $list){ $match = 1; last; } elsif ($item ne $list) { $match = 0; } } return $match; } $m = 1; @list2 = ( ); foreach $list1 (@list1){ $m = compare ($list1, @list2); if ($m == 0){ push (@list2, $list1); } } } print "Content-type: text/html\n\n"; print "\n"; print "Search Results\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "

Nagios Search Results for $opt: $h

"; print "
"; print "\n"; print "\n"; #print "\n"; print "\n"; if ($h ne "Illegal Input") { $n = scalar(@list2); foreach $list2 (@list2){ # print "\n"; print "\n"; } print "
\n"; # print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
View Extra Host Notes View Notifications for this host View Host status \n"; print "\n"; print "
$list2
\n"; print "
\n"; print "

$n Matching Host(s) Entries Found.

"; print "
\n"; } elsif ($h eq "Illegal Input") { print "\n"; print "

Please use ? to search All hosts

\n"; print "
\n"; } print "

The $script_name (version:$script_version) is developed by Priyantha Kumara, Wightman Telecom

\n"; print "\n";