File: locator.c

package info (click to toggle)
xymon 4.3.30-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,288 kB
  • sloc: ansic: 69,112; sh: 3,595; makefile: 857; javascript: 452; perl: 48
file content (526 lines) | stat: -rw-r--r-- 12,648 bytes parent folder | download | duplicates (2)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*----------------------------------------------------------------------------*/
/* Xymon monitor library.                                                     */
/*                                                                            */
/* This is a library module, part of libxymon.                                */
/* It contains routines for communicating with the Xymon locator service.     */
/*                                                                            */
/* Copyright (C) 2006-2011 Henrik Storner <henrik@storner.dk>                 */
/*                                                                            */
/* This program is released under the GNU General Public License (GPL),       */
/* version 2. See the file "COPYING" for details.                             */
/*                                                                            */
/*----------------------------------------------------------------------------*/

static char rcsid[] = "$Id: locator.c 8069 2019-07-23 15:29:06Z jccleaver $";

#include <sys/time.h>
#include <sys/types.h>

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include "libxymon.h"

#include <signal.h>

const char *servicetype_names[] = { "rrd", "client", "alert", "history", "hostdata" };

static struct sockaddr_in myaddr;
static socklen_t myaddrsz = 0;
static int locatorsocket = -1;

#define DEFAULT_CACHETIMEOUT (15*60) 	/* 15 minutes */
static void * locatorcache[ST_MAX];
static int havecache[ST_MAX] = {0,};
static int cachetimeout[ST_MAX] = {0,};

typedef struct cacheitm_t {
	char *key, *resp;
	time_t tstamp;
} cacheitm_t;


enum locator_servicetype_t get_servicetype(char *typestr)
{
	enum locator_servicetype_t res = 0;

	while ((res < ST_MAX) && (strcmp(typestr, servicetype_names[res]))) res++;

	return res;
}

static int call_locator(char *buf, size_t bufsz)
{
	int n, bytesleft;
	fd_set fds;
	struct timeval tmo;
	char *bufp;

	/* First, send the request */
	bufp = buf;
	bytesleft = strlen(buf)+1;
	do {
		FD_ZERO(&fds);
		FD_SET(locatorsocket, &fds);
		tmo.tv_sec = 5;
		tmo.tv_usec = 0;
		n = select(locatorsocket+1, NULL, &fds, NULL, &tmo);

		if (n == 0) {
			errprintf("Send to locator failed: Timeout\n");
			return -1;
		}
		else if (n == -1) {
			errprintf("Send to locator failed: %s\n", strerror(errno));
			return -1;
		}

		n = send(locatorsocket, bufp, bytesleft, 0);
		if (n == -1) {
			errprintf("Send to locator failed: %s\n", strerror(errno));
			return -1;
		}
		else {
			bytesleft -= n;
			bufp += n;
		}
	} while (bytesleft > 0);

	/* Then read the response */
	FD_ZERO(&fds);
	FD_SET(locatorsocket, &fds);
	tmo.tv_sec = 5;
	tmo.tv_usec = 0;
	n = select(locatorsocket+1, &fds, NULL, NULL, &tmo);

	if (n > 0) {
		n = recv(locatorsocket, buf, bufsz-1, 0);

		if (n == -1) {
			errprintf("call_locator() recv() failed: %s\n", strerror(errno));
			return -1;
		}
		buf[n] = '\0';
	}
	else {
		errprintf("call_locator() comm failure: %s\n",
			  (n == 0) ? "Timeout" : strerror(errno));
		return -1;
	}

	return 0;
}

static char *locator_querycache(enum locator_servicetype_t svc, char *key)
{
	xtreePos_t handle;
	cacheitm_t *itm;

	if (!havecache[svc]) return NULL;

	handle = xtreeFind(locatorcache[svc], key);
	if (handle == xtreeEnd(locatorcache[svc])) return NULL;

	itm = (cacheitm_t *)xtreeData(locatorcache[svc], handle);
	return (itm->tstamp + cachetimeout[svc]) > getcurrenttime(NULL) ? itm->resp : NULL;
}


