#!/usr/bin/perl # Nagios plugin for monitoring currentcost CC128 devices # Written by Adrian Bridgett (c) Bitcube Ltd. 2009 # Released under GPL v3 license # For opsview use lib '/usr/local/nagios/perl/lib', '/usr/local/nagios/lib'; use warnings; use strict; use Nagios::Plugin; my $baudrate=57600; my $dev="/dev/ttyUSB0"; my $VERSION = "0.02"; my $np = Nagios::Plugin->new( usage => "Usage: %s [-t] [-w warning] [-c critical] [-d device]", version => $VERSION, blurb => "Checks current electicity usage on CurrentCost CC128 devices", ); $np->add_arg( spec => "warning|w=i", help => qq{-w, --warning=INTEGER Warning if electricity usage (in Watts) is more than this (default: 1000)}, default => 1000, ); $np->add_arg( spec => "critical|c=i", help => qq{-c, --critical=INTEGER Critical if electricity usage (in Watts) is more than this (default: 2000)}, default => 2000, ); $np->add_arg( spec => "device|d=s", help => qq{-d, --device=STRING Device that currentcost is plugged into (default: $dev)}, default => $dev, ); $np->add_arg( spec => "temp|t", help => qq{-t, --temp Monitor temperature, rather than current}, ); $np->getopts; $dev = $np->opts->device if ($np->opts->device); (-r "$dev") || die "Unable to read from $dev\n"; (system ("/bin/stty -F $dev $baudrate >/dev/null") == 0) or die "Unable to run stty on $dev ($?)"; open CC, "<$dev" or $np->nagios_exit(UNKNOWN, "Unable to open device $dev"); $_ = ; close CC; if (! $np->opts->temp && /\0*(\d+)\<\/watts\>/) { my $watts = $1; $np->add_perfdata(label => "Watts", value => $watts); $np->nagios_exit(CRITICAL, "Watts: $watts") if ($watts >= $np->opts->critical); $np->nagios_exit(WARNING, "Watts: $watts") if ($watts >= $np->opts->warning); $np->nagios_exit(OK, "Watts: $watts"); } elsif ($np->opts->temp && /\(-?[\d\.]+)\<\/tmpr\>/) { my $temp = $1; $np->add_perfdata(label => "Temp", value => $temp); $np->nagios_exit(CRITICAL, "Temp: $temp") if ($temp >= $np->opts->critical); $np->nagios_exit(WARNING, "Temp: $temp") if ($temp >= $np->opts->warning); $np->nagios_exit(OK, "Temp: $temp"); } else { $np->nagios_exit(UNKNOWN, "Failed to match output ($_)"); }