plugin-pg__connections: pg__connections

File pg__connections, 4.3 kB (added by ajaja, 6 years ago)
Line 
1 #!/usr/bin/perl -w
2 # Plugin for monitor postgres connections.
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 use DBI;
50
51 my $dbhost = $ENV{'dbhost'} || '127.0.0.1';
52 my $dbname = $ENV{'dbname'} || 'template1';
53 my $dbuser = $ENV{'dbuser'} || 'postgres';
54 my $dbuserx = $ENV{'dbuserx'} || '';
55 my $dbport = $ENV{'dbport'} || '5432';
56 my $dbpass = $ENV{'dbpass'} || '';
57
58 # Check for DBD::Pg
59 if (! eval "require DBD::Pg;") {
60      exit 1;
61 }
62
63 my $dsn = "DBI:Pg:dbname=$dbname;host=$dbhost;port=$dbport";
64 #print "$dsn\n";
65 my $dbh = DBI->connect ($dsn, $dbuser,
66                         $dbpass,
67                         {RaiseError =>1}) || die "";
68
69
70
71 if (exists $ARGV[0]) {
72     if ($ARGV[0] eq 'autoconf') {
73         # Check for DBD::Pg
74         if (! eval "require DBD::Pg;") {
75              print "no (DBD::Pg not found)";
76              exit 1;
77         }
78         if ($dbh) {
79             print "yes\n";
80             exit 0;
81         } else {
82             print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
83             exit 1;
84         }
85     }
86
87     if ($ARGV[0] eq "config") {
88         my $sql_max = "SHOW max_connections;";
89         my $sth_max = $dbh->prepare($sql_max);
90         $sth_max->execute();
91         my ($max_connections) = $sth_max->fetchrow();
92         my $warning = int ($max_connections * 0.7);
93         my $critical = int ($max_connections * 0.8);
94         print "graph_title PostgresSQL active connections\n";
95         print "graph_args -l 0 --base 1000\n";
96         print "graph_vlabel Connections\n";
97         print "graph_category Postgresql\n";
98         print "graph_info Shows active Postgresql connections from $dbname\n";
99        
100
101         my $sql = "select datname, count(*) from pg_stat_activity group by datname";
102         my $sth = $dbh->prepare($sql);
103         $sth->execute();
104         my $setarea = "yes";
105         while ( my ($datname,$curr_conn) = $sth->fetchrow_array ) {
106                 print "$datname.label $datname active connections\n";
107                 print "$datname.info $datname active connections\n";
108                 print "$datname.type GAUGE\n";
109                 if ($setarea eq "yes") {
110                         print "$datname.draw AREA\n";
111                         $setarea="";
112                 } else {
113                         print "$datname.draw STACK\n";
114                 }
115         }
116
117         print "max_connections.label Max. connections\n";
118         print "max_connections.info Max. connections\n";
119         print "max_connections.type GAUGE\n";
120
121         print "warning $warning\n";
122         print "critical $critical\n";
123         exit 0;
124     }
125 }
126
127
128 # select datname, count(*) from pg_stat_activity group by datname
129 my $sql_max = "SHOW max_connections;";
130 my $sth_max = $dbh->prepare($sql_max);
131 $sth_max->execute();
132 my ($max_connections) = $sth_max->fetchrow();
133
134 #my $sql = "SELECT COUNT (*) FROM pg_stat_activity;";
135 my $sql = "select datname, count(*) from pg_stat_activity group by datname";
136 my $sth = $dbh->prepare($sql);
137 $sth->execute();
138 while ( my ($datname,$curr_conn) = $sth->fetchrow_array ) {
139         print "$datname.value $curr_conn\n";
140 }
141 print "max_connections.value $max_connections\n";