Monday 9 January 2012

Port Check with Nagios


Multiple Port Check Plugin for Nagios

#!/bin/bash
#
# Usage: .//check_system_pp
#
# Discription:
# This plugin determine whether the server
# is running properly. It will check the following:
# * Are all the required TCP/IP ports open or not?
#
# Created:      25.12.2011 (IN)
#
#
#

##################################################################################
##
#
# Ports to check

PORT1="21"
PORT2="22"
PORT3="53"
PORT4="80"

##################################################################################

PATH="/usr/bin:/usr/sbin:/bin:/sbin"

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

print_gpl() {
    echo "This program is free software; you can redistribute it and/or modify"
    echo "it under the terms of the GNU General Public License as published by"
    echo "the Free Software Foundation; either version 2 of the License, or"
    echo "(at your option) any later version."
    echo ""
    echo "This program is distributed in the hope that it will be useful,"
    echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
    echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
    echo "GNU General Public License for more details."
    echo ""
    echo "You should have received a copy of the GNU General Public License"
    echo "along with this program; if not, write to the Free Software"
    echo "Nilesh Kumar, New delhi 110067, India"
}

print_help(){
        echo "System process and port check script for Nagios."
        echo "Tested on RHE3/4/5/6, CentOS 4/5/6, Fedora 4, Solaris 9"
        echo ""
        echo "Usage: ./check_system_pp"
        echo "Website: http://www.nagiosexchange.org"
        echo ""
    print_gpl
}

while test -n "$1"
do
        case "$1" in
          *) print_help; exit $STATE_OK;;
        esac
done

check_port1()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT1`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port1=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port1=$STATE_OK
    fi
}
check_port1

check_port2()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT2`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port2=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port2=$STATE_OK
    fi
}
check_port2

check_port3()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT3`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port3=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port3=$STATE_OK
    fi
}
check_port3

check_port4()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT4`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port4=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port4=$STATE_OK
    fi
}
check_port4

port_exit1=`expr $exit_port1`
port_exit2=`expr $exit_port2`
port_exit3=`expr $exit_port3`
port_exit4=`expr $exit_port4`

#
if [ $port_exit1 -eq "0" ]; then
    p1u=$PORT1,; else
    p1d=$PORT1,
fi
#
if [ $port_exit2 -eq "0" ]; then
    p2u=$PORT2,; else
    p2d=$PORT2,
fi
#
if [ $port_exit3 -eq "0" ]; then
    p3u=$PORT3,; else
    p3d=$PORT3,
fi
#
if [ $port_exit4 -eq "0" ]; then
    p4u=$PORT4; else
    p4d=$PORT4
fi
#

UP=`echo $p1u $p2u $p3u $p4u`
DOWN=`echo $p1d $p2d $p3d $p4d`

final_exit=`expr $exit_port1 + $exit_port2 + $exit_port3 + $exit_port4`

if [ $final_exit -eq "0" ]; then
    echo "Port $p1u $p2u $p3u $p4u UP and no any port DOWN"
    exitstatus=$STATE_OK
elif [ $final_exit -eq "1" ]; then
    echo "Port $p1u $p2u $p3u $p4u UP and Port $p1d $p2d $p3d $p4d DOWN"
    exitstatus=$STATE_WARNING
elif [ $final_exit -ge "1" ]; then
    echo "Port $p1u $p2u $p3u $p4u UP and Port $p1d $p2d $p3d $p4d DOWN"
    exitstatus=$STATE_CRITICAL
fi

exit $exitstatus

RAID Metadata Error


BIOS RAID Metadata Error

This type of error may be occurred due to if the hard drive previously installed in those systems which configured with hardware RAID and when we use this hard drive in another system that has not hardware RAID so that error may appear and during installation of Linux OS on create partition windows the hard drive latter may be appear like as “/dev/mapper/…”. Until we correct this error, OS cannot install on this hard drive. If may be install Linux OS but cannot boot the OS.

To correct this problem follow the instructions bellow.

First of all attach the error hard drive as secondary with any Linux primary hard drive and boot the system with primary hard drive.

Now open terminal and run the following command on the terminal.
# fdisk   -l                               (To check the details of disk status).
# cat /dev/zero > /dev/sdx    (where sdx is our secondary hard drive letter).

It will repair the all block of hard drive as zero i.e. low level formatting.

This command will take time as depend upon size of hard drive it will take about 1Hr or more than this to take terminal back.

After terminal is getting back turn off the system and unattached the secondary hard drive.

If we want to check the hard drive RAID Metadata problem is solved or not. Attach only this hard drive in the system and boot the system with Linux DVD and check this on were we create partitions during Linux installation. Here should show the hard drive physical path as “/dev/hda” for IDE or “/dev/sad” for SATA/SCSI.

Access NTFS from Linux

Access NTFS file-system from Linux

There are some method of accessing windows system from a Linux system one of them I use to prefer is ntfs-3g driver.

First of all, we must have “ntfs-3g” package for accessing windows NTFS file system through Linux file system which can be download through  this link http://www.ntfs-3g.org/

            Package Recuired:
1. fuse-2.6.0
2. ntfs-3g-1.913.tgz

Check to see if you have the required package, FUSE 2.6.0 or later. We assume our system already has one, follow the given process as…
# yum list installed | grep fuse
# tar xvzf ntfs-3g-1.913.tgz              (Extract the ntfs-3g driver package)

Install ntfs-3g driver as…
# cd ntfs-3g-1.913
# ./configure
# make
# make install

Try to mount NTFS hard disk in /mnt or we can make another mount point (Folder) for access it.
# mkdir /mnt/windows
# mount  -t ntfs-3g /dev/sdb1 /mnt/windows

If it had already been mounted somehow, unmount it first.
# umount /meia/My\ Book/
# mount -t ntfs-3g /dev/sdb1 /mnt/windows

Sometimes, you may need to enforce mounting like
# mount -t ntfs-3g /dev/sdb1 /mnt/windows -o force

To unmount the volume, type
# umount /mnt/windows

To view the all mount file-system information, type
# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /mnt/windows type fuseblk (rw,nosuid,nodev,noatime,allow_other,blksize=4096)

# more /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0

Note: Sometimes we need “fuse” to access it. So Installing FUSE (File-system in User space). Download the latest stable release from http://fuse.sourceforge.net

Extract the fuse package for installing.
# tar xvzf fuse-2.7.0.tar.gz

Install FUSE by using these steps.
# cd fuse-2.7.0
# ./configure
# make
# make install

Boot to UEFI Mode or legacy BIOS mode

Boot to UEFI Mode or legacy BIOS mode Choose UEFI or legacy BIOS modes while installing Windows. After Windows is installed, if you nee...