static void locator_updatecache(enum locator_servicetype_t svc, char *key, char *resp)
{
	xtreePos_t handle;
	cacheitm_t *newitm;

	if (!havecache[svc]) return;

	handle = xtreeFind(locatorcache[svc], key);
	if (handle == xtreeEnd(locatorcache[svc])) {
		newitm = (cacheitm_t *)calloc(1, sizeof(cacheitm_t));
		newitm->key = strdup(key);
		newitm->resp = strdup(resp);
		if (xtreeAdd(locatorcache[svc], newitm->key, newitm) != XTREE_STATUS_OK) {
			xfree(newitm->key);
			xfree(newitm->resp);
			xfree(newitm);
		}
	}
	else {
		newitm = (cacheitm_t *)xtreeData(locatorcache[svc], handle);
		if (newitm->resp) xfree(newitm->resp);
		newitm->resp = strdup(resp);
		newitm->tstamp = getcurrenttime(NULL);
	}
}


void locator_flushcache(enum locator_servicetype_t svc, char *key)
{
	xtreePos_t handle;

	if (!havecache[svc]) return;

	if (key) {
		handle = xtreeFind(locatorcache[svc], key);
		if (handle != xtreeEnd(locatorcache[svc])) {
			cacheitm_t *itm = (cacheitm_t *)xtreeData(locatorcache[svc], handle);
			itm->tstamp = 0;
		}
	}
	else {
		for (handle = xtreeFirst(locatorcache[svc]); (handle != xtreeEnd(locatorcache[svc])); handle = xtreeNext(locatorcache[svc], handle)) {
			cacheitm_t *itm = (cacheitm_t *)xtreeData(locatorcache[svc], handle);
			itm->tstamp = 0;
		}
	}
}


void locator_prepcache(enum locator_servicetype_t svc, int timeout)
{
	if (!havecache[svc]) {
		locatorcache[svc] = xtreeNew(strcasecmp);
		havecache[svc] = 1;
	}
	else {
		locator_flushcache(svc, NULL);
	}

	cachetimeout[svc] = ((timeout>0) ? timeout : DEFAULT_CACHETIMEOUT);
}


char *locator_cmd(char *cmd)
{
	static char pingbuf[512];
	int res;

	strncpy(pingbuf, cmd, sizeof(pingbuf));
	res = call_locator(pingbuf, sizeof(pingbuf));

	return (res == 0) ? pingbuf : NULL;
}

char *locator_ping(void)
{
	return locator_cmd("p");
}

int locator_init(char *ipport)
{
	char *ip, *p;
	int portnum;

	if (locatorsocket >= 0) {
		close(locatorsocket);
		locatorsocket = -1;
	}

	locatorsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (locatorsocket == -1) {
		errprintf("Cannot get socket: %s\n", strerror(errno));
		return -1;
	}

	ip = strdup(ipport);
	p = strchr(ip, ':'); 
	if (p) {
		*p = '\0';
		portnum = atoi(p+1);
	}
	else {
		portnum = atoi(xgetenv("XYMONDPORT"));
	}

	memset(&myaddr, 0, sizeof(myaddr));
	inet_aton(ip, (struct in_addr *) &myaddr.sin_addr.s_addr);
	myaddr.sin_port = htons(portnum);
	myaddr.sin_family = AF_INET;
	myaddrsz = sizeof(myaddr);

	if (connect(locatorsocket, (struct sockaddr *)&myaddr, myaddrsz) == -1) {
		errprintf("Cannot set target address for locator: %s:%d (%s)\n",
			ip, portnum, strerror(errno));
		close(locatorsocket);
		locatorsocket = -1;
		return -1;
	}

	fcntl(locatorsocket, F_SETFL, O_NONBLOCK);

	return (locator_ping() ? 0 : -1);
}


