Friday, April 8, 2011

how to pass arguments for check_db in nagios



File Download
Description
Shell Script wrapper
Compiled DbCheck Class
DbCheck java source code
JSAP java command line parser

Remote oracle database check using JDBC drivers. Supports custom SQL queries and regular expression match. Provides similar funcationality as sitescope Db monitor.


This plugin can check almost every aspect of oracle database, written in java for portability.

Compiled with JDK 1.5.0_06

Uses JSAP command line parser http://www.martiansoftware.com/jsap/

to compile : copy DbCheck.java JSAP-2.0a.jar in a directory, expand JSAP-2.0a.jar in the same directory using command jar xvf JSAP-2.0a.jar

Use : javac -cp .:com/martiansoftware/jsap/JSAP.* DbCheck.java

this should generate DbCheck.class file

Setup Nagios Environment:

Copy DbCheck.class, check_db to /usr/local/nagios/libexec
create /usr/local/nagios/libexec/lib directory
Copy JSAP-2.0a.jar, classes12.jar (Take from $ORACLE_HOME/jdbc/lib), sql files to /usr/local/nagios/libexec/lib directory

Nagios plugin to check output of a sql query, matched with a regular expression. Currently supports numeric data checks only.
--help

Print help message

-H

Hostname/ip address of database server.

-p

Listener port, default is 1521. (default: 1521)

-s

Oracle database SID.

-l

Oracle database user to connect.

-x

Oracle database user password.

-f

Full path of sql file to be executed.

-r

PERL style Regular Expression to match in SQL output. Check value must
be enclosed between (), should match only one field and must be enclosed
between single quotes. Example 'SYSTEM.*,.*,(.*)'

-w

Warning value for matched expression. Normally warning < critical value.
If your check want to alert when result < warning , set critical less
than warning

-c

Critical value for matched expression. Should be more than Warning
1. If not check behaviour is reversed, see warning help text above.

-L

Label for matched regex value i.e FieldName.
Usage: DbCheck --help -H -p -s -l -x -f -r -w -c -L 



   

CHECK_NRPE: Received 0 bytes from daemon


Verify the check_nrpe error message

Just for testing purpose, let us assume that you are execuing the following check_nrpe command that displays the “CHECK_NRPE: Received 0 bytes from daemon.” error message.

$ /usr/local/nagios/libexec/check_nrpe -H 192.168.1.20 -c check_disk -a 60 80 /dev/sdb1
CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.

If you view the /var/log/messages on the remote host, (in the above example, that is 192.168.1.20), you’ll see the nrpe error “Error: Request contained command arguments!” as shown below, indicating that check_nrpe is not enabled to take the command arguments.

$ tail -f /var/log/messages
Dec 5 11:11:52 dev-db xinetd[2536]: START: nrpe pid=24187 from=192.168.101.108
Dec 5 11:11:52 dev-db nrpe[24187]: Error: Request contained command arguments!
Dec 5 11:11:52 dev-db nrpe[24187]: Client request was invalid, bailing out...
Dec 5 11:11:52 dev-db xinetd[2536]: EXIT: nrpe status=0 pid=24187 duration=0(sec)

Enable check_nrpe command arguments

To enable command arguments in NRPE, you should do the following two things

1. Configure NRPE with –enable-command-args

Typically when you install NRPE on the remote host, you’ll do ./configure without any arguments. To enable support for command arguments in the NRPE daemon, you should install it with –enable-command-args as shown below.

[remotehost]# tar xvfz nrpe-2.12.tar.gz
[remotehost]# cd nrpe-2.12
[remotehost]# ./configure --enable-command-args
[remotehost]# make all
[remotehost]# make install-plugin
[remotehost]# make install-daemon
[remotehost]# make install-daemon-config
[remotehost]# make install-xinetd


2. Modify nrpe.cfg and set dont_blame_nrpe

Modify the /usr/local/nagios/etc/nrpe.cfg on the remote server and set the dont_blame_nrpe directive to 1 as shown below.
$ /usr/local/nagios/etc/nrpe.cfg
dont_blame_nrpe=1
Execute check_nrpe with command arguments

After the above two changes, if you execute the check_nrpe for this particular remote host, you’ll not see the error message anymore as shown below.

How to pass arguments to CHECK_NRPE?

$ /usr/local/nagios/libexec/check_nrpe -H 192.168.1.20 -c check_disk -a 60 80 /dev/sdb1
DISK OK - free space: / 111199 MB (92% inode=99%);| /=9319MB;101662;114370;0;127078

Security Warning

Enabling NRPE command line arguments is a security risk. If you don’t know what you are doing, don’t enable this.
Probably by now you’ve already figured out that you can’t blame NRPE if something goes wrong. After all you did set dont_blame_nrpe to 1.



Monday, April 4, 2011

Traceroute shell script for linux/unix


Before you run the below script you need to save server list in /tmp/servlist. The below mentioned bash script you need to put in crontab to run as per the schedule. This script will send the traceroute whenever node/server is down. Please comment if this script is useful for you.

#!/bin/bash
#Author : Ranjith Kumar R
#Purpose : To send the traceroute whenever server/node is down.
#Version : V1.0
#Date :April 05, 2011

Hostname=`/bin/hostname`
Trace="/bin/traceroute -I"

