Improve plugin output
[infodrom/icinga-plugins] / check_ticker
1 #! /usr/bin/perl -w
2
3 #   Copyright (c) 2014  Joey Schulze <joey@infodrom.org>
4 #
5 #   This program is free software; you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation; either version 2 of the License, or
8 #   (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 of
12 #   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; if not, write to the Free Software
17 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
18
19 use strict;
20 use Getopt::Long;
21 use lib '/usr/lib/nagios/plugins';
22 use utils qw(%ERRORS);
23 use POSIX qw(strftime);
24
25 my $config = {
26     'host' => undef,
27     'warn' => 24,
28     'crit' => 72,
29
30 };
31
32 my $ticker;
33 Getopt::Long::Configure('no_ignore_case');
34 GetOptions
35     'ticker=s' => \$ticker,
36     'warn=s' => \$config->{warn},
37     'crit=s' => \$config->{crit},
38     'help' => \&print_help;
39
40 sub print_help
41 {
42     print<<EOT;
43 help
44 EOT
45     exit $ERRORS{'OK'};
46 }
47
48 sub path
49 {
50     my $name = shift;
51     my $month = shift;
52
53     return sprintf("/var/www/private-lists/ticker-%s/ticker-%s.%s", $name, $name, $month);
54 }
55
56 my $month = strftime '%Y%m', localtime;
57 my $fname = path $ticker, $month;
58
59 if (-r $fname) {
60     my $mtime = (stat(_))[9];
61
62     if ($mtime < time - ($config->{crit} * 60*60)) {
63         printf "TICKER CRITICAL - %s older than %d hours\n", $fname, $config->{crit};
64         exit $ERRORS{'CRITICAL'};
65     } elsif ($mtime < time - ($config->{warn} * 60*60)) {
66         printf "TICKER WARNING - %s older than %d hours\n", $fname, $config->{warn};
67         exit $ERRORS{'WARNING'};
68     }
69
70     my $diff = (time - $mtime) / 60;
71     my $time = $diff % 60;
72     if ($diff > 60) {
73         $time = sprintf('%d:%02d hours', $diff / 60, $time);
74     } else {
75         $time .= ' minutes';
76     }
77
78     printf "TICKER OK - %s updated %s ago\n", $ticker, $time;
79     exit $ERRORS{'OK'};
80 } else {
81     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
82     my $monthstart = time - ($mday-1) * 60*60*24 - $hour*60*60 - $min*60 - $sec;
83
84     if (time > $monthstart + ($config->{crit} * 60*60)) {
85         printf "TICKER CRITICAL - %s not updated for %d hours\n", $ticker, $config->{crit};
86         exit $ERRORS{'CRITICAL'};
87     } elsif (time > $monthstart + ($config->{warn} * 60*60)) {
88         printf "TICKER WARNING - %s not updated for %d hours\n", $ticker, $config->{warn};
89         exit $ERRORS{'WARNING'};
90     }
91
92     my $diff = (time - $monthstart) / 60;
93     my $time = $diff % 60;
94     if ($diff > 60) {
95         $time = sprintf('%d:%02d', $diff / 60, $time);
96     } else {
97         $time .= ' minutes';
98     }
99
100     printf "TICKER OK - %s not updated for %s\n", $ticker, $time;
101     exit $ERRORS{'OK'};
102 }
103