[CentOS] configuration of nagios

thunderzep zgreenfelder at gmail.com
Wed Jul 3 12:58:24 UTC 2013


On 07/03/2013 08:14 AM, Scott Robbins wrote:
> On Wed, Jul 03, 2013 at 07:59:38AM -0400, mark wrote:
>> On 07/03/13 05:37, Earl A Ramirez wrote:
>>> On Jul 3, 2013 5:35 PM, "Rolly Aquino" <rolly at xend.com.ph> wrote:
>>>>
>>>>      can i request for configuration of CentOS nagios for moniotoring our
>>>> servers and switches
>>>>
>>  > What have you done or tried?  Did you check Google?
>>
>> Have you even read the documentation that comes with the package?
> 
> Nagios documentation, as I recall, is pretty poor.  Max had a great wiki
> article on it, but it's dated.  Still, if you look at the CentOS wiki
> article, enough of it may be the same for you to get started.
> 
> Once you understand the wiki article, you can probably then begin to work
> with the nagios documentation, which becomes less obscure once you've
> gotten a grasp of it.
> 

I'm not sure if it'll help or make things worse, but inline below is a
perl script I created to make nagios configuration a bit easier for me.
$LOCATION is a directory you've created and added a line for in
nagios.cfg, something like 'cfg_dir=/etc/nagios/auto', then feed in data
files that look like:

#hostname,ip:host-type srv-type  :ht:ssh:hts:tcp:tcps:crit,warn
router:linux-server:generic-service:0:0:0:80,443:0
nemesis,1.2.3.4:linux-server:generic-service:0:22:0:3128:0:95,98 -i /dev/sr0
spawn:linux-server:generic-service:80:22:0:53:0:85,95
dengar:linux-server:generic-service:80,8080:0:0:0:0:0

so for the 'nemesis' line, it creates a config for host nemesis, doesn't
use DNS, but sets the ip to 1.2.3.4, sets it as a generic linux server,
all services as generic, doesn't monitor http (ht set to zero means no
monitor), checks ssh on port 22, no https check, does a generic tcp
connection check for squid (3128), no ssl tcp checks, and then my own
custom file system monitor for space that probably everyone other than
me should ignore.

so if you save this file as hosts.example, save the perl script below as
'parse' and run 'perl parse hosts.example' it'd create a nagios host
group called 'example' with all these hosts grouped together and their
various ports/services setup to be monitored.

I'm sure there are tons of potential pitfalls, and this script should be
considered beta at best, but I thought it was useful to me, so maybe it
could be for someone else.

oh, and the , configs.   dengar is a good example it's monitoring http
connections on both port 80 and 8080

and as a final note, the standard disclaimer should be understood; I
offer this code with no sort of warranty whatsoever.

--
#!/usr/bin/perl
#read through a config file and generate nagios configs.
#created by zgreenfelder at gmail.com
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

$FILE=$ARGV[0];
$LOCATION="/etc/nagios/auto";
$HGN=$FILE;
$HGN=~s/hosts\.//;
system("rm $LOCATION/$HGN\:\:*");
open(FH,"< $FILE") || die "can not open file $FILE: $!";
while(<FH>){
   @httpp=();
   @httpsp=();
   @sshp=();
   @tcpp=();
   @tcpsp=();
   $https=0; $sshs=0;
   $fswarn=0; $fscrit=0;
   $_=~s/#.*//;
   chomp $_;
   if($_){
     @rae=split(/:/,$_);
     $hname=$rae[0];
     if($hname=~/,/){
       $ip=$hname;
       $ip=~s/.*,//;
       $hname=~s/,.*//;
     }else{
       $ip=`host $hname`;
       $ip=~s/.*has address //;
     }
     $fname=`host $hname`;
     $fname=~s/has address.*//;
     if($fname=~"not found"){
       $fname=$hname;
     }
     chomp $fname; chomp $hname;
     $hostp=$rae[1];
     $servp=$rae[2];
     $https=$rae[3];
     $sshs=$rae[4];
     $httpss=$rae[5];
     $tcps=$rae[6];
     $tcpss=$rae[7];
     $fsc=$rae[8];
     if($https){
         if($https=~/,/){
            @httpp=split(/,/,$https);
         }else{
            $httpp[0]=$https;
         }
     }
     if($httpss){
         if($httpss=~/,/){
            @httpsp=split(/,/,$httpss);
         }else{
            $httpsp[0]=$httpss;
         }
     }
     if($tcps){
         if($tcps=~/,/){
            @tcpsp=split(/,/,$tcps);
         }else{
            $tcpsp[0]=$tcps;
         }
     }
     if($tcpss){
         if($tcpss=~/,/){
            @tcpssp=split(/,/,$tcpss);
         }else{
            $tcpssp[0]=$tcpss;
         }
     }
     if($sshs){
       if($sshs=~/,/){
         @sshp=split(/,/,$sshs);
       }else{
         $sshp[0]=$sshs;
       }
     }
     if($fsc){
       #print "fsc is $fsc\n";
       if($fsc=~/,/){
         @fscval=split(/,/,$fsc);
         $fswarn=$fscval[0];
         $fscrit=$fscval[1];
         if($fscval[2]){
           print "can not parse $fsc for file system check.\n";
           print "  expected value warn%,crit%\n";
         }
         if(($fscrit)&&(($fscrit>100)||($fscrit<1))){
            print "values for monitoring file system- $fsc should be
percent\n";
         }
         if(($fswarn)&&(($fswarn>100)||($fswarn<1))){
            print "values for monitoring file system- $fsc should be
percent\n";
         }
       }else{
         $fscrit=$fsc;
       }
     }
     #start processing
     if($hg){
       $hg="$hg,$fname";
     }else{
       $hg="$fname";
     }
     open(OFH,"> $LOCATION/$HGN\:\:$hname.cfg") || die "error opening
outout fil
e $LOCATION/$hname.cfg: $!";
     print OFH "#autogen config file for nagios from: $zepscript\n";
     print OFH "define host {\n";
     print OFH "\tuse\t$hostp\n";
     print OFH "\thost_name\t$fname\n";
     print OFH "\talias\t$hname\n";
     print OFH "\taddress\t$ip\n";
     print OFH "}\n\n";
     print OFH "define service{\n";
     print OFH "\tuse\t$servp\n";
     print OFH "\thost_name\t$fname\n";
     print OFH "\tservice_description\tPING\n";
     print OFH "\tcheck_command\tcheck_ping!100.0,20%!500.0,60%\n";
     print OFH "}\n\n";
     foreach $x (@httpp){
        print "http for $fname on $x\n";
        print OFH "define service{\n";
        print OFH "\tuse\t$servp\n";
        print OFH "\thost_name\t$fname\n";
        print OFH "\tservice_description\tHTTP-$x\n";
        print OFH "\tcheck_command\tcheck_http!\"-p $x\"\n";
        print OFH "\tnotifications_enabled\t1\n";
        print OFH "}\n\n";
     }
     foreach $x (@sshp){
        print "ssh for $fname on $x\n";
        print OFH "define service{\n";
        print OFH "\tuse\t$servp\n";
        print OFH "\thost_name\t$fname\n";
        print OFH "\tservice_description\tSSH-$x\n";
        print OFH "\tcheck_command\tcheck_ssh!\"-p $x\"\n";
        print OFH "\tnotifications_enabled\t1\n";
        print OFH "}\n\n";
     }
     foreach $x (@httpsp){
        print "https for $fname on $x\n";
        print OFH "define service{\n";
        print OFH "\tuse\t$servp\n";
        print OFH "\thost_name\t$fname\n";
        print OFH "\tservice_description\tHTTPS-$x\n";
        print OFH "\tcheck_command\tcheck_http!%-p%$x!%-S\n";
        print OFH "\tnotifications_enabled\t1\n";
        print OFH "}\n\n";
     }
     foreach $x (@tcpsp){
        print "tcp for $fname on $x\n";
        print OFH "define service{\n";
        print OFH "\tuse\t$servp\n";
        print OFH "\thost_name\t$fname\n";
        print OFH "\tservice_description\tTCP-$x\n";
        print OFH "\tcheck_command\tcheck_tcp!\"$x\"\n";
        print OFH "\tnotifications_enabled\t1\n";
        print OFH "}\n\n";
     }
     foreach $x (@tcpssp){
        print "tcpS for $fname on $x\n";
        print OFH "define service{\n";
        print OFH "\tuse\t$servp\n";
        print OFH "\thost_name\t$fname\n";
        print OFH "\tservice_description\tTCPS-$x\n";
        print OFH "\tcheck_command\tcheck_tcp!\"$x\"!\"-S\"\n";
        print OFH "\tnotifications_enabled\t1\n";
        print OFH "}\n\n";
     }
     if(($fswarn||$fscrit)){
        print "file system check values crit $fscrit";
        $cmdp="-c $fscrit ";
        if($fswarn){
          $cmdp=$cmdp . "-w $fswarn ";
          print " warn $fswarn ";
        }
        print "\n";
        print OFH "define service{\n";
        print OFH "\tuse\t$servp\n";
        print OFH "\thost_name\t$fname\n";
        print OFH "\tservice_description\tFS-Check-$fscrit-$fswarn\n";
        print OFH "\tcheck_command\tcheck_FS_ssh!\"$cmdp\"\n";
        print OFH "\tnotifications_enabled\t1\n";
        print OFH "}\n\n";
     }
     close(OFH);
   }
   open(OFH,"> $LOCATION//$HGN\:\:$HGN.cfg") || die "error opening
outout file $
LOCATION/$HGN.cfg: $!";
   print OFH "define hostgroup{\n";
   print OFH "\thostgroup_name\t$HGN\n";
   print OFH "\talias\tauto-generated $HGN\n";
   print OFH "\tmembers\t$hg\n";
   print OFH "}\n";
   close(OFH);
}
--



More information about the CentOS mailing list