for i in `cat /tmp/servlist`
do
test=`fping $i | grep unreachable | awk '{print $1'}`
echo $test
if [ -z "$test" ]; then
sleep 1
else
$Trace $test | mail -s "$test is Down!!! Traceroute from $Hostname to $test" youremailid@domain.com &
fi
done

  

Tuesday, March 22, 2011

Accessing Remote Server Attributes using WMI using VbScript

https://docs.google.com/leaf?id=0B7kjumexSzS1MTk4ZjcxNjktODg5Ny00ZjFjLTg4MWEtMjJhMmMyNjdlNjI0&hl=en

Sunday, February 27, 2011

How to delete the files more than 24 hours in linux


For e.g. delete the files from /tmp/test directory

find /tmp/test -mmin +1440 -type f -exec rm -f {} \;

Thursday, February 17, 2011

How to reset mysql root password in linux

Step # 1 : Stop mysql service

# /etc/init.d/mysqld stop

Step # 2: Start to MySQL server w/o password:


# mysqld_safe --skip-grant-tables &

Step # 3: Connect to mysql server using mysql client:


# mysql -u root

Step # 4: Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Step # 5: Stop MySQL Server:

# /etc/init.d/mysqld stop

Step # 6: Start MySQL server and test it

# /etc/init.d/mysqld start
# mysql -u root -p


Saturday, February 12, 2011

Nagios Log Monitoring(line by line) Plugin for linux

#!/bin/bash
#Purpose         : To monitor the log line by line
#Authors         : Ranjith Kumar R
#Date            : 29th March 2014
#Version         : V2.0

PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="V1.0"
ECHO="/bin/echo"
STATE_UNKNOWN=3
STATE_OK=0
STATE_CRITICAL=2
TAIL="/usr/bin/tail"
MAIL="/bin/mail"
PRINT="/usr/bin/printf"
DATE=`/bin/date`
CONTACTEMAIL="ranjith@test.com"
print_usage() {
    echo "Usage: $PROGNAME -F LOGFILEPATH -q query -c critical count of string match"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}
print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "Log file pattern detector plugin for Nagios"
    echo ""
    support
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 6 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi
# Grab the command line arguments
#LOGFILEPATH=$1
#query=$2
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $REVISION
            exit $STATE_OK
            ;;
        --filename)
            LOGFILEPATH=$2
            shift
            ;;
        -F)
            LOGFILEPATH=$2
            shift
            ;;
        --query)
            query=$2
            shift
            ;;
        -q)
            query=$2
            shift
            ;;
        --critical)
            critical=$2
            shift
            ;;
        -c)
            critical=$2
            shift
            ;;
        -x)
            exitstatus=$2
            shift
            ;;
        --exitstatus)
            exitstatus=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done
if [ -r $LOGFILEPATH ]; then

echo "$LOGFILEPATH has read permission" > /dev/null

else

echo "Nagios unable to read $LOGFILEPATH file, please check the file permission"

exitstatus=$STATE_CRITICAL
        exit $exitstatus

fi

query1=`echo $LOGFILEPATH |  awk -F"/" '{print $NF}'`.`echo $query | awk '{print $1}'`

                if [ -f "/usr/local/nagios/libexec/lastline.$query1" ]; then
                count=0
                else
                echo 0 > /usr/local/nagios/libexec/lastline.$query1
                fi
COUNT=0
LA="/usr/local/nagios/libexec/lastline.$query1"
LASTLINE=`cat /usr/local/nagios/libexec/lastline.$query1`
NEWLINE=`cat $LOGFILEPATH | wc -l`
if [ "$NEWLINE" -lt "$LASTLINE" ];then
echo 0 > /usr/local/nagios/libexec/lastline.$query1
fi
if [ "$NEWLINE" -gt "$LASTLINE" ];then
LINE=$(expr $NEWLINE - $LASTLINE)
echo $NEWLINE > $LA
COUNT=`$TAIL -$LINE $LOGFILEPATH | egrep -c "$query"`
MATCHLINE=`$TAIL -$LINE $LOGFILEPATH | egrep -i "$query"`
        if [ "$COUNT" -ge "$critical" ];then
        $ECHO -e "CRITICAL Matches per line for $query is $COUNT, please refer the below error log.\n$MATCHLINE\nLast Line is $LASTLINE and New Line is $NEWLINE ";echo '|' "count=$COUNT;;$critical"
$PRINT "%b" "***** CRITICAL *****\n\nNotification Type: CRITICAL\n\nCRITICAL Matches per line for $query is $COUNT, please refer the below error log.\n\n$MATCHLINE\n\nDate&Time: $DATE" | $MAIL -s "** CRITICAL Alert: $query **" $CONTACTEMAIL
        exitstatus=$STATE_CRITICAL
        exit $exitstatus
        fi
fi
if [ "$NEWLINE" -eq "$LASTLINE" ] || [ "$COUNT" -lt "$critical" ];then
   $ECHO "OK - $COUNT pattern matches found,Last Line is $LASTLINE and New Line is $NEWLINE";echo '|' "count=$COUNT;;$critical"
   exitstatus=$STATE_OK
   exit $exitstatus
else
   $ECHO "UNKNOWN, Last Line is $LASTLINE and New Line is $NEWLINE";echo '|' "count=$COUNT;;$critical"
   exitstatus=$STATE_UNKNOWN
   exit $exitstatus
fi
fi
--------------------------------------------------------------
e.g. ./check_log -F logfilepath(/var/log/messages) -q string(message) -c number of match
--------------------------------------------------------------------
It will through an error whenever there is a match.