summaryrefslogtreecommitdiff
path: root/src/pmdas/dbping/pmdadbping.pl
blob: ec8f2a0547d47f335e0404b60d55d9da59c83fdc (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#
# Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
# Copyright (c) 2008 Aconex.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# 

use strict;
use warnings;
use PCP::PMDA;
use vars qw( $pmda $stamp $response $status $timestamp );

my $delay = $ARGV[0];	# delay in seconds between database ping's
$delay = 60 unless defined($delay);
my $dbprobe = pmda_config('PCP_PMDAS_DIR') . '/dbping/dbprobe.pl';
$dbprobe = "perl " . $dbprobe . " $delay";
my ( $stamp, $response, $status, $timestamp ) = ( 0, 0, 1, 0 );

sub dbping_probe_callback
{
    ( $_ ) = @_;
    ($stamp, $response) = split(/\t/);

    # $pmda->log("dbping_probe_callback: time=$stamp resp=$response\n");

    if (defined($stamp) && defined($response)) {
	$timestamp = $stamp;
	$status = 0;
    } else {
	$response = -1;
	$status = 1;	# bad result, keep old $timestamp
    }
}

sub dbping_fetch_callback	# must return array of value,status
{
    my ($cluster, $item, $inst) = @_;

    # $pmda->log("dbping_fetch_callback $cluster:$item ($inst)\n");

    return (PM_ERR_INST, 0) unless ($inst == PM_IN_NULL);

    if ($cluster == 0) {
	if ($item == 0)		{ return ($response, 1); }
	elsif ($item == 1)	{ return ($status, 1); }
    }
    elsif ($cluster == 1) {
	if ($item == 0)		{ return ($timestamp, 1); }
	elsif ($item == 1)	{ return ($delay, 1); }
    }
    return (PM_ERR_PMID, 0);
}

sub dbping_store_callback	# must return a single value (scalar context)
{
    my ($cluster, $item, $inst, $val) = @_;
    my $sts = 0;

    # $pmda->log("dbping_store_callback $cluster:$item ($inst) $val\n");

    if ($cluster == 1 && $item == 1) {
	$delay = $val;
	return 0;
    }
    elsif ( ($cluster == 0 && ($item == 0 || $item == 1))
	|| ($cluster == 1 && $item == 0) ) {
	return PM_ERR_PERMISSION;
    }
    return PM_ERR_PMID;
}

$pmda = PCP::PMDA->new('dbping', 244);

$pmda->add_metric(pmda_pmid(0,0), PM_TYPE_DOUBLE, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,1,0,0,PM_TIME_SEC,0),
		  'dbping.response_time',
		  'Length of time taken to access the database', '');
$pmda->add_metric(pmda_pmid(0,1), PM_TYPE_32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,0,0,0,0),
		  'dbping.status',
		  'Success state of last attempt to ping the database', '');
$pmda->add_metric(pmda_pmid(1,0), PM_TYPE_STRING, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,0,0,0,0,0),
		  'dbping.control.timestamp',
		  'Time of last successful database ping', '');
$pmda->add_metric(pmda_pmid(1,1), PM_TYPE_U32, PM_INDOM_NULL,
		  PM_SEM_INSTANT, pmda_units(0,1,0,0,PM_TIME_SEC,0),
		  'dbping.control.delay',
		  'Time to sleep between database ping attempts', '');
$pmda->set_fetch_callback( \&dbping_fetch_callback );
$pmda->set_store_callback( \&dbping_store_callback );
$pmda->add_pipe( $dbprobe, \&dbping_probe_callback, 0 );
$pmda->set_user('pcp');
$pmda->run;

=pod

=head1 NAME

pmdadbping - database response time and availability PMDA

=head1 DESCRIPTION

Simple database response time measurement PMDA.  dbprobe.pl(1)
should be configured to use the type of DBI appropriate
for your database, which includes: RDBMS flavour, user/password,
delay between "ping" requests, and the SQL statement to use.
B<pmdadbping> runs dbprobe.pl(1), and exports the performance
measurements it makes available as PCP metrics.

=head1 INSTALLATION

Configure dbprobe.pl(1) - it uses a configuration file from
(in this order):

=over

=item * /etc/pcpdbi.conf

=item * $PCP_PMDAS_DIR/dbping/dbprobe.conf

=back

This file can contain overridden values (Perl code) for the settings
listed at the start of dbprobe.pl, namely:

=over

=item * database name (see DBI(3) for details)

=item * database user name

=item * database pass word

=item * SQL statement to measure (probe)

=item * delay between probes

=back

Once this is setup, you can access the names and values for the
dbping performance metrics by doing the following as root:

	# cd $PCP_PMDAS_DIR/dbping
	# ./Install

If you want to undo the installation, do the following as root:

	# cd $PCP_PMDAS_DIR/dbping
	# ./Remove

B<pmdadbping> is launched by pmcd(1) and should never be executed
directly.  The Install and Remove scripts notify pmcd(1) when
the agent is installed or removed.

=head1 FILES

=over

=item $PCP_PMDAS_DIR/dbping/probes.stp

probe configuration file for stap(1), run by B<pmdadbping>

=item $PCP_PMDAS_DIR/dbping/Install

installation script for the B<pmdadbping> agent

=item $PCP_PMDAS_DIR/dbping/Remove

undo installation script for the B<pmdadbping> agent

=item $PCP_LOG_DIR/pmcd/dbping.log

default log file for error messages from B<pmdadbping>

=back

=head1 SEE ALSO

pmcd(1), dbprobe.pl(1) and DBI(3).