3 # Copyright (c) 2014 Joey Schulze <joey@infodrom.org>
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.
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.
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
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';
35 'debitor=s' => \$config->{customer},
36 'rate=s' => \$config->{rate},
37 'warn=s' => \$config->{warn},
38 'crit=s' => \$config->{crit},
39 'help' => \&print_help;
49 my $cfg = Config::INI::Reader->read_file(CONFIG);
52 printf "ERROR: Configuration %s not found\n", CONFIG;
53 exit $ERRORS{UNKNOWN};
56 unless ($config->{customer} && $config->{rate}) {
57 printf "ERROR: Debitor or rate not set\n";
58 exit $ERRORS{UNKNOWN};
61 my $dsn = sprintf('dbi:Pg:dbname=%s', $cfg->{infocon}{dbname});
62 my $dbh = DBI->connect($dsn, $cfg->{infocon}{dbuser}, $cfg->{infocon}{dbpass},
66 print "Access to database denied!\n";
67 exit $ERRORS{UNKNOWN};
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'",
74 $config->{customer}, 'unknown');
75 my $sth = $dbh->prepare($sql);
77 my $row = $sth->fetchrow_hashref;
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'};
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'};
89 if (defined $row->{open} && defined $row->{hours}) {
90 printf "OK: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours};
92 printf "OK: %s - Nyms\n", $config->{customer};