blib/lib/Nagios/Scrape.pm

filter: Statementstmt 48.4% 31 / 64 Branchbran 54.5% 12 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 61.5% 8 / 13 Podpod 100% 8 / 8 totaltotal 57.1% 64 / 112 CC 13 SCAR 15.9
CC
13
Cov
52%
parse_service_content
12.0
new
6.0
get_service_status
6.0
CRAP 4.9
↑ Summary Build »
1 package Nagios::Scrape;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Nagios::Scrape - Scrapes and Parses the status.cgi page of a Nagios installation
13  
14 =head1 VERSION
15  
16 Version 0.03
17  
18 =cut
19  
20 3 our $VERSION = '0.03';
21  
22 =head1 SYNOPSIS
23  
24 This module uses LWP to retrieve the status.cgi page of a Nagios installation, parses
25 the data into a manageable format, and then makes it accessible.
26  
27 This is a more lightweight solution to Nagios installations where the status.dat file
28 can reach 1+mb in size.
29  
30 use Nagios::Scrape;
31  
32 my $foo = Nagios::Scrape->new(username => $username, password => $password, url => $url);
33 @service_alerts = $foo->get_service_status();
34 @host_alerts = $foo->get_host_status();
35  
36 =head1 SUBROUTINES/METHODS
37  
38 =head2 new
39  
40 Creates a new class given a username and password.
41  
42 my $nagios = Nagios::Scrape->new(username => $username, password => $password, url => $url);
43  
44 =cut
45  
46 sub new {
47 6 ▶ my ( $class, %attrs ) = @_;
Subroutine new: called CC=6 SCAR=17.9
Pod documented
48  
49 6 ▶ throw Error::Simple("Username is required") if ( !defined( $attrs{username} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs{"username"} 1 5 6
50 5 ▶ throw Error::Simple("Password is required") if ( !defined( $attrs{password} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs{"password"} 1 4 5
51 4 ▶ throw Error::Simple("URL is required") if ( !defined( $attrs{url} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs{"url"} 1 3 4
52  
53 throw Error::Simple("Invalid URL. Example: http://localhost/cgi-bin/status.cgi")
54 3 ▶ if ( ( $attrs{url} !~ m/^http/ ) || ( $attrs{url} !~ m/status.cgi$/ ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"url"} =~ /^http/ and $attrs{"url"} =~ /status.cgi$/ 2 1 3
Condition100%3/3
$attrs{"url"} =~ /^http/ and $attrs{"url"} =~ /status.cgi$/
!ll&&!rl&&r
111
MC/DC100%2/2
$attrs{"url"} =~ /^http/ and $attrs{"url"} =~ /status.cgi$/
$attrs{"url"} =~ /^http/ $attrs{"url"} =~ /status.cgi$/
Truth table100%3/3
A and B
A= $attrs{"url"} =~ /^http/B= $attrs{"url"} =~ /status.cgi$/
ABresult
0X0
100
111
55  
56 # Sets default values for host and service states
57 1 $attrs{host_state} = 12;
58 1 $attrs{service_state} = 28;
59  
60 1 bless \%attrs, $class;
61  
62 }
63  
64 =head2 host_state
65  
66 This method allows you to filter certain host states. The table is as follows:
67  
68 Hosts:
69 PENDING 1
70 UP 2
71 DOWN 4
72 UNREACHABLE 8
73  
74 Add the number for each state that you want to see. For example, to see DOWN
75 and UNREACHABLE states, set this value to 12. (Default value).
76  
77 =cut
78  
79 sub host_state {
80 3 ▶ my ( $self, $state ) = @_;
Subroutine host_state: called CC=2 SCAR=6.9
Pod documented
81 3 ▶ $self->{host_state} = $state if ( defined($state) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 1 2 3
82 3 return $self->{host_state};
83 }
84  
85 =head2 service_state
86  
87 This method allows you to filter certain service states. The table is as follows:
88  
89 Services:
90 PENDING 1
91 OK 2
92 WARNING 4
93 UNKNOWN 8
94 CRITICAL 16
95  
96 Add the number for each state you would like to see. For example, to see WARNING,
97 UNKNOWN, and CRITICAL states, set the number to 28. (Default value).
98  
99 =cut
100  
101 sub service_state {
102 3 ▶ my ( $self, $state ) = @_;
Subroutine service_state: called CC=2 SCAR=6.9
Pod documented
103 3 ▶ $self->{service_state} = $state if ( defined($state) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 1 2 3
104 3 return $self->{service_state};
105 }
106  
107 =head2 get_service_status
108  
109 Connects to given URL and retrieves the requested service statuses
110  
111 =cut
112  
113 sub get_service_status {
114 0 ▶ my $self = shift;
Subroutine get_service_status: not called CC=2 SCAR=17.9
Pod documented
115 0 my $ua = LWP::UserAgent->new;
116 my $req =
117 HTTP::Request->new( GET => $self->{url}
118 . '?host=all&noheader=yes&servicestatustypes='
119 0 . $self->{service_state} );
120 0 $req->authorization_basic( $self->{username}, $self->{password} );
121 0 my $response = $ua->request($req);
122  
123 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
124 die( "Could not connect to "
125 0 . $self->{url} . " "
126 . $response->status_line
127 . "\n" );
128 }
129  
130 0 return $self->parse_service_content( $response->content );
131  
132 }
133  
134 =head2 get_host_status
135  
136 Connects to given url and retrieves host statuses
137  
138 =cut
139  
140 sub get_host_status {
141 0 ▶ my $self = shift;
Subroutine get_host_status: not called CC=2 SCAR=17.9
Pod documented
142 0 my $ua = LWP::UserAgent->new;
143 my $req =
144 HTTP::Request->new( GET => $self->{url}
145 . '?hostgroup=all&noheader=yes&style=hostdetail&hoststatustypes='
146 0 . $self->{host_state} );
147 0 $req->authorization_basic( $self->{username}, $self->{password} );
148 0 my $response = $ua->request($req);
149  
150 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
151 die( "Could not connect to "
152 0 . $self->{url} . " "
153 . $response->status_line
154 . "\n" );
155 }
156  
157 0 return $self->parse_host_content( $response->content );
158 }
159  
160  
161 =head2 parse_service_content
162  
163 Will parse the service status page into a manageable array of hashed service details.
164  
165 =cut
166  
167 sub parse_service_content {
168 0 ▶ my ( $self, $content ) = @_;
Subroutine parse_service_content: not called CC=3 SCAR=24.8
Pod documented
169  
170 0 my @alerts;
171 my $host;
172  
173 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[ (?:<TD\s+align=left\s+valign=center\s+CLASS='status(?:Even|Odd|HOST[A-Z]+)'> <A\s+HREF='extinfo.cgi .+? # Host name - will be empty TD pair if this is a continuation # of a host with multiple alerts '>([^<]+)</A>|<TD></TD>) .+? # ' Service description >([^<]+)</A> .+? # Status CLASS='status[A-Z]+'>([A-Z]+)</TD> .+? # ' Time nowrap>([^<]+)</TD> .+? # Duration nowrap>([^<]+)</TD> .+? # Attempts >([^<]+)</TD> .+? # Status Information >([^<]+)</TD> .+? ]gimsx) 0 0 0
174 $content =~ m%
175 (?:<TD\s+align=left\s+valign=center\s+CLASS='status(?:Even|Odd|HOST[A-Z]+)'>
176 <A\s+HREF='extinfo.cgi
177 .+?
178 # Host name - will be empty TD pair if this is a continuation
179 # of a host with multiple alerts
180 '>([^<]+)</A>|<TD></TD>)
181 .+?
182 # ' Service description
183 >([^<]+)</A>
184 .+?
185 # Status
186 CLASS='status[A-Z]+'>([A-Z]+)</TD>
187 .+?
188 # ' Time
189 nowrap>([^<]+)</TD>
190 .+?
191 # Duration
192 nowrap>([^<]+)</TD>
193 .+?
194 # Attempts
195 >([^<]+)</TD>
196 .+?
197 # Status Information
198 >([^<]+)</TD>
199 .+?
200 %xsmgi
201 )
202 {
203  
204 # Host might be empty if this is a host with multiple alerts
205 0 ▶ $host = $1 if (defined($1));
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 0 0
206  
207 0 my $alert = {
208 'type' => 'service',
209 'host' => $self->decode_html($host),
210 'service' => $self->decode_html($2),
211 'status' => $self->decode_html($3),
212 'time' => $self->decode_html($4),
213 'duration' => $self->decode_html($5),
214 'attempts' => $self->decode_html($6),
215 'information' => $self->decode_html($7)
216 };
217  
218 0 push( @alerts, $alert );
219  
220 }
221  
222 0 return @alerts;
223 }
224  
225 =head2 parse_host_content
226  
227 Will parse the host status page into a manageable array of hashed service details.
228  
229 =cut
230  
231 sub parse_host_content {
232 0 ▶ my ($self, $content) = @_;
Subroutine parse_host_content: not called CC=2 SCAR=17.9
Pod documented
233 0 my @alerts;
234  
235 0 ▶ while ($content =~ m%
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[ <TD\s+align=left\s+valign=center\s+CLASS='statusHOST[A-Z]+'> .+? # Host name >([^<]+)</A> .+? # Status <TD\s+CLASS='statusHOST[A-Z]+'>([^<]+)</TD> .+? # Time nowrap>([^<]+)</TD> .+? # Duration nowrap>([^<]+)</TD> .+? # Status Information >([^<]+)</TD> .+? ]gimsx) 0 0 0
236 <TD\s+align=left\s+valign=center\s+CLASS='statusHOST[A-Z]+'>
237 .+?
238 # Host name
239 >([^<]+)</A>
240 .+?
241 # Status
242 <TD\s+CLASS='statusHOST[A-Z]+'>([^<]+)</TD>
243 .+?
244 # Time
245 nowrap>([^<]+)</TD>
246 .+?
247 # Duration
248 nowrap>([^<]+)</TD>
249 .+?
250 # Status Information
251 >([^<]+)</TD>
252 .+?
253 %xsmgi) {
254  
255 0 my $alert = {
256 'type' => 'host',
257 'host' => $self->decode_html($1),
258 'status' => $self->decode_html($2),
259 'time' => $self->decode_html($3),
260 'duration' => $self->decode_html($4),
261 'information' => $self->decode_html($5)
262 };
263 0 push(@alerts, $alert);
264 }
265  
266 0 return @alerts;
267 }
268  
269 =head2 decode_html
270  
271 Simple helper method that smooths out HTML strings from Nagios status.cgi page
272  
273 =cut
274  
275 sub decode_html {
276 0 ▶ my ( $self, $string ) = @_;
Subroutine decode_html: not called CC=1 SCAR=6.9
Pod documented
277 0 $string = CGI::unescapeHTML($string);
278 0 $string =~ s/nbsp//g;
279  
280 0 return $string;
281 }
282  
283 =head1 AUTHOR
284  
285 Joe Topjian, C<< <joe at terrarum.net> >>
286  
287 =head1 BUGS
288  
289 Please report any bugs or feature requests to C<bug-nagios-scrape at rt.cpan.org>, or through
290 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nagios-Scrape>. I will be notified, and then you'll
291 automatically be notified of progress on your bug as I make changes.
292  
293  
294  
295  
296 =head1 SUPPORT
297  
298 You can find documentation for this module with the perldoc command.
299  
300 perldoc Nagios::Scrape
301  
302  
303 =head1 ACKNOWLEDGEMENTS
304  
305 Some of this code was taken from www.nagios3book.com/nagios-3-enm/tts/nagios-ttsd.pl which is no longer online.
306  
307 =head1 LICENSE AND COPYRIGHT
308  
309 Copyright 2010 Joe Topjian.
310  
311 This program is free software; you can redistribute it and/or modify it
312 under the terms of either: the GNU General Public License as published
313 by the Free Software Foundation; or the Artistic License.
314  
315 See http://dev.perl.org/licenses/ for more information.
316  
317  
318 =cut
319  
320 3 1; # End of Nagios::Scrape
↑ Summary Build »