Skip to content

Instantly share code, notes, and snippets.

@stsimb
Created April 4, 2016 09:18
Show Gist options
  • Select an option

  • Save stsimb/87b243dfe482880ae19772fbfa1f1fc6 to your computer and use it in GitHub Desktop.

Select an option

Save stsimb/87b243dfe482880ae19772fbfa1f1fc6 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# stsimb sep 2014
# webservice to fetch any munin metric via http
# install in muninhost /var/www/cgi-bin and use via apache
# e.g. http://muninhost/cgi-bin/munin-value.cgi?hostname=it-web-04.cloud.forthnet.prv&plugin=cpu&metric=user
use CGI qw(:standard);
my $hostname = lc(param("hostname")); # hostname
my ($host, $dom) = split (/\./, $hostname, 2); # autodetect domain from hostname
my $domain = lc(param("domain")) || $dom; # but allow to override using param
my $plugin = param("plugin"); # plugin
my $metric = param("metric"); # metric
my $cmd ="";
print header; # print standard cgi header
if ($hostname eq "") {
$cmd = "echo need hostname";
} elsif ($plugin eq "") {
$cmd = "echo list | /usr/bin/nc -w 5 $hostname 4949";
print "available plugins for $hostname<pre>";
} elsif ($metric eq "") {
$cmd = "echo fetch $plugin | /usr/bin/nc -w 5 $hostname 4949 | /usr/bin/awk '{print \$1}' | /bin/sed -e 's/.value\$//g'";
print "available metrics for $plugin plugin of $hostname<pre>";
} else {
$cmd = "echo fetch $plugin | /usr/bin/nc -w 30 $hostname 4949 | /bin/grep \"^$metric.value\" | /usr/bin/awk '{print \$2}'";
}
$value = `$cmd`;
print "$value";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment