-
Notifications
You must be signed in to change notification settings - Fork 0
/
freenetis-monitord.sh
123 lines (100 loc) · 3.87 KB
/
freenetis-monitord.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
################################################################################
# #
# This script serves for monitoring from IS FreeNetIS #
# #
# author Kliment Michal 2012 #
# email [email protected] #
# #
# name freenetis-monitord.sh #
# version 0.9.7 #
# #
################################################################################
# Version
VERSION="0.9.7"
#Load variables from config file
CONFIG=/etc/freenetis/freenetis-monitoring.conf
# temporary file with list of IP addresses to monitor
HOSTS_INPUT=`mktemp`
# temporary file with result to send
HOSTS_OUTPUT=`mktemp`
FPING="/usr/bin/fping"
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Get OS name
function get_ip()
{
OS=`uname`
IO="" # store IP
case $OS in
Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
FreeBSD|OpenBSD) IP=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
*) IP="Unknown";;
esac
echo -n "$IP"
}
# Vesion info? Only possible arg.
if [ $# -eq 1 ]; then
if [ "$1" == "version" ]; then
echo "$VERSION"
exit 0
fi
fi
# Load variables
if [ -e $CONFIG ]; then
. $CONFIG
else
echo "Config file is missing at path $CONFIG."
echo "Terminating..."
exit 0
fi
# try to create log file if not exists
if [ ! -e "$LOG_FILE" ] ; then
set +e
touch "$LOG_FILE"
set -e
fi
# disable logging if log cannot be written
if [ ! -w "$LOG_FILE" ]; then
echo "Cannot write to $LOG_FILE file => disabling logging"
LOG_FILE=/dev/null
fi
# endless loop
while true;
do
echo "Downloading list of IP addresses to monitor..."
# get ip addresses from FreenetIS
http_code=`wget --no-check-certificate --server-response -q "$HOSTS_INPUT_URL$1" -O "$HOSTS_INPUT" 2>&1 | awk '/^ HTTP/{print $2}'`
## access forbidden
if [ "$http_code" = "403" ]; then
# write to log if not already written
if [ -z "$ERROR_WRITEN" ]; then
echo -n "`date -R` " 1>&2
echo -n "Wrong configuration of FreenetIS - access from monitoring server not allowed. " 1>&2
echo -n "Set IP address of monnitoring server to \"" 1>&2
get_ip 1>&2
echo "\" in FreenetIS settings." 1>&2
ERROR_WRITEN=1
fi
echo "Wrong configuration of FreenetIS - access from monitoring server not allowed."
echo "Set IP address of monnitoring server to \"" $(get_ip) "\" in FreenetIS settings."
echo ""
continue
fi
# use fping to get ip addresses states
$FPING -e -f "$HOSTS_INPUT" 2>>$LOG_FILE | awk '{print "ip[]="$1"&state[]="$3"&lat[]="substr($4,2)}' ORS='&' > "$HOSTS_OUTPUT"
# remove temporary file with IP addresses to monitor
rm -f "$HOSTS_INPUT"
echo "Sending result data back to FreenetIS..."
echo ""
# send file with result back to FreenetIS
wget -qO- --no-check-certificate --post-file="$HOSTS_OUTPUT" "$HOSTS_OUTPUT_URL"
# remove temporary file with result
rm -f "$HOSTS_OUTPUT"
done