Monitoring

Z Freenetis Wiki
Přejít na: navigace, hledání
#!/bin/bash

# URL with IP addresses to monitor
HOSTS_INPUT_URL="http://freenetis.slfree.net/cs/web_interface/monitoring_hosts/"

# URL to which we send result
HOSTS_OUTPUT_URL="http://freenetis.slfree.net/cs/web_interface/monitoring_states/"

# temporary file with list of IP addresses to monitor
HOSTS_INPUT=`mktemp`

# temporary file with result to send
HOSTS_OUTPUT=`mktemp`

echo "Downloading list of IP addresses to monitor..."

# get ip addresses from FreenetIS
wget -q "$HOSTS_INPUT_URL$1" -O "$HOSTS_INPUT"

# use fping to get ip addresses states
fping -e -f "$HOSTS_INPUT" 2>/dev/null | while read host
do
	# ip address
	ip=`echo $host | awk '{print $1}'`

	# state of host (alive or unreachable)
	state=`echo $host | awk '{print $3}'`

	# latency of host (only for alive state)
	lat=`echo $host | awk '{print $4}' | sed 's/(//'`

	# do not add ampersand to beginning of file with result
	if [ -s "$HOSTS_OUTPUT" ];
	then
		echo -n "&" >> "$HOSTS_OUTPUT";
	fi

	# add variables to file with result
	echo "ip[]=$ip&state[]=$state&lat[]=$lat" >> "$HOSTS_OUTPUT"
done

# remove temporary file with IP addresses to monitor
rm "$HOSTS_INPUT"

echo "Sending result data back to FreenetIS..."

# send file with result back to FreenetIS
wget -q --post-file="$HOSTS_OUTPUT" "$HOSTS_OUTPUT_URL"

# remove temporary file with result
rm "$HOSTS_OUTPUT"