make this plugin caching more stable
authormh <mh@immerda.ch>
Thu, 6 Dec 2012 21:58:02 +0000 (22:58 +0100)
committermh <mh@immerda.ch>
Thu, 6 Dec 2012 21:58:02 +0000 (22:58 +0100)
files/plugins/xen_traffic_all

index c5bbfbb..b6f668d 100644 (file)
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # Author: mario manno <projects@manno.name>
 # Description: measure traffic for all xen hosts
 # 
@@ -32,6 +32,21 @@ if [ "$1" = "autoconf" ]; then
     exit 0
 fi
 
+# we update network devices only twice an hour
+function net_state {
+  dom=$1
+  (find /var/lib/munin/plugin-state/xm_net_$dom.state -mmin -30 > /dev/null 2>&1) && \
+    [ `cat /var/lib/munin/plugin-state/xm_net_$dom.state | wc -l` -gt 0 ]
+  if [ $? -gt 0 ] || [ ! -f /var/lib/munin/plugin-state/xm_net_$dom.state ]; then
+    content=$(/usr/sbin/xm network-list $dom)
+    if [ $? -eq 0 ]; then
+      echo "${content}" | egrep "^[0-9]+" | sed 's@^.*vif/\([0-9]*\)/\([0-9]*\).*$@vif\1.\2@' > /var/lib/munin/plugin-state/xm_net_$dom.state
+    else
+      [ -f /var/lib/munin/plugin-state/xm_net_$dom.state ] && rm /var/lib/munin/plugin-state/xm_net_$dom.state
+    fi
+  fi
+}
+
 if [ "$1" = "config" ]; then
     echo 'graph_title Xen Traffic'
     echo 'graph_vlabel bits received (-) / sent (+) per ${graph_period}'
@@ -39,43 +54,38 @@ if [ "$1" = "config" ]; then
     echo 'graph_category xen'
     DOMAINS=$(cat /var/lib/munin/plugin-state/xm_list.state | awk '{print $1}' | egrep -v "^(Name|Domain-0)")
     for dom in $DOMAINS; do
-        # we update network devices only twice an hour
-    ((find /var/lib/munin/plugin-state/xm_net_$dom.state -mmin -30 > /dev/null 2>&1) && \
-      [ `cat /var/lib/munin/plugin-state/xm_net_$dom.state | wc -l` -gt 0 ]) || \
-        (/usr/sbin/xm network-list $dom |\
-          egrep "^[0-9]+" | sed 's@^.*vif/\([0-9]*\)/\([0-9]*\).*$@vif\1.\2@' > /var/lib/munin/plugin-state/xm_net_$dom.state)
-      devs=$(cat /var/lib/munin/plugin-state/xm_net_$dom.state)
-      real_name=$( echo $dom | sed -e's/-/_/g' )
-      name=$real_name
-      for dev in $devs; do
-        if [ ${#devs} -gt 1 ]; then
-          name=$real_name"_"`echo $dev | sed 's/\./\_/'`
-        fi
+      net_state $dom
+      if [ -f /var/lib/munin/plugin-state/xm_net_$dom.state ]; then
+        devs=$(cat /var/lib/munin/plugin-state/xm_net_$dom.state)
+        real_name=$( echo $dom | sed -e's/-/_/g' )
+        name=$real_name
+        for dev in $devs; do
+          if [ ${#devs} -gt 1 ]; then
+            name=$real_name"_"`echo $dev | sed 's/\./\_/'`
+          fi
         
-        echo $name'Down.label received'
-        echo $name'Down.type COUNTER'
-        echo $name'Down.graph no'
-        echo "${name}Down.cdef ${name}Down,8,*"
-        echo "${name}Up.label ${name}"
-        echo $name'Up.type COUNTER'
-        echo "${name}Up.negative ${name}Down"
-        echo "${name}Up.cdef ${name}Up,8,*"
-      done
+          echo $name'Down.label received'
+          echo $name'Down.type COUNTER'
+          echo $name'Down.graph no'
+          echo "${name}Down.cdef ${name}Down,8,*"
+          echo "${name}Up.label ${name}"
+          echo $name'Up.type COUNTER'
+          echo "${name}Up.negative ${name}Down"
+          echo "${name}Up.cdef ${name}Up,8,*"
+        done
+      fi
     done
     exit 0
 fi
 
 DOMAINS=$(cat /var/lib/munin/plugin-state/xm_list.state | awk '{print $1}' | egrep -v "^(Name|Domain-0)")
 for dom in $DOMAINS; do
-    # we update network devices only twice an hour
-    ((find /var/lib/munin/plugin-state/xm_net_$dom.state -mmin -30 > /dev/null 2>&1) && \
-      [ `cat /var/lib/munin/plugin-state/xm_net_$dom.state | wc -l` -gt 0 ]) || \
-      (/usr/sbin/xm network-list $dom |\
-        egrep "^[0-9]+" | sed 's@^.*vif/\([0-9]*\)/\([0-9]*\).*$@vif\1.\2@' > /var/lib/munin/plugin-state/xm_net_$dom.state)
-    devs=$(cat /var/lib/munin/plugin-state/xm_net_$dom.state)
-    real_name=$( echo $dom | sed -e's/-/_/g' )
-    name=$real_name
-    for dev in $devs; do
+    net_state $dom
+    if [ -f /var/lib/munin/plugin-state/xm_net_$dom.state ]; then
+      devs=$(cat /var/lib/munin/plugin-state/xm_net_$dom.state)
+      real_name=$( echo $dom | sed -e's/-/_/g' )
+      name=$real_name
+      for dev in $devs; do
             if [ ${#devs} -gt 1 ]; then
                 name=$real_name"_"`echo $dev | sed 's/\./\_/'`
             fi
@@ -86,6 +96,7 @@ for dom in $DOMAINS; do
                     print name"Down.value " $1 "\n"name"Up.value " $9 \
                 }' \
             /proc/net/dev
-     done
+       done
+    fi
 done