src_bin/sneck

filter: Statementstmt 97.0% 132 / 136 Branchbran 100% 50 / 50 Conditioncond 94.4% 17 / 18 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 97.3% 222 / 228 CC 1 SCAR 0.0
CC
1
Cov
98%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« /usr/local/bin/perldoc ↑ Summary
1 #!perl
2  
3 =head1 NAME
4  
5 sneck - a boopable LibreNMS JSON style SNMP extend for remotely running nagios style checks akin to NRPE
6  
7 =head1 SYNOPSIS
8  
9 sneck -u [B<-C> <cache file>] [B<-f> <config file>] [B<-p>] [B<-i>] [B<-d>] [B<-q>] [B<-l>|B<-L>] [B<-P> <pid dir>] [B<-r>] [B<-T> <seconds>] [B<-s> <signal>] [B<-k>|B<-K>]
10  
11 sneck -c [B<-C> <cache file>] [B<-f> <config file>] [B<-b>]
12  
13 sneck [B<-f> <config file>] [B<-p>] [B<-i>] [B<-d>] [B<-q>] [B<-r>] [B<-T> <seconds>] [B<-s> <signal>] [B<-k>|B<-K>]
14  
15 sneck -t [B<-f> <config file>]
16  
17 sneck -h
18  
19 sneck -v
20  
21 =head1 DESCRIPTION
22  
23 For the config file format, see L<Monitoring::Sneck::Config>. For the
24 output, see L<Monitoring::Sneck>.
25  
26 The cache file, PID dir, locking, and check timeout settings may also
27 be set in the config file. Flags override those. See OPTIONS in L<Monitoring::Sneck::Config>.
28  
29 =head1 FLAGS
30  
31 =head2 -f <config file>
32  
33 The config file to use.
34  
35 Files ending in .yaml or .yml are read as YAML, which needs YAML::XS.
36 See L<Monitoring::Sneck::Config> for both formats.
37  
38 Default :: /usr/local/etc/sneck.conf
39  
40 =head2 -p
41  
42 Pretty print the JSON.
43  
44 =head2 -C <cache file>
45  
46 The cache file to use.
47  
48 Overrides cache_file in the config.
49  
50 Default :: /var/cache/sneck.cache
51  
52 A secondary cache file based on this name
53 is also created. By default it is
54 /var/cache/sneck.cache.snmp and always holds
55 the GZip+BASE64 compressed version.
56  
57 Compliance check state, used for interval, is kept in the cache file
58 name with '.compliance' added, so by default
59 /var/cache/sneck.cache.compliance. It is only used if a compliance
60 check has a interval.
61  
62 =head2 -u
63  
64 Update the cache file. Will also print what was written to it.
65  
66 =head2 -c
67  
68 Print the cache file. Please note that B<-p> or B<-i> won't affect
69 this as this flag only reads/prints the cache file.
70  
71 If the cache file does not exist or can't be read, error JSON saying
72 so is printed and it exits 3.
73  
74 =head2 -b
75  
76 When used with B<-c>, print the LibreNMS style GZip+BASE64
77 compressed cache instead.
78  
79 =head2 -i
80  
81 Includes the config file used.
82  
83 =head2 -d
84  
85 Print debugging info to stderr.
86  
87 =head2 -q
88  
89 Don't print the results. Exit quietly. Meant for use with B<-u>.
90  
91 =head2 -l
92  
93 Enable locking for B<-u> so more than one instance can't run at a time.
94 Overrides locking in the config.
95  
96 The PID file is sneck.pid in the directory set by B<-P>.
97  
98 =head2 -L
99  
100 Disable locking, overriding locking in the config. Can not be used with
101 B<-l>.
102  
103 =head2 -P <pid dir>
104  
105 The directory for the PID file used by B<-l>. The user running sneck
106 must be able to write to it.
107  
108 Overrides pid_dir in the config.
109  
110 Default :: /var/run
111  
112 =head2 -r
113  
114 Run any restarts whose checks failed. Without this, restarts are only
115 reported as 'restarts disabled'. Meant to be used with B<-u> from cron.
116 Never use it from snmpd.
117  
118 Restart state, used for min_interval and max_retries, is kept in the
119 cache file name with '.restarts' added, so by default
120 /var/cache/sneck.cache.restarts.
121  
122 Use locking with this to make sure two runs can't restart things at the
123 same time. Without it, a warning saying so is printed to stderr.
124  
125 See L<Monitoring::Sneck::Config> for how restarts are configured.
126  
127 =head2 -T <seconds>
128  
129 Seconds to wait on each check before giving up on it. Overrides
130 check_timeout in the config.
131  
132 Default :: 30
133  
134 =head2 -s <signal>
135  
136 Signal to send a check on timeout, such as TERM. Overrides
137 check_timeout_signal in the config. 'none' sends no signal, even if the
138 config sets one.
139  
140 Default :: none
141  
142 =head2 -k
143  
144 Also send the timeout signal to all child processes of a check.
145 Overrides check_kill_sub_pids in the config. Does nothing without a
146 timeout signal. This is the default.
147  
148 =head2 -K
149  
150 Do not send the timeout signal to child processes of a check. Overrides
151 check_kill_sub_pids in the config. Can not be used with B<-k>.
152  
153 =head2 -t
154  
155 Test the config file. Prints any errors and warnings, then exits 0 if
156 there are no errors and 1 if there are or the file can't be read.
157 Warnings, such as a check using a undefined variable, do not affect the
158 exit code.
159  
160 =head2 -h, --help
161  
162 Print the help.
163  
164 =head2 -v, --version
165  
166 Print the version.
167  
168 =cut
169  
170 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 50 ▶use Getopt::Long qw( GetOptions );
Subroutine BEGIN: called CC=1 SCAR=0.0
173 50 ▶use File::Slurp qw(read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
174 50 ▶use JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
175 50 ▶use Monitoring::Sneck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
176 50 ▶use Monitoring::Sneck::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
177 50 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
178 50 ▶use IO::Compress::Gzip qw(gzip);
Subroutine BEGIN: called CC=1 SCAR=0.0
179 50 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
180 50 ▶use Proc::PID::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 2 ▶sub main::VERSION_MESSAGE {
Subroutine VERSION_MESSAGE: called CC=1 SCAR=0.0
183 2 print 'sneck v. ' . $Monitoring::Sneck::VERSION . "\n";
184 2 exit 255;
185 }
186  
187 2 ▶sub main::HELP_MESSAGE {
Subroutine HELP_MESSAGE: called CC=1 SCAR=0.0
188 2 pod2usage( -exitval => 255, -verbose => 2, -output => \*STDOUT, );
189 }
190  
191 50 my $cache_file;
192 50 my $config_file = '/usr/local/etc/sneck.conf';
193 50 my $update;
194 my $print_cache;
195 50 my $fallback;
196 50 my $help;
197 50 my $version;
198 50 my $pretty;
199 50 my $include;
200 50 my $compress;
201 50 my $debug;
202 50 my $quiet;
203 50 my $locking;
204 50 my $no_locking;
205 50 my $pid_dir;
206 50 my $test_config;
207 50 my $restart;
208 50 my $check_timeout;
209 50 my $check_timeout_signal;
210 50 my $kill_sub_pids;
211 50 my $no_kill_sub_pids;
212 50 Getopt::Long::Configure('no_ignore_case');
213 50 Getopt::Long::Configure('bundling');
214 50 GetOptions(
215 'version' => \$version,
216 'v' => \$version,
217 'help' => \$help,
218 'h' => \$help,
219 'c' => \$print_cache,
220 'f=s' => \$config_file,
221 'C=s' => \$cache_file,
222 'p' => \$pretty,
223 'u' => \$update,
224 'i' => \$include,
225 'b' => \$compress,
226 'd' => \$debug,
227 'q' => \$quiet,
228 'l' => \$locking,
229 'L' => \$no_locking,
230 'P=s' => \$pid_dir,
231 't' => \$test_config,
232 'r' => \$restart,
233 'T=s' => \$check_timeout,
234 's=s' => \$check_timeout_signal,
235 'k' => \$kill_sub_pids,
236 'K' => \$no_kill_sub_pids,
237 );
238  
239 # print version or help if requested
240 50 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 2 48 50
241 2 main::HELP_MESSAGE;
242 0 exit 42;
243 }
244 48 ▶if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 2 46 48
245 2 main::VERSION_MESSAGE;
246 0 exit 42;
247 }
248  
249 46 ▶if ( $locking && $no_locking ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locking and $no_locking) 1 45 46
Condition100%3/3
$locking and $no_locking
!ll&&!rl&&r
4141
MC/DC100%2/2
$locking and $no_locking
$locking $no_locking
Truth table100%3/3
A and B
A= $lockingB= $no_locking
ABresult
0X0
100
111
250 1 die("-l and -L can not be used together\n");
251 }
252 45 ▶if ( $kill_sub_pids && $no_kill_sub_pids ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kill_sub_pids and $no_kill_sub_pids) 1 44 45
Condition100%3/3
$kill_sub_pids and $no_kill_sub_pids
!ll&&!rl&&r
4311
MC/DC100%2/2
$kill_sub_pids and $no_kill_sub_pids
$kill_sub_pids $no_kill_sub_pids
Truth table100%3/3
A and B
A= $kill_sub_pidsB= $no_kill_sub_pids
ABresult
0X0
100
111
253 1 die("-k and -K can not be used together\n");
254 }
255  
256 # test the config and exit if requested
257 44 ▶if ($test_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_config) 8 36 44
258 8 my $parsed_config;
259 8 eval { $parsed_config = Monitoring::Sneck::Config->new( { file => $config_file } ); };
260 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
261 1 print 'error: ' . $@;
262 1 exit 1;
263 }
264  
265 7 foreach my $error ( $parsed_config->errors ) {
266 6 print 'error: ' . $error->{where} . ': ' . $error->{message} . "\n";
267 }
268 7 foreach my $warning ( $parsed_config->warnings ) {
269 8 print 'warning: ' . $warning->{where} . ': ' . $warning->{message} . "\n";
270 }
271  
272 7 ▶ if ( !$parsed_config->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed_config->is_valid) 3 4 7
273 3 exit 1;
274 }
275 4 print 'config OK: ' . $config_file . "\n";
276 4 exit 0;
277 } ## end if ($test_config)
278  
279 # options from the config fill in anything not given on the command line,
280 # using the valid ones even if the config has other errors, so the error
281 # still ends up in the right cache file
282 36 my %config_options;
283 36 eval { %config_options = %{ Monitoring::Sneck::Config->new( { file => $config_file } )->options }; };
284 36 ▶if ( !defined($cache_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache_file) 11 25 36
285 11 ▶ $cache_file = defined( $config_options{cache_file} ) ? $config_options{cache_file} : '/var/cache/sneck.cache';
Branch: 1 branch
BranchTrueFalseTotal
defined $config_options{'cache_file'} ? : 3 8 11
286 }
287 36 ▶if ( !defined($pid_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid_dir) 32 4 36
288 32 ▶ $pid_dir = defined( $config_options{pid_dir} ) ? $config_options{pid_dir} : '/var/run';
Branch: 1 branch
BranchTrueFalseTotal
defined $config_options{'pid_dir'} ? : 4 28 32
289 }
290 36 ▶if ( !$locking && !$no_locking && $config_options{locking} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $locking || $no_locking and $config_options{"locking"}) 2 34 36
Condition100%6/6
not $locking || $no_locking and $config_options{"locking"}
!ll&&!rl&&r
5292
$locking || $no_locking
l!l&&r!l&&!r
4131
MC/DC100%3/3
not $locking || $no_locking and $config_options{"locking"}
$locking $no_locking $config_options{"locking"}
Truth table100%4/4
not(A || B) and C
A= $lockingB= $no_lockingC= $config_options{"locking"}
ABCresult
1XX0
01X0
0000
0011
291 2 $locking = 1;
292 }
293  
294 # prints the cache and exit if requested
295 36 ▶if ($print_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print_cache) 5 31 36
296 5 ▶ if ($compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress) 2 3 5
297 2 $cache_file = $cache_file . '.snmp';
298 }
299  
300 5 ▶ if ( !-f $cache_file || !-r $cache_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cache_file and -r $cache_file) 2 3 5
Condition66%2/3
-f $cache_file and -r $cache_file
!ll&&!rl&&r
203
MC/DC50%1/2
-f $cache_file and -r $cache_file
-f $cache_file -r $cache_file
Truth table66%2/3
A and B
A= -f $cache_fileB= -r $cache_file
ABresult
0X0
100
111
301 2 my $error = 'Cache file does not exist or is not readable "' . $cache_file . '"';
302 2 my $possible_error
303 = { error => 1, version => 1, errorString => $error, data => { alert => 1, alertString => $error } };
304 2 print encode_json($possible_error) . "\n";
305 2 exit 3;
306 }
307 3 my $cache = read_file($cache_file);
308  
309 3 print $cache;
310  
311 3 exit;
312 } ## end if ($print_cache)
313  
314 31 ▶if ( $restart && !$locking ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart and not $locking) 3 28 31
Condition100%3/3
$restart and not $locking
!ll&&!rl&&r
2623
MC/DC100%2/2
$restart and not $locking
$restart not $locking
Truth table100%3/3
A and not(B)
A= $restartB= not $locking
ABresult
0X0
100
111
315 3 warn("-r used without locking, so overlapping runs may restart things more than once\n");
316 }
317  
318 31 ▶my $sneck = Monitoring::Sneck->new(
Branch: 2 branches
BranchTrueFalseTotal
$kill_sub_pids ? : 1 30 31
$no_kill_sub_pids ? : 1 29 30
319 {
320 config => $config_file,
321 include => $include,
322 debug => $debug,
323 restart => $restart,
324 state_file => $cache_file . '.restarts',
325 compliance_state_file => $cache_file . '.compliance',
326 check_timeout => $check_timeout,
327 check_timeout_signal => $check_timeout_signal,
328 check_kill_sub_pids => $kill_sub_pids ? 1 : $no_kill_sub_pids ? 0 : undef,
329 }
330 );
331 # -l if asked for
332 31 ▶if ($locking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locking) 6 25 31
333 6 my $alive;
334 6 eval { $alive = Proc::PID::File->running( dir => $pid_dir, name => 'sneck' ); };
335 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 3 6
336 3 die( 'locking enabled and PID file check failed... ' . $@ );
337 0 exit 255;
338 }
339 3 ▶ if ($alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alive) 1 2 3
340 1 die( 'Already running as ' . $alive );
341 0 exit 255;
342 }
343 } ## end if ($locking)
344 27 my $returned = $sneck->run;
345  
346 # encode it and print it
347 27 my $json = JSON->new->utf8->canonical(1);
348 27 ▶if ($pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pretty) 2 25 27
349 2 $json->pretty;
350 }
351 27 my $raw_json = $json->encode($returned);
352  
353 27 my $compressed_string;
354 27 gzip \$raw_json => \$compressed_string;
355 27 my $compressed = encode_base64($compressed_string);
356 27 $compressed =~ s/\n//g;
357 27 $compressed = $compressed . "\n";
358 27 my $snmp = $compressed;
359  
360 # non-pretty does not include a new line, so add it
361 27 ▶if ( !$pretty ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pretty) 25 2 27
362 25 $raw_json = $raw_json . "\n";
363 }
364  
365 # don't print it if told to be quiet via -q
366 27 ▶if ( !$quiet ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quiet) 19 8 27
367 19 print $raw_json;
368 }
369  
370 27 ▶if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 16 11 27
371 16 write_file( $cache_file, { atomic => 1 }, $raw_json );
372 15 write_file( $cache_file . '.snmp', { atomic => 1 }, $snmp );
373 }
« /usr/local/bin/perldoc ↑ Summary