From 7c7576b82f540a094867e0f7867c3d107cf4db6e Mon Sep 17 00:00:00 2001
From: Chris Nehren <cnehren@omniti.com>
Date: Mon, 17 Jun 2013 14:34:54 -0400
Subject: [PATCH] Surprised we didn't have this already, a module to run a
 command and report its output.

---
 lib/Core/WatchOutput.pm | 76 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 lib/Core/WatchOutput.pm

diff --git a/lib/Core/WatchOutput.pm b/lib/Core/WatchOutput.pm
new file mode 100644
index 0000000..1bc2e95
--- /dev/null
+++ b/lib/Core/WatchOutput.pm
@@ -0,0 +1,76 @@
+package Core::WatchOutput;
+
+use strict;
+use warnings;
+
+use base 'Resmon::Module';
+
+use Resmon::ExtComm qw(run_command cache_command);
+
+=pod
+
+=head1 NAME
+
+Core::WatchOutout - watch a command's output, reporting it
+
+=head1 SYNOPSIS
+
+ Core::WatchOutput {
+     command: cmd => "/bin/ls"
+ }
+
+=head1 DESCRIPTION
+
+This check will run a given command and return the output as the
+"output" metric.
+
+=head1 CONFIGURATION
+
+=over
+
+=item command
+
+This is a string of the command name and any arguments.
+
+=back
+
+=head1 METRICS
+
+=over
+
+=item output
+
+The STDOUT of the command.
+
+=item return_code
+
+The exit code of the command.
+
+=back
+
+=cut
+
+sub new {
+    my ($class, $check_name, $config) = @_;
+    my $self = $class->SUPER::new($check_name, $config);
+
+    bless($self, $class);
+    return $self;
+}
+
+sub handler {
+    my $self = shift;
+    my $config = $self->{config};
+    my $command = $self->{command};
+
+    my $output = run_command($command);
+    my $status = $? >> 8;
+    chomp $output;
+
+    return {
+        "output" => [$self->{check_name}, "s"],
+        "return_code" => [$status, "i"],
+    };
+};
+
+1;
-- 
GitLab