23 marzo 2010

Altri 2 script bash...

Per motivi vari (non legati direttamente al lavoro) m'è capitato di dover "riciclare" ed adattare uno script bash che avevo fatto tipo 5 anni fa (!).. E me ne sono venuti fuori due di scripts.

Questo è da ficcare nel crontab e ha come obiettivo quello di verificare che un servizio/daemon sia *realmente* disponibile ed utilizzabile via internet. Richiede la presenza sul server in questione di telnet e di wget oppure curl. Attenzione, è stato scritto per un amico per il suo server (a cui non ho accesso "diretto"), quindi usatelo a vostro rischio e pericolo.

chk_service.sh

#!/bin/bash

OWNER_UID=1000           # UID of user owning the daemon to be checked
ROOT_UID=0               # Only users with UID 0 have root privileges.
E_WRONGUID=67            # Some arbitrary exit error.
DAEMON=quelchevolete     # Name of the service to kill/restart
TO=10                    # timeout of connection in seconds
MT=15                   # maximum time to run curl/wget
ADDR=123.456.789.123     # address of service to check upon
PORT=1234                # port
STRING=Quelkevipare      # string to look for in service's output

# change this to suit your needs/preferences
LOGFILE=/path/to/chk_$DAEMON.log

if [ "$UID" -ne "$ROOT_UID" ] && [ "$UID" -ne "$OWNER_UID" ]
then
    echo "Must be root or owner to run this script."
    exit $E_WRONGUID
fi

if [ ! -d "$LOGFILE" ]
then
    touch $LOGFILE
    chmod 640 $LOGFILE
fi

# using telnet to see if $ADDR it is accepting connections on $PORT
# the use of exit 1 instead of calling a restart is because that service
# MAY be right in the middle of rebooting...

echo -en "o $ADDR $PORT\nquit\n" |/usr/bin/telnet 2>/dev/null | /bin/grep 'Connected to' > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
    echo `/bin/date +'%b %e %H:%M:%S'`" -- Server $ADDR NOT accepting connections on port $PORT!!!" >> $LOGFILE
    exit 1
else

# check if $DAEMON is outputting the right output (uncomment curl if you don't have wget and viceversa)

# using curl...
#    echo "\r\n" | /usr/bin/curl -T "-" -s --connect-timeout $TO --max-time $MT telnet://$ADDR:$PORT | /bin/grep $STRING > /dev/null 2>&1

# using wget...
    /usr/bin/wget -qO - "$@" http://$ADDR:$PORT | /bin/grep $STRING > /dev/null 2>&1
    /usr/bin/wget -t 1 --read-timeout=$TO -T $MT -qO - "$@" http://$ADDR:$PORT | /bin/grep $STRING > /dev/null 2>&1

    if [ "$?" -eq 0 ]; then
        echo `/bin/date +'%b %e %H:%M:%S'`" -- $DAEMON is active." >> $LOGFILE
        exit 0
    else  # trying to re-start the missing service
        echo `/bin/date +'%b %e %H:%M:%S'`" -- $DAEMON died between last check and this! Killing it..." >> $LOGFILE
# PLEASE put here the command you would use for restarting the $DAEMON
#        /usr/bin/killall $DAEMON > /dev/null 2>&1

        if [ "$?" -eq 0 ]; then
            echo `/bin/date +'%b %e %H:%M:%S'`" -- $DAEMON killed successfully" >> $LOGFILE
            exit 0
        else
            echo `/bin/date +'%b %e %H:%M:%S'`" -- Unable to kill $DAEMON. Sorry!!!" >> $LOGFILE
            exit 1
        fi
    fi
fi

Questo invece controlla da "remoto" se un daemon/servizio su un dato server è disponibile o no ed avvisa di conseguenza l'utente. Funziona di sicuro in Gnome. Che funga anche dalla consolle non saprei, è ancora da testare fino in fondo. Anche questo sarebbe da ficcare dentro crontab (il che delle volte presenta degli inconvenienti dal punto di vista dell'utente)...

chk_remote_service.sh

#!/bin/bash

OWNER_UID=1000           # UID of user owning the daemon to be checked
ROOT_UID=0               # Only users with UID 0 have root privileges.
E_WRONGUID=67            # Some arbitrary exit error.
DAEMON=quelchevolete     # Name of the service to kill/restart
TO=10                    # timeout of connection in seconds
MT=15                   # maximum time to run curl/wget
ADDR=123.456.789.123     # address of service to check upon
PORT=1234                # port
STRING=Quelkevipare      # string to look for in service's output

# change this to suit your needs/preferences
LOGFILE=/path/to/chk_$DAEMON.log

if [ "$UID" -ne "$ROOT_UID" ] && [ "$UID" -ne "$OWNER_UID" ]
then
    echo "Must be root or owner to run this script."
    exit $E_WRONGUID
fi

if [ ! -d "$LOGFILE" ]
then
    touch $LOGFILE
    chmod 640 $LOGFILE
fi

# using telnet to see if $ADDR it is accepting connections on $PORT
echo -en "o $ADDR $PORT\nquit\n" |/usr/bin/telnet 2>/dev/null | /bin/grep 'Connected to' > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
    echo `/bin/date +'%b %e %H:%M:%S'`" -- Server $ADDR NOT accepting connections on port $PORT!!!" >> $LOGFILE
    TXT="Server hosting $DAEMON DOWN\!"
else

# check if $DAEMON is outputting the right output (uncomment curl if you don't have wget and viceversa)

# using curl...
#    echo "\r\n" | /usr/bin/curl -T "-" -s --connect-timeout $TO --max-time $MT telnet://$ADDR:$PORT | /bin/grep $STRING > /dev/null 2>&1

# using wget...
    /usr/bin/wget -qO - "$@" http://$ADDR:$PORT | /bin/grep $STRING > /dev/null 2>&1
    /usr/bin/wget -t 1 --read-timeout=$TO -T $MT -qO - "$@" http://$ADDR:$PORT | /bin/grep $STRING > /dev/null 2>&1

    if [ "$?" -eq 0 ]; then
        echo `/bin/date +'%b %e %H:%M:%S'`" -- $DAEMON is active." >> $LOGFILE
        TXT="$DAEMON is UP\!"
    else
        echo `/bin/date +'%b %e %H:%M:%S'`" -- $DAEMON died between last check and this!" >> $LOGFILE
        TXT="$DAEMON is DOWN\!"
    fi
fi

# you may need to put "xhost local:username > /dev/null" in .bashrc or .bash_profile FIRST
# if you don't have/use gnome, please uncomment xmessage and comment off zenity.
# want zenity in front of everything? edit zenity.glade & change the "focus_on_map" property to "True"

/usr/bin/lsof -c xinit > /dev/null 2>&1
if [ "$?" -eq 0 ]; then
#    /usr/bin/xmessage -display :0.0 -center -timeout 5 $TXT
    /usr/bin/zenity --display=:0.0 --warning --width=160 --height=40 --timeout=5 --text="$TXT"
else
    /usr/bin/lsof -lc bash | grep /bin/bash | grep "\ $OWNER_UID\ \|\ $ROOT_UID\ " > /dev/null 2>&1
    if [ "$?" -eq 0 ]; then
        /usr/bin/dialog --timeout 5 --msgbox "$TXT" 6 20
    else
        /usr/bin/wall "$TXT"
    fi
fi
exit 0

Nessun commento:

Posta un commento

NOTA BENE: Se vi ostinate a commentare posts di un mese fa, scatta la moderazione (se non sono in vacanza, controllo quasi ogni giorno)... Ovviamente, se siete spammers vi cancello :)