Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / postgresql / pgbouncer_ @ 647632f7

Historique | Voir | Annoter | Télécharger (9,38 ko)

1
#!/usr/bin/perl -w
2

    
3
# re-write of python version of pgbouncer stats
4
# data from stats, pools (cleint, server)
5

    
6
use strict;
7
use Munin::Plugin;
8
use DBD::Pg;
9

    
10
# check that multigraph is avaailable
11
need_multigraph();
12
# get the script name
13
my $plugin_name = $Munin::Plugin::me;
14
# set the DB connection vars
15
my $db_user = $ENV{'pgbouncer_user'}  || 'postgres';
16
my $db_port = $ENV{'pgbouncer_port'}  || '6432';
17
my $db_host = $ENV{'pgbouncer_host'}  || 'localhost';
18
my $db_pass = $ENV{'pgbouncer_pass'}  || '';
19
my $db_name = 'pgbouncer';
20
my @data = ();
21
# get the DB (pool) name we want to fetch
22
$plugin_name =~ /pgbouncer_(.*)$/;
23
my $pool_name = $1;
24
# bail if no name
25
if (!$pool_name)
26
{
27
	print "Cannot get pool name\n";
28
	exit 1; 
29
}
30

    
31
# command line arguments for autconf and config
32
if (defined($ARGV[0]))
33
{
34
	# autoconf, nothing to do
35
	if ($ARGV[0] eq 'autoconf')
36
	{
37
		my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass);
38
		if (!$dbh)
39
		{
40
			print "no\n";
41
			exit 1;
42
		}
43
		else
44
		{
45
			print "yes\n";
46
			exit 0;
47
		}
48
		$dbh->disconnect();
49
	}
50

    
51
	if ($ARGV[0] eq 'config')
