#!/usr/bin/perl # nagios: +epn =pod =head1 NAME check_enviromux_mini.pl - Check status of all sensors on the Enviromux Mini =head1 SYNOPSIS This script will check the status of all sensors on an Enviromux Mini Server Environment Monitoring System device. This script will check the following sensors on the device: =over 4 =item * Temperature Sensor 1-2 =item * Humidity Sensor 1-2 =item * Dry Contact 1-4 =item * Water Status =back All thresholds are set from the device administrative interface, so no threshold specifications need to be passed in by the user. The script will output Nagios perfdata for all sensors for trending purposes. If a sensor is not active, it will not be output in perfdata. =back =cut sub check_enviromux_mini { use strict; use FindBin; use lib "$FindBin::Bin/lib"; use Nagios::Plugin::SNMP; use Nenm::Utils; my $LABEL = 'ENVIROMUX-MINI'; my $USAGE = <new( 'shortname' => $LABEL, 'usage' => $USAGE ); $PLUGIN->getopts; $Nenm::Utils::DEBUG = $PLUGIN->opts->get('snmp-debug'); my $BASE_OID = '1.3.6.1.4.1.3699.1.1.3'; my %sensors = ( 'temperatureSensor1' => {qw( 1.1.1.0 temperatureSensor1CurrentValue 1.1.2.0 temperatureSensor1Alert 2.2.1.0 temperatureSensor1Name 2.2.2.0 temperatureSensor1Unit 2.2.3.0 temperatureSensor1LowThreshold 2.2.4.0 temperatureSensor1HighThreshold )}, 'temperatureSensor2' => {qw( 1.2.1.0 temperatureSensor2CurrentValue 1.2.2.0 temperatureSensor2Alert 2.3.1.0 temperatureSensor2Name 2.3.2.0 temperatureSensor2Unit 2.3.3.0 temperatureSensor2LowThreshold 2.3.4.0 temperatureSensor2HighThreshold )}, 'humiditySensor1' => {qw( 1.3.1.0 humiditySensor1CurrentValue 1.3.2.0 humiditySensor1Alert 2.4.1.0 humiditySensor1Name 2.4.2.0 humiditySensor1LowThreshold 2.4.3.0 humiditySensor1HighThreshold )}, 'humiditySensor2' => {qw( 1.4.1.0 humiditySensor2CurrentValue 1.4.2.0 humiditySensor2Alert 2.5.1.0 humiditySensor2Name 2.5.2.0 humiditySensor2LowThreshold 2.5.3.0 humiditySensor2HighThreshold )}, 'dryContact1' => {qw( 1.5.1.0 dryContact1Status 1.5.2.0 dryContact1Alert 2.6.1.0 dryContact1Name 2.6.2.0 dryContact1AlertStatus )}, 'dryContact2' => {qw( 1.6.1.0 dryContact2Status 1.6.2.0 dryContact2Alert 2.7.1.0 dryContact2Name 2.7.2.0 dryContact2AlertStatus )}, 'dryContact3' => {qw( 1.7.1.0 dryContact3Status 1.7.2.0 dryContact3Alert 2.8.1.0 dryContact3Name 2.8.2.0 dryContact3AlertStatus )}, 'dryContact4' => {qw( 1.8.1.0 dryContact4Status 1.8.2.0 dryContact4Alert 2.9.1.0 dryContact4Name 2.9.2.0 dryContact4AlertStatus )}, 'water' => {qw( 1.9.1.0 waterStatus 1.9.2.0 waterAlert 2.10.1.0 waterName 2.10.2.0 waterAlertStatus )} ); my @critical; my @ok; my $perfdata = ""; for my $sensor (sort keys %sensors) { if ($sensor =~ m/temperature/) { $perfdata .= check_temp($sensor, $sensors{$sensor}, \@ok, \@critical); } elsif ($sensor =~ m/umidity/) { $perfdata .= check_humidity($sensor, $sensors{$sensor}, \@ok, \@critical); } elsif ($sensor =~ m/ontact/) { $perfdata .= check_contact($sensor, $sensors{$sensor}, \@ok, \@critical); } elsif ($sensor =~ m/water/) { $perfdata .= check_water($sensor, $sensors{$sensor}, \@ok, \@critical); } } my $output = "$LABEL "; my $level = OK; if (scalar(@critical) > 0) { $output .= 'CRITICAL - ' . join(', ', @critical) . ' '; $level = CRITICAL; } if (scalar(@ok) > 0) { $output .= ' OK - ' . join(', ', @ok); } print "$output | $perfdata \n"; return $level; sub check_temp { my $name = shift; my $data = shift; my $ok = shift; my $critical = shift; Nenm::Utils::debug("Checking temperature sensor $name"); my $rs = snmp_get($data); if ($rs->{"${name}CurrentValue"} eq "655350") { Nenm::Utils::debug("$name not active: skipping"); return ''; } my $value = sprintf("%.2f", $rs->{"${name}CurrentValue"} / 10); my $unit = $rs->{"${name}Unit"}; my $low = $rs->{"${name}LowThreshold"}; my $high = $rs->{"${name}HighThreshold"}; my $s_unit = ($unit =~ m/^(.)/)[0]; my $output = $rs->{"${name}Name"} . ": $value$s_unit"; if ($rs->{"${name}Alert"} == 1) { $output .= " - ALERT "; if ($value >= $high) { $output .= "(>= $high$s_unit)"; } if ($value <= $low) { $output .= "(<= $low$s_unit)"; } push(@$critical, $output); } else { push(@$ok, $output); } my $perf_label = $name; $perf_label =~ s/([a-z])([A-Z])/$1_\L$2/g; return "'temperature_${perf_label}'=$value$s_unit;0;$low:$high "; } sub check_humidity { my $name = shift; my $data = shift; my $ok = shift; my $critical = shift; Nenm::Utils::debug("Checking humidity sensor $name"); my $rs = snmp_get($data); if ($rs->{"${name}CurrentValue"} eq "655350") { Nenm::Utils::debug("$name not active: skipping"); return ''; } my $value = sprintf("%.2f", $rs->{"${name}CurrentValue"} / 10); my $low = $rs->{"${name}LowThreshold"}; my $high = $rs->{"${name}HighThreshold"}; my $output = $rs->{"${name}Name"} . ": $value\%"; if ($rs->{"${name}Alert"} == 1) { if ($value >= $high) { $output .= " (>= $high\%)"; } if ($value <= $low) { $output .= " (<= $low\%)"; } push(@$critical, $output); } else { push(@$ok, $output); } my $perf_label = $name; $perf_label =~ s/([a-z])([A-Z])/$1_\L$2/g; return "'humidity_${perf_label}'=$value\%;0;$low:$high;0;100 "; } sub check_contact { my $name = shift; my $data = shift; my $ok = shift; my $critical = shift; my %alert_on_states = qw( 0 open 1 closed ); my %states = qw( 0 open 1 closed ); Nenm::Utils::debug("Checking contact sensor $name"); my $rs = snmp_get($data); my $state = $states{$rs->{"${name}Status"}}; my $alert_on = $alert_on_states{$rs->{"${name}AlertStatus"}}; my $output = $rs->{"${name}Name"} . ": $state"; if ($rs->{"${name}Alert"} == 1) { push(@$critical, $output); } else { $output .= " (alert when $alert_on)"; push(@$ok, $output); } my $perf_label = $name; $perf_label =~ s/([a-z])([A-Z])/$1_\L$2/g; return "'contact_${perf_label}'=" . $rs->{"${name}Status"} . ';0;' . $rs->{"${name}AlertStatus"} . ';0;1 '; } sub check_water { my $name = shift; my $data = shift; my $ok = shift; my $critical = shift; my %alert_on_states = qw( 0 open 1 closed ); my %states = qw( 0 open 1 closed ); Nenm::Utils::debug("Checking water sensor $name"); my $rs = snmp_get($data); my $state = $states{$rs->{"${name}Status"}}; my $alert_on = $alert_on_states{$rs->{"${name}AlertStatus"}}; my $output = $rs->{"${name}Name"} . ": $state"; if ($rs->{"${name}Alert"} == 1) { push(@$critical, $output); } else { $output .= " (alert when $alert_on)"; push(@$ok, $output); } my $perf_label = $name; $perf_label =~ s/([a-z])([A-Z])/$1_\L$2/g; return "'${perf_label}'=" . $rs->{"${name}Status"} . ';0;' . $rs->{"${name}AlertStatus"} . ';0;1 '; } sub snmp_get { my $data = shift; local($_); my @oids = map { "${BASE_OID}.$_"; } keys %$data; Nenm::Utils::debug("SNMP get " . join(', ', @oids)); my $get_results = $PLUGIN->get(@oids); my $results; for my $oid (keys %$get_results) { my $value = $get_results->{$oid}; $oid =~ s/$BASE_OID\.//; $results->{$data->{$oid}} = $value; Nenm::Utils::debug("Set $data->{$oid} to $value"); } return $results; } } exit check_enviromux_mini();