/[nagios-plugins-perl]/trunk/plugins/check_livestatus.pl
ViewVC logotype

Contents of /trunk/plugins/check_livestatus.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 163 - (show annotations) (download)
Fri Nov 13 11:18:19 2015 UTC (5 years ago) by racvision
File MIME type: text/plain
File size: 4106 byte(s)
remove SVG dependancy
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2014 - Stéphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty
12 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # you should have received a copy of the GNU General Public License
16 # along with this program (or with Nagios); if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA
19 #
20
21 use strict;
22 use warnings;
23 use utf8;
24
25
26 use Locale::gettext;
27
28 use Nagios::Plugin ;
29 use POSIX qw(:signal_h setlocale);
30
31 use IO::Socket;
32 use IO::Socket::UNIX;
33
34 #use SVG;
35 use JSON::XS;
36
37 use Data::Dumper;
38
39 my $DEBUG = 0;
40
41 our $livestatusSocket = '/var/lib/nagios/livestatus.sock';
42
43
44 my $VERSION = '1.0';
45
46 my $TIMEOUT = 9;
47
48 # i18n :
49 setlocale(LC_MESSAGES, '');
50 textdomain('nagios-plugins-perl');
51
52 # Retention files path (save previous values) :
53 # FIXME: Make this configurable
54 my $TempPath = '/tmp/';
55
56
57 my $np = Nagios::Plugin->new(
58 version => $VERSION,
59 blurb => _gt('Plugin to livestatus status'),
60 usage => "Usage: %s [ -v|--verbose ] ",
61 timeout => $TIMEOUT+1
62 );
63
64 $np->add_arg (
65 spec => 'debug|d',
66 help => _gt('Debug level'),
67 default => 0,
68 required => 0,
69 );
70
71 $np->getopts;
72 $DEBUG = $np->opts->get('debug');
73
74
75
76 my $query = sprintf("GET status\nOutputFormat: json\n");
77 my $resp = querysocket("$query\n");
78
79 if (!$resp) {
80
81 $np->nagios_exit(CRITICAL,_gt('Livestatus request failed !'));
82 }
83 my $statusJson = decode_json(join('',@{$resp}));
84
85
86 # print Dumper (\$statusJson);
87
88
89 my %data = (
90 'program_version' => '?',
91 'livestatus_version' => '?',
92 );
93
94 for (my $i = 0; $i < @{$statusJson->[0]}; $i++ ) {
95 my $key = $statusJson->[0][$i];
96 my $val = $statusJson->[1][$i];
97 $data{$key} = $val;
98 debug( sprintf(' %-30s : %s',$key,$val) );
99 }
100
101
102 # print STDERR Dumper (\%data);
103
104
105
106 if ( defined($data{'num_services'}) ) {
107 $np->add_perfdata(
108 'label' => 'num_services',
109 'value' => $data{'num_services'},
110 'uom' => 'services'
111 );
112 }
113 if ( defined($data{'num_hosts'}) ) {
114 $np->add_perfdata(
115 'label' => 'num_hosts',
116 'value' => $data{'num_hosts'},
117 'uom' => 'hosts'
118 );
119 }
120
121 if ( defined($data{'service_checks_rate'}) ) {
122 $np->add_perfdata(
123 'label' => 'service_checks_rate',
124 'value' => $data{'service_checks_rate'},
125 'uom' => 'chk/s'
126 );
127 }
128 if ( defined($data{'host_checks_rate'}) ) {
129 $np->add_perfdata(
130 'label' => 'host_checks_rate',
131 'value' => $data{'host_checks_rate'},
132 'uom' => 'chk/s'
133 );
134 }
135
136
137 if ( defined($data{'livechecks_rate'}) ) {
138 $np->add_perfdata(
139 'label' => 'livechecks_rate',
140 'value' => $data{'livechecks_rate'},
141 'uom' => 'chk/s'
142 );
143 }
144 if ( defined($data{'external_commands_rate'}) ) {
145 $np->add_perfdata(
146 'label' => 'ext_cmd_rate',
147 'value' => $data{'external_commands_rate'},
148 'uom' => 'cmd/s'
149 );
150 }
151 if ( defined($data{'forks_rate'}) ) {
152 $np->add_perfdata(
153 'label' => 'forks_rate',
154 'value' => $data{'forks_rate'},
155 'uom' => 'fork/s'
156 );
157 }
158 if ( defined($data{'connections_rate'}) ) {
159 $np->add_perfdata(
160 'label' => 'livestatus_conn_rate',
161 'value' => $data{'connections_rate'},
162 'uom' => 'conn/s'
163 );
164 }
165
166
167
168
169
170 $np->nagios_exit(OK,sprintf(_gt('Livestatus (v%s) is running on v%s monitoring'),$data{'livestatus_version'},$data{'program_version'}));
171
172
173
174
175
176 # Gettext wrapper
177 sub _gt {
178 return gettext($_[0]);
179 }
180
181 sub debug {
182 my ($msg) = @_;
183 print STDERR "DEBUG: $msg\n" if $DEBUG;
184 }
185 # Requètes livestatus
186 sub querysocket {
187 my ($q) = @_;
188 debug("querysocket($q)");
189 my $sockout = IO::Socket::UNIX->new(Peer => $livestatusSocket, Type => SOCK_STREAM) or die "Error connecting to unix domain socket: $@";
190 print $sockout "$q\n";
191 my @ret = <$sockout>;
192 close($sockout);
193 return \@ret;
194 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8