|
1 #!/usr/bin/perl |
|
2 |
|
3 use strict; |
|
4 use warnings; |
|
5 use LWP::Simple; |
|
6 use File::Basename; |
|
7 use Getopt::Long; |
|
8 use Pod::Usage; |
|
9 |
|
10 delete @ENV{ grep /^LC_/ => keys %ENV }; |
|
11 $ENV{LANG} = "C"; |
|
12 $ENV{LC_ALL} = "C"; |
|
13 |
|
14 sub version($$); |
|
15 sub get_temp($$); |
|
16 sub report($$$); |
|
17 |
|
18 my %ERRORS = ( |
|
19 OK => 0, |
|
20 WARNING => 1, |
|
21 CRITICAL => 2, |
|
22 UNKNOWN => 3, |
|
23 DEPENDENT => 4 |
|
24 ); |
|
25 |
|
26 my $ME = basename $0; |
|
27 my $NAME = "ROOMTEMP"; |
|
28 my $VERSION = "0.1"; |
|
29 |
|
30 my %opt = ( |
|
31 warning => 35.00, |
|
32 critical => 40.00, |
|
33 url => 'http://thermo.local.site/status.xml', |
|
34 search => '<temp>[+-]\s+([\d\.]+)</temp>' |
|
35 ); |
|
36 |
|
37 MAIN: { |
|
38 Getopt::Long::Configure('bundling'); |
|
39 GetOptions( |
|
40 "w|warning=f" => \$opt{warning}, |
|
41 "c|critical=f" => \$opt{critical}, |
|
42 "u|url=s" => \$opt{url}, |
|
43 "s|search=s" => \$opt{search}, |
|
44 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
|
45 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
|
46 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
|
47 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
|
48 |
|
49 my ($temp) = get_temp( \$opt{url}, \$opt{search} ); |
|
50 report( \$temp, \$opt{warning}, \$opt{critical} ); |
|
51 } |
|
52 |
|
53 sub get_temp($$) { |
|
54 my ( $url, $search ) = @_; |
|
55 my $temp; |
|
56 my $content = get($$url); |
|
57 if ( !$content ) { |
|
58 print "$NAME UNKNOWN: can't get status informations from URL\n"; |
|
59 exit $ERRORS{UNKNOWN}; |
|
60 } |
|
61 $content =~ /$$search/; |
|
62 |
|
63 if ( !$1 ) { |
|
64 print "$NAME UNKNOWN: can't get temperature from search string\n"; |
|
65 exit $ERRORS{UNKNOWN}; |
|
66 } |
|
67 $temp = sprintf( "%.2f", $1 ); |
|
68 return $temp; |
|
69 } |
|
70 |
|
71 sub report($$$) { |
|
72 my ( $temp, $warning, $critical ) = @_; |
|
73 $warning = sprintf( "%.2f", $$warning ); |
|
74 $critical = sprintf( "%.2f", $$critical ); |
|
75 |
|
76 $$temp =~ /^(\d+)\.(\d+)/; |
|
77 my $ta = $1; |
|
78 my $tb = $2; |
|
79 |
|
80 $warning =~ /^(\d+)\.(\d+)/; |
|
81 my $wa = $1; |
|
82 my $wb = $2; |
|
83 |
|
84 $critical =~ /^(\d+)\.(\d+)/; |
|
85 my $ca = $1; |
|
86 my $cb = $2; |
|
87 |
|
88 if ( $ta > $ca ) { |
|
89 print |
|
90 "$NAME CRITICAL: +$$temp C | roomtemp=$$temp;$warning;$critical\n"; |
|
91 exit $ERRORS{CRITICAL}; |
|
92 } |
|
93 if ( ( $ta == $ca ) && ( $tb >= $cb ) ) { |
|
94 print |
|
95 "$NAME CRITICAL: +$$temp C | roomtemp=$$temp;$warning;$critical\n"; |
|
96 exit $ERRORS{CRITICAL}; |
|
97 } |
|
98 if ( $ta > $wa ) { |
|
99 print "$NAME WARNING: +$$temp C | roomtemp=$$temp;$warning;$critical\n"; |
|
100 exit $ERRORS{WARNING}; |
|
101 } |
|
102 if ( ( $ta == $wa ) && ( $tb >= $wb ) ) { |
|
103 print "$NAME WARNING: +$$temp C | roomtemp=$$temp;$warning;$critical\n"; |
|
104 exit $ERRORS{WARNING}; |
|
105 } |
|
106 print "$NAME OK: +$$temp C | roomtemp=$$temp;$warning;$critical\n"; |
|
107 exit $ERRORS{OK}; |
|
108 } |
|
109 |
|
110 sub version($$) { |
|
111 my ( $progname, $version ) = @_; |
|
112 |
|
113 print <<_VERSION; |
|
114 $progname version $version |
|
115 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
116 |
|
117 $ME comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
118 and you are welcome to redistribute it under certain conditions. |
|
119 See the GNU General Public Licence for details. |
|
120 _VERSION |
|
121 } |
|
122 |
|
123 __END__ |
|
124 |
|
125 =head1 NAME |
|
126 |
|
127 check_roomtemp - nagios plugin to check room temperature |
|
128 |
|
129 =head1 SYNOPSIS |
|
130 |
|
131 check_roomtemp [B<-w>|B<--warning> I<floating_point>] [B<-c>|B<--critical> I<floating_point>] |
|
132 |
|
133 check_roomtemp [B<-u>|B<--url> I<string>] |
|
134 |
|
135 check_roomtemp [B<-s>|B<--search> I<string>] |
|
136 |
|
137 check_roomtemp [B<-h>|B<--help>] |
|
138 |
|
139 check_roomtemp [B<-m>|B<--man>] |
|
140 |
|
141 check_roomtemp [B<-V>|B<--version>] |
|
142 |
|
143 =head1 OPTIONS |
|
144 |
|
145 =over |
|
146 |
|
147 =item B<-w>|B<--warning> I<integer> |
|
148 |
|
149 Exit with WARNING status if temperature is greater than or equal this value (default: 35.00) |
|
150 |
|
151 =item B<-c>|B<--critical> I<floating point> |
|
152 |
|
153 Exit with CRITICAL status if temperature is greater than or equal this value (default: 40.00) |
|
154 |
|
155 =item B<-u>|B<--url> I<string> |
|
156 |
|
157 URL for geting status information (default: 'http://thermo1.vic.site/status.xml') |
|
158 |
|
159 =item B<-s>|B<--search> I<string> |
|
160 |
|
161 Search string for the temperature on the given url (default: '<temp>[+-]\s+([\d\.]+)</temp>') |
|
162 |
|
163 =item B<-h>|B<--help> |
|
164 |
|
165 Print detailed help screen. |
|
166 |
|
167 =item B<-m>|B<--man> |
|
168 |
|
169 Print manual page. |
|
170 |
|
171 =item B<-V>|B<--version> |
|
172 |
|
173 Print version information. |
|
174 |
|
175 =back |
|
176 |
|
177 =head1 DESCRIPTION |
|
178 |
|
179 This nagios plugin check the room temperature. |
|
180 |
|
181 =head1 VERSION |
|
182 |
|
183 This man page is current for version 0.1 of B<check_roomtemp>. |
|
184 |
|
185 =head1 AUTHOR |
|
186 |
|
187 Written by Christian Arnold L<arnold@schlittermann.de> |
|
188 |
|
189 =head1 COPYRIGHT |
|
190 |
|
191 Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support. |
|
192 This is free software, and you are welcome to redistribute it under certain conditions. |
|
193 See the GNU General Public Licence for details. |
|
194 |
|
195 =cut |