2 # collectd - OpenVZ collectd plugin
3 # Copyright (C) 2009 Jonathan Kolb
5 # This program is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # Jonathan Kolb <jon at b0g.us>
23 package Collectd::Plugins::OpenVZ;
28 use Collectd qw( :all );
30 my @cpu_instances = ('user', 'nice', 'system', 'idle', 'wait', 'interrupt', 'softirq', 'steal');
31 my @if_instances = ('if_octets', 'if_packets', 'if_errors');
32 my $vzctl = '/usr/sbin/vzctl';
33 my $vzlist = '/usr/sbin/vzlist';
39 my %v = (time => time(), interval => plugin_get_interval());
40 my (@veids, $veid, $name, $key, $val, $i, @lines, @parts, @counters);
42 @veids = map { s/ //g; $_; } split(/\n/, `$vzlist -Ho veid`);
44 foreach $veid (@veids)
46 ($name = `$vzlist -Ho name $veid`) =~ s/^\s*(.*?)\s*$/$1/;
47 $name = $veid if ($name =~ /^-$/);
51 #####################################################################
54 $v{'plugin'} = 'interface';
55 delete $v{'plugin_instance'};
57 @lines = split(/\n/, `$vzctl exec $veid cat /proc/net/dev`);
63 ($key = $parts[0]) =~ s/^\s*(.*?)\s*$/$1/;
64 ($val = $parts[1]) =~ s/^\s*(.*?)\s*$/$1/;
65 @counters = split(/ +/, $val);
67 $v{'type_instance'} = $key;
68 for ($key = 0; $key <= $#if_instances; ++$key)
70 $v{'type'} = $if_instances[$key];
71 $v{'values'} = [ $counters[$key], $counters[$key + 8] ];
72 plugin_dispatch_values(\%v);
76 #####################################################################
83 @lines = split(/\n/, `$vzctl exec $veid cat /proc/stat`);
86 next if (!/^cpu[0-9]/);
88 @counters = split(/ +/);
91 # Remove once OpenVZ bug 1376 is resolved
92 if (48485 == $counters[3])
94 $counters[3] = $last_stat->{"$veid-$i-idle"};
95 $counters[4] = $last_stat->{"$veid-$i-wait"};
99 $last_stat->{"$veid-$i-idle"} = $counters[3];
100 $last_stat->{"$veid-$i-wait"} = $counters[4];
103 $v{'plugin_instance'} = $i++;
104 for ($key = 0; $key <= $#counters; ++$key)
106 $v{'type_instance'} = $cpu_instances[$key];
107 $v{'values'} = [ $counters[$key] ];
108 plugin_dispatch_values(\%v);
112 #####################################################################
116 delete $v{'plugin_instance'};
119 $val = join(' ', map { (split)[1] } split(/\n/, `$vzctl exec $veid cat /proc/mounts`));
120 @lines = split(/\n/, `$vzctl exec $veid stat -tf $val`);
124 next if (0 == $parts[7]);
126 $val = substr($parts[0], 1);
127 $val = 'root' if ($val =~ /^$/);
130 $v{'type_instance'} = $val;
131 $v{'values'} = [ $parts[5] * ($parts[6] - $parts[7]), $parts[5] * $parts[7] ];
132 plugin_dispatch_values(\%v);
135 #####################################################################
138 $v{'plugin'} = 'load';
139 delete $v{'plugin_instance'};
141 delete $v{'type_instance'};
143 @parts = split(/ +/, `$vzctl exec $veid cat /proc/loadavg`);
144 $v{'values'} = [ $parts[0], $parts[1], $parts[2] ];
145 plugin_dispatch_values(\%v);
147 #####################################################################
150 my $ps_states = { 'paging' => 0, 'blocked' => 0, 'zombies' => 0, 'stopped' => 0,
151 'running' => 0, 'sleeping' => 0 };
152 my $state_map = { 'R' => 'running', 'S' => 'sleeping', 'D' => 'blocked',
153 'Z' => 'zombies', 'T' => 'stopped', 'W' => 'paging' };
155 $v{'plugin'} = 'processes';
156 delete $v{'plugin_instance'};
157 $v{'type'} = 'ps_state';
159 @lines = map { (split)[2] } split(/\n/, `$vzctl exec $veid cat '/proc/[0-9]*/stat'`);
160 foreach $key (@lines)
162 ++$ps_states->{$state_map->{$key}};
165 foreach $key (keys %{$ps_states})
167 $v{'type_instance'} = $key;
168 $v{'values'} = [ $ps_states->{$key} ];
169 plugin_dispatch_values(\%v);
172 #####################################################################
175 $v{'plugin'} = 'users';
176 delete $v{'plugin_instance'};
177 $v{'type'} = 'users';
178 delete $v{'type_instance'};
180 @lines = split(/\n/, `$vzctl exec $veid w -h`);
181 $v{'values'} = [ scalar(@lines) ];
182 plugin_dispatch_values(\%v);
188 plugin_register(TYPE_READ, 'OpenVZ', 'openvz_read');