int locator_register_server(char *servername, enum locator_servicetype_t svctype, int weight, enum locator_sticky_t sticky, char *extras)
{
	SBUF_DEFINE(buf);
	int bufsz;
	int res;

	bufsz = strlen(servername) + 100;
	if (extras) bufsz += (strlen(extras) + 1);
	SBUF_MALLOC(buf, bufsz);

	if (sticky == LOC_SINGLESERVER) weight = -1;
	snprintf(buf, buf_buflen, "S|%s|%s|%d|%d|%s", servername, servicetype_names[svctype], weight, 
		((sticky == LOC_STICKY) ? 1 : 0), (extras ? extras : ""));

	res = call_locator(buf, bufsz);

	xfree(buf);
	return res;
}

int locator_register_host(char *hostname, enum locator_servicetype_t svctype, char *servername)
{
	SBUF_DEFINE(buf);
	int bufsz;
	int res;

	bufsz = strlen(servername) + strlen(hostname) + 100;
	SBUF_MALLOC(buf, bufsz);
	snprintf(buf, buf_buflen, "H|%s|%s|%s", hostname, servicetype_names[svctype], servername);

	res = call_locator(buf, bufsz);

	xfree(buf);
	return res;
}

int locator_rename_host(char *oldhostname, char *newhostname, enum locator_servicetype_t svctype)
{
	SBUF_DEFINE(buf);
	int bufsz;
	int res;

	bufsz = strlen(oldhostname) + strlen(newhostname) + 100;
	SBUF_MALLOC(buf, bufsz);
	snprintf(buf, buf_buflen, "M|%s|%s|%s", oldhostname, servicetype_names[svctype], newhostname);

	res = call_locator(buf, bufsz);

	xfree(buf);
	return res;
}

char *locator_query(char *hostname, enum locator_servicetype_t svctype, char **extras)
{
	STATIC_SBUF_DEFINE(buf);
	static int bufsz = 0;
	int res, bufneeded;

	bufneeded = strlen(hostname) + 100;
	if (extras) bufneeded += 1024;
	if (!buf) {
		bufsz = bufneeded;
		SBUF_MALLOC(buf, bufsz);
	}
	else if (bufneeded > bufsz) {
		bufsz = bufneeded;
		SBUF_REALLOC(buf, bufsz);
	}

	if (havecache[svctype] && !extras) {
		char *cachedata = locator_querycache(svctype, hostname);
		if (cachedata) return cachedata;
	}

	snprintf(buf, buf_buflen, "Q|%s|%s", servicetype_names[svctype], hostname);
	if (extras) buf[0] = 'X';
	res = call_locator(buf, bufsz);
	if (res == -1) return NULL;

	switch (*buf) {
	  case '!': /* This host is fixed on an available server */
	  case '*': /* Roaming host */
		if (extras) {
			*extras = strchr(buf+2, '|');
			if (**extras == '|') {
				**extras = '\0';
				(*extras)++;
			}
		}
		if (havecache[svctype] && !extras) locator_updatecache(svctype, hostname, buf+2);
		return ((strlen(buf) > 2) ? buf+2 : NULL);

	  case '?': /* No available server to handle the request */
		locator_flushcache(svctype, hostname);
		return NULL;
	}

	return NULL;
}

int locator_serverdown(char *servername, enum locator_servicetype_t svctype)
{
	SBUF_DEFINE(buf);
	int bufsz;
	int res;

	bufsz = strlen(servername) + 100;
	SBUF_MALLOC(buf, bufsz);
	snprintf(buf, buf_buflen, "D|%s|%s", servername, servicetype_names[svctype]);

	res = call_locator(buf, bufsz);
	locator_flushcache(svctype, NULL);

	xfree(buf);
	return res;
}

int locator_serverup(char *servername, enum locator_servicetype_t svctype)
{
	SBUF_DEFINE(buf);
	int bufsz;
	int res;

	bufsz = strlen(servername) + 100;
	SBUF_MALLOC(buf, bufsz);
	snprintf(buf, buf_buflen, "U|%s|%s", servername, servicetype_names[svctype]);

	res = call_locator(buf, bufsz);

	xfree(buf);
	return res;
}

