#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

temperature - Plugin to monitor external termperature as reported by TEMPer USB thermometer

=head1 CONFIGURATION

This plugin must run with root privileges

=head1 CONFIGURATION EXAMPLE

/etc/munin/plugin-conf.d/global or other file in that dir must contain:

 [temperature]
  user root

=head1 AUTHORS

Original Author: Day Barr http://www.DayBarr.com

=head2 CONTRIBUTORS

=over 4

=item Magnus Sulland

For the essential CPAN module Device::USB::PCSensor::HidTEMPer without which this would have been a lot more difficult.

=back

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut
use strict;
use warnings;
use Munin::Plugin;

if ($ARGV[0] and $ARGV[0] eq "autoconf") {
    eval {
        require Device::USB::PCSensor::HidTEMPer;
        Device::USB::PCSensor::HidTEMPer->import();
    };
    if ($@) {
	print "no (Device::USB::PCSensor::HidTEMPer module not found)\n";
	exit 0;
    }
    print "yes\n";
    exit 0;
}

require Device::USB::PCSensor::HidTEMPer;

my $pcsensor = Device::USB::PCSensor::HidTEMPer->new();
my @devices = $pcsensor->list_devices();

if ($ARGV[0] and $ARGV[0] eq "config") {
    print "graph_vlabel Celsius\n";
    print "graph_title USB Thermometers\n";
    print "graph_category sensors\n";
    print "graph_scale no\n";
    print "graph_info This graph shows the temperature(s) reported by USB Thermometer(s) attached to the machine.\n";
    my $i=0;
    foreach my $device ( @devices ){
        print "temp_".$i."_internal.label Internal $i\n" if defined $device->internal();
        print "temp_".$i."_external.label External $i\n" if defined $device->external();
        ++$i;
    }
    exit 0;
}

my $i=0;
foreach my $device ( @devices ){
    print "temp_".$i."_internal.value ".$device->internal()->celsius()."\n" if defined $device->internal();
    print "temp_".$i."_external.value ".$device->external()->celsius()."\n" if defined $device->external();
    ++$i;
}
exit 0;

# vim:syntax=perl:ts=4
