#!/usr/pkg/bin/perl
#
# Plugin to monitor temperature inside Sun systems
#
# Usage: Place in /etc/munin/node.d/ (or link it there  using ln -s)
#
# Parameters understood:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#
# Revision 1.1	2004/04/16 Richard van den Berg <richard@vdberg.org>
#
#%# family=auto
#%# capabilities=autoconf

$arch=`uname -m`;
chomp($arch);
$prtdiag="/usr/platform/$arch/sbin/prtdiag";
@tempnam=();
@tempval=();

if($ARGV[0] eq "config") {
	printconfig();
	exit(0);
}

if($ARGV[0] eq "autoconf") {
	if(! -x $prtdiag) {
		print "no\n";
		exit(1);
	}
	gettemps();
	if($#tempnam>=0) {
		print "yes\n";
		exit(0);
	}

	print "no\n";
	exit(1);
}

gettemps();
printtemps();

exit(0);

sub printtemps {
	for($i=0;$i<=$#tempnam;$i++){
		$name=$tempnam[$i];
		$name =~ s/\s+/_/go;
		print "temp_$name.value $tempval[$i]\n";
	}
}

sub gettemps {
	open(PRTDIAG,"$prtdiag -v|") || return;
	$found=0;
	while(<PRTDIAG>) {
		$found=1 if(m/^[^\s].*temperature/i);
		$found=0 if(m/^$/);
		if($found) {
			$tmp=substr($_,8);
			if($tmp =~ m/ ([0-9]+)/g) {
			  push(@tempval,$1);
				push(@tempnam,substr($_,0,index($_,"  ")));
				$tempnam[$#tempnam] =~ s/^\s//go;
				$tempnam[$#tempnam] =~ s#^.*/##go;
			}
		}
	}
	close(PRTDIAG);
}

sub printconfig {
	gettemps();
	print "graph_title Temperature\n";
	print "graph_args -l 0\n";
	print "graph_category sensors\n";
	print "graph_vlabel temp in C\n";
	for($i=0;$i<=$#tempnam;$i++){
		$name=$tempnam[$i];
		$name =~ s/\s+/_/go;
		print "temp_$name.label $tempnam[$i]\n";
	}
}
