root/resources/testmod

Revision 5bd565fe59f22420dba6fb903956a5fa6f8e3102, 2.3 kB (checked in by Mark Harrison <mark@omniti.com>, 8 years ago)

Wildcard module support in testmod (refs #12)

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@410 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100755
Line 
1 #!/usr/bin/env perl
2 # testmod - A simple script to test a resmon module
3 # Usage:
4 #
5 # testmod Module::Name check_name key val key val
6 #
7 # module_name and check_name are required, the key val pairs are passed to the
8 # module as its configuration.
9 #
10 # This script should be run from the module dir. For example, if you wanted to
11 # run Core::Sample, then cd to resmon/lib (i.e. just outside the Core
12 # directory), and run:
13 #
14 #   ../resources/testmod Core::Sample some_check_name arg1 foo arg2 bar
15
16 use strict;
17 use warnings;
18
19 use lib '.';
20
21 # Hack to mark modules we provide here as already loaded
22 $INC{"Resmon/Module.pm"} = $0;
23 $INC{"Resmon/ExtComm.pm"} = $0;
24
25 package Resmon::Module;
26 use strict;
27 use warnings;
28
29 sub new {
30     my ($class, $check_name, $config) = @_;
31     my $self = {};
32     $self->{config} = $config;
33     $self->{check_name} = $check_name;
34     bless ($self, $class);
35     return $self;
36 }
37
38 1;
39
40 package Resmon::ExtComm;
41 use strict;
42 use warnings;
43
44 use base "Exporter";
45 our @EXPORT_OK = qw/cache_command run_command/;
46
47 sub cache_command($$) {
48     my $command = shift;
49     run_command($command);
50 }
51
52 sub run_command {
53     my @cmd = @_;
54     pipe(my ($r, $w));
55     my $pid = fork();
56     if($pid) {
57         close($w);
58         my @lines = <$r>;
59         waitpid($pid, 0);
60         return join("", @lines);
61     } else {
62         eval {
63             open(STDOUT, ">&", $w);
64             close($r);
65             exec(@cmd);
66         };
67         exit();
68     }
69 }
70
71 1;
72
73 package main;
74
75 my $module = shift;        # Module to test
76 my $check_name = shift;
77 my $kvs = {@ARGV};
78
79 if (!$module || !$check_name) {
80     print "Usage: $0 module_name check_name [[key] [val]] [[key] [val]]\n";
81     exit 1;
82 }
83
84 eval "use $module;";
85 if ($@) {
86     print "$@\n";
87     exit 1;
88 }
89 my $obj = $module->new($check_name, $kvs);
90 my $metrics;
91 if ($check_name ne "*") {
92     eval { $metrics = { $check_name => $obj->handler() }; };
93 } else {
94     eval { $metrics = $obj->wildcard_handler(); };
95 }
96 if ($@) {
97     chomp $@;
98     $metrics = { $check_name => { "error" => ["$@", "s"] } };
99 }
100
101 for my $cn (sort keys %$metrics) {
102     print "$module $cn\n\n";
103     for my $k (sort keys %{$metrics->{$cn}}) {
104         my $v = $metrics->{$cn}->{$k};
105         # Default type is auto
106         if (ref($v) ne "ARRAY") {
107             $v = [$v, "0"];
108         }
109         printf "%15s = %s (%s)\n", $k , $v->[0], $v->[1];
110     }
111 }
Note: See TracBrowser for help on using the browser.