2493e36cda9f6528c5d15e5336a44e4f333316a2
[collectd.git] / bindings / perl / lib / Collectd / Plugins / Monitorus.pm
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 #
21
22 package Collectd::Plugin::Monitorus;
23
24 use strict;
25 use warnings;
26
27 use Collectd qw( :all );
28 use LWP;
29 use threads::shared;
30
31 use constant NUM_OF_INTERVALS => 90;
32
33 my $intervalcnt :shared;
34 $intervalcnt=NUM_OF_INTERVALS;
35 my $prev_value :shared;
36 $prev_value=0;
37
38 plugin_register (TYPE_READ, "monitorus", "monitorus_read");
39 plugin_register (TYPE_LOG, "monitorus", "monitorus_log");
40
41 sub monitorus_read
42 {
43         my $vl = { plugin => 'monitorus' };
44
45         # Only retrieve a value occasionally in order to not overload mon.itor.us
46         if (++$intervalcnt<NUM_OF_INTERVALS) { # e.g. 180 * 10 secs / 60 seconds/min = 30 minutes
47                 $vl->{'values'} = [ $prev_value ];
48                 plugin_dispatch_values ('gauge', $vl);
49                 return 1;
50         }
51
52         $intervalcnt=0;
53
54         my $site = 'http://mon.itor.us';
55         my $username = 'me@example.org';
56         my $target = $site.'/user/api/'.$username.'/secretpassword';
57
58         my $ua = LWP::UserAgent->new;
59         my $req = HTTP::Request->new(GET => "$target");
60         $req->header('Accept' => 'text/html');          #Accept HTML Page
61
62         my $key;
63         my $res = $ua->get($target);
64         if ($res->is_success) {# Success....all content of page has been received
65                 $res->content() =~ m/\[CDATA\[(.*)\]\]/;
66                 $key = $1;
67         } else {
68                 INFO("monitorus: Error in retrieving login page.");
69         }
70
71         $target = $site.'/test/api/'.$key.'/testNames';
72         my $testid;
73         $res = $ua->get($target);
74         if ($res->is_success) {# Success....all content of page has been received
75                 $res->content() =~ m/<test id='(.*)'><!\[CDATA\[sitetest_http\]\]/;
76                 $testid = $1;
77         } else {
78                 INFO("monitorus: Error in retrieving testNames page.");
79         }
80
81         #$target = $site.'/test/api/'.$key.'/testinfo/'.$testid.'/-240';
82         #$target = $site.'/test/api/'.$key.'/test/'.$testid.'/27/5/2009/1/3/-240';
83         $target = $site.'/test/api/'.$key.'/testsLastValues/1/3';
84
85         my $result;
86         my $value;
87         $res = $ua->get($target);
88         if ($res->is_success) {# Success....all content of page has been received
89                 $res->content() =~ m/\<\/row\>\s*(\<row\>.*?sitetest_http.*?\<\/row\>)/s;
90                 $result = $1;
91                 $result =~ s/\<cell\>.*?CDATA.*?\<\/cell\>//g;
92                 $result =~ m|\<cell\>([0-9]*)\<\/cell\>|;
93                 $value = $1;
94         } else {
95                 INFO("monitorus: Error in retrieving testsLastValues page.");
96         }
97
98         $prev_value = $value;
99         $vl->{'values'} = [ $value ];
100         plugin_dispatch_values ('gauge', $vl);
101
102         return 1;
103 }
104
105 # This function is called when plugin_log () has been used.
106 sub monitorus_log
107 {
108         my $level = shift;
109         my $msg   = shift;
110
111         print "LOG: $level - $msg\n";
112         return 1;
113 } # monitorus_log ()
114
115 1;