#!/usr/bin/perl
# $Id$
# check_ted - nagios plugin to monitor a ted5000 gateway
#   by Matthew Wall
#
# Copyright (c) 2011 Matthew Wall, all rights reserved
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#
# Revision History
#   0.1 initial release
#
# Prerequisites
#   curl must be installed
#
# How to use this plugin
#   This plugin will query a ted5000 gateway by downloading the /stats.htm
# page from the gateway.  It then parses this output for the requested data
# on the requested MTUs.  By default, the plugin returns voltage, power, and
# kVA on all available MTUs.
#   The plugin output contains the number of MTUs.  The plugin performance
# data depends on what was specified.
#   Specific and/or individual MTUs can be requested.
#
# Configuration
#   Create a command for the plugin something like this:
#
# define command {
#   command_name check_ted
#   command_line $USER1$/check_ted -H $HOSTADDRESS$
# }
#
# Examples
#
# check_ted -H 192.168.1.50
# OK: 4 MTUs | mtu1_kva=542 mtu1_power=520 mtu1_voltage=120.9 mtu2_kva=130 mtu2_power=128 mtu2_voltage=120.4 mtu3_kva=2676 mtu3_power=2312 mtu3_voltage=120.8 mtu4_kva=248 mtu4_power=214 mtu4_voltage=120.2
#
# check_ted -H 192.168.1.50 -mtu 3 -data Voltage -data kVA
# OK: 4 MTUs | mtu3_kva=2752 mtu3_voltage=121
#
# check_ted -H 192.168.1.50 -mtu 2
# OK: 4 MTUs | mtu2_kva=132 mtu2_power=128 mtu2_voltage=120.5


use strict;

my @STATE = ('OK', 'WARN', 'CRITICAL', 'UNKNOWN');
my $IDX_OK=0;
my $IDX_WARN=1;
my $IDX_CRIT=2;
my $IDX_UNKNOWN=3;

my $curl = '/usr/bin/curl';
my $hostname = q();
my $state = $IDX_UNKNOWN;

my $alldata = 0;
my @default_data = ('Voltage', 'Power', 'kVA');
my $default_mtus = '1 2 3 4';
my @desired_data;
my $desired_mtus = q();

while($ARGV[0]) {
    my $arg = shift;
    if ($arg eq '-H') {
        $hostname = shift;
    } elsif ($arg eq '-data') {
        $arg = shift;
        if ($arg eq 'all') {
            $alldata = 1;
        } else {
            push @desired_data, $arg;
        }
    } elsif ($arg eq '-mtu') {
        $arg = shift;
        if ($arg eq 'all') {
            $desired_mtus = "1 2 3 4";
        } else {
            $desired_mtus .= $arg;
        }
    } else {
        if ($arg ne '--help') {
            print "unknown argument: $arg\n";
            print "\n";
        }
        print "usage: check_ted.pl -H hostname [-data tag] [-mtu index]\n";
        exit $state;
    }
}

if ($hostname eq q()) {
    print "$STATE[$state]: no host specified\n";
    exit $state;
}

if (! -x $curl) {
    print "$STATE[$state]: cannot find curl at $curl\n";
    exit $state;
}

@desired_data = @default_data if $#desired_data < 0;
$desired_mtus = $default_mtus if $desired_mtus eq q();

my @result = `$curl http://$hostname/stats.htm 2>&1`;

my $out = q();
my $n = 1;
my %values;
my $nmtu = 0;
my $block = q();
foreach my $line (@result) {
    if ($line =~ /curl: \(7\)/) {
        $state = $IDX_WARN;
        chop($out = $line);
        last;
    }
    if ($line =~ /<th>MTU \d<\/th>/) {
        my ($x) = $line =~ /MTU (\d)/;
        $nmtu = $x if $x > $nmtu;
        next;
    }
    my $foundtag = q();
    if ($alldata) {
        if ($line =~ /<th>.*:<\/th>/) {
            ($foundtag) = $line =~ /<th>(.*):<\/th>/;
        }
    } else {
        foreach my $tag (@desired_data) {
            if ($line =~ /<th>$tag:<\/th>/) {
                $foundtag = $tag;
            }
        }
    }
    if ($foundtag ne q()) {
        $block = $foundtag;
        $block =~ tr/A-Z/a-z/;
        $block =~ s/\W/_/g;
        $n = 1;
        next;
    }
    if ($block ne q() && $line =~ /<td style=.*>\d+<\/td>/) {
        if ($desired_mtus =~ /$n/) {
            my ($v) = $line =~ /<td style=.*>(\d+)<\/td>/;
            $v /= 10 if $block eq 'voltage';
            $values{"mtu${n}_${block}"} = $v;
        }
        $n += 1;
        next;
    }
    $block = q();
}

my $pdata = q();
foreach my $k (sort keys %values) {
    $pdata .= " $k=$values{$k}";
}
if ($out eq q()) {
    if ($nmtu > 0) {
        $out = "$nmtu MTUs";
        $state = $IDX_OK;
    } else {
        $out = 'no data';
    }
}

my $output = "$STATE[$state]: $out";
$output .= " |$pdata" if $pdata ne q();
$output .= "\n";
print $output;
exit $state;
