Chronosbox

pingping – Checking if servers is on.

by Felipe 'chronos' Prenholato on Sunday, 05 April/2009, under Linux, Planet GentooBR, Servers, Tips

Above is my pingping script. This script send a mail alert when some server stop, based on regular ICMP requests. Read it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
# This script send ICMP requests for each server on SERVIDORES list and sends
# emails for emails in LISTA_EMAILS list why one of servers doesn't reply
# You only receive alerts if in last time that script request server we have a success reply
# or if we not tested server yet.
#
# Author: Felipe Rafael Prenholato - chronos@gentoobr.org
# IRC: nick chronos at irc.freenode.net
# Date: 21 de Junho de 2006.
#
# This script is under GPLv2. Read it at http://www.gnu.org/licenses/gpl-2.0.txt.
# Any changes please send me a mail :).
#
 
# IP or name of servers to test
SERVIDORES="elise prime odissey"
 
# emailo to send alerts
LISTA_EMAILS="chronos@gentoobr.org philipe_rp@yahoo.com.br"
 
# date format to send in email
data=$(date +"%T de %d/%m/%Y")
 
# this function sends ping
ping_server() {
 
local pingreturn
local serversts
local TMPFILE
local server
 
server=$1
TMPFILE="/tmp/ping_$server.tmp"
 
# Debug
#echo "Server: $server"
#echo "Temp file: $TMPFILE"
 
# ping and get return
ping -c3 $server &>/dev/null;
pingreturn=$?;
 
# 0 means ok
if [ "$pingreturn" == "0" ]
then
echo "1" > $TMPFILE
fi
 
# 1 means problems
if [ "$pingreturn" == "1" ]
then
 
if ! [ -e $TMPFILE ]
then
echo "1" > $TMPFILE
fi
 
serversts=$(cat $TMPFILE)
 
# send mail if have problems
if [ "$serversts" == "1" ]
then
 
echo "0" > $TMPFILE
msg="Servidor $server fora do ar em $data. Contatar responsavéis."
ass="Servidor $server fora do ar."
LC_ALL="en_US.ISO-8859-1" echo "$msg" | mail -s "$ass" $LISTA_EMAILS
fi
 
fi
 
}
 
# roda ping_server para cada servidor.
for e in $(echo $SERVIDORES);
do
ping_server $e;
done

Install this script in one server that can send ICMP requests to servers in lists. Very old versions of postfix doesn’t works because this script needs -a option in mail command. Use as:

./script

Run in cron and have peace :D

:, , , , ,

1 Comment for this entry

  • Leandro Miranda

    Precisa de algum serviço instalado no servidor para que o email seja enviado, fiz modificaçao no script somente do ip do servidor e dos emails e não ta enviando email, e eu ja tentei muitas coisas, inclusive ja mudei as permiçoes do script com o chmod +x script

    se tiver como me da uma luz favor responde ai.. abraço e otimo post…

Leave a Reply

StatPress

Visits today: 5 Visits since 6 de April de 2009: 15861 Visitors now: %visitoronline%