int locator_serverforget(char *servername, enum locator_servicetype_t svctype)
{
	SBUF_DEFINE(buf);
	int bufsz;
	int res;

	bufsz = strlen(servername) + 100;
	SBUF_MALLOC(buf, bufsz);
	snprintf(buf, buf_buflen, "F|%s|%s", servername, servicetype_names[svctype]);

	res = call_locator(buf, bufsz);
	locator_flushcache(svctype, NULL);

	xfree(buf);
	return res;
}



#ifdef STANDALONE

int main(int argc, char *argv[])
{
	char buf[1024];
	int done = 0;
	char *res;

	if (argc < 2) {
		printf("Usage: %s IP:PORT\n", argv[0]);
		return 1;
	}

	if (locator_init(argv[1]) == -1) {
		printf("Locator ping failed\n");
		return 1;
	}
	else {
		printf("Locator is available\n");
	}

	while (!done) {
		char *p, *p1, *p2, *p3, *p4, *p5, *p6, *p7;
		char *extras;

		printf("Commands:\n");
		printf("  r(egister) s servername type weight sticky\n");
		printf("  r(egister) h servername type hostname\n");
		printf("  d(own)       servername type\n");
		printf("  u(p)         servername type\n");
		printf("  f(orget)     servername type\n");
		printf("  q(uery)      hostname type\n");
		printf("  x(query)     hostname type\n");
		printf("  p(ing)\n");
		printf("  s(ave state)\n");
		printf(">"); fflush(stdout);
		done = (fgets(buf, sizeof(buf), stdin) == NULL); if (done) continue;

		p = strchr(buf, '\n'); if (p) *p = '\0';
		p1 = p2 = p3 = p4 = p5 = p6 = p7 = NULL;

		p1 = strtok(buf, " ");
		if (p1) p2 = strtok(NULL, " ");
		if (p2) p3 = strtok(NULL, " ");
		if (p3) p4 = strtok(NULL, " ");
		if (p4) p5 = strtok(NULL, " ");
		if (p5) p6 = strtok(NULL, " ");
		if (p6) p7 = strtok(NULL, "\r\n");

		switch (*p1) {
		  case 'R': case 'r':
			if (*p2 == 's') {
				enum locator_servicetype_t svc;
				enum locator_sticky_t sticky;
				int weight;

				svc = get_servicetype(p4);
				weight = (p5 ? atoi(p5) : 1);
				sticky = ((p6 && (atoi(p6) == 1)) ? LOC_STICKY : LOC_ROAMING);

				printf("%s\n", locator_register_server(p3, svc, weight, sticky, p7) ? "Failed" : "OK");
			}
			else if (*p2 == 'h') {
				printf("%s\n", locator_register_host(p5, get_servicetype(p4), p3) ? "Failed" : "OK");
			}
			break;

		  case 'D': case 'd':
			printf("%s\n", locator_serverdown(p2, get_servicetype(p3)) ? "Failed" : "OK");
			break;

		  case 'U': case 'u':
			printf("%s\n", locator_serverup(p2, get_servicetype(p3)) ? "Failed" : "OK");
			break;

		  case 'F': case 'f':
			printf("%s\n", locator_serverforget(p2, get_servicetype(p3)) ? "Failed" : "OK");
			break;

		  case 'Q': case 'q':
		  case 'X': case 'x':
			extras = NULL;
			res = locator_query(p2, get_servicetype(p3), (*p1 == 'x') ? &extras : NULL);
			if (res) {
				printf("Result: %s\n", res); 
				if (extras) printf("  Extras gave: %s\n", extras);
			}
			else {
				printf("Failed\n");
			}
			break;

		  case 'P': case 'p':
			p = locator_cmd("p");
			if (p == NULL) printf("Failed\n"); else printf("OK: %s\n", p);
			break;

		  case 'S': case 's':
			p = locator_cmd("@");
			if (p == NULL) printf("Failed\n"); else printf("OK: %s\n", p);
			break;
		}
	}

	return 0;
}

#endif