Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / system / freeboxuptime @ d0216f00

Historique | Voir | Annoter | Télécharger (3,56 ko)

1
#!/bin/bash
2
#
3
# Plugin made by Dju
4
# useful to know your freebox uptime, and to see when it has rebooted ;)
5
#
6
# Uses nmap to get the uptime (by tcp connection, RFC1323)
7
# ports usually opened on the freebox: 80 554 9100
8
#
9
# ----------------------------------------------------------------------------------------------------
10
# nmap example with the -O option
11
# on a freebox v5
12
#
13
# Starting Nmap 4.62 ( http://nmap.org ) at 2010-12-17 02:25 CET
14
# Interesting ports on mafreebox.freebox.fr (212.27.38.253):
15
# Not shown: 1712 filtered ports
16
# PORT     STATE SERVICE
17
# 80/tcp   open  http
18
# 554/tcp  open  rtsp
19
# 9100/tcp open  jetdirect
20
# Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
21
# Device type: remote management
22
# Running: HP embedded
23
# OS details: HP Onboard Administrator management console
24
# Uptime: 7.226 days (since Thu Dec  9 21:01:44 2010)
25
# 
26
# OS detection performed. Please report any incorrect results at http://nmap.org/submit/ .
27
# Nmap done: 1 IP address (1 host up) scanned in 29.279 seconds
28
# ----------------------------------------------------------------------------------------------------
29
#
30
# by using nmap on a specific tcp port, the detection is pretty fast (2-5 seconds)
31
# with this command: nmap -O --osscan-guess -p80 remote_host
32
#
33
# if you dont want nmap to query your freebox each time, set CACHE_HOURS=n
34
# to keep the uptime in cache for n hours
35
# if not, set CACHE_HOURS=0
36
#
37
# to allow this plugin to use nmap, edit /etc/munin/plugin-conf.d/munin-node and add
38
# [FreeboxUptime]
39
# user root
40
#
41
# exit value if error:
42
# 1: nmap not installed
43
# 2: freebox not reachable by ping
44
# 3: uptime not found in nmap result
45
#
46
# Magic markers - optional - used by installation scripts and
47
# munin-config:
48
#
49
#%# family=manual
50
#%# capabilities=autoconf
51

    
52
CACHE_FILE=/var/lib/munin/plugin-state/FreeboxUptime.cache
53
CACHE_HOURS=3
54
NMAP=$(which nmap)
55
TCP_PORT=9100
56
FREEBOX_HOST=mafreebox.freebox.fr
57

    
58
# check if network connection is ok
59
if ping -c1 -w1 -s20 $FREEBOX_HOST 2>/dev/null > /dev/null; then
60
   PING=1
61
else
62
   PING=0
63
fi
64

    
65
if [ "$1" = "autoconf" ]; then
66
	if [ -z "$NMAP" ]; then
67
		echo "no (nmap not installed)"
68
		exit 1
69
	else
70
		if [ $PING -eq 0 ]; then
71
			echo "no (Freebox not reachable)"
72
			exit 2
73
		else
74
			echo yes 
75
			exit 0
76
		fi
77
	fi
78
fi
79

    
80

    
81
if [ "$1" = "config" ]; then
82

    
83
	echo 'graph_title Freebox Uptime'
84
	echo 'graph_category system'
85
	echo 'graph_args --base 1000 -l 0 '
86
	echo 'graph_vlabel uptime in days'
87
	graph_info="Shows the uptime of your freebox (cache: ${CACHE_HOURS}h"
88
	if [ -f $CACHE_FILE ]; then
89
		lastCheck=$(stat -c %z $CACHE_FILE | cut -d"." -f1)
90
		lastReboot=$(awk -F"@" '{print $2}' $CACHE_FILE)	
91
		graph_info="${graph_info} - last check: ${lastCheck} - last reboot: $lastReboot"
92
	else
93
		graph_info="${graph_info})"
94
	fi
95
	echo "graph_info ${graph_info}"
96
	echo 'uptime.label uptime'
97
	echo 'uptime.draw AREA'
98
	exit 0
99
fi
100

    
101

    
102
if [ -z "$NMAP" ]; then
103
	exit 1
104
elif [ $PING -eq 0 ]; then
105
	exit 2
106
else
107
	use_nmap=1
108
	if [ -f $CACHE_FILE ]; then
109
		ts_cache=$(stat -c %Y $CACHE_FILE)
110
		ts_now=$(date "+%s")
111
		sec_diff=$(expr $ts_now - $ts_cache)
112
		hours_diff=$(expr $sec_diff / 3600)
113
		if [ $hours_diff -lt $CACHE_HOURS ]; then
114
			use_nmap=0
115
			uptime=$(cat $CACHE_FILE | awk -F"@" '{print $1}')
116
		fi
117
	fi
118
	if [ $use_nmap -eq 1 ]; then
119
		uptime=$(nmap -O --osscan-guess -p${TCP_PORT} ${FREEBOX_HOST} | grep Uptime)
120
		if [ -z "$uptime" ]; then
121
			exit 3
122
		else
123
			lastReboot=$(echo $uptime | grep -o '(since .*)' | sed 's/(since //g')
124
			uptime=$(echo $uptime | awk '{print $2}')
125
			echo "${uptime}@${lastReboot}" > $CACHE_FILE
126
		fi
127
	fi
128
	echo "uptime.value ${uptime}"
129
fi