#!/bin/perl use Device::SerialPort; use Getopt::Long; my $port = Device::SerialPort->new("/dev/ttyUSB0"); my $char = ""; my $data = ""; my $warning = ""; my $critical = ""; my $help = ""; my $fahrenheit = ""; my $celsius = ""; GetOptions ('h' => \$help, 'm' => \$celsius, 'f' => \$fahrenheit ,'w=s' => \$warning, 'c=s' => \$critical); if ( $help ) { print "Temperature checker for sensor by Todd\n"; print "--------------------------------------\n"; print "-h this help listing\n"; print "-m get temperature in Celsius\n"; print "-f get temperature in Fahrenheit\n"; print "-w # Warning if above this temperature\n"; print "-c # critical if above this temperature\n"; exit 0; } $port->baudrate(9600); # you may change this value $port->databits(8); # but not this and the two following $port->parity("none"); $port->stopbits(1); my $tEnd = time()+2; # 2 seconds in future while (time()< $tEnd) { # end latest after 2 seconds my $c = $port->lookfor(); # char or nothing next if $c eq ""; # restart if noting last; } while (1) { # and all the rest of the gremlins as they come in one piece my $c = $port->lookfor(); # get the next one last if $c eq ""; # or we're done } while (1) { $char = $port->lookfor(); if ($char == "Temperature Sensor Ready") { if ( $celsius ) { $port->write("C\r\n"); }else{ $port->write("F\r\n"); } }else{ $data = $char; last; } } chop($data); if ($data > $critical ) { print "CRITICAL: Temperature is $data | Temperature=$data;$warning;$crtical;0;100\n"; exit 2; } if ( $data > $warning ) { print "WARNING: Temperature is $data | Temperature=$data;$warning;$crtical;0;100\n"; exit 1; } if ( $data < $warning ) { print "OK: Temperature is $data | Temperature=$data;$warning;$critical;0;100\n"; exit 0; } print "The Temp s $char\n"; print "Warning is set to $warning\n"; print "Critical is set to $critical\n"; exit 2;