summaryrefslogtreecommitdiff
path: root/contrib/pgxc_ctl/monitor.c
blob: f14c24bb4773ea59fffe69cf378dda46180d1f5a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*-------------------------------------------------------------------------
 *
 * monitor.c
 *
 *    Monitoring module of Postgres-XC configuration and operation tool.
 *
 * Portions Copyright (c) 2013 Postgres-XC Development Group
 *
 *-------------------------------------------------------------------------
 */
/*
 * This module is imported from /contrib/pgxc_monitor, to provide monitoring
 * feature of each pgstgres-xc components.
 */
#include "gtm/gtm_client.h"
#include "gtm/libpq-fe.h"

#include <stdlib.h>
#include <getopt.h>
#include "utils.h"
#include "variables.h"
/* This is an ugly hack to avoid conflict between gtm_c.h and pgxc_ctl.h */
#undef true
#undef false
#include "pgxc_ctl_log.h"
#include "varnames.h"
#include "config.h"
#include "monitor.h"

/* Define all the node types */
typedef enum
{
	NONE = 0,
	GTM,	/* GTM or GTM-proxy */
	NODE	/* Coordinator or Datanode */
} nodetype_t;

#define GetToken() (line = get_word(line, &token))
#define testToken(word) ((token != NULL) && (strcmp(token, word) == 0))
#define TestToken(word) ((token != NULL) && (strcasecmp(token, word) == 0))

static void printResult(int res, char *what, char *name)
{
	if (res == 0)
	{
		if (name)
			elog(NOTICE, "Running: %s %s\n", what, name);
		else
			elog(NOTICE, "Running: %s\n", what);
	}
	else
	{
		if (name)
			elog(NOTICE, "Not running: %s %s\n", what, name);
		else
			elog(NOTICE, "Not running: %s\n", what);
	}
}

static void monitor_gtm_master(void)
{
	if (doesExist(VAR_gtmMasterServer, 0) && doesExist(VAR_gtmMasterPort, 0))
		return(printResult(do_gtm_ping(sval(VAR_gtmMasterServer), atoi(sval(VAR_gtmMasterPort))), "gtm master", NULL));
	else
		elog(NOTICE, "GTM master not running\n");
}

static void monitor_gtm_slave(void)
{
	if (doesExist(VAR_gtmSlaveServer, 0) && doesExist(VAR_gtmSlavePort, 0))
		return(printResult(do_gtm_ping(sval(VAR_gtmSlaveServer), atoi(sval(VAR_gtmSlavePort))), "gtm slave", NULL));
}

static void monitor_gtm_proxy(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = gtmProxyIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a gtm proxy.\n", actualNodeList[ii]);
			continue;
		}
		printResult(do_gtm_ping(aval(VAR_gtmProxyServers)[idx], atoi(aval(VAR_gtmProxyPorts)[idx])),
					"gtm proxy", actualNodeList[ii]);
	}
}
	
	
static void monitor_coordinator_master(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = coordIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a coordinator\n", actualNodeList[ii]);
			continue;
		}
		printResult(pingNode(aval(VAR_coordMasterServers)[idx], aval(VAR_coordPorts)[idx]), 
					"coordinator master", actualNodeList[ii]);
	}
}

static void monitor_coordinator_slave(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	if (!isVarYes(VAR_coordSlave))
	{
		elog(ERROR, "ERROR: coordinator slave is not configured.\n");
		return;
	}
	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = coordIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a coordinator\n", actualNodeList[ii]);
			continue;
		}
		/* Need to check again if the slave is configured */
		if (!doesExist(VAR_coordSlaveServers, idx) || is_none(aval(VAR_coordSlaveServers)[idx]))
			elog(ERROR, "ERROR: coordinator slave %s is not configured\n", actualNodeList[ii]);
		else
			printResult(pingNode(aval(VAR_coordSlaveServers)[idx], aval(VAR_coordSlavePorts)[idx]), 
						"coordinator slave", actualNodeList[ii]);
	}
}

static void monitor_coordinator(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = coordIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a coordinator\n", actualNodeList[ii]);
			continue;
		}
		printResult(pingNode(aval(VAR_coordMasterServers)[idx], aval(VAR_coordPorts)[idx]), 
					"coordinator master", actualNodeList[ii]);
		if (doesExist(VAR_coordSlaveServers, idx) && !is_none(aval(VAR_coordSlaveServers)[idx]))
			printResult(pingNodeSlave(aval(VAR_coordSlaveServers)[idx],
						aval(VAR_coordSlaveDirs)[idx]),
						"coordinator slave", actualNodeList[ii]);
	}
}
static void monitor_datanode_master(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = datanodeIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a datanode\n", actualNodeList[ii]);
			continue;
		}
		printResult(pingNode(aval(VAR_datanodeMasterServers)[idx], aval(VAR_datanodePorts)[idx]), 
					"datanode master", actualNodeList[ii]);
	}
}

static void monitor_datanode_slave(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	if (!isVarYes(VAR_datanodeSlave))
	{
		elog(ERROR, "ERROR: datanode slave is not configured.\n");
		return;
	}
	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = datanodeIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a datanode\n", actualNodeList[ii]);
			continue;
		}
		if (doesExist(VAR_datanodeSlaveServers, idx) && !is_none(aval(VAR_datanodeSlaveServers)[idx]))
			printResult(pingNodeSlave(aval(VAR_datanodeSlaveServers)[idx],
						aval(VAR_datanodeSlaveDirs)[idx]), 
						"datanode slave", actualNodeList[ii]);
		else
			elog(ERROR, "ERROR: datanode slave %s is not configured.\n", actualNodeList[ii]);
	}
}

static void monitor_datanode(char **nodeList)
{
	char **actualNodeList;
	int ii;
	int idx;

	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((idx = datanodeIdx(actualNodeList[ii])) < 0)
		{
			elog(ERROR, "ERROR: %s is not a datanode\n", actualNodeList[ii]);
			continue;
		}
		printResult(pingNode(aval(VAR_datanodeMasterServers)[idx], aval(VAR_datanodePorts)[idx]), 
					"datanode master", actualNodeList[ii]);
		if (doesExist(VAR_datanodeSlaveServers, idx) && !is_none(aval(VAR_datanodeSlaveServers)[idx]))
			printResult(pingNodeSlave(aval(VAR_datanodeSlaveServers)[idx],
						aval(VAR_datanodeSlaveDirs)[idx]),
						"datanode slave", actualNodeList[ii]);
	}
}

static void monitor_something(char **nodeList)
{
	char **actualNodeList;
	int ii;
	char *wkNodeList[2];
	NodeType type;

	wkNodeList[1] = NULL;
	actualNodeList = makeActualNodeList(nodeList);
	for (ii = 0; actualNodeList[ii]; ii++)
	{
		if ((type = getNodeType(actualNodeList[ii])) == NodeType_GTM)
		{
			monitor_gtm_master();
			if (isVarYes(VAR_gtmSlave))
				monitor_gtm_slave();
			continue;
		}
		else if (type == NodeType_GTM_PROXY)
		{
			wkNodeList[0] = actualNodeList[ii];
			monitor_gtm_proxy(wkNodeList);
			continue;
		}
		else if (type == NodeType_COORDINATOR)
		{
			wkNodeList[0] = actualNodeList[ii];
			monitor_coordinator(wkNodeList);
			continue;
		}
		else if (type == NodeType_DATANODE)
		{
			wkNodeList[0] = actualNodeList[ii];
			monitor_datanode(wkNodeList);
			continue;
		}
		else
		{
			elog(ERROR, "ERROR: %s is not found in any node.\n", actualNodeList[ii]);
			continue;
		}
	}
}

	

void do_monitor_command(char *line)
{
	char *token;
	int rc = 0;

	if (!GetToken())
	{
		elog(ERROR, "ERROR: no monitor command options found.\n");
		return;
	}
	if (TestToken("gtm"))
	{
		if (!GetToken() || TestToken("all"))
		{
			/* Ping GTM */
			monitor_gtm_master();
			if (isVarYes(VAR_gtmSlave))
				monitor_gtm_slave();
		}
		else if (TestToken("master"))
			monitor_gtm_master();
		else if (TestToken("slave"))
		{
			if (isVarYes(VAR_gtmSlave))
				monitor_gtm_slave();
			else
				elog(ERROR, "ERROR: gtm slave is not configured.\n"), rc=-1;
		}
		else
			elog(ERROR, "Invalid monitor gtm command option.\n"), rc=-1;
		return;
	}
	else if (TestToken("gtm_proxy"))
	{
		if (!GetToken() || TestToken("all"))
			monitor_gtm_proxy(aval(VAR_gtmProxyNames));
		else
		{
			char **nodeList = NULL;
			do
				AddMember(nodeList, token);
			while (GetToken());
			monitor_gtm_proxy(nodeList);
			CleanArray(nodeList);
		}
		return;
	}
	else if (TestToken("coordinator"))
	{
		if (!GetToken() || TestToken("all"))
		{
			monitor_coordinator_master(aval(VAR_coordNames));
			if (isVarYes(VAR_coordSlave))
				monitor_coordinator_slave(aval(VAR_coordNames));
			return;
		}
		else if (TestToken("master"))
		{
			if (!GetToken() || TestToken("all"))
				monitor_coordinator_master(aval(VAR_coordNames));
			else
			{
				char **nodeList = NULL;
				do
					AddMember(nodeList, token);
				while (GetToken());
				monitor_coordinator_master(nodeList);
				CleanArray(nodeList);
			}
		}
		else if (TestToken("slave"))
		{
			if (!isVarYes(VAR_coordSlave))
				elog(ERROR, "ERROR: coordinator slave is not configured.\n"), rc = -1;
			else
				if (!GetToken() || TestToken("all"))
					monitor_coordinator_slave(aval(VAR_coordNames));
				else
				{
					char **nodeList = NULL;
					do
						AddMember(nodeList, token);
					while (GetToken());
					monitor_coordinator_slave(nodeList);
					CleanArray(nodeList);
				}
		}
		else
		{
			char **nodeList= NULL;
			do
				AddMember(nodeList, token);
			while(GetToken());
			monitor_coordinator(nodeList);
			CleanArray(nodeList);
		}
	}
	else if (TestToken("datanode"))
	{
		if (!GetToken() || TestToken("all"))
		{
			monitor_datanode_master(aval(VAR_datanodeNames));
			if (isVarYes(VAR_coordSlave))
				monitor_datanode_slave(aval(VAR_datanodeNames));
		}
		else if (TestToken("master"))
		{
			if (!GetToken() || TestToken("all"))
				monitor_datanode_master(aval(VAR_datanodeNames));
			else
			{
				char **nodeList = NULL;
				do
					AddMember(nodeList, token);
				while (GetToken());
				monitor_datanode_master(nodeList);
				CleanArray(nodeList);
			}
		}
		else if (TestToken("slave"))
		{
			if (!isVarYes(VAR_coordSlave))
				elog(ERROR, "ERROR: datanode slave is not configured.\n"), rc = -1;
			else
				if (!GetToken() || TestToken("all"))
					monitor_datanode_slave(aval(VAR_coordNames));
				else
				{
					char **nodeList = NULL;
					do
						AddMember(nodeList, token);
					while (GetToken());
					monitor_datanode_slave(nodeList);
					CleanArray(nodeList);
				}
		}
		else
		{
			char **nodeList= NULL;
			do
				AddMember(nodeList, token);
			while(GetToken());
			monitor_datanode(nodeList);
			CleanArray(nodeList);
		}
	}
	else if (TestToken("all"))
	{
		monitor_gtm_master();
		if (isVarYes(VAR_gtmSlave))
			monitor_gtm_slave();
		if (isVarYes(VAR_gtmProxy))
			monitor_gtm_proxy(aval(VAR_gtmProxyNames));
		monitor_coordinator(aval(VAR_coordNames));
		monitor_datanode(aval(VAR_datanodeNames));
	}
	else
	{
		char **nodeList = NULL;
		do
			AddMember(nodeList, token);
		while (GetToken());
		monitor_something(nodeList);
		CleanArray(nodeList);
	}
	return;
}

/*
 * Ping a given GTM or GTM-proxy
 */
int
do_gtm_ping(char *host, int port)
{
	char connect_str[MAXPATH+1];
	GTM_Conn *conn;

	if (host == NULL)
	{
		elog(ERROR, "ERROR: no hostname is specified.\n");
		return -1;
	}
	if (port <= 0)
	{
		elog(ERROR, "ERROR: Invalid port number, %d.\n", port);
		return -1;
	}
	/* Use 60s as connection timeout */
	sprintf(connect_str, "host=%s port=%d node_name=%s remote_type=%d postmaster=0 connect_timeout=60",
			host, port, myName, GTM_NODE_COORDINATOR);
	if ((conn = PQconnectGTM(connect_str)) == NULL)
	{
		elog(DEBUG3, "DEBUG3: Could not connect to %s, %d\n", host, port);
		return -1;
	}
	GTMPQfinish(conn);
	return 0;
}