-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_pgconn.pl
executable file
·76 lines (63 loc) · 1.82 KB
/
check_pgconn.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
use DBI;
# Partially cleaned up version from
# https://www.monitoringexchange.org/inventory/Check-Plugins/Database/PostgreSQL/check_pgsql_connections
# by Oliver Falk <oliver@linux-kernel.at>, 2014
use strict;
use warnings;
use Getopt::Long;
my $host;
my $dbname = 'postgres';
my $dbuser = 'postgres';
my $dbport = 5432;
my $warn = 70;
my $crit = 80;
GetOptions(
"host|h=s" => \$host,
"dbname|d=s" => \$dbname,
"dbuser|u=s" => \$dbuser,
"dbport|p=i" => \$dbport,
"warning|w=i" => \$warn,
"critical|c=i" => \$crit,
);
if($warn > $crit) {
print "Warning level ($warn) must be greater than critical level ($crit)\n";
exit -1;
}
unless($host) {
print "Provide an IP or hostname using --host\n";
exit -1;
}
# Default to UNKNOWN status
my $status = 3;
my $Con = "DBI:Pg:dbname=$dbname;host=$host;port=$dbport";
my $Dbh = DBI->connect($Con, $dbuser, '', {
RaiseError => 1
}) || die "Unable to access Database $dbname on host $host as user $dbuser.\nError returned was: ". $DBI::errstr;
my $sql_max = "SHOW max_connections;";
my $sth_max = $Dbh->prepare($sql_max);
$sth_max->execute();
my $max_conn;
while (my ($mconn) = $sth_max->fetchrow()) {
$max_conn = $mconn;
}
my $sql_curr = "SELECT COUNT(*) FROM pg_stat_activity;";
my $sth_curr = $Dbh->prepare($sql_curr);
$sth_curr->execute();
my $curr_conn;
while ((my $conn) = $sth_curr->fetchrow()) {
$curr_conn = $conn;
}
my $avail_conn = $max_conn-$curr_conn;
my $avail_pct = $avail_conn/$max_conn*100;
my $used_pct = sprintf("%2.1f", $curr_conn/$max_conn*100);
if ($avail_pct < (100 - $warn) || $avail_conn < (100 - $warn)) {
$status = 2;
} elsif ($avail_pct < (100 - $crit) || $avail_conn < (100 - $crit)) {
$status = 1;
} else {
$status = 0;
}
my $msg = "$curr_conn of $max_conn Connections Used ($used_pct%) | used=$used_pct%;$warn;$crit;;\n";
print $msg;
exit $status;