From 706cc847954b52da039d86f2655874c83a3d3140 Mon Sep 17 00:00:00 2001 From: Jeff Green Date: Tue, 8 Dec 2009 18:28:34 +0100 Subject: [PATCH] =?utf8?q?perl/=E2=80=A6/Monitorus.pm:=20Added=20Perl-base?= =?utf8?q?d=20plugin=20to=20query=20statistics=20from=20mon.itor.us.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bindings/perl/lib/Collectd/Plugins/Monitorus.pm | 115 ++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 bindings/perl/lib/Collectd/Plugins/Monitorus.pm diff --git a/bindings/perl/lib/Collectd/Plugins/Monitorus.pm b/bindings/perl/lib/Collectd/Plugins/Monitorus.pm new file mode 100644 index 00000000..2493e36c --- /dev/null +++ b/bindings/perl/lib/Collectd/Plugins/Monitorus.pm @@ -0,0 +1,115 @@ +# +# collectd - mon.itor.us collectd plugin +# Copyright (C) 2009 Jeff Green +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; only version 2 of the License is applicable. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# Authors: +# Jeff Green +# + +package Collectd::Plugin::Monitorus; + +use strict; +use warnings; + +use Collectd qw( :all ); +use LWP; +use threads::shared; + +use constant NUM_OF_INTERVALS => 90; + +my $intervalcnt :shared; +$intervalcnt=NUM_OF_INTERVALS; +my $prev_value :shared; +$prev_value=0; + +plugin_register (TYPE_READ, "monitorus", "monitorus_read"); +plugin_register (TYPE_LOG, "monitorus", "monitorus_log"); + +sub monitorus_read +{ + my $vl = { plugin => 'monitorus' }; + + # Only retrieve a value occasionally in order to not overload mon.itor.us + if (++$intervalcnt{'values'} = [ $prev_value ]; + plugin_dispatch_values ('gauge', $vl); + return 1; + } + + $intervalcnt=0; + + my $site = 'http://mon.itor.us'; + my $username = 'me@example.org'; + my $target = $site.'/user/api/'.$username.'/secretpassword'; + + my $ua = LWP::UserAgent->new; + my $req = HTTP::Request->new(GET => "$target"); + $req->header('Accept' => 'text/html'); #Accept HTML Page + + my $key; + my $res = $ua->get($target); + if ($res->is_success) {# Success....all content of page has been received + $res->content() =~ m/\[CDATA\[(.*)\]\]/; + $key = $1; + } else { + INFO("monitorus: Error in retrieving login page."); + } + + $target = $site.'/test/api/'.$key.'/testNames'; + my $testid; + $res = $ua->get($target); + if ($res->is_success) {# Success....all content of page has been received + $res->content() =~ m/get($target); + if ($res->is_success) {# Success....all content of page has been received + $res->content() =~ m/\<\/row\>\s*(\.*?sitetest_http.*?\<\/row\>)/s; + $result = $1; + $result =~ s/\.*?CDATA.*?\<\/cell\>//g; + $result =~ m|\([0-9]*)\<\/cell\>|; + $value = $1; + } else { + INFO("monitorus: Error in retrieving testsLastValues page."); + } + + $prev_value = $value; + $vl->{'values'} = [ $value ]; + plugin_dispatch_values ('gauge', $vl); + + return 1; +} + +# This function is called when plugin_log () has been used. +sub monitorus_log +{ + my $level = shift; + my $msg = shift; + + print "LOG: $level - $msg\n"; + return 1; +} # monitorus_log () + +1; -- 2.11.0