root / plugins / router / freeboxuptime @ 7cf2dda9
Historique | Voir | Annoter | Télécharger (3,53 ko)
| 1 | 2a2a73a6 | Dju | #!/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 | 17f78427 | Lars Kruse | # |
| 26 | 2a2a73a6 | Dju | # 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 | 8713eb37 | Lars Kruse | # if you don't want nmap to query your freebox each time, set CACHE_HOURS=n |
| 34 | 2a2a73a6 | Dju | # 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 | 4b2fcbf8 | Lars Kruse | CACHE_FILE=$MUNIN_PLUGSTATE/FreeboxUptime.cache |
| 53 | 2a2a73a6 | Dju | 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 | else |
||
| 69 | if [ $PING -eq 0 ]; then |
||
| 70 | echo "no (Freebox not reachable)" |
||
| 71 | else |
||
| 72 | 17f78427 | Lars Kruse | echo yes |
| 73 | 2a2a73a6 | Dju | fi |
| 74 | fi |
||
| 75 | e4cd049b | Lars Kruse | exit 0 |
| 76 | 2a2a73a6 | Dju | fi |
| 77 | |||
| 78 | |||
| 79 | if [ "$1" = "config" ]; then |
||
| 80 | |||
| 81 | echo 'graph_title Freebox Uptime' |
||
| 82 | 7f23c8cf | Kenyon Ralph | echo 'graph_category system' |
| 83 | 2a2a73a6 | Dju | echo 'graph_args --base 1000 -l 0 ' |
| 84 | echo 'graph_vlabel uptime in days' |
||
| 85 | graph_info="Shows the uptime of your freebox (cache: ${CACHE_HOURS}h"
|
||
| 86 | if [ -f $CACHE_FILE ]; then |
||
| 87 | lastCheck=$(stat -c %z $CACHE_FILE | cut -d"." -f1) |
||
| 88 | 17f78427 | Lars Kruse | lastReboot=$(awk -F"@" '{print $2}' $CACHE_FILE)
|
| 89 | 2a2a73a6 | Dju | graph_info="${graph_info} - last check: ${lastCheck} - last reboot: $lastReboot"
|
| 90 | else |
||
| 91 | graph_info="${graph_info})"
|
||
| 92 | fi |
||
| 93 | echo "graph_info ${graph_info}"
|
||
| 94 | echo 'uptime.label uptime' |
||
| 95 | echo 'uptime.draw AREA' |
||
| 96 | exit 0 |
||
| 97 | fi |
||
| 98 | |||
| 99 | |||
| 100 | if [ -z "$NMAP" ]; then |
||
| 101 | exit 1 |
||
| 102 | elif [ $PING -eq 0 ]; then |
||
| 103 | exit 2 |
||
| 104 | else |
||
| 105 | use_nmap=1 |
||
| 106 | if [ -f $CACHE_FILE ]; then |
||
| 107 | ts_cache=$(stat -c %Y $CACHE_FILE) |
||
| 108 | ts_now=$(date "+%s") |
||
| 109 | sec_diff=$(expr $ts_now - $ts_cache) |
||
| 110 | hours_diff=$(expr $sec_diff / 3600) |
||
| 111 | if [ $hours_diff -lt $CACHE_HOURS ]; then |
||
| 112 | use_nmap=0 |
||
| 113 | uptime=$(cat $CACHE_FILE | awk -F"@" '{print $1}')
|
||
| 114 | fi |
||
| 115 | fi |
||
| 116 | if [ $use_nmap -eq 1 ]; then |
||
| 117 | uptime=$(nmap -O --osscan-guess -p${TCP_PORT} ${FREEBOX_HOST} | grep Uptime)
|
||
| 118 | if [ -z "$uptime" ]; then |
||
| 119 | exit 3 |
||
| 120 | else |
||
| 121 | lastReboot=$(echo $uptime | grep -o '(since .*)' | sed 's/(since //g') |
||
| 122 | uptime=$(echo $uptime | awk '{print $2}')
|
||
| 123 | echo "${uptime}@${lastReboot}" > $CACHE_FILE
|
||
| 124 | fi |
||
| 125 | fi |
||
| 126 | echo "uptime.value ${uptime}"
|
||
| 127 | fi |
