File: loadhosts_net.c

package info (click to toggle)
xymon 4.3.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,384 kB
  • sloc: ansic: 69,137; sh: 3,601; makefile: 863; javascript: 452; perl: 48
file content (101 lines) | stat: -rw-r--r-- 3,109 bytes parent folder | download | duplicates (3)
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
/*----------------------------------------------------------------------------*/
/* Xymon monitor library.                                                     */
/*                                                                            */
/* This is a library module for Xymon, responsible for loading the host       */
/* configuration from xymond, for either a single host or all hosts.          */
/*                                                                            */
/* Copyright (C) 2011-2011 Henrik Storner <henrik@hswn.dk>                    */
/*                                                                            */
/* This program is released under the GNU General Public License (GPL),       */
/* version 2. See the file "COPYING" for details.                             */
/*                                                                            */
/*----------------------------------------------------------------------------*/

static char rcsid_net[] = "$Id: loadhosts_file.c 6745 2011-09-04 06:01:06Z storner $";

static char *hivalhost = NULL;
static char *hivals[XMH_LAST] = { NULL, };
static char *hivalbuf = NULL;
static namelist_t hival_hostinfo;	/* Used as token for userspace. Also holds raw data in "elems" */

int load_hostinfo(char *targethost)
{
	sendreturn_t *sres;
	sendresult_t sendstat;
	SBUF_DEFINE(msg);
	char *bol, *eoln, *key, *val;
	int elemsize = 0;

	xmh_item_list_setup();

	if (hivalhost) {
		xfree(hivalhost);
		hivalhost = NULL;
	}
	if (hivalbuf) {
		xfree(hivalbuf); hivalbuf = NULL;
		xfree(hival_hostinfo.elems);
	}

	if (!targethost) return -1;

	SBUF_MALLOC(msg, 200 + strlen(targethost));
	snprintf(msg, msg_buflen, "hostinfo clone=%s", targethost);

	sres = newsendreturnbuf(1, NULL);
	sendstat = sendmessage(msg, NULL, XYMON_TIMEOUT, sres);
	xfree(msg);
	if (sendstat != XYMONSEND_OK) {
		errprintf("Cannot load hostinfo\n");
		return -1;
	}

	hivalbuf = getsendreturnstr(sres, 1);
	if (strlen(hivalbuf) == 0) {
		errprintf("No such host\n");
		return -2;
	}

	hivalhost = strdup(targethost);
	memset(hivals, 0, sizeof(hivals));
	memset(&hival_hostinfo, 0, sizeof(hival_hostinfo));
	hival_hostinfo.elems = (char **)calloc(1, sizeof(char *));

	bol = hivalbuf;
	while (bol && *bol) {
		int idx;

		/* 
		 * The "clone" output is multiline: 
		 * Lines beginning with XMH_ are the item-values, 
		 * all others are elem entries.
		 */

		eoln = strchr(bol, '\n');
		if (eoln) *eoln = '\0';

		key = bol;
		if (strncmp(key, "XMH_", 4) == 0) {
			val = strchr(bol, ':'); if (val) { *val = '\0'; val++; }
			idx = xmh_key_idx(key);
			if ((idx >= 0) && (idx < XMH_LAST)) hivals[idx] = val;
		}
		else {
			elemsize++;
			hival_hostinfo.elems = (char **)realloc(hival_hostinfo.elems, (elemsize+1)*sizeof(char *));
			hival_hostinfo.elems[elemsize-1] = bol;
		}

		bol = (eoln ? eoln+1 : NULL);
	}
	hival_hostinfo.elems[elemsize] = NULL;

	hival_hostinfo.hostname = hivals[XMH_HOSTNAME];
	if (hivals[XMH_IP]) 
		strncpy(hival_hostinfo.ip, hivals[XMH_IP], sizeof(hival_hostinfo.ip));
	else
		*(hival_hostinfo.ip) = '\0';

	return 0;
}