root / plugins / postgresql / pg__connections @ 17f78427
Historique | Voir | Annoter | Télécharger (3,7 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# Plugin for monitor postgres connections without DBI. |
| 3 |
# |
| 4 |
# Licenced under GPL v2. |
| 5 |
# |
| 6 |
# Usage: |
| 7 |
# |
| 8 |
# Symlink into /etc/munin/plugins/ and add the monitored |
| 9 |
# database to the filename. e.g.: |
| 10 |
# |
| 11 |
# ln -s /usr/share/munin/plugins/pg__connections \ |
| 12 |
# /etc/munin/plugins/pg_<databasename>_connections |
| 13 |
# This should, however, be given through autoconf and suggest. |
| 14 |
# |
| 15 |
# If required, give username, password and/or Postgresql server |
| 16 |
# host through environment variables. |
| 17 |
# |
| 18 |
# You must also activate Postgresql statistics. See |
| 19 |
# http://www.postgresql.org/docs/7.4/interactive/monitoring-stats.html |
| 20 |
# for how to enable this. Specifically, the following lines must |
| 21 |
# exist in your postgresql.conf: |
| 22 |
# |
| 23 |
# stats_start_collector = true |
| 24 |
# stats_block_level = true |
| 25 |
# |
| 26 |
# |
| 27 |
# Parameters: |
| 28 |
# |
| 29 |
# config (required) |
| 30 |
# |
| 31 |
# Config variables: |
| 32 |
# |
| 33 |
# dbhost - Which database server to use. Defaults to |
| 34 |
# 'localhost'. |
| 35 |
# dbname - Which database to use. Defaults to template1 |
| 36 |
# dbuser - A Postgresql user account with read permission to |
| 37 |
# the given database. Defaults to |
| 38 |
# 'postgres'. Anyway, Munin must be told which user |
| 39 |
# this plugin should be run as. |
| 40 |
# dbpass - The corresponding password, if |
| 41 |
# applicable. Default to undef. Remember that |
| 42 |
# pg_hba.conf must be configured accordingly. |
| 43 |
# |
| 44 |
# Magic markers |
| 45 |
#%# family=auto |
| 46 |
#%# capabilities=autoconf |
| 47 |
|
| 48 |
use strict; |
| 49 |
|
| 50 |
my $psql = $ENV{'psql'} || '/usr/bin/psql';
|
| 51 |
my $dbhost = $ENV{'dbhost'} || '127.0.0.1';
|
| 52 |
my $dbname = $ENV{'dbname'} || 'template1';
|
| 53 |
my $dbuser = $ENV{'dbuser'} || 'munin';
|
| 54 |
#my $dbuserx = $ENV{'dbuserx'} || '';
|
| 55 |
my $dbport = $ENV{'dbport'} || '5432';
|
| 56 |
my $dbpass = $ENV{'dbpass'} || 'munin';
|
| 57 |
|
| 58 |
my $COMMAND = "PGPASSWORD='$dbpass' '$psql' '$dbname' -h '$dbhost' -U '$dbuser' -p '$dbport' -A -F, -t "; |
| 59 |
|
| 60 |
if (exists $ARGV[0]) {
|
| 61 |
if ($ARGV[0] eq "config") {
|
| 62 |
my $sqlCommand = "$COMMAND -c 'SHOW max_connections;'"; |
| 63 |
my ($max_connections) = `$sqlCommand`; |
| 64 |
my $warning = int ($max_connections * 0.7); |
| 65 |
my $critical = int ($max_connections * 0.8); |
| 66 |
print "graph_title PostgresSQL active connections\n"; |
| 67 |
print "graph_args -l 0 --base 1000\n"; |
| 68 |
print "graph_vlabel Connections\n"; |
| 69 |
print "graph_category db\n"; |
| 70 |
print "graph_info Shows active Postgresql connections from $dbname\n"; |
| 71 |
|
| 72 |
|
| 73 |
$sqlCommand = "$COMMAND -c 'select datname, count(*) from pg_stat_activity group by datname;'"; |
| 74 |
open(SERVICE, "$sqlCommand |") |
| 75 |
or die("Could not execute '$sqlCommand': $!");
|
| 76 |
|
| 77 |
my $setarea = "yes"; |
| 78 |
while (<SERVICE>) {
|
| 79 |
my ($datname, $curr_conn) = (m/(\w+).*?(\d+(?:\.\d+)?)/); |
| 80 |
|
| 81 |
print "$datname.label $datname active connections\n"; |
| 82 |
print "$datname.info $datname active connections\n"; |
| 83 |
print "$datname.type GAUGE\n"; |
| 84 |
if ($setarea eq "yes") {
|
| 85 |
print "$datname.draw AREA\n"; |
| 86 |
$setarea=""; |
| 87 |
} else {
|
| 88 |
print "$datname.draw STACK\n"; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
print "max_connections.label Max. connections\n"; |
| 93 |
print "max_connections.info Max. connections\n"; |
| 94 |
print "max_connections.type GAUGE\n"; |
| 95 |
|
| 96 |
print "warning $warning\n"; |
| 97 |
print "critical $critical\n"; |
| 98 |
exit 0; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
# select datname, count(*) from pg_stat_activity group by datname |
| 103 |
my $sqlCommand = "$COMMAND -c 'SHOW max_connections;'"; |
| 104 |
my ($max_connections) = `$sqlCommand`; |
| 105 |
|
| 106 |
$sqlCommand = "$COMMAND -c 'select datname, count(*) from pg_stat_activity group by datname;'"; |
| 107 |
open(SERVICE, "$sqlCommand |") |
| 108 |
or die("Could not execute '$sqlCommand': $!");
|
| 109 |
|
| 110 |
while (<SERVICE>) {
|
| 111 |
my ($datname, $curr_conn) = (m/(\w+).*?(\d+(?:\.\d+)?)/); |
| 112 |
print "$datname.value $curr_conn\n"; |
| 113 |
} |
| 114 |
print "max_connections.value $max_connections\n"; |
