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 $vzctl = '/usr/sbin/vzctl';
31 my $vzlist = '/usr/sbin/vzlist';
33 # Since OpenVZ is container based, all guests see all the host's CPUs,
34 # and would report the same data. So we disable CPU by default.
35 my $enable_interface = 1;
39 my $enable_processes = 1;
42 sub interface_read($$) {
45 my ($key, $val, @lines, @parts, @counters);
46 my @if_instances = ('if_octets', 'if_packets', 'if_errors');
47 my %v = _build_report_hash($name);
49 $v{'plugin'} = 'interface';
50 delete $v{'plugin_instance'};
52 @lines = split(/\n/, `$vzctl exec $veid cat /proc/net/dev`);
57 ($key = $parts[0]) =~ s/^\s*(.*?)\s*$/$1/;
58 ($val = $parts[1]) =~ s/^\s*(.*?)\s*$/$1/;
59 @counters = split(/ +/, $val);
61 $v{'type_instance'} = $key;
62 for ($key = 0; $key <= $#if_instances; ++$key) {
63 $v{'type'} = $if_instances[$key];
64 $v{'values'} = [ $counters[$key], $counters[$key + 8] ];
65 plugin_dispatch_values(\%v);
73 my ($key, $val, $i, @lines, @counters);
74 my @cpu_instances = ('user', 'nice', 'system', 'idle', 'wait', 'interrupt', 'softirq', 'steal');
76 my %v = _build_report_hash($name);
82 @lines = split(/\n/, `$vzctl exec $veid cat /proc/stat`);
84 next if (!/^cpu[0-9]/);
86 @counters = split(/ +/);
89 # Remove once OpenVZ bug 1376 is resolved
90 if (48485 == $counters[3]) {
91 $counters[3] = $last_stat->{"$veid-$i-idle"};
92 $counters[4] = $last_stat->{"$veid-$i-wait"};
95 $last_stat->{"$veid-$i-idle"} = $counters[3];
96 $last_stat->{"$veid-$i-wait"} = $counters[4];
99 $v{'plugin_instance'} = $i++;
100 for ($key = 0; $key <= $#counters; ++$key) {
101 $v{'type_instance'} = $cpu_instances[$key];
102 $v{'values'} = [ $counters[$key] ];
103 plugin_dispatch_values(\%v);
111 my ($key, $val, @lines, @parts);
112 my %v = _build_report_hash($name);
115 delete $v{'plugin_instance'};
118 $val = join(' ', map { (split)[1] } split(/\n/, `$vzctl exec $veid cat /proc/mounts`));
119 @lines = split(/\n/, `$vzctl exec $veid stat -tf $val`);
122 next if (0 == $parts[7]);
124 $val = substr($parts[0], 1);
125 $val = 'root' if ($val =~ /^$/);
128 $v{'type_instance'} = $val;
129 $v{'values'} = [ $parts[5] * ($parts[6] - $parts[7]), $parts[5] * $parts[7] ];
130 plugin_dispatch_values(\%v);
137 my ($key, $val, @lines, @parts);
138 my %v = _build_report_hash($name);
140 $v{'plugin'} = 'load';
141 delete $v{'plugin_instance'};
143 delete $v{'type_instance'};
145 @parts = split(/ +/, `$vzctl exec $veid cat /proc/loadavg`);
146 $v{'values'} = [ $parts[0], $parts[1], $parts[2] ];
147 plugin_dispatch_values(\%v);
150 sub processes_read($$) {
153 my ($key, $val, @lines);
154 my %v = _build_report_hash($name);
156 my $ps_states = { 'paging' => 0, 'blocked' => 0, 'zombies' => 0, 'stopped' => 0,
157 'running' => 0, 'sleeping' => 0 };
158 my $state_map = { 'R' => 'running', 'S' => 'sleeping', 'D' => 'blocked',
159 'Z' => 'zombies', 'T' => 'stopped', 'W' => 'paging' };
161 $v{'plugin'} = 'processes';
162 delete $v{'plugin_instance'};
163 $v{'type'} = 'ps_state';
165 @lines = map { (split)[2] } split(/\n/, `$vzctl exec $veid cat '/proc/[0-9]*/stat'`);
166 foreach $key (@lines) {
167 ++$ps_states->{$state_map->{$key}};
170 foreach $key (keys %{$ps_states}) {
171 $v{'type_instance'} = $key;
172 $v{'values'} = [ $ps_states->{$key} ];
173 plugin_dispatch_values(\%v);
180 my ($key, $val, @lines);
181 my %v = _build_report_hash($name);
183 $v{'plugin'} = 'users';
184 delete $v{'plugin_instance'};
185 $v{'type'} = 'users';
186 delete $v{'type_instance'};
188 @lines = split(/\n/, `$vzctl exec $veid w -h`);
189 $v{'values'} = [ scalar(@lines) ];
190 plugin_dispatch_values(\%v);
193 sub _build_report_hash($) {
195 return (time => time(), interval => plugin_get_interval(), host => $name);
199 my (@veids, $veid, $name);
201 @veids = map { s/ //g; $_; } split(/\n/, `$vzlist -Ho veid`);
203 foreach $veid (@veids) {
204 ($name = `$vzlist -Ho name $veid`) =~ s/^\s*(.*?)\s*$/$1/;
205 ($name = `$vzlist -Ho hostname $veid`) =~ s/^\s*(.*?)\s*$/$1/ if($name =~ /^-$/);
206 $name = $veid if ($name =~ /^-$/);
208 if($enable_interface) {
209 interface_read($veid, $name);
213 cpu_read($veid, $name);
217 df_read($veid, $name);
221 load_read($veid, $name);
224 if($enable_processes) {
225 processes_read($veid, $name);
229 users_read($veid, $name);
236 plugin_register(TYPE_READ, 'OpenVZ', 'openvz_read');