2 * CTDB Performance Metrics Domain Agent (PMDA) for Performance Co-Pilot (PCP)
4 * Copyright (c) 1995,2004 Silicon Graphics, Inc. All Rights Reserved.
5 * Copyright (c) 2011 David Disseldorp
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "system/network.h"
28 #include "lib/util/time.h"
30 #include "ctdb_private.h"
31 #include "ctdb_client.h"
32 #include "ctdb_protocol.h"
34 #include "common/system.h"
35 #include "common/common.h"
37 #include <pcp/pmapi.h>
46 * This PMDA connects to the locally running ctdbd daemon and pulls
47 * statistics for export via PCP. The ctdbd Unix domain socket path can be
48 * specified with the CTDB_SOCKET environment variable, otherwise the default
53 * All metrics supported in this PMDA - one table entry for each.
54 * The 4th field specifies the serial number of the instance domain
55 * for the metric, and must be either PM_INDOM_NULL (denoting a
56 * metric that only ever has a single value), or the serial number
57 * of one of the instance domains declared in the instance domain table
58 * (i.e. in indomtab, above).
60 static pmdaMetric metrictab[] = {
62 { NULL, { PMDA_PMID(0,0), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
63 PMDA_PMUNITS(0,0,0,0,0,0) }, },
65 { NULL, { PMDA_PMID(0,1), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
66 PMDA_PMUNITS(0,0,0,0,0,0) }, },
68 { NULL, { PMDA_PMID(0,2), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
69 PMDA_PMUNITS(0,0,0,0,0,0) }, },
70 /* client_packets_sent */
71 { NULL, { PMDA_PMID(0,3), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
72 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
73 /* client_packets_recv */
74 { NULL, { PMDA_PMID(0,4), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
75 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
76 /* node_packets_sent */
77 { NULL, { PMDA_PMID(0,5), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
78 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
79 /* node_packets_recv */
80 { NULL, { PMDA_PMID(0,6), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
81 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
82 /* keepalive_packets_sent */
83 { NULL, { PMDA_PMID(0,7), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
84 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
85 /* keepalive_packets_recv */
86 { NULL, { PMDA_PMID(0,8), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
87 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
89 { NULL, { PMDA_PMID(1,0), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
90 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
92 { NULL, { PMDA_PMID(1,1), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
93 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
95 { NULL, { PMDA_PMID(1,2), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
96 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
98 { NULL, { PMDA_PMID(1,3), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
99 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
101 { NULL, { PMDA_PMID(1,4), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
102 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
104 { NULL, { PMDA_PMID(1,5), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
105 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
107 { NULL, { PMDA_PMID(1,6), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
108 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
110 { NULL, { PMDA_PMID(1,7), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
111 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
113 { NULL, { PMDA_PMID(2,0), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
114 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
116 { NULL, { PMDA_PMID(2,1), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
117 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
119 { NULL, { PMDA_PMID(2,2), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
120 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
122 { NULL, { PMDA_PMID(3,0), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
123 PMDA_PMUNITS(0,0,1,0,0,0) }, },
125 { NULL, { PMDA_PMID(3,1), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
126 PMDA_PMUNITS(0,0,1,0,0,0) }, },
128 { NULL, { PMDA_PMID(3,2), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
129 PMDA_PMUNITS(0,0,1,0,0,0) }, },
131 { NULL, { PMDA_PMID(0,9), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
132 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
134 { NULL, { PMDA_PMID(0,10), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
135 PMDA_PMUNITS(0,0,0,0,0,0) }, },
136 /* locks.num_calls */
137 { NULL, { PMDA_PMID(0,11), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
138 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
139 /* locks.num_pending */
140 { NULL, { PMDA_PMID(0,12), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
141 PMDA_PMUNITS(0,0,0,0,0,0) }, },
142 /* childwrite_calls */
143 { NULL, { PMDA_PMID(0,13), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_COUNTER,
144 PMDA_PMUNITS(0,0,1,0,0,PM_COUNT_ONE) }, },
145 /* pending_childwrite_calls */
146 { NULL, { PMDA_PMID(0,14), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
147 PMDA_PMUNITS(0,0,0,0,0,0) }, },
149 { NULL, { PMDA_PMID(0,15), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
150 PMDA_PMUNITS(1,0,0,PM_SPACE_BYTE,0,0) }, },
152 { NULL, { PMDA_PMID(0,16), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
153 PMDA_PMUNITS(0,0,0,0,0,0) }, },
154 /* reclock.ctdbd.max */
155 { NULL, { PMDA_PMID(0,17), PM_TYPE_DOUBLE, PM_INDOM_NULL, PM_SEM_INSTANT,
156 PMDA_PMUNITS(0,1,0,0,PM_TIME_SEC,0) }, },
157 /* reclock.recd.max */
158 { NULL, { PMDA_PMID(0,18), PM_TYPE_DOUBLE, PM_INDOM_NULL, PM_SEM_INSTANT,
159 PMDA_PMUNITS(0,1,0,0,PM_TIME_SEC,0) }, },
160 /* call_latency.max */
161 { NULL, { PMDA_PMID(0,19), PM_TYPE_DOUBLE, PM_INDOM_NULL, PM_SEM_INSTANT,
162 PMDA_PMUNITS(0,1,0,0,PM_TIME_SEC,0) }, },
163 /* locks.latency.max */
164 { NULL, { PMDA_PMID(0,20), PM_TYPE_DOUBLE, PM_INDOM_NULL, PM_SEM_INSTANT,
165 PMDA_PMUNITS(0,1,0,0,PM_TIME_SEC,0) }, },
166 /* childwrite_latency.max */
167 { NULL, { PMDA_PMID(0,21), PM_TYPE_DOUBLE, PM_INDOM_NULL, PM_SEM_INSTANT,
168 PMDA_PMUNITS(0,1,0,0,PM_TIME_SEC,0) }, },
170 { NULL, { PMDA_PMID(0,22), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
171 PMDA_PMUNITS(0,0,0,0,0,0) }, },
174 static struct tevent_context *ev;
175 static struct ctdb_context *ctdb;
176 static struct ctdb_statistics *stats;
179 pmda_ctdb_q_read_cb(uint8_t *data, size_t cnt, void *args)
182 fprintf(stderr, "ctdbd unreachable\n");
183 /* cleanup on request timeout */
187 ctdb_client_read_cb(data, cnt, args);
192 pmda_ctdb_daemon_connect(void)
194 const char *socket_name;
196 struct sockaddr_un addr;
198 ev = tevent_context_init(NULL);
200 fprintf(stderr, "Failed to init event ctx\n");
204 ctdb = ctdb_init(ev);
206 fprintf(stderr, "Failed to init ctdb\n");
210 socket_name = getenv("CTDB_SOCKET");
211 if (socket_name == NULL) {
212 socket_name = CTDB_SOCKET;
215 ret = ctdb_set_socketname(ctdb, socket_name);
217 fprintf(stderr, "ctdb_set_socketname failed - %s\n",
223 * ctdb_socket_connect() sets a default queue callback handler that
224 * calls exit() if ctdbd is unavailable on recv, use our own wrapper to
228 memset(&addr, 0, sizeof(addr));
229 addr.sun_family = AF_UNIX;
230 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
232 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
233 if (ctdb->daemon.sd == -1) {
234 fprintf(stderr, "Failed to open client socket\n");
238 set_nonblocking(ctdb->daemon.sd);
239 set_close_on_exec(ctdb->daemon.sd);
241 if (connect(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
242 fprintf(stderr, "Failed to connect to ctdb daemon via %s\n",
247 ctdb->daemon.queue = ctdb_queue_setup(ctdb, ctdb, ctdb->daemon.sd,
249 pmda_ctdb_q_read_cb, ctdb,
251 if (ctdb->daemon.queue == NULL) {
252 fprintf(stderr, "Failed to setup queue\n");
256 ctdb->pnn = ctdb_ctrl_getpnn(ctdb, timeval_current_ofs(3, 0),
258 if (ctdb->pnn == (uint32_t)-1) {
259 fprintf(stderr, "Failed to get ctdb pnn\n");
265 close(ctdb->daemon.sd);
275 pmda_ctdb_daemon_disconnect(void)
278 ctdb->methods->shutdown(ctdb);
281 if (ctdb->daemon.sd != -1) {
282 close(ctdb->daemon.sd);
291 fill_base(unsigned int item, pmAtomValue *atom)
295 atom->ul = stats->num_clients;
298 atom->ul = stats->frozen;
301 atom->ul = stats->recovering;
304 atom->ul = stats->client_packets_sent;
307 atom->ul = stats->client_packets_recv;
310 atom->ul = stats->node_packets_sent;
313 atom->ul = stats->node_packets_recv;
316 atom->ul = stats->keepalive_packets_sent;
319 atom->ul = stats->keepalive_packets_recv;
322 atom->ul = stats->total_calls;
325 atom->ul = stats->pending_calls;
328 atom->ul = stats->locks.num_calls;
331 atom->ul = stats->locks.num_pending;
334 atom->ul = stats->childwrite_calls;
337 atom->ul = stats->pending_childwrite_calls;
340 atom->ul = stats->memory_used;
343 atom->ul = stats->max_hop_count;
346 atom->d = stats->reclock.ctdbd.max;
349 atom->d = stats->reclock.recd.max;
352 atom->d = stats->call_latency.max;
355 atom->d = stats->locks.latency.max;
358 atom->d = stats->childwrite_latency.max;
361 atom->d = stats->num_recoveries;
371 fill_node(unsigned int item, pmAtomValue *atom)
375 atom->ul = stats->node.req_call;
378 atom->ul = stats->node.reply_call;
381 atom->ul = stats->node.req_dmaster;
384 atom->ul = stats->node.reply_dmaster;
387 atom->ul = stats->node.reply_error;
390 atom->ul = stats->node.req_message;
393 atom->ul = stats->node.req_control;
396 atom->ul = stats->node.reply_control;
407 fill_client(unsigned int item, pmAtomValue *atom)
411 atom->ul = stats->client.req_call;
414 atom->ul = stats->client.req_message;
417 atom->ul = stats->client.req_control;
427 fill_timeout(unsigned int item, pmAtomValue *atom)
431 atom->ul = stats->timeouts.call;
434 atom->ul = stats->timeouts.control;
437 atom->ul = stats->timeouts.traverse;
447 * callback provided to pmdaFetch
450 pmda_ctdb_fetch_cb(pmdaMetric *mdesc, unsigned int inst, pmAtomValue *atom)
453 __pmID_int *id = (__pmID_int *)&(mdesc->m_desc.pmid);
455 if (inst != PM_IN_NULL) {
460 fprintf(stderr, "stats not available\n");
466 switch (id->cluster) {
468 ret = fill_base(id->item, atom);
474 ret = fill_node(id->item, atom);
480 ret = fill_client(id->item, atom);
486 ret = fill_timeout(id->item, atom);
501 * This routine is called once for each pmFetch(3) operation, so is a
502 * good place to do once-per-fetch functions, such as value caching or
503 * instance domain evaluation.
506 pmda_ctdb_fetch(int numpmid, pmID pmidlist[], pmResult **resp, pmdaExt *pmda)
511 struct timeval ctdb_timeout;
514 fprintf(stderr, "attempting reconnect to ctdbd\n");
515 ret = pmda_ctdb_daemon_connect();
517 fprintf(stderr, "reconnect failed\n");
522 ctdb_timeout = timeval_current_ofs(1, 0);
523 ret = ctdb_control(ctdb, ctdb->pnn, 0,
524 CTDB_CONTROL_STATISTICS, 0, tdb_null,
525 ctdb, &data, &res, &ctdb_timeout, NULL);
527 if (ret != 0 || res != 0) {
528 fprintf(stderr, "ctdb control for statistics failed, reconnecting\n");
529 pmda_ctdb_daemon_disconnect();
534 stats = (struct ctdb_statistics *)data.dptr;
536 if (data.dsize != sizeof(struct ctdb_statistics)) {
537 fprintf(stderr, "incorrect statistics size %zu - not %zu\n",
538 data.dsize, sizeof(struct ctdb_statistics));
543 ret = pmdaFetch(numpmid, pmidlist, resp, pmda);
551 void pmda_ctdb_init(pmdaInterface *dp);
554 * Initialise the agent
557 pmda_ctdb_init(pmdaInterface *dp)
559 if (dp->status != 0) {
563 dp->version.two.fetch = pmda_ctdb_fetch;
564 pmdaSetFetchCallBack(dp, pmda_ctdb_fetch_cb);
566 pmdaInit(dp, NULL, 0, metrictab,
567 (sizeof(metrictab) / sizeof(metrictab[0])));
573 static char buf[MAXPATHLEN];
576 snprintf(buf, sizeof(buf), "%s/ctdb/help",
577 pmGetConfig("PCP_PMDAS_DIR"));
585 fprintf(stderr, "Usage: %s [options]\n\n", pmProgname);
587 " -d domain use domain (numeric) for metrics domain of PMDA\n"
588 " -l logfile write log into logfile rather than using default log name\n"
589 "\nExactly one of the following options may appear:\n"
590 " -i port expect PMCD to connect on given inet port (number or name)\n"
591 " -p expect PMCD to supply stdin/stdout (pipe)\n"
592 " -u socket expect PMCD to connect on given unix domain socket\n",
598 * Set up the agent if running as a daemon.
601 main(int argc, char **argv)
604 char log_file[] = "pmda_ctdb.log";
605 pmdaInterface dispatch;
607 __pmSetProgname(argv[0]);
609 pmdaDaemon(&dispatch, PMDA_INTERFACE_2, pmProgname, CTDB,
610 log_file, helpfile());
612 if (pmdaGetOpt(argc, argv, "d:i:l:pu:?", &dispatch, &err) != EOF) {
620 pmdaOpenLog(&dispatch);
621 pmda_ctdb_init(&dispatch);
622 pmdaConnect(&dispatch);