1 /*----------------------------------------------------------------------------*/
2 /* Xymon overview webpage generator tool.                                     */
3 /*                                                                            */
4 /* Various utility functions specific to xymongen. Generally useful code is   */
5 /* in the library.                                                            */
6 /*                                                                            */
7 /* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk>                 */
8 /*                                                                            */
9 /* This program is released under the GNU General Public License (GPL),       */
10 /* version 2. See the file "COPYING" for details.                             */
11 /*                                                                            */
12 /*----------------------------------------------------------------------------*/
13 
14 static char rcsid[] = "$Id: util.c 6746 2011-09-04 06:02:41Z storner $";
15 
16 #include <limits.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <utime.h>
22 #include <unistd.h>
23 
24 #include "xymongen.h"
25 #include "util.h"
26 
27 char *htmlextension = ".html"; /* Filename extension for generated HTML files */
28 
29 static void * hosttree;
30 static int havehosttree = 0;
31 static void * columntree;
32 static int havecolumntree = 0;
33 
hostpage_link(host_t * host)34 char *hostpage_link(host_t *host)
35 {
36 	/* Provide a link to the page where this host lives, relative to XYMONWEB */
37 
38 	static char pagelink[PATH_MAX];
39 	char tmppath[PATH_MAX];
40 	xymongen_page_t *pgwalk;
41 
42 	if (host->parent && (strlen(((xymongen_page_t *)host->parent)->name) > 0)) {
43 		sprintf(pagelink, "%s%s", ((xymongen_page_t *)host->parent)->name, htmlextension);
44 		for (pgwalk = host->parent; (pgwalk); pgwalk = pgwalk->parent) {
45 			if (strlen(pgwalk->name)) {
46 				sprintf(tmppath, "%s/%s", pgwalk->name, pagelink);
47 				strcpy(pagelink, tmppath);
48 			}
49 		}
50 	}
51 	else {
52 		sprintf(pagelink, "xymon%s", htmlextension);
53 	}
54 
55 	return pagelink;
56 }
57 
58 
hostpage_name(host_t * host)59 char *hostpage_name(host_t *host)
60 {
61 	/* Provide a link to the page where this host lives */
62 
63 	static char pagename[PATH_MAX];
64 	char tmpname[PATH_MAX];
65 	xymongen_page_t *pgwalk;
66 
67 	if (host->parent && (strlen(((xymongen_page_t *)host->parent)->name) > 0)) {
68 		pagename[0] = '\0';
69 		for (pgwalk = host->parent; (pgwalk); pgwalk = pgwalk->parent) {
70 			if (strlen(pgwalk->name)) {
71 				strcpy(tmpname, pgwalk->title);
72 				if (strlen(pagename)) {
73 					strcat(tmpname, "/");
74 					strcat(tmpname, pagename);
75 				}
76 				strcpy(pagename, tmpname);
77 			}
78 		}
79 	}
80 	else {
81 		sprintf(pagename, "Top page");
82 	}
83 
84 	return pagename;
85 }
86 
87 
88 
checknopropagation(char * testname,char * noproptests)89 static int checknopropagation(char *testname, char *noproptests)
90 {
91 	if (noproptests == NULL) return 0;
92 
93 	if (strcmp(noproptests, ",*,") == 0) return 1;
94 	if (strstr(noproptests, testname) != NULL) return 1;
95 
96 	return 0;
97 }
98 
checkpropagation(host_t * host,char * test,int color,int acked)99 int checkpropagation(host_t *host, char *test, int color, int acked)
100 {
101 	/* NB: Default is to propagate test, i.e. return 1 */
102 	char *testname;
103 	int result = 1;
104 
105 	if (!host) return 1;
106 
107 	testname = (char *) malloc(strlen(test)+3);
108 	sprintf(testname, ",%s,", test);
109 	if (acked) {
110 		if (checknopropagation(testname, host->nopropacktests)) result = 0;
111 	}
112 
113 	if (result) {
114 		if (color == COL_RED) {
115 			if (checknopropagation(testname, host->nopropredtests)) result = 0;
116 		}
117 		else if (color == COL_YELLOW) {
118 			if (checknopropagation(testname, host->nopropyellowtests)) result = 0;
119 			if (checknopropagation(testname, host->nopropredtests)) result = 0;
120 		}
121 		else if (color == COL_PURPLE) {
122 			if (checknopropagation(testname, host->noproppurpletests)) result = 0;
123 		}
124 	}
125 
126 	xfree(testname);
127 	return result;
128 }
129 
130 
find_host(char * hostname)131 host_t *find_host(char *hostname)
132 {
133 	xtreePos_t handle;
134 
135 	if (havehosttree == 0) return NULL;
136 
137 	/* Search for the host */
138 	handle = xtreeFind(hosttree, hostname);
139 	if (handle != xtreeEnd(hosttree)) {
140 		hostlist_t *entry = (hostlist_t *)xtreeData(hosttree, handle);
141 		return (entry ? entry->hostentry : NULL);
142 	}
143 
144 	return NULL;
145 }
146 
host_exists(char * hostname)147 int host_exists(char *hostname)
148 {
149 	return (find_host(hostname) != NULL);
150 }
151 
find_hostlist(char * hostname)152 hostlist_t *find_hostlist(char *hostname)
153 {
154 	xtreePos_t handle;
155 
156 	if (havehosttree == 0) return NULL;
157 
158 	/* Search for the host */
159 	handle = xtreeFind(hosttree, hostname);
160 	if (handle != xtreeEnd(hosttree)) {
161 		hostlist_t *entry = (hostlist_t *)xtreeData(hosttree, handle);
162 		return entry;
163 	}
164 
165 	return NULL;
166 }
167 
add_to_hostlist(hostlist_t * rec)168 void add_to_hostlist(hostlist_t *rec)
169 {
170 	if (havehosttree == 0) {
171 		hosttree = xtreeNew(strcasecmp);
172 		havehosttree = 1;
173 	}
174 
175 	xtreeAdd(hosttree, rec->hostentry->hostname, rec);
176 }
177 
178 static xtreePos_t hostlistwalk;
hostlistBegin(void)179 hostlist_t *hostlistBegin(void)
180 {
181 	if (havehosttree == 0) return NULL;
182 
183 	hostlistwalk = xtreeFirst(hosttree);
184 
185 	if (hostlistwalk != xtreeEnd(hosttree)) {
186 		return (hostlist_t *)xtreeData(hosttree, hostlistwalk);
187 	}
188 	else {
189 		return NULL;
190 	}
191 }
192 
hostlistNext(void)193 hostlist_t *hostlistNext(void)
194 {
195 	if (havehosttree == 0) return NULL;
196 
197 	if (hostlistwalk != xtreeEnd(hosttree)) hostlistwalk = xtreeNext(hosttree, hostlistwalk);
198 
199 	if (hostlistwalk != xtreeEnd(hosttree)) {
200 		return (hostlist_t *)xtreeData(hosttree, hostlistwalk);
201 	}
202 	else {
203 		return NULL;
204 	}
205 }
206 
find_or_create_column(char * testname,int create)207 xymongen_col_t *find_or_create_column(char *testname, int create)
208 {
209 	xymongen_col_t *newcol = NULL;
210 	xtreePos_t handle;
211 
212 	dbgprintf("find_or_create_column(%s)\n", textornull(testname));
213 
214 	if (havecolumntree == 0) {
215 		columntree = xtreeNew(strcasecmp);
216 		havecolumntree = 1;
217 	}
218 
219 	handle = xtreeFind(columntree, testname);
220 	if (handle != xtreeEnd(columntree)) newcol = (xymongen_col_t *)xtreeData(columntree, handle);
221 
222 	if (newcol == NULL) {
223 		if (!create) return NULL;
224 
225 		newcol = (xymongen_col_t *) calloc(1, sizeof(xymongen_col_t));
226 		newcol->name = strdup(testname);
227 		newcol->listname = (char *)malloc(strlen(testname)+1+2);
228 		sprintf(newcol->listname, ",%s,", testname);
229 
230 		xtreeAdd(columntree, newcol->name, newcol);
231 	}
232 
233 	return newcol;
234 }
235 
236 
wantedcolumn(char * current,char * wanted)237 int wantedcolumn(char *current, char *wanted)
238 {
239 	char *tag;
240 	int result;
241 
242 	tag = (char *) malloc(strlen(current)+3);
243 	sprintf(tag, "|%s|", current);
244 	result = (strstr(wanted, tag) != NULL);
245 
246 	xfree(tag);
247 	return result;
248 }
249 
250