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 # We probably don't care about loopback transfer
43 my @ignored_interfaces = ( "lo" );
46 my ($veid, $name) = @_;
47 my @rx_fields = qw(if_octets if_packets if_errors drop fifo frame compressed multicast);
48 my @tx_fields = qw(if_octets if_packets if_errors drop fifo frame compressed);
49 my %v = _build_report_hash($name);
51 my @lines = `$vzctl exec $veid cat /proc/net/dev`;
53 for my $line (@lines) {
54 # skip explanatory text
57 $line =~ s/^\s+|\s+$//g;
59 my ($iface, %rx, %tx);
61 # read /proc/net/dev fields
62 ($iface, @rx{@rx_fields}, @tx{@tx_fields}) = split /[: ]+/, $line;
64 # Skip this interface if it is in the ignored list
65 next if grep { $iface eq $_ } @ignored_interfaces;
67 for my $instance (qw(if_octets if_packets if_errors)) {
68 plugin_dispatch_values({
69 'plugin' => 'interface',
70 'plugin_instance' => $iface,
72 'values' => [ $rx{$instance}, $tx{$instance} ],
82 my ($key, $val, $i, @lines, @counters);
83 my @cpu_instances = ('user', 'nice', 'system', 'idle', 'wait', 'interrupt', 'softirq', 'steal');
85 my %v = _build_report_hash($name);
91 @lines = split(/\n/, `$vzctl exec $veid cat /proc/stat`);
93 next if (!/^cpu[0-9]/);
95 @counters = split(/ +/);
98 # Remove once OpenVZ bug 1376 is resolved
99 if (48485 == $counters[3]) {
100 $counters[3] = $last_stat->{"$veid-$i-idle"};
101 $counters[4] = $last_stat->{"$veid-$i-wait"};
104 $last_stat->{"$veid-$i-idle"} = $counters[3];
105 $last_stat->{"$veid-$i-wait"} = $counters[4];
108 $v{'plugin_instance'} = $i++;
109 for ($key = 0; $key <= $#counters; ++$key) {
110 $v{'type_instance'} = $cpu_instances[$key];
111 $v{'values'} = [ $counters[$key] ];
112 plugin_dispatch_values(\%v);
120 my ($key, $val, @lines, @parts);
121 my %v = _build_report_hash($name);
124 delete $v{'plugin_instance'};
127 $val = join(' ', map { (split)[1] } split(/\n/, `$vzctl exec $veid cat /proc/mounts`));
128 @lines = split(/\n/, `$vzctl exec $veid stat -tf $val`);
131 next if (0 == $parts[7]);
133 $val = substr($parts[0], 1);
134 $val = 'root' if ($val =~ /^$/);
137 $v{'type_instance'} = $val;
138 $v{'values'} = [ $parts[5] * ($parts[6] - $parts[7]), $parts[5] * $parts[7] ];
139 plugin_dispatch_values(\%v);
146 my ($key, $val, @lines, @parts);
147 my %v = _build_report_hash($name);
149 $v{'plugin'} = 'load';
150 delete $v{'plugin_instance'};
152 delete $v{'type_instance'};
154 @parts = split(/ +/, `$vzctl exec $veid cat /proc/loadavg`);
155 $v{'values'} = [ $parts[0], $parts[1], $parts[2] ];
156 plugin_dispatch_values(\%v);
162 my ($key, $val, @lines);
163 my %v = _build_report_hash($name);
165 my $ps_states = { 'paging' => 0, 'blocked' => 0, 'zombies' => 0, 'stopped' => 0,
166 'running' => 0, 'sleeping' => 0 };
167 my $state_map = { 'R' => 'running', 'S' => 'sleeping', 'D' => 'blocked',
168 'Z' => 'zombies', 'T' => 'stopped', 'W' => 'paging' };
170 $v{'plugin'} = 'processes';
171 delete $v{'plugin_instance'};
172 $v{'type'} = 'ps_state';
174 @lines = map { (split)[2] } split(/\n/, `$vzctl exec $veid cat '/proc/[0-9]*/stat'`);
175 foreach $key (@lines) {
176 ++$ps_states->{$state_map->{$key}};
179 foreach $key (keys %{$ps_states}) {
180 $v{'type_instance'} = $key;
181 $v{'values'} = [ $ps_states->{$key} ];
182 plugin_dispatch_values(\%v);
189 my ($key, $val, @lines);
190 my %v = _build_report_hash($name);
192 $v{'plugin'} = 'users';
193 delete $v{'plugin_instance'};
194 $v{'type'} = 'users';
195 delete $v{'type_instance'};
197 @lines = split(/\n/, `$vzctl exec $veid w -h`);
198 $v{'values'} = [ scalar(@lines) ];
199 plugin_dispatch_values(\%v);
202 sub _build_report_hash {
204 return (time => time(), interval => plugin_get_interval(), host => $name);
208 my (@veids, $veid, $name);
210 @veids = map { s/ //g; $_; } split(/\n/, `$vzlist -Ho veid`);
212 foreach $veid (@veids) {
213 ($name = `$vzlist -Ho name $veid`) =~ s/^\s*(.*?)\s*$/$1/;
214 ($name = `$vzlist -Ho hostname $veid`) =~ s/^\s*(.*?)\s*$/$1/ if($name =~ /^-$/);
215 $name = $veid if ($name =~ /^-$/);
217 if($enable_interface) {
218 interface_read($veid, $name);
222 cpu_read($veid, $name);
226 df_read($veid, $name);
230 load_read($veid, $name);
233 if($enable_processes) {
234 processes_read($veid, $name);
238 users_read($veid, $name);
245 plugin_register(TYPE_READ, 'OpenVZ', 'openvz_read');