#!/usr/bin/env perl
# testmod - A simple script to test a resmon module
# Usage:
#
# testmod Module::Name check_name key val key val
#
# module_name and check_name are required, the key val pairs are passed to the
# module as its configuration.
#
# This script should be run from the module dir. For example, if you wanted to
# run Core::Sample, then cd to resmon/lib (i.e. just outside the Core
# directory), and run:
#
#   ../resources/testmod Core::Sample some_check_name arg1 foo arg2 bar

use strict;
use warnings;

use File::Basename;

# Look for modules in the current directory (for modules outside of the main
# resmon distribution)
use lib '.';

# Default resmon lib dir for Resmon::Module and Resmon::ExtComm
# This assumes the testmod script is in resources/testmod. Change this (or add
# another lib dir) if you move the testmod script.
use lib dirname($0) . "/../lib";

my $module = shift;        # Module to test
my $check_name = shift;
my $kvs = {@ARGV};

if (!$module || !$check_name) {
    print "Usage: $0 module_name check_name [[key] [val]] [[key] [val]]\n";
    exit 1;
}

eval "use $module;";
if ($@) {
    print "$@\n";
    exit 1;
}
my $obj = $module->new($check_name, $kvs);
my $metrics;
if ($check_name ne "*") {
    eval { $metrics = { $check_name => $obj->handler() }; };
} else {
    eval { $metrics = $obj->wildcard_handler(); };
}
if ($@) {
    chomp $@;
    $metrics = { $check_name => { "error" => ["$@", "s"] } };
}

for my $cn (sort keys %$metrics) {
    print "$module $cn\n\n";
    for my $k (sort keys %{$metrics->{$cn}}) {
        my $v = $metrics->{$cn}->{$k};
        # Default type is auto
        if (ref($v) ne "ARRAY") {
            $v = [$v, "0"];
        }
        printf "%15s = %s (%s)\n", $k , $v->[0], $v->[1];
    }
}
