#! /usr/bin/perl -w # Copyright (c) 2014 Joey Schulze # # 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<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} && $row->{open} > $config->{crit}) { printf "CRITICAL: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours}; exit $ERRORS{'CRITICAL'}; } if ($row->{open} && $row->{open} > $config->{warn}) { printf "WARNING: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours}; exit $ERRORS{'WARNING'}; } if (defined $row->{open} && defined $row->{hours}) { printf "OK: %s - EUR %.2f (%.2fh)\n", $config->{customer}, $row->{open}, $row->{hours}; } else { printf "OK: %s - Nyms\n", $config->{customer}; } exit $ERRORS{'OK'};