Révision 28cf364e
Initial version
| plugins/other/if_uptime | ||
|---|---|---|
| 1 |
#!/bin/bash |
|
| 2 |
# This script is intended for use with Munin to monitor |
|
| 3 |
# network interface uptime. Usually this would be ppp0 or smth. |
|
| 4 |
# Tested under Ubuntu Gutsy. |
|
| 5 |
# v. 1.00, 12/15/2007 |
|
| 6 |
# (c) Alex Yanchenko (yanchenko{at}gmail.com), 2007
|
|
| 7 |
# Distributed under GPL v.3 (http://www.gnu.org/licenses/gpl-3.0.txt) |
|
| 8 |
# |
|
| 9 |
# The plugin can utilize automatic configuration, |
|
| 10 |
# here are the basic steps (require root privileges): |
|
| 11 |
# 1. Copy it as /usr/share/munin/plugins/if_uptime |
|
| 12 |
# 2. Customize interfaces to monitor |
|
| 13 |
# 3. Make executable: "chmod 755 /usr/share/munin/plugins/if_uptime" |
|
| 14 |
# 4. Run "munin-node-configure --shell", you should see smth like |
|
| 15 |
# "ln -s /usr/share/munin/plugins/if_uptime /etc/munin/plugins/if_uptime" |
|
| 16 |
# Multiple interface monitoring is supported as well. Customize below. |
|
| 17 |
# 5. Run the proposed command to create a link. |
|
| 18 |
# 6. To verify, run "munin-node-configure", you should notice the "if_uptime" record |
|
| 19 |
# |
|
| 20 |
# Plugin | Used | Suggestions |
|
| 21 |
# ------ | ---- | ----------- |
|
| 22 |
# if_uptime | yes | |
|
| 23 |
# |
|
| 24 |
# 7. Restart munin: "/etc/init.d/munin-node restart" |
|
| 25 |
# 8. Hold on for 5 minutes at most and watch the graph appear. |
|
| 26 |
# |
|
| 27 |
#%# family=contrib |
|
| 28 |
#%# capabilities=autoconf |
|
| 29 |
|
|
| 30 |
#----- PROPERTIES START -----# |
|
| 31 |
# An array of external interfaces to monitor, space-separated. |
|
| 32 |
# In case vnstat is installed, interface names will be fetced |
|
| 33 |
# from it, 'nicknames'included. |
|
| 34 |
INTERFACES=(ppp0) |
|
| 35 |
|
|
| 36 |
# Uptime metrics. |
|
| 37 |
# Literals of seconds, minutes, hours and days are accepted. |
|
| 38 |
METRICS=minutes |
|
| 39 |
#----- PROPERTIES END -----# |
|
| 40 |
|
|
| 41 |
# Function to get interface name from vnstat if it's available. |
|
| 42 |
# Accepts interface name as the only argument. |
|
| 43 |
function IF_NAME() {
|
|
| 44 |
ARG=$1 |
|
| 45 |
if [[ $(which vnstat &>/dev/null; echo $?) == 0 ]] |
|
| 46 |
then |
|
| 47 |
IF_NAME="$(vnstat | grep "$ARG" | cut -d" " -f2,3 | cut -d":" -f1)" |
|
| 48 |
else |
|
| 49 |
IF_NAME="$ARG" |
|
| 50 |
fi |
|
| 51 |
echo $IF_NAME |
|
| 52 |
} |
|
| 53 |
|
|
| 54 |
# Converts seconds (uptime value) into desired metrics value. |
|
| 55 |
# Accepts uptime value as the only argument. Relies on the |
|
| 56 |
# METRICS variable set above. |
|
| 57 |
function SECONDS_CONVERTER() {
|
|
| 58 |
ARG=$1 |
|
| 59 |
case $METRICS in |
|
| 60 |
seconds) |
|
| 61 |
echo "$ARG" |
|
| 62 |
;; |
|
| 63 |
minutes) |
|
| 64 |
echo $((ARG / 60)) |
|
| 65 |
;; |
|
| 66 |
days) |
|
| 67 |
echo $((ARG / 86400)) |
|
| 68 |
;; |
|
| 69 |
hours) |
|
| 70 |
echo $((ARG / 3600)) |
|
| 71 |
;; |
|
| 72 |
*) |
|
| 73 |
echo "value value" |
|
| 74 |
exit 1 |
|
| 75 |
;; |
|
| 76 |
esac |
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
# Helper function that collects and stores data. |
|
| 80 |
# Accepts interface name as the only argument. |
|
| 81 |
function FETCH_DATA() {
|
|
| 82 |
|
|
| 83 |
IF="$1" |
|
| 84 |
# File to store data between rounds. Per-line values: |
|
| 85 |
# bytes received |
|
| 86 |
# timestamp |
|
| 87 |
# total uptime |
|
| 88 |
FILE=/tmp/if_uptime_"$IF" |
|
| 89 |
|
|
| 90 |
if [[ !(-e "$FILE") ]] |
|
| 91 |
then |
|
| 92 |
cat > $FILE << EOF |
|
| 93 |
0 |
|
| 94 |
$(date +%s) |
|
| 95 |
0 |
|
| 96 |
EOF |
|
| 97 |
fi |
|
| 98 |
# Read previous values |
|
| 99 |
previous_received=$(head -n 1 < $FILE) |
|
| 100 |
previous_timestamp=$(head -n 2 < $FILE | tail -n 1) |
|
| 101 |
previous_uptime=$(tail -n 1 < $FILE) |
|
| 102 |
# Get current values |
|
| 103 |
current_received=$(grep "$IF" < /proc/net/dev | sed 's|: *|:|g' | cut -d":" -f2 | cut -d" " -f1) |
|
| 104 |
current_timestamp=$(date +%s) |
|
| 105 |
# Evaluate whether interface went down |
|
| 106 |
if [[ $current_received -gt $previous_received ]] |
|
| 107 |
then |
|
| 108 |
current_uptime=$(( $previous_uptime+($current_timestamp-$previous_timestamp) )) |
|
| 109 |
else |
|
| 110 |
current_uptime="0" |
|
| 111 |
fi |
|
| 112 |
# Write new values |
|
| 113 |
cat > $FILE << EOF |
|
| 114 |
$current_received |
|
| 115 |
$current_timestamp |
|
| 116 |
$current_uptime |
|
| 117 |
EOF |
|
| 118 |
|
|
| 119 |
} |
|
| 120 |
# Munin routines |
|
| 121 |
case "$1" in |
|
| 122 |
autoconf) |
|
| 123 |
if [[ $(ifconfig &> /dev/null; echo "$?") == 0 ]]; then |
|
| 124 |
echo yes |
|
| 125 |
exit 0 |
|
| 126 |
else |
|
| 127 |
echo "no (ifconfig doesn't work out)" |
|
| 128 |
exit 1 |
|
| 129 |
fi |
|
| 130 |
exit 0 |
|
| 131 |
;; |
|
| 132 |
config) |
|
| 133 |
cat << EOM |
|
| 134 |
graph_title Interface Uptime Meter |
|
| 135 |
graph_category network |
|
| 136 |
graph_info Get to know how often your ISP violates SLA. |
|
| 137 |
graph_vlabel Interface uptime, $METRICS |
|
| 138 |
graph_args --base 1000 --lower-limit 0 |
|
| 139 |
EOM |
|
| 140 |
for (( i=0; i<"${#INTERFACES[*]}"; i++ ))
|
|
| 141 |
do |
|
| 142 |
cat << EOM |
|
| 143 |
${INTERFACES[$i]}.draw LINE3
|
|
| 144 |
${INTERFACES[$i]}.label $(IF_NAME ${INTERFACES[$i]})
|
|
| 145 |
EOM |
|
| 146 |
done |
|
| 147 |
exit 0 |
|
| 148 |
;; |
|
| 149 |
*) |
|
| 150 |
# Print data for Munin |
|
| 151 |
for (( i=0; i<"${#INTERFACES[*]}"; i++ ))
|
|
| 152 |
do |
|
| 153 |
FETCH_DATA ${INTERFACES[$i]}
|
|
| 154 |
echo "${INTERFACES[$i]}.value $(SECONDS_CONVERTER $current_uptime)"
|
|
| 155 |
done |
|
| 156 |
exit 0 |
|
| 157 |
;; |
|
| 158 |
esac |
|
Formats disponibles : Unified diff