Monday, October 24, 2011

Nagios Customize Script

Since Nagios is only using check_nrpe to call / execute the shell script. We can use this command to execute our own write shell script.
Below is the example of script to check the public IP and give output to nagios
-------------------------------------------------------------------------------

#!/bin/sh

IP=`curl -s ifconfig.me`
LEASE=202.147.38.202 

if [ $IP == $LEASE ]; then

        echo $IP
        exit 0

else
        echo $IP
        exit 2

fi

-------------------------------------------------------------------------------

According to nagios, it use exit code to determine the output to give what kind of response.
so below is the exit code


State OK = 0
State Warning = 1
State Critical = 2
State unknown = 3
State Dependent = 4

----------------------------------------------------------

Now we will add this script to the client side and define the command to nagios

We will need to edit nrpe.cfg file and add this into it

command[check_lease]=/etc/nagios/check_lease

this command will allow nagios server to call this command
----------------------------------------------------------

Then we need to define this command at client side for the path of the script to execute at commands.cfg

define command{
     command_name    check_lease
     command_line    /bin/bash /etc/nagios/check_lease
     }

This will state check_lease is refer to which shell script
----------------------------------------------------------

The last part is add the check command to the server side


define service{
      use                             local-service
      host_name                       my-proxy
      service_description             lease
      check_command                   check_nrpe!check_lease
  }

Wednesday, October 12, 2011

check backup server

==== client side ====
we will make a script in each server to pump date into a file. so when we check at backup server, we will check if this file was backup to backup server since it will update the date everyday.\\

below is the script contain (/root/record)
----------------- Start ---------------------------
#! /bin/sh

echo `date` > /root/assessment

then use crontab -e to run this script everyday
0 12 * * * /root/record > /dev/null 2>&1

----------------- End ---------------------------

==== backup server side ====
at the server side, i had make a folder name assessment and put the script inside.\\
below is the script contain (check_assessment)

------------------- Start -------------------------------
#! /bin/sh

echo "assessment done on `date`" > /mnt/backup/assessment/assessment_result.txt -> this is to pump the date it run the script
echo " " >> /mnt/backup/assessment/assessment_result.txt -> give an empty space

for host in `cat /mnt/backup/assessment/list`    # -> this is to loop the server list
do
echo $host >> /mnt/backup/assessment/assessment_result.txt -> this is to pump the server name before it start check so we know the result is belong to which server

for n in {1..9} -> loop to check folder 01-09
do
FILE="/mnt/backup/$host/0$n/root/assessment"


if [ -f $FILE ];
then
cat $FILE >> /mnt/backup/assessment/assessment_result.txt -> if the file exist, copy the date to assessment_result.txt
else
echo "day $n didnt backup" >> /mnt/backup/assessment/assessment_result.txt -> if the file not exist, echo what day it didnt backup
fi

done


for n in {10..31} -> loop day 10-31
do
FILE="/mnt/backup/$HOST/$n/root/assessment"

if [ -f $FILE ];
then
cat $FILE >> /mnt/backup/assessment/assessment_result.txt
else
echo "day $n didnt backup" >> /mnt/backup/assessment/assessment_result.txt
fi

done

echo " " >> /mnt/backup/assessment/assessment_result.txt

done

uuencode /mnt/backup/assessment/assessment_result.txt /mnt/backup/assessment/assessment_result.txt | mail -s "[My-Backup] monthly check result" sat.support@my.offgamers.com -> this is to email the result to us as attachment

--------------------------- End -----------------------------------------

then put the server name you want this script to check into a file name list
example:-
my-domain
my-proxy
my-dns1
my-dns2

then use crontab -e to run this script every first day of the month