Handle case with no current work
[infodrom/icinga-plugins] / check_infocon
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 DBI;
21 use Getopt::Long;
22 use Config::INI::Reader;
23 use lib '/usr/lib/nagios/plugins';
24 use utils qw(%ERRORS);
25 use constant CONFIG => '/etc/nagios-plugins/check_infocon.conf';
26
27 my $config = {
28     'customer' => undef,
29     'rate' => undef,
30     'warn' => 1000,
31     'crit' => 2000,
32 };
33
34 GetOptions
35     'debitor=s' => \$config->{customer},
36     'rate=s' => \$config->{rate},
37     'warn=s' => \$config->{warn},
38     'crit=s' => \$config->{crit},
39     'help' => \&print_help;
40
41 sub print_help
42 {
43     print<<EOT;
44 help
45 EOT
46     exit $ERRORS{'OK'};
47 }
48
49 my $cfg = Config::INI::Reader->read_file(CONFIG);
50
51 unless ($cfg) {
52     printf "ERROR: Configuration %s not found\n", CONFIG;
53     exit $ERRORS{UNKNOWN};
54 }
55
56 unless ($config->{customer} && $config->{rate}) {
57     printf "ERROR: Debitor or rate not set\n";
58     exit $ERRORS{UNKNOWN};
59 }
60
61 my $dsn = sprintf('dbi:Pg:dbname=%s', $cfg->{infocon}{dbname});
62 my $dbh = DBI->connect($dsn, $cfg->{infocon}{dbuser}, $cfg->{infocon}{dbpass},
63                        {RaiseError => 0});
64
65 if (!$dbh) {
66     print "Access to database denied!\n";
67     exit $ERRORS{UNKNOWN};
68 }
69
70 my $sql = sprintf("SELECT cast(sum(time) as float)/60 AS hours, cast(sum(time) as float)/60 * %d AS open " .
71                   "FROM stempel JOIN stempel_status ON status = stempel_status.id " .
72                   "WHERE customer = '%s' AND name = '%s'",
73                   $config->{rate},
74                   $config->{customer}, 'unknown');
75 my $sth = $dbh->prepare($sql);
76 $sth->execute;
77 my $row = $sth->fetchrow_hashref;
78
79 if ($row->{open} && $row->{open} > $config->{crit}) {
80     printf "CRITICAL: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours};
81     exit $ERRORS{'CRITICAL'};
82 }
83
84 if ($row->{open} && $row->{open} > $config->{warn}) {
85     printf "WARNING: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours};
86     exit $ERRORS{'WARNING'};
87 }
88
89 if (defined $row->{open} && defined $row->{hours}) {
90     printf "OK: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours};
91 } else {
92     printf "OK: %s - Nyms\n", $config->{customer};
93 }
94 exit $ERRORS{'OK'};