52
	{
53
		# create the basic RRD
54
		# stats: average connections
55
		print "multigraph ".$plugin_name."_stats_avg_req\n";
56
		print "graph_title PgBouncer $pool_name average connections\n";
57
		print "graph_args --base 1000\n"; # numbers not bytes
58
		print "graph_vlabel Average connections\n";
59
		print "graph_scale no\n"; # so we do not print "micro, milli, kilo, etc"
60
    	print "graph_category pgbouncer\n";
61
		print $pool_name."_avg_req.type GAUGE\n";
62
		print $pool_name."_avg_req.label Avg Req\n";
63
		print $pool_name."_avg_req.min 0\n";
64
		print $pool_name."_avg_req.draw LINE2\n";
65
		# stats: average time for query
66
		print "multigraph ".$plugin_name."_stats_avg_query\n";
67
		print "graph_title PgBouncer $pool_name average query time\n";
68
		print "graph_args --base 1000\n"; # numbers not bytes
69
		print "graph_vlabel Average time per query (microseconds)\n";
70
    	print "graph_category pgbouncer\n";
71
		print $pool_name."_avg_query.type GAUGE\n";
72
		print $pool_name."_avg_query.label Avg Time\n";
73
		print $pool_name."_avg_query.min 0\n";
74
		print $pool_name."_avg_query.draw LINE2\n";
75
		# stats: in/out bytes
76
		print "multigraph ".$plugin_name."_stats_bytesinout\n";
77
		print "graph_title PgBouncer $pool_name average bytes received/sent\n";
78
		print "graph_args --base 1024\n"; # numbers in bytes
79
		print "graph_vlabel Average bytes received (-)/sent (+)\n";
80
    	print "graph_category pgbouncer\n";
81
		# bytes received
82
		print $pool_name."_avg_recv.type GAUGE\n";
83
		print $pool_name."_avg_recv.label Avg received\n";
84
		print $pool_name."_avg_recv.min 0\n";
85
		print $pool_name."_avg_recv.draw LINE1\n";
86
		print $pool_name."_avg_recv.graph no\n";
87
		# bytes sent
88
		print $pool_name."_avg_sent.type GAUGE\n";
89
		print $pool_name."_avg_sent.label Avg rcvd/sent\n";
90
		print $pool_name."_avg_sent.min 0\n";
91
		print $pool_name."_avg_sent.draw LINE1\n";
92
		print $pool_name."_avg_sent.negative ".$pool_name."_avg_recv\n";
93
		# pools: server (sv_)
94
		print "multigraph ".$plugin_name."_pools_server\n";
95
		print "graph_title PgBouncer $pool_name servers\n";
96
		print "graph_category pgbouncer\n";
97
		print "graph_args --base 1000\n"; # numbers not bytes
98
		print "graph_vlabel Server connections\n";
99
		print "graph_scale no\n";
100
		# active connections
101
		print $pool_name."_server_active.label active\n";
102
		print $pool_name."_server_active.min 0\n";
103
		print $pool_name."_server_active.type GAUGE\n";
104
		print $pool_name."_server_active.draw AREA\n";
105
		# idle connections
106
		print $pool_name."_server_idle.label idle\n";
107
		print $pool_name."_server_idle.min 0\n";
108
		print $pool_name."_server_idle.type GAUGE\n";
109
		print $pool_name."_server_idle.draw STACK\n";
110
		# used connections
111
		print $pool_name."_server_used.label used\n";
112
		print $pool_name."_server_used.min 0\n";
113
		print $pool_name."_server_used.type GAUGE\n";
114
		print $pool_name."_server_used.draw STACK\n";
115
		# tested connections
116
		print $pool_name."_server_tested.label tested\n";
117
		print $pool_name."_server_tested.min 0\n";
118
		print $pool_name."_server_tested.type GAUGE\n";
119
		print $pool_name."_server_tested.draw STACK\n";
120
		# logged in connections
121
		print $pool_name."_server_login.label login\n";
122
		print $pool_name."_server_login.min 0\n";
123
		print $pool_name."_server_login.type GAUGE\n";
124
		print $pool_name."_server_login.draw STACK\n";
125
		# pools: client (cl_)
126
		print "multigraph ".$plugin_name."_pools_client\n";
127
		print "graph_title PgBouncer $pool_name clients\n";
128
		print "graph_category pgbouncer\n";
129
		print "graph_args --base 1000\n"; # numbers not bytes
130
		print "graph_vlabel Client connections\n";
131
		print "graph_scale no\n";
132
		# active client connections
133
		print $pool_name."_client_active.label active\n";
134
		print $pool_name."_client_active.min 0\n";
135
		print $pool_name."_client_active.type GAUGE\n";
136
		print $pool_name."_client_active.draw AREA\n";
137
		# waiting client connections
138
		print $pool_name."_client_waiting.label waiting\n";
139
		print $pool_name."_client_waiting.min 0\n";
140
		print $pool_name."_client_waiting.type GAUGE\n";
141
		print $pool_name."_client_waiting.draw STACK\n";
142
		# pools: maxwait (longest waiting connection, should be 0)
143
		print "multigraph ".$plugin_name."_pools_maxwait\n";
144
		print "graph_title PgBouncer $pool_name maximum waiting time\n";
145
		print "graph_args --base 1000\n"; # numbers not bytes
146
		print "graph_vlabel Maximum wait time (seconds)\n";
147
    	print "graph_category pgbouncer\n";
148
		print $pool_name."_maxwait.type GAUGE\n";
149
		print $pool_name."_maxwait.label Wait Time\n";
150
		print $pool_name."_maxwait.min 0\n";
151
		print $pool_name."_maxwait.draw LINE2\n";
152
		print $pool_name."_maxwait.warning 1\n"; # warn if not 0
153
		print $pool_name."_maxwait.critical 10\n"; # go critical if 10 seconds waiting
154
		# END graph
155
		exit 0;
156
	}
157
}
158

    
159
# connect to data
160
my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass);
161
# go trough each set and get the data
162
foreach my $get ('pools', 'stats')
163
{
164
	# prep and execute the show query
165
	my $pre = $dbh->prepare("SHOW $get");
166
	$pre->execute();
167
	while (@data = $pre->fetchrow)
168
	{
169
		# first defines the pool
170
		if ($data[0] eq $pool_name)
171
		{
172
			# print values for the stats: average reqeust, average query time, bytes in/out
173
			if ($get eq 'stats')
174
			{
175
				print "multigraph ".$plugin_name."_".$get."_avg_req\n";
176
				print $pool_name."_avg_req.value ".$data[5]."\n";
177
				print "multigraph ".$plugin_name."_".$get."_avg_query\n";
178
				print $pool_name."_avg_query.value ".$data[8]."\n";
179
				print "multigraph ".$plugin_name."_".$get."_bytesinout\n";
180
				print $pool_name."_avg_recv.value ".$data[6]."\n";
181
				print $pool_name."_avg_sent.value ".$data[7]."\n";
182
			}
183
			# print data for the pools: server, client
184
			if ($get eq 'pools')
185
			{
186
				print "multigraph ".$plugin_name."_".$get."_server\n";
187
				print $pool_name."_server_active.value ".$data[4]."\n";
188
				print $pool_name."_server_idle.value ".$data[5]."\n";
189
				print $pool_name."_server_used.value ".$data[6]."\n";
190
				print $pool_name."_server_tested.value ".$data[7]."\n";
191
				print $pool_name."_server_login.value ".$data[8]."\n";
192
				print "multigraph ".$plugin_name."_".$get."_client\n";
193
				print $pool_name."_client_active.value ".$data[2]."\n";
194
				print $pool_name."_client_waiting.value ".$data[3]."\n";
195
				print "multigraph ".$plugin_name."_".$get."_maxwait\n";
196
				print $pool_name."_maxwait.value ".$data[9]."\n";
197
			}
198
		}
199
	}
200
}
201
# close connection
202
$dbh->disconnect();
203

    
204
exit 0;
205

    
206
__END__
207

    
208
=head1 NAME
209

    
210
pgbouncer_ is a plugin to get the pool and stat values for a single pgbouncer pool name
211

    
212
=head1 APPLICATION
213

    
214
perl and DBD::Pg is required, and pgbounce must been installed with a correct setup access for a stat account
215

    
216
=head1 CONFIGURATION
217

    
218
the plugin that will be run needs to have the pool name after the plguin base name.
219

    
220
=head2 plugin configuration
221

    
222
eg: pgbouncer_foo will run for the pool named foo.
223

    
224
see SHOW POOLS database list for the pool name
225

    
226
=head2 munin plugin config file
227

    
228
in the plugin config file under the [pgbouncer] name the access information ca be set.
229

    
230
eg:
231
  [pgbouncer*]
232
    env.pgbouncer_pass barfoo
233

    
234
more extended would be:
235
  [pgbouncer*]
236
    env.pgbouncer_pass barfoo
237
    env.pgbouncer_user bar
238
    env.pgbouncer_port 6542
239
    env.pgbouncer_host localhost
240

    
241
The database name is always pgbouncer
242

    
243
=head1 OUTPUT
244

    
245
The plugin will output 5 graphs in the group pgbouncer
246

    
247
=head2 Average bytes received/sent
248

    
249
This graph will show the average bytes sent and received by the pgbouncer for this pool
250

    
251
=head2 Avaerage connections
252

    
253
This graph will show the average amount of connections to the pgbouncer for this pool 
254

    
255
=head2 Average query time
256

    
257
This graph shows the average query time as processed by the pgbouncer for this pool in microseconds. The data will be shorted by standard SI. eg, m = milli, k = kilo.
258

    
259
So 4.61K is 4610 milliseconds
260

    
261
=head2 Client connections
262

    
263
This graph shows the active and waiting client connections to pgbouncer for this pool
264

    
265
=head2 Server connections
266

    
267
This graph shows the server connections to pgbouncer for this pool. The following data sets are shown: active, idle, used, tested, login
268

    
269
=head2 Max wait
270

    
271
how long the oldest cllient the queue has waited, should be always 0
272

    
273
=head1 ACKNOWLEDGEMENTS
274

    
275
Original idea derived from a simple python script by Dimitri Fontaine
276

    
277
=head1 SEE ALSO
278

    
279
See further info on stats and pools on the pgbouncer homepage:
280
  http://pgbouncer.projects.postgresql.org/doc/usage.html#_show_commands
281

    
282
=head1 VERSION
283

    
284
1.0
285

    
286
=head1 AUTHOR
287

    
288
Clemens Schwaighofer <gullevek@gullevek.org>
289

    
290
=head1 LICENSE
291

    
292
GPLv2
293

    
294

    
295
=cut