Friday, June 24, 2016

NRPE agent installation script for Linux

#!/bin/bash

OUTPUTFILE="/usr/local/nagios/libexec/tools/linux/nrpe-agent-installation.txt"

for server in `cat /usr/local/nagios/libexec/tools/serverlist.txt`

do

FLAG=0

for password in `cat /usr/local/nagios/libexec/tools/linux/password.txt`

do

/usr/local/nagios/libexec/tools/sftp_put.pl -h $server -u root -p $password -s "file" -f "/usr/local/nagios/libexec/tools/linux/test.txt" -t "/tmp/test.txt"

TEST=`echo $?`

if [ "$TEST" -eq "0" ];then

echo -e "$server \t $password" >> $OUTPUTFILE

#create nagios user

/usr/local/nagios/libexec/check_openssh.pl -H $server -u root -p $password -s command -C "useradd -m -d /var/spool/nagios -p "naZaxHcLCJwT2" nagios"

#Install NRPE Agent#

/usr/local/nagios/libexec/check_openssh.pl -H $server -u root -p $password -s command -C "yum -y install nagios-common-3.5.1-1.el6.x86_64 nagios-plugins-all-2.0.3-3.el6.x86_64 nagios-plugins-nrpe-2.15-7.el6.x86_64 nagios-nrpe-2.14-1.el6.rf.x86_64"

#Upload nrpe.cfg golden copy#

/usr/local/nagios/libexec/tools/sftp_put.pl -h $server -u root -p $password -s "file" -f "/usr/local/nagios/libexec/tools/linux/nrpe_cfg/nrpe.cfg" -t "/etc/nagios/nrpe.cfg"

#Upload nagios sudoers file#

/usr/local/nagios/libexec/tools/sftp_put.pl -h $server -u root -p $password -s "file" -f "/usr/local/nagios/libexec/tools/linux/nagios-sudo/nagios" -t "/etc/sudoers.d/nagios"

#Change the permissions of the nagios directories#

/usr/local/nagios/libexec/check_openssh.pl -H $server -u root -p $password -s command -C "chown -R nagios:nagios /etc/nagios/;chown -R nagios:nagios /usr/lib64/nagios/"

FLAG=1

break

fi

done

if [ "$FLAG" -eq "0" ];then

echo -e "$server \t No Password" >> $OUTPUTFILE

fi

done
#######################################
#Dependencies : 
#######################################

Friday, June 17, 2016

check_openssh.pl Plugin for Nagios

#!/usr/bin/perl
use Net::OpenSSH;
use Getopt::Long;
use Switch;
Getopt::Long::Configure('bundling');
$status=GetOptions
    (

     "H=s"   => \$dname, "DeviceName=s"     => \$dname,
     "u=s"   => \$uname, "UserName=s"    => \$uname,
     "p=s"   => \$password, "Password=s" => \$password,
     "w=s"   => \$warn,  "Warning=i" => \$warn,
     "c=s"   => \$crit, "Critical=i" => \$crit,
     "m=s"   => \$mount, "Mount=s"  => \$mount,
     "s=s"   => \$switch, "Switch=s" => \$switch,
     "C=s"   => \$command, "Command=s" => \$command
    );
        if ( $status == 0 ) {
        print_usage();
        }

#-- set up a new connection
# my $ssh = Net::OpenSSH->new('$uname:$password@$dname');
my $ssh = Net::OpenSSH->new($dname,user => $uname,password => $password);
$ssh->error and die "Connection error" . $ssh->error;

switch($switch)
{
case "command" {
                $ssh->system("$command");
                $ret = $ssh->error;
                $ret =~ s/\D//g;
                exit $ret;
                }
}
sub print_usage {
        print <
    Version:1.0

    Usage: check_openssh.pl -H host -u username -p password -w warn -c crit -m mode

 -H STRING or IPADDRESS
        Check interface on the indicated host.

 -u STRING VALUE
        Logon name of the remote UNIX server

 -p STRING VALUE
        Password to access the remote UNIX server

 -w INTEGER VALUE
        Warning condition depending on the Plug-in's used at remote server or Switch mode used

 -c INTEGER VALUE
        Critical condition depending on the Plug-in's used at remote server or Switch mode used
 -s STRING VALUE
        Switch mode is a string used to run the remote command or script
        memory  : memory usage
        check_load: Server Load
        check_disk: Disk Utilization on Server(provide mount point). e.g. -s check_disk -m /
        db_status : Check the health of Oracle DB server
        command : For running command at remote host. E.g. -s command -C "ls"
EOU

        exit 3;
}