Projet

Général

Profil

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

root / plugins / postgresql / pgbouncer_ @ 2c912170

Historique | Voir | Annoter | Télécharger (10,3 ko)

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

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

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

    
10
# check that multigraph is available
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_pool = $ENV{'pgbouncer_pool'}  || '';
20
my $db_name = 'pgbouncer';
21
my @data = ();
22
# get the DB (pool) name we want to fetch
23
$plugin_name =~ /pgbouncer_(.*)$/;
24
my $plugin_suffix = $1;
25
#if pool name is specified explicitly in config file
26
#use plugin name together with pool name in graph title:
27
my $pool_name = ($db_pool) ? $db_pool : $plugin_suffix;
28
my $plugin_title = ($db_pool) ? $plugin_suffix." ".$pool_name : $pool_name;
29

    
30
# bail if no name
31
if (!$pool_name)
32
{
33
	print "Cannot get pool name\n";
34
	exit 1;
35
}
36

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

    
56
	if ($ARGV[0] eq 'config')
57
	{
58
		# create the basic RRD
59
		# stats: average connections
60

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

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

    
213
exit 0;
214

    
215
__END__
216

    
217
=head1 NAME
218

    
219
pgbouncer_ is a plugin to get the pool and stat values for a single pgbouncer pool name
220

    
221
=head1 APPLICATION
222

    
223
perl and DBD::Pg is required, and pgbouncer must been installed with a correct setup access for a stat account
224

    
225
=head1 CONFIGURATION
226

    
227
the plugin that will be run needs to have the pool name after the plugin base name.
228
alternatively, pool name can be specified in config file as env.pgbouncer_pool option, separating plugin name from pool name.
229

    
230
=head2 plugin configuration
231

    
232
eg: pgbouncer_foo will run for the pool named foo.
233

    
234
see SHOW POOLS database list for the pool name
235

    
236
=head2 munin plugin config file
237

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

    
240
eg:
241
  [pgbouncer*]
242
    env.pgbouncer_pass barfoo
243

    
244
more extended would be:
245
  [pgbouncer*]
246
    env.pgbouncer_pass barfoo
247
    env.pgbouncer_user bar
248
    env.pgbouncer_port 6542
249
    env.pgbouncer_host localhost
250

    
251
another example, where different pgbouncers (and so munin plugins) connecting to same db:
252
   [pgbouncer_weblogin]
253
     env.pgbouncer_pass barfoo
254
     env.pgbouncer_user bar
255
     env.pgbouncer_port 6542
256
     env.pgbouncer_host localhost
257
     env.pgbouncer_pool dbname
258

    
259
   [pgbouncer_webmain]
260
     env.pgbouncer_pass barfoo
261
     env.pgbouncer_user bar
262
     env.pgbouncer_port 6543
263
     env.pgbouncer_host localhost
264
     env.pgbouncer_pool dbname
265

    
266
The database name is always pgbouncer
267

    
268
=head1 OUTPUT
269

    
270
The plugin will output 5 graphs in the group pgbouncer
271

    
272
=head2 Average bytes received/sent
273

    
274
This graph will show the average bytes sent and received by the pgbouncer for this pool
275

    
276
=head2 Average connections
277

    
278
This graph will show the average amount of connections to the pgbouncer for this pool
279

    
280
=head2 Average query time
281

    
282
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.
283

    
284
So 4.61K is 4610 milliseconds
285

    
286
=head2 Client connections
287

    
288
This graph shows the active and waiting client connections to pgbouncer for this pool
289

    
290
=head2 Server connections
291

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

    
294
=head2 Max wait
295

    
296
how long the oldest client the queue has waited, should be always 0
297

    
298
=head1 ACKNOWLEDGEMENTS
299

    
300
Original idea derived from a simple python script by Dimitri Fontaine
301

    
302
=head1 SEE ALSO
303

    
304
See further info on stats and pools on the pgbouncer homepage:
305
  http://pgbouncer.projects.postgresql.org/doc/usage.html#_show_commands
306

    
307
=head1 VERSION
308

    
309
1.0
310

    
311
=head1 AUTHOR
312

    
313
Clemens Schwaighofer <gullevek@gullevek.org>
314

    
315
=head1 LICENSE
316

    
317
GPLv2
318

    
319

    
320
=cut