Add special check to warn before customers get too high invoices
authorJoey Schulze <joey@infodrom.org>
Wed, 14 May 2014 21:25:18 +0000 (23:25 +0200)
committerJoey Schulze <joey@infodrom.org>
Wed, 14 May 2014 21:25:41 +0000 (23:25 +0200)
check_infocon [new file with mode: 0755]

diff --git a/check_infocon b/check_infocon
new file mode 100755 (executable)
index 0000000..d92af73
--- /dev/null
@@ -0,0 +1,90 @@
+#! /usr/bin/perl -w
+
+#   Copyright (c) 2014  Joey Schulze <joey@infodrom.org>
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 2 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program; if not, write to the Free Software
+#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
+
+use strict;
+use DBI;
+use Getopt::Long;
+use Config::INI::Reader;
+use lib '/usr/lib/nagios/plugins';
+use utils qw(%ERRORS);
+use constant CONFIG => '/etc/nagios-plugins/check_infocon.conf';
+
+my $config = {
+    'customer' => undef,
+    'rate' => undef,
+    'warn' => 1000,
+    'crit' => 2000,
+};
+
+GetOptions
+    'debitor=s' => \$config->{customer},
+    'rate=s' => \$config->{rate},
+    'warn=s' => \$config->{warn},
+    'crit=s' => \$config->{crit},
+    'help' => \&print_help;
+
+sub print_help
+{
+    print<<EOT;
+help
+EOT
+    exit $ERRORS{'OK'};
+}
+
+my $cfg = Config::INI::Reader->read_file(CONFIG);
+
+unless ($cfg) {
+    printf "ERROR: Configuration %s not found\n", CONFIG;
+    exit $ERRORS{UNKNOWN};
+}
+
+unless ($config->{customer} && $config->{rate}) {
+    printf "ERROR: Debitor or rate not set\n";
+    exit $ERRORS{UNKNOWN};
+}
+
+my $dsn = sprintf('dbi:Pg:dbname=%s', $cfg->{infocon}{dbname});
+my $dbh = DBI->connect($dsn, $cfg->{infocon}{dbuser}, $cfg->{infocon}{dbpass},
+                      {RaiseError => 0});
+
+if (!$dbh) {
+    print "Access to database denied!\n";
+    exit $ERRORS{UNKNOWN};
+}
+
+my $sql = sprintf("SELECT cast(sum(time) as float)/60 AS hours, cast(sum(time) as float)/60 * %d AS open " .
+                 "FROM stempel JOIN stempel_status ON status = stempel_status.id " .
+                 "WHERE customer = '%s' AND name = '%s'",
+                 $config->{rate},
+                 $config->{customer}, 'unknown');
+my $sth = $dbh->prepare($sql);
+$sth->execute;
+my $row = $sth->fetchrow_hashref;
+
+if ($row->{open} > $config->{crit}) {
+    printf "CRITICAL: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours};
+    exit $ERRORS{'CRITICAL'};
+}
+
+if ($row->{open} > $config->{warn}) {
+
+    exit $ERRORS{'WARNING'};
+}
+
+printf "OK: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours};
+exit $ERRORS{'OK'};