[collectd] Plugin cpu and "merged" stat

Greg greg at easyflirt.com
Wed Jan 14 14:32:52 CET 2009


> If someone do the patch, yes.
> I'm not C experienced, so I'm starting a perl plugin that will do this. 
> About perl plugin, did someone have a 4.4 real one, it will save me time ?
>
>   

I have wrote this plugin:

package Collectd::Plugins::Cpu;

use strict;
use warnings;
use Switch;

use Collectd qw( :all );

sub cpu_read
{
	my $vl = { plugin => 'cpu' };
	open(ST, "/proc/stat") or return 0;
	while (<ST>)
	{
		next unless /^cpu /;
		chomp;
		my @values = split(/ /);
		for(my $i=0; $i<scalar(@values); $i++)
		{
			my $type = "";
			switch($i)
			{
				case 1	{ $type = "user"; }
				case 2	{ $type = "nice"; }
				case 3	{ $type = "system"; }
				case 4	{ $type = "idle"; }
				case 5	{ $type = "wait"; }
				case 6	{ $type = "interrupt"; }
				case 7	{ $type = "softirq"; }
				case 8	{ $type = "steal"; }
				else	{ $type = ""; }
			}

			if ($type)
			{
				$vl->{'values'} = [ $values[$i] ];
				plugin_dispatch_values($type, $vl);
			}
		}
	}
	close ST;
	return 1;
}

sub cpu_write
{
	my ($type, $ds, $vl) = @_;
	for (my $i = 0; $i < scalar (@$ds); ++$i) 
	{
		print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
	}
	return 1;
}
plugin_register (TYPE_DATASET, "user", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "nice", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "system", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "idle", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "wait", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "interrupt", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "softirq", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);
plugin_register (TYPE_DATASET, "steal", [{ name => 'user', type => DS_TYPE_COUNTER, min => 0, max => 4294967295 }]);

plugin_register (TYPE_READ, "cpu", "cpu_read");
plugin_register (TYPE_WRITE, "cpu", "cpu_write");



-- 
Greg




More information about the collectd mailing list