From 630f4d609e1e58a74abb04963ce68c9fc4bab4a1 Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 6 Dec 2012 22:11:10 +0100 Subject: replace xen-cpu plugin with a better plugin --- files/plugins/xen-cpu | 121 ----------------------------- files/plugins/xen_cpu | 189 ++++++++++++++++++++++++++++++++++++++++++++++ manifests/plugins/dom0.pp | 2 +- 3 files changed, 190 insertions(+), 122 deletions(-) delete mode 100755 files/plugins/xen-cpu create mode 100644 files/plugins/xen_cpu diff --git a/files/plugins/xen-cpu b/files/plugins/xen-cpu deleted file mode 100755 index b456a14..0000000 --- a/files/plugins/xen-cpu +++ /dev/null @@ -1,121 +0,0 @@ -#!/usr/bin/perl -wT -# -# Script to minitor the cpu usage of Xen domains -# -# Author: Adam Crews shroom com> -# -# License: GPL -# Based on the origional xen script from Matthias Pfafferodt, syntron at web.de -# -# Note: Your munin config must run this as root. -# -# Parameters -# config (required) -# autoconf (optional - used by munin-config) -# -#%# family=auto -#%# capabilities=autoconf - -# Define where to find xm tools -my $XM = '/usr/sbin/xm'; -my $XMTOP = '/usr/sbin/xentop'; - -############## -# You should not need to edit anything below here -# - -use strict; - -$ENV{PATH} = '/bin:/usr/bin:/usr/sbin'; - -# we cache xm list for 5 min for perfomance reasons -system('((find /var/lib/munin/plugin-state/xm_list.state -mmin -5 2>&1 | grep -qE \'^\/var\/lib\/munin\/plugin-state\/xm_list\.state$\') && [ `cat /var/lib/munin/plugin-state/xm_list.state | wc -l` -gt 1 ]) || /usr/sbin/xm list | grep -v "^Name .* Console$" > /var/lib/munin/plugin-state/xm_list.state'); -system('((find /var/lib/munin/plugin-state/xm_top.state -mmin -5 2>&1 | grep -qE \'^\/var\/lib\/munin\/plugin-state\/xm_top\.state$\') && [ `cat /var/lib/munin/plugin-state/xm_top.state | wc -l` -gt 1 ]) || /usr/sbin/xentop -b -i1 > /var/lib/munin/plugin-state/xm_top.state'); - -my $arg; undef($arg); -if (defined($ARGV[0])) { - $arg = 'config' if ($ARGV[0] eq 'config'); - $arg = 'autoconf' if ($ARGV[0] eq 'autoconf'); - - if ( "$arg" eq 'autoconf') { - if ( -e $XM && -e $XMTOP ) { - print "yes\n"; - exit 0; - } else { - print "no ($XM and/or $XMTOP not found\n"; - exit 1; - } - } - - if ( "$arg" eq 'config') { - my %cnf; undef(%cnf); - %cnf = ( - 'graph_title' => 'Xen Domain CPU Usage', - 'graph_args' => '--base 1000 -l 0 --upper-limit 100 --rigid', - 'graph_vlabel' => 'Percent (%)', - 'graph_category' => 'xen', - 'graph_info' => 'Display the % of CPU Usage for each domain', - ); - - my @domains = `cat /var/lib/munin/plugin-state/xm_list.state`; - # the header line is not in the cached file - #shift(@domains); # we dont need the header line - my $cnt = "0"; - foreach my $domain ( @domains ) { - my ($dom,undef) = split(/\s/, $domain, 2); - # we need to change - and . to _ or things get weird with the graphs - # some decent quoting would probably fix this, but this works for now - $dom =~ s/[-.]/_/g; - - $cnf{ "$dom" . '.label' } = "$dom"; - $cnf{ "$dom" . '.draw' } = 'STACK'; - $cnf{ "$dom" . '.min' } = '0'; - $cnf{ "$dom" . '.max' } = '100'; - $cnf{ "$dom" . '.info' } = '% CPU used for ' . "$dom"; - - if ( "$cnt" == "0") { $cnf{$dom.'.draw'} = 'AREA'; } - $cnt++; - } - - foreach my $key (sort(keys(%cnf))) { - print "$key $cnf{$key}\n"; - } - exit 0; - } -} - -# Nothing was passed as an argument, so let's just return the proper values - -my @stats = `cat /var/lib/munin/plugin-state/xm_top.state`; - -# remove the first 4 items that are junk that we don't need. -shift(@stats); -shift(@stats); -shift(@stats); -shift(@stats); - -my %vals; undef(%vals); - -foreach my $domain (@stats) { - # trim the leading whitespace - $domain =~ s/^\s+//; - my @v_tmp = split(/\s+/, $domain); - - # we need to change - and . to _ or things get weird with the graphs - # some decent quoting would probably fix this, but this works for now - $v_tmp[0] =~ s/[-.]/_/g; - - $vals{$v_tmp[0]}{'cpu_percent'} = $v_tmp[3]; - $vals{$v_tmp[0]}{'vcpu'} = $v_tmp[8]; - if ( $vals{$v_tmp[0]}{'vcpu'} =~ m/n\/a/ ) { - my $cpu = `grep -c "processor" < /proc/cpuinfo`; - if ( $cpu =~ m/^(\d+)$/ ) { - $vals{$v_tmp[0]}{'vcpu'} = $1; - } - } -} - -foreach my $key (sort(keys(%vals))) { - print "$key.value " . ($vals{$key}{'cpu_percent'}/$vals{'Domain_0'}{'vcpu'}), "\n"; -} - diff --git a/files/plugins/xen_cpu b/files/plugins/xen_cpu new file mode 100644 index 0000000..382c8b5 --- /dev/null +++ b/files/plugins/xen_cpu @@ -0,0 +1,189 @@ +#!/usr/bin/perl -w +# +# xen_cpu_v2.pl 1.00 +# Script to minitor the CPU usage of Xen domains +# Zoltan HERPAI (c) 2009, wigyori@uid0.hu +# +# Based loosely on Adam Crews' xen_cpu script +# +# This script tries to measure the CPU usage of the Xen guests +# accurately. +# The problem with the current monitoring script is that these +# scripts use the CPU output of xentop or xm list, which might be +# inaccurate due to the resources used up at the time of the query by +# the xm or xentop command. +# +# This script stores the previous value of the CPU sec value of the given +# guests in a tempfile, then does some simple calculations to get the real +# CPU usage percentage of the guests. +# + +#%# family=auto +#%# capabilities=autoconf + +use strict; +use POSIX; + +# Define where to find xm tools +my $XM = '/usr/sbin/xm'; +my $XMTOP = '/usr/sbin/xentop'; +my $curtime = time(); +my $basename = `/usr/bin/env basename $0`; chop ($basename); +my $TEMPFILE = "/tmp/$basename"; + +my $debug = 0; + +############## +# You should not need to edit anything below here +# + +$ENV{PATH} = '/bin:/usr/bin:/usr/sbin'; + +my $arg; +if ( defined($ARGV[0]) ) +{ + if ( $ARGV[0] eq 'config' ) + { + $arg = 'config'; + } + if ( $ARGV[0] eq 'autoconf' ) + { + $arg = 'autoconf'; + } + + if ( $arg eq 'autoconf') + { + if ( -e $XM && -e $XMTOP ) + { + print "yes\n"; + exit 0; + } + else + { + print "no ($XM and/or $XMTOP not found\n"; + exit 1; + } + } + + if ( $arg eq 'config' ) + { + my %cnf; + %cnf = ( + 'graph_title' => 'Xen Domain CPU Usage v2', + 'graph_args' => '--base 1000 -l 0 --upper-limit 100 --rigid', + 'graph_vlabel' => 'Percent (%)', + 'graph_category' => 'xen', + 'graph_info' => 'Display the % of CPU Usage for each domain', + ); + + my @domains = `$XM list`; + my $cnt = 0; + shift(@domains); # we dont need the header line + foreach my $domain ( @domains ) + { + my ($dom,undef) = split(/\s/, $domain, 2); + $dom =~ s/[-.]/_/g; + $cnf{ "$dom" . '.label' } = "$dom"; + $cnf{ "$dom" . '.draw' } = 'STACK'; + $cnf{ "$dom" . '.min' } = '0'; + $cnf{ "$dom" . '.max' } = '100'; + $cnf{ "$dom" . '.info' } = '% CPU used for ' . "$dom"; +# $cnf{ "$dom" . '.draw' } = 'AREA'; + if ( $cnt == 0 ) + { + $cnf{ "$dom" . '.draw' } = 'AREA'; + } + $cnt++; + } + foreach my $key (sort(keys(%cnf))) + { + print "$key $cnf{$key}\n"; + } + exit 0; + } +} + +my @xmlist = `$XM list`; +shift (@xmlist); + +my %dom; my $name; my $oldtime; +# Read old data +if ( -e $TEMPFILE ) +{ + open(FH, "<", $TEMPFILE) or die $!; + $oldtime = ; + + if ( $debug ) + { + print "Oldtime: $oldtime\n"; + } + + while () + { + # Get the guest name and its CPU usage, and store it in $dom + $_ =~ /(\S+)\s+\S+\s+\S+\s+\d+\s+\S+\s+(\S+)/; + $dom{$1}->{'oldtime'} = $2; + } + + close FH; +} + +my $diff; my $cpusum = 0; +foreach my $domain ( @xmlist ) +{ + # Get the domains' name and current CPU usage, store it in $dom + $domain =~ /(\S+)\s+\S+\s+\S+\s+\d+\s+\S+\s+(\S+)/; + $dom{$1}->{'newtime'} = $2; + + $diff = $dom{$1}->{'newtime'} - $dom{$1}->{'oldtime'}; + $diff = sprintf("%.2f", $diff); + + # Calc the diff between old and new cputime, or reset the counter + if ( $diff < 0 ) + { + $diff = $dom{$1}->{'newtime'}; + } + $dom{$1}->{'diff'} = $diff; + + # Calc a sum CPU usage + $cpusum = $cpusum + $diff; +} + +my $numcpus = `$XM info |grep nr_cpus |cut -d \: -f 2`; +my $timediff = $curtime - $oldtime; +my $tcpuavail = $numcpus * $timediff; + +if ( $debug ) +{ + print "UsedCPUtime sum: $cpusum\n"; + print "CPUs: $numcpus\n"; + print "Timediff: $timediff\n"; + print "Total CPU time available: $tcpuavail\n"; +} + +my $key; my $value; +while (($key, $value) = each %dom) +{ + # Calc a percentage based on the used CPU time sum + my $tmp = 0; + $tmp = ( $dom{$key}->{'diff'} / $cpusum ) * 100; + $dom{$key}->{'pc_time'} = sprintf("%.2f", $tmp); + + # Calc a percentage based on the _total_ available CPU time + $tmp = 0; + $tmp = ( $dom{$key}->{'diff'} / $tcpuavail ) * 100; + $dom{$key}->{'pc_tcpu'} = sprintf("%.2f", $tmp); + + if ( $debug ) + { + print "$key newtime: ".$dom{$key}->{'newtime'}.", oldtime: ".$dom{$key}->{'oldtime'}.", diff: ".$dom{$key}->{'diff'}.", pc_bytime ".$dom{$key}->{'pc_time'}.", pc_bytcpu ".$dom{$key}->{'pc_tcpu'}."\n"; + } + print "$key.value ".$dom{$key}->{'pc_tcpu'}."\n"; +} + +# We'll need to log out the current "xm list" output, and the current time also. +open(FH, ">", $TEMPFILE) or die $!; +print FH $curtime."\n"; +print FH @xmlist; +close FH; + diff --git a/manifests/plugins/dom0.pp b/manifests/plugins/dom0.pp index ed4f62c..44995fc 100644 --- a/manifests/plugins/dom0.pp +++ b/manifests/plugins/dom0.pp @@ -1,6 +1,6 @@ class munin::plugins::dom0 { munin::plugin::deploy { - [ 'xen', 'xen-cpu', 'xen_memory', 'xen_mem', + [ 'xen', 'xen_cpu', 'xen_memory', 'xen_mem', 'xen_vm', 'xen_vbd', 'xen_traffic_all' ]: config => 'user root'; } -- cgit v1.2.3