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;
}



Monday, May 30, 2016

Net::SFTP::Foreign - FTP protocol to put files from folder or file to remote server.

#!/usr/bin/perl
use Switch;
use Getopt::Long;
use Net::SFTP::Foreign;

#./sftp_put.pl -h hello.domainname.com -u root -p root123 -s "folder" -f "/usr/local/nagios/libexec/tools/linux/" -t "/usr/local/nagios/libexec/tools/linux/"

# ./sftp_put.pl -h hello.domainname.com -u root -p root123 -s "file" -f "/usr/local/nagios/etc/nrpe.cfg" -t "/usr/local/nagios/libexec/tools/linux/nrpe.cfg"

my $status = GetOptions(
        "host|h=s" => \$host,
        "username|u=s"  => \$username,
        "password|p=s"   => \$password,
        "from_dir|f=s"  => \$FROM_DIR,
        "to_dir|t=s"    => \$TO_DIR,
        "switch|s=s"    => \$fcopy,
);

if ( $status == 0 ) {
        print_usage();
        exit(3);
}

my $sftp;
$sftp = Net::SFTP::Foreign->new (
    $host,
    timeout         => 240,
    user            => $username,
    password        => $password,
    autodie         => 1,

);
switch ($fcopy)
{
case "folder"
{
$sftp->die_on_error("Unable to establish SFTP connection");
my $directory = $FROM_DIR;
opendir (DIR, $directory) or die $!;
while (my $file = readdir(DIR))
{
next unless ($file =~ m/^[^\.]/);
my $TO_FILE = $TO_DIR.$file;
my $FROM_FILE = $FROM_DIR.$file;
#printf "To files:$TO_FILE\n";
#printf "From files:$FROM_FILE\n";
$sftp->rput($FROM_FILE ,$TO_FILE );
}
if($sftp->status)
{
print "Unsuccessfull filecopy copy to destination\n";
}
#$sftp->get("/usr/local/nagios/etc/nrpe.cfg","/usr/local/nagios/libexec/tools/linux/nrpe.cfg");
if($sftp->status)
{
print "Unsuccessfull filecopy from destination\n";
}

}
case "file"
{
my $TO_FILE = $TO_DIR.$file;
my $FROM_FILE = $FROM_DIR.$file;
$sftp->put($FROM_FILE ,$TO_FILE);
if($sftp->status)
{
print "Unsuccessfull filecopy to destination\n";
}
}

else {printf "check the option properly\n"}

}
$sftp->disconnect;
closedir(DIR);

sub print_usage {
        print <
Usage: sftp_put.pl -H host -u username -p password -s "file|folder" -f "from path" -t "to path"
Options:

    -H --host IPADDRESS
        Check interface on the indicated host.
    -u --username
        Provided the user name for the remote SFTP server, which has a privilage to read/write files
    -p --password
        Password for the remote SFTP server
    -f --from
       copy file/files from the location depending on the switch used
    -t --to
        put the file/files in this location depending on the switch used
    -s --switch
        it can be either one of file ot folder
EOU
}

Net::SFTP::Foreign - FTP protocol to get files from folder or file from remote server.

#!/usr/bin/perl
use Switch;
use Getopt::Long;
use Net::SFTP::Foreign;

#./sftp_get.pl -h hello.domainname.com -u root -p root123 -s "folder" -f "/usr/local/nagios/libexec/tools/linux/" -t "/usr/local/nagios/libexec/tools/linux/"
#./sftp_get.pl -h hello.domainname.com -u root -p root123 -s "file" -f "/usr/local/nagios/libexec/tools/linux/ran.txt" -t "/usr/local/nagios/libexec/tools/linux/ran.txt"

my $status = GetOptions(
        "host|h=s" => \$host,
        "username|u=s"  => \$username,
        "password|p=s"   => \$password,
        "from_dir|f=s"  => \$FROM_DIR,
        "to_dir|t=s"    => \$TO_DIR,
        "switch|s=s"    => \$fcopy,
);

if ( $status == 0 ) {
        print_usage();
        exit(3);
}

my $sftp;
$sftp = Net::SFTP::Foreign->new (
    $host,
    timeout         => 240,
    user            => $username,
    password        => $password,
    autodie         => 1,

);
switch ($fcopy)
{
case "folder"
{

$sftp->die_on_error("Unable to establish SFTP connection");
my $rlist = $sftp->ls("$TO_DIR",no_wanted => qr/^\./);
for (@$rlist)
{
#print "$_->{filename}\n";
my $FROM_FILE=$FROM_DIR.$_->{filename};
my $TO_FILE=$TO_DIR.$_->{filename};
#printf "From Files:$FROM_FILE \n";
#printf "To Files:$TO_FILE\n";
$sftp->get("$FROM_FILE","$TO_FILE");
}
if($sftp->status)
{
print "Unsuccessfull filecopy copy to destination\n";
}
}

case "file"
{
$sftp->get("$FROM_DIR","$TO_DIR");
}

else {printf "check the option properly\n"}

}

$sftp->disconnect;
closedir(DIR);



sub print_usage {
        print <Usage: sftp_get.pl -H host -u username -p password -s "file|folder" -f "from path" -t "to path"
Options:

    -H --host IPADDRESS
        Check interface on the indicated host.
    -u --username
        Provided the user name for the remote SFTP server, which has a privilage to read/write files
    -p --password
        Password for the remote SFTP server
    -f --from
       copy file/files from the location depending on the switch used
    -t --to
        put the file/files in this location depending on the switch used
    -s --switch
        it can be either one of file ot folder
EOU
}


Friday, May 6, 2016

MSSQL connection using ADODB connection and Recordset

'declare the variables
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL
'define the connection string, specify database driver
ConnString="DRIVER={SQL Server};SERVER=server01.domian.com;UID=service_account;" & _
PWD=P@ssw0rd;DATABASE=Database_name
'declare the SQL statement that will query the database
SQL = "Select * from table"

'create an instance of the ADO connection and recordset objects
Set Connection = CreateObject("ADODB.Connection")
Set Recordset = CreateObject("ADODB.Recordset")
'Open the connection to the database
Connection.Open ConnString
'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection
Wscript.Echo "User" &VbTab& "AppName" &VbTab& "nDays"
'first of all determine whether there are any records
If Recordset.EOF Then
Wscript.Echo ("No records returned.")
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof  
Wscript.Echo Recordset.Fields(0) &VbTab& Recordset.Fields(1) &VbTab& Recordset.Fields(2)
'Wscript.Echo VbNewline   
Recordset.MoveNext
Loop
End If
'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing