Révision 8d941d04
Adding simple and useful ping plugin
| plugins/network/ping/ping | ||
|---|---|---|
| 1 |
#!/bin/bash |
|
| 2 |
|
|
| 3 |
# ping revision 2 (Jul 2012) |
|
| 4 |
# |
|
| 5 |
# This plugin shows ping statistics for several hosts on a single graph |
|
| 6 |
# |
|
| 7 |
# Configuration: |
|
| 8 |
# Add [ping] section with 'hosts' set to space-separated list of hostnames or addresses. |
|
| 9 |
# You can prefix host name with '<type>:' to change IP resolution mode, |
|
| 10 |
# e.g. 'AAAA:domain.tld' will request IPv6 record. |
|
| 11 |
# Always prefix IPv6 addresses with 'AAAA:' (internal limitation). |
|
| 12 |
# |
|
| 13 |
# Dependencies: |
|
| 14 |
# host util is required |
|
| 15 |
# |
|
| 16 |
# OS: |
|
| 17 |
# Linux |
|
| 18 |
# |
|
| 19 |
# Author: Artem Sheremet <dot.doom@gmail.com> |
|
| 20 |
# |
|
| 21 |
|
|
| 22 |
function normalize() {
|
|
| 23 |
echo ${1//[^a-zA-Z0-9]/_}
|
|
| 24 |
} |
|
| 25 |
|
|
| 26 |
function host_type() {
|
|
| 27 |
case $1 in |
|
| 28 |
*:*) |
|
| 29 |
echo ${1/:*/}
|
|
| 30 |
;; |
|
| 31 |
*) |
|
| 32 |
echo A |
|
| 33 |
;; |
|
| 34 |
esac |
|
| 35 |
} |
|
| 36 |
|
|
| 37 |
function host_name() {
|
|
| 38 |
echo "${1/*:/}"
|
|
| 39 |
} |
|
| 40 |
|
|
| 41 |
function ipof() {
|
|
| 42 |
host -t $(host_type $1) $(host_name $1) | |
|
| 43 |
sort | grep -Eo 'address .*' | cut -d' ' -f2 | head -1 |
|
| 44 |
} |
|
| 45 |
|
|
| 46 |
if [[ "$1" == "config" ]]; then |
|
| 47 |
echo 'graph_title Ping statistics |
|
| 48 |
graph_vlabel Time,ms |
|
| 49 |
graph_category network |
|
| 50 |
graph_info ping statistics for several hosts' |
|
| 51 |
for host in $hosts; do |
|
| 52 |
echo "$(normalize $host).label $(host_name $host) ($(ipof $host))" |
|
| 53 |
done |
|
| 54 |
exit 0 |
|
| 55 |
fi |
|
| 56 |
|
|
| 57 |
SYNC_FILE=/tmp/munin-ping-sync |
|
| 58 |
echo -n >"$SYNC_FILE" |
|
| 59 |
|
|
| 60 |
COUNT=0 |
|
| 61 |
for host in $hosts; do |
|
| 62 |
{
|
|
| 63 |
case $(host_type $host) in |
|
| 64 |
AAAA) |
|
| 65 |
PING=ping6 |
|
| 66 |
;; |
|
| 67 |
*) |
|
| 68 |
PING=ping |
|
| 69 |
;; |
|
| 70 |
esac |
|
| 71 |
TIME=$($PING -w 1 -c 1 `host_name $host` | awk ' |
|
| 72 |
BEGIN {
|
|
| 73 |
found = 0 |
|
| 74 |
t = 0 |
|
| 75 |
} |
|
| 76 |
|
|
| 77 |
$1 == "rtt" { found = 1; t = $4; }
|
|
| 78 |
|
|
| 79 |
END {
|
|
| 80 |
if (found == 1) {
|
|
| 81 |
split(t, parts, "/"); |
|
| 82 |
print parts[1] |
|
| 83 |
} else {
|
|
| 84 |
print "U" |
|
| 85 |
} |
|
| 86 |
} |
|
| 87 |
') |
|
| 88 |
|
|
| 89 |
echo $(normalize $host).value $TIME |
|
| 90 |
echo ok >>"$SYNC_FILE" |
|
| 91 |
} & |
|
| 92 |
let COUNT=$COUNT+1 |
|
| 93 |
done |
|
| 94 |
|
|
| 95 |
# wait for background processes to finish |
|
| 96 |
while [ $(wc -l <"$SYNC_FILE") -lt $COUNT ]; do |
|
| 97 |
sleep 0.2 |
|
| 98 |
done |
|
Formats disponibles : Unified diff