root / plugins / ping / ping-with-ceil @ d2161137
Historique | Voir | Annoter | Télécharger (2,53 ko)
| 1 | bb7a4bc2 | Michele Petrazzo | #!/bin/sh |
|---|---|---|---|
| 2 | # |
||
| 3 | # Copyright (C) 2010 Michele Petrazzo <michele.petrazzo@gmail.com> |
||
| 4 | # |
||
| 5 | # This program is free software; you can redistribute it and/or |
||
| 6 | # modify it under the terms of the GNU General Public License |
||
| 7 | # as published by the Free Software Foundation; version 2 dated June, |
||
| 8 | # 1991. |
||
| 9 | # |
||
| 10 | # This program is distributed in the hope that it will be useful, |
||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | # GNU General Public License for more details. |
||
| 14 | # |
||
| 15 | # You should have received a copy of the GNU General Public License |
||
| 16 | # along with this program; if not, write to the Free Software |
||
| 17 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
| 18 | # |
||
| 19 | # Python (2.5+) version of the plugin to monitor ping times. |
||
| 20 | # Evolution from the standard shipped with munin by adding a ceil. |
||
| 21 | # |
||
| 22 | # Thanks to "Jimmy Olsen" for the base. |
||
| 23 | # |
||
| 24 | # Parameters: |
||
| 25 | # |
||
| 26 | # ping_args - Arguments to ping (default "-c 2") |
||
| 27 | # ping_args2 - Arguments after the host name (required for Solaris) |
||
| 28 | # ping - Ping program to use |
||
| 29 | # host - Host to ping |
||
| 30 | # |
||
| 31 | # Arguments for Solaris: |
||
| 32 | # ping_args -s |
||
| 33 | # ping_args2 56 2 |
||
| 34 | # |
||
| 35 | |||
| 36 | file_host=`basename $0 | sed 's/^ping_ceil_//g'` |
||
| 37 | host=${host:-${file_host:-www.google.com}}
|
||
| 38 | max_ping="250" # Leave empty if not need or an integer (in ms) |
||
| 39 | |||
| 40 | test -z "$ping" && ping="ping" |
||
| 41 | test -z "$ping_args" && ping_args="-c 2" |
||
| 42 | |||
| 43 | cmd="$ping $ping_args $ping_args2" |
||
| 44 | |||
| 45 | if [ "$1" = "config" ]; then |
||
| 46 | echo "graph_title Ping times to $host" |
||
| 47 | echo 'graph_args --base 1000 -l 0' |
||
| 48 | echo 'graph_vlabel seconds' |
||
| 49 | echo 'graph_category network' |
||
| 50 | echo 'graph_info This graph shows ping RTT statistics.' |
||
| 51 | echo "ping.label $host" |
||
| 52 | echo "ping.info Ping RTT statistics for $host." |
||
| 53 | echo 'ping.draw LINE2' |
||
| 54 | echo 'packetloss.label packet loss' |
||
| 55 | echo 'packetloss.graph no' |
||
| 56 | exit 0 |
||
| 57 | fi |
||
| 58 | |||
| 59 | |||
| 60 | ( |
||
| 61 | cat << EOF |
||
| 62 | import os |
||
| 63 | from subprocess import Popen, PIPE |
||
| 64 | bash_cmd = "$cmd" |
||
| 65 | command = bash_cmd.split() + ["$host"] |
||
| 66 | max_ping = float("$max_ping") if "$max_ping" else 0
|
||
| 67 | |||
| 68 | try: |
||
| 69 | c = Popen(command, stdout=PIPE, stderr=PIPE) |
||
| 70 | except OSError: |
||
| 71 | print "command error:", command |
||
| 72 | raise |
||
| 73 | |||
| 74 | out, err = c.communicate() |
||
| 75 | |||
| 76 | for line in out.split("\n"):
|
||
| 77 | if "packet loss" in line: |
||
| 78 | print "packetloss.value", line.split(",")[2].split()[0].replace("%", "")
|
||
| 79 | elif "rtt min/avg/max/mdev" in line: |
||
| 80 | v = float(line.split("=")[1].split("/")[1])
|
||
| 81 | v = min(v, max_ping) if max_ping else v |
||
| 82 | print "ping.value", "%.6f" % (v / 1000) |
||
| 83 | |||
| 84 | EOF |
||
| 85 | ) | python |
