<?php
#
# Author George Hansper george@hansper.id.au
# Contributer to work without iowait data: Kirk Hammond
# Plugin: check_cpu.py (by Kirk Hammond)
# Note: George wrote a version of the plugin based on the original by Kirk that included iowait time.
## Kirk has taken the template written by George and modified it for his check_cpu.py script that does not include iowait.

#   1st graph: Overall CPU usage with Overall IO Wait
#   2nd graph: CPU usage for each core (up to 8 cores)
#   subsequent graphs: repeat of 2nd for additional CPU cores if present

# Change the value of $max_cpus_per_graph to suit your taste
# Values higher than 16 will re-use colors from the $cpu_colors list
$max_cpus_per_graph = 8;


$cpu_colors = [ 'c00000','c08000','c0c000','00c000','00a0a0','0000c0','c000c0','808080',
		'e06060','e0a060','e0e060','60e060','60c0c0','6060e0','e060e0','c0c0c0' ];

##########################################################################################
#error_log("names: ". join(" ",array_keys($NAME) ) );
#error_log("values: ". join(" ",array_values($NAME) ) );


$ndx_iowait_total=0;
for ( $ndx=1; $ndx <= count($NAME); $ndx++ ) {
	error_log("$ndx: $NAME[$ndx]");
	if( preg_match("/[0-9]$/",$NAME[$ndx]) ) {
		$ndx_cpus[] = $ndx;
	} else {
		# This is really always 1
		$ndx_total = $ndx;
	}
}

$cpu_list = preg_grep("/[0-9]$/",$NAME);

$def[1]=""; $opt[1]=""; $ds_name[1]="";
$ds_name[1] = "Total CPU (all cores)";
$opt[1] = "--vertical-label \"cpu percent\" -l0  --title \"Total CPU for $hostname / $servicedesc\" ";

# Graph Total (average) CPU usage
$def[1]  .= rrd::def("total_cpu",           $RRDFILE[$ndx_total], $DS[$ndx_total], "MAX");
$def[1]  .= rrd::area("total_cpu", "#c0c0ff");
$def[1]  .= rrd::line1("total_cpu", "#0000c0",$NAME[$ndx_total]."\t\t");
$def[1]  .= rrd::gprint("total_cpu", array("LAST", "AVERAGE", "MAX"), "%6.2lf");

if ($WARN[$ndx_total] != "") {
    $def[1] .= "HRULE:".$WARN[$ndx_total]."#FFFF00 ";
}
if ($CRIT[$ndx_total] != "") {
    $def[1] .= "HRULE:".$CRIT[$ndx_total]."#FF0000 ";       
}

$def_n=1;
# Graph Per-Core CPU usage
$color_ndx=0;

for( $cpu_n=0; $cpu_n<count($ndx_cpus); $cpu_n++) {
	if ( $cpu_n % $max_cpus_per_graph == 0 ) {
		# Start a new graph
		$def_n++;
		$cpu_start = $ndx_cpus[$cpu_n];
		if ( $cpu_n + $max_cpus_per_graph >= count($ndx_cpus) ) {
			$cpu_end=$ndx_cpus[count($ndx_cpus)-1];
		} else {
			$cpu_end = $ndx_cpus[$cpu_n + $max_cpus_per_graph - 1];
		}
		$def[$def_n]='';
		$ds_name[$def_n] = "CPU per core";
		$opt[$def_n] = "--vertical-label \"cpu percent\" -l0  --title \"Single-core CPU for ".$NAME[$cpu_start]."-".$NAME[$cpu_end]." $hostname / $servicedesc\" ";
		if ($WARN[$ndx_cpus[0]] != "") {
		    $def[$def_n] .= "HRULE:".$WARN[$ndx_cpus[0]]."#FFFF00 ";
		}
		if ($CRIT[$ndx_cpus[0]] != "") {
		    $def[$def_n] .= "HRULE:".$CRIT[$ndx_cpus[0]]."#FF0000 ";       
		}
	}
	$ndx=$ndx_cpus[$cpu_n];
	$name = $NAME[$ndx];
	$color = $cpu_colors[$color_ndx];
	$def[$def_n]  .= rrd::def("$name",           $RRDFILE[$ndx], $DS[$ndx], "MAX");
	$def[$def_n]  .= rrd::line1("$name", "#$color",$NAME[$ndx]."\t");
	$def[$def_n]  .= rrd::gprint("$name", array("LAST", "AVERAGE", "MAX"), "%6.2lf ");
	if ( $color_ndx == $max_cpus_per_graph -1 ) {
		$def_n++;
	}
	if ( count($ndx_cpus) <= 6 && $color_ndx<4 ) {
		$color_ndx += 2;
	} else {
		$color_ndx++;
	}
	$color_ndx %= min($max_cpus_per_graph,count($cpu_colors));
}

#error_log($def[1]);
#error_log($def[2]);
#error_log($def[3]);
?>
