1 #
2 # collectd - mon.itor.us collectd plugin
3 # Copyright (C) 2009 Jeff Green
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; only version 2 of the License is applicable.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 #
18 # Authors:
19 # Jeff Green <jeff at kikisoso.org>
20 #
22 package Collectd::Plugin::Monitorus;
24 use strict;
25 use warnings;
27 use Collectd qw( :all );
28 use LWP;
29 use threads::shared;
31 use constant NUM_OF_INTERVALS => 90;
33 my $intervalcnt :shared;
34 $intervalcnt=NUM_OF_INTERVALS;
35 my $prev_value :shared;
36 $prev_value=0;
38 plugin_register (TYPE_READ, "monitorus", "monitorus_read");
40 sub monitorus_read
41 {
42 my $vl = { plugin => 'monitorus' };
44 # Only retrieve a value occasionally in order to not overload mon.itor.us
45 if (++$intervalcnt<NUM_OF_INTERVALS) { # e.g. 180 * 10 secs / 60 seconds/min = 30 minutes
46 $vl->{'values'} = [ $prev_value ];
47 plugin_dispatch_values ('gauge', $vl);
48 return 1;
49 }
51 $intervalcnt=0;
53 my $site = 'http://mon.itor.us';
54 my $username = 'me@example.org';
55 my $target = $site.'/user/api/'.$username.'/secretpassword';
57 my $ua = LWP::UserAgent->new;
58 my $req = HTTP::Request->new(GET => "$target");
59 $req->header('Accept' => 'text/html'); #Accept HTML Page
61 my $key;
62 my $res = $ua->get($target);
63 if ($res->is_success) {# Success....all content of page has been received
64 $res->content() =~ m/\[CDATA\[(.*)\]\]/;
65 $key = $1;
66 } else {
67 INFO("monitorus: Error in retrieving login page.");
68 }
70 $target = $site.'/test/api/'.$key.'/testNames';
71 my $testid;
72 $res = $ua->get($target);
73 if ($res->is_success) {# Success....all content of page has been received
74 $res->content() =~ m/<test id='(.*)'><!\[CDATA\[sitetest_http\]\]/;
75 $testid = $1;
76 } else {
77 INFO("monitorus: Error in retrieving testNames page.");
78 }
80 #$target = $site.'/test/api/'.$key.'/testinfo/'.$testid.'/-240';
81 #$target = $site.'/test/api/'.$key.'/test/'.$testid.'/27/5/2009/1/3/-240';
82 $target = $site.'/test/api/'.$key.'/testsLastValues/1/3';
84 my $result;
85 my $value;
86 $res = $ua->get($target);
87 if ($res->is_success) {# Success....all content of page has been received
88 $res->content() =~ m/\<\/row\>\s*(\<row\>.*?sitetest_http.*?\<\/row\>)/s;
89 $result = $1;
90 $result =~ s/\<cell\>.*?CDATA.*?\<\/cell\>//g;
91 $result =~ m|\<cell\>([0-9]*)\<\/cell\>|;
92 $value = $1;
93 } else {
94 INFO("monitorus: Error in retrieving testsLastValues page.");
95 }
97 $prev_value = $value;
98 $vl->{'values'} = [ $value ];
99 plugin_dispatch_values ('gauge', $vl);
101 return 1;
102 }
104 1;