ESensors Advanced Environmental Monitor EM32-Xe
Posted: Fri Jan 18, 2019 1:51 pm
When will Nagios support the Advanced Environmental Monitor EM32-Xe? I asked about this last year and was told it was coming.
Support for Nagios products and services
https://support.nagios.com/forum/
Code: Select all
curl -k -L 'http://ESENSORIP/index.html?em345678' -v
curl -k -L 'http://ESENSORIP/status.xml' -v
curl -k -L 'https://ESENSORIP/index.html?em345678' -v
curl -k -L 'https://ESENSORIP/status.xml' -vCode: Select all
#!/usr/bin/perl
#
# License to use per the terms of the GNU Public License (GPL)
#
# CHANGES:
# Modified by Nagios Enterprises January 2010 to return performance data
#
#$Id$
# configure nagios utils
use lib "/usr/local/nagios/libexec";
use utils qw($TIMEOUT %ERRORS);
use strict;
use Getopt::Long;
use Data::Dumper;
use LWP;
use XML::Simple;
Getopt::Long::Configure('bundling');
my $opt_debug = 0;
my $opt_ver = 0;
my $opt_help = 0;
my $opt_typ = '';
my $opt_temp = 'x/x,x/x';
my $opt_hum = 'x/x,x/x';
my $opt_illum = 'x/x,x/x';
my $opt_timeout = $TIMEOUT;
GetOptions
( "version|v" => \$opt_ver,
"debug|d" => \$opt_debug,
"help|h" => \$opt_help,
"type=s" => \$opt_typ,
"temp|temperature=s" => \$opt_temp,
"hum|humidity=s" => \$opt_hum,
"illum|illumination=s" => \$opt_illum,
);
my $sensor = $ARGV[0] || &syntax();
if ($opt_help) {
&syntax();
}
#############################################
my @temp = split(/[\/,]/, $opt_temp);
my @hum = split(/[\/,]/, $opt_hum);
my @illum = split(/[\/,]/, $opt_illum);
my $vals = &read_sensor($sensor, $opt_timeout);
my @msgs = ();
my $condition = $ERRORS{'OK'};
&check_value('temperature', $vals, @temp);
&check_value('humidity', $vals, @hum);
&check_value('illumination', $vals, @illum);
if ($#msgs > -1) {
print join("; ", @msgs);
}
if ($opt_typ eq '') {
#print "[";
print "Temp: $vals->{'temperature'} $vals->{'temp-unit'}, ";
print "Humidity: $vals->{'humidity'}, ";
print "Illum: $vals->{'illumination'}\n";
#print "]\n";
} else {
#print "[";
if ($opt_typ eq "temp") {
print "Temp: $vals->{'temperature'} $vals->{'temp-unit'}";
print "|";
print "temperature=$vals->{'temperature'}$vals->{'temp-unit'};;;; ";
} elsif ($opt_typ eq "hum") {
print "Humidity: $vals->{'humidity'}";
print "|";
print "humidity=$vals->{'humidity'};;;; ";
} elsif ($opt_typ eq "illum") {
print "Illum: $vals->{'illumination'}";
print "|";
print "illumination=$vals->{'illumination'};;;; ";
}
print "\n";
}
exit($condition);
#############################################
sub check_value {
my ($type, $vals, $w_lo, $w_hi, $c_lo, $c_hi) = @_;
my $current = $vals->{$type};
my $uctype = ucfirst $type;
if ($c_lo ne 'x' && $current < $c_lo) {
push(@msgs, "CRITICAL LOW $uctype (<$c_lo) - ");
$condition = $ERRORS{'CRITICAL'};
} elsif ($c_hi ne 'x' && $current > $c_hi) {
push(@msgs, "CRITICAL HIGH $uctype (>$c_hi) - ");
$condition = $ERRORS{'CRITICAL'};
} elsif ($w_lo ne 'x' && $current < $w_lo) {
push(@msgs, "WARNING LOW $uctype (<$w_lo) - ");
$condition = $ERRORS{'WARNING'};
} elsif ($w_hi ne 'x' && $current > $w_hi) {
push(@msgs, "WARNING HIGH $uctype (>$w_hi) - ");
$condition = $ERRORS{'WARNING'};
}
};
sub syntax {
print <<END;
Syntax: $0 [options] {sensor}
{sensor} is the address of sensor on network (name or IP)
--debug
print debug messages to stdout
--timeout=x
how long to wait before failing. Default=$opt_timeout
--type={all|temp|hum|illum}
which sensor data you want to retrieve.
If you did not specify this option, then this plugin will return all
sensor data.
--{temp|hum|illum}=warnlow/warnhi,critlow/crithi
four values are required. 'x' is used to specify no value.
Example:
check_em01.pl --type=temp --temp=65/75,60/80
check_em01.pl --type=all --temp=65/75,60/80 --hum=30/40,25/50 --illum=40/70,30/80
END
exit($ERRORS{'UNKNOWN'});
}
sub read_sensor {
my ($host,$timeout) = @_;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => "http://$host/status.xml");
my $remote = $ua->request($req);
if (!$remote) {
&debug("connect error: $!\n");
print "failed to connect\n";
exit($ERRORS{'UNKNOWN'});
}
&debug("connected to $host:80\n");
my $xml = new XML::Simple;
my $data = XMLin($remote->content);
&debug(Dumper($data)."\n");
my $didalarm = 0;
my $hdrs = {};
my $read = {};
eval {
local $SIG{'ALRM'} = sub { $didalarm=1; die "alarm\n"; };
alarm($timeout);
$read->{'temp-unit'} = "$data->{tun0}";
$read->{'temperature'} = "$data->{tm0}";
$read->{'humidity'} = "$data->{hu0}";
$read->{'illumination'} = "$data->{il0}";
alarm(0);
};
if ($@) {
die if $didalarm != 1;
&debug("timeout(alarm) during sensor read\n");
print "Unable to read sensor\n";
exit($ERRORS{'UNKNOWN'});
}
return $read;
};
sub debug {
my ($msg) = @_;
if ($opt_debug) {
print STDERR $msg;
}
};
Code: Select all
yum install perl-XML-Simple -y
chown apache.nagios /usr/local/nagios/libexec/check_em.pl
chmod +x /usr/local/nagios/libexec/check_em.pl 192.168.1.4 --type=all
/usr/local/nagios/libexec/check_em.pl 192.168.1.4 --type=all --temp=65/75,60/80 --hum=30/40,25/50 --illum=40/70,30/80Code: Select all
chown apache.nagios /usr/local/nagios/libexec/check_em.pl
chmod +x /usr/local/nagios/libexec/check_em.pl
/usr/local/nagios/libexec/check_em.pl 192.168.1.4
/usr/local/nagios/libexec/check_em.pl 192.168.1.4 --type=all --temp=65/75,60/80 --hum=30/40,25/50 --illum=40/70,30/80Code: Select all
vi /usr/local/nagios/libexec/check_em.plCode: Select all
:set ff?Code: Select all
:set ff=unix