Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / lxc / lxc_net @ 2ce1b321

Historique | Voir | Annoter | Télécharger (3,59 ko)

1 e3c40a04 Bjoern Boschman
#!/bin/sh
2
3
: << =cut
4
5
=head1 NAME
6
7
lxc_net - Munin plugin to graph traffic of active LXC containers.
8
9
=head1 APPLICABLE SYSTEMS
10
11
LXC container with "lxc.network.type=veth" and "lxc.network.veth.pair" settings.
12
13
=head1 CONFIGURATION
14
15
env.lxcpath - Set the path where LXC containers are stored, default: /var/lib/lxc
16
env.exclude - Removing containers from graphs, default: empty
17
18
  [lxc_net]
19
    env.lxcpath /var/lib/lxc
20
    env.exclude container1 container2
21
22
=head1 INTERPRETATION
23
24
This plugin reads a "lxc.network.veth.pair" setting from "config" file of each container,
25
because lxc-start command creates a random named veth device without the setting.
26
27
If your xen config (/var/lib/lxc/GUEST_NAME/config does not contain this parameter, 
28 fba800ae Veres Lajos
then you have to fill it, because if every guest restart generate new device name, then the graph will be useless
29 e3c40a04 Bjoern Boschman
( example config : lxc.network.veth.pair = vethsamba )
30
31
=head1 AUTHOR
32
33
mitty   mitty@mitty.jp
34
35
=head1 LICENSE
36
37
2-clause BSD License
38
39
=head1 MAGIC MARKERS
40
41
 #%# familiy=auto
42
 #%# capabilities=autoconf
43
44
=cut
45
46
. $MUNIN_LIBDIR/plugins/plugin.sh
47
48
lxcpath=${lxcpath:-/var/lib/lxc}
49
50
if [ "$1" = "autoconf" ]; then
51
    if [ ! -r /proc/net/dev ]; then
52
        echo "no (/proc/net/dev cannot be read)"
53
        exit 0
54
    fi
55
    if [ ! -e "$lxcpath" ]; then
56
        echo "no ($lxcdir is not present)"
57
        exit 0
58
    fi
59
    
60
    echo yes
61
fi
62
63
actives=""
64
for guest in `ls $lxcpath`; do
65
    if [ `echo $exclude | grep -c "\b$guest\b"` -eq 1 ]; then
66
        continue;
67
    fi
68
    if [ -f "$lxcpath/$guest/config" ]; then
69 9cc4259f Niklas Yann Wettengel
        devices=`grep '^lxc\.network\.veth\.pair[ \t]*=[ \t]*' $lxcpath/$guest/config | \
70 e3c40a04 Bjoern Boschman
            awk '{ split($0, a, /=/); gsub(/[ \t]/, "", a[2]); print a[2]; }'`
71 9cc4259f Niklas Yann Wettengel
        if [ -n "$devices" ]; then
72
            for device in $devices; do
73
                device_re=`echo $device | sed -e 's/\./\\\\./g'`
74
                if [ `grep -c "^ *$device_re:" /proc/net/dev` -eq 1 ]; then
75
                    actives="$actives $guest"
76
                    eval "dev_$(clean_fieldname $guest)=$device"
77
                fi
78
            done
79 e3c40a04 Bjoern Boschman
        fi
80
    fi
81
done
82
83
if [ "$1" = "config" ]; then
84
    echo "graph_title Network traffic"
85
    echo "graph_args --base 1000"
86
    echo "graph_vlabel bits in (-) / out (+) per ${graph_period}"
87 ff883dee dipohl
    echo "graph_category network"
88 e3c40a04 Bjoern Boschman
    echo "graph_info This graph shows the traffic of active LXC containers."
89
    
90
    for guestname in $actives; do
91
        guest=$(clean_fieldname $guestname)
92
        device=$(eval 'echo $dev_'$guest)
93
        bps="U"
94
        if [ -r /sys/class/net/$device/speed ]; then
95
            bps=$(cat /sys/class/net/$device/speed)
96
            bps=$(($bps * 1000 * 1000))
97
        fi
98
        
99
        echo "${guest}_down.label $guestname"
100
        echo "${guest}_down.type DERIVE"
101
        echo "${guest}_down.graph no"
102
        echo "${guest}_down.cdef ${guest}_down,8,*"
103
        echo "${guest}_down.min 0"
104
        echo "${guest}_down.max $bps"
105
        echo "${guest}_up.label $guestname"
106
        echo "${guest}_up.type DERIVE"
107
        echo "${guest}_up.negative ${guest}_down"
108
        echo "${guest}_up.cdef ${guest}_up,8,*"
109
        echo "${guest}_up.min 0"
110
        echo "${guest}_up.max $bps"
111
   done
112
   exit 0
113
fi
114
115
116
for guest in $actives; do
117
    guest=$(clean_fieldname $guest)
118
    device=$(eval 'echo $dev_'$guest)
119
    device_re=`echo $device | sed -e 's/\./\\\\./g'`
120
    line=`grep "^ *$device_re:" /proc/net/dev`
121
    echo -n "${guest}_down.value "
122
    echo $line | awk '{
123
            split($0, a, /: */); 
124
            print  $2;
125
    }'
126
    echo -n "${guest}_up.value "
127
    echo $line | awk '{
128
            split($0, a, /: */); 
129
            print  $10; 
130
        }'
131
done