summaryrefslogtreecommitdiff
path: root/files/plugins/xen_traffic_all
blob: 72f3b275bfee2e336b05f7e6f191f31de102759b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# Author: mario manno <projects@manno.name>
# Description: measure traffic for all xen hosts
# 
# 16.07.2008: improved the Plugin by Puzzle ITC to
# enable the traffic monitoring for domains
# with more than 1 one interface
# If there is more than one device, the domainname
# will get the vif name appended.
#
#%# family=auto
#%# capabilities=autoconf

# we cache xm list for 5 min for perfomance reasons
((find /var/lib/munin/plugin-state/xm_list.state -mmin -5 2>&1 | grep -qE '^\/var\/lib\/munin\/plugin-state\/xm_list\.state$') && \
    [ `cat /var/lib/munin/plugin-state/xm_list.state | wc -l` -gt 1 ]) || \
    /usr/sbin/xm list | grep -v "^Name .* ID" > /var/lib/munin/plugin-state/xm_list.state

if [ "$1" = "autoconf" ]; then
    if which xm > /dev/null ; then
        echo yes
    else
        echo "no (xm not found)"
        exit 1
    fi
    if [ -r /proc/net/dev ]; then
        echo yes
    else
        echo "no (/proc/net/dev not found)"
        exit 1
    fi
    exit 0
fi

# we update network devices only twice an hour
function net_state {
  dom=$1
  if [ `find /var/lib/munin/plugin-state/xm_net_$dom.state -mmin +30 2> /dev/null | wc -l` -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}'
    echo 'graph_args --base 1024 -l 0'
    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
      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
      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
    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
            awk -v name="$name" -v interface="$dev" \
                'BEGIN { gsub(/\./, "\\.", interface) } \
                $1 ~ "^" interface ":" {
                split($0, a, /: */); $0 = a[2]; \
                    print name"Down.value " $1 "\n"name"Up.value " $9 \
                }' \
            /proc/net/dev
       done
    fi
done