1 /**
2  * @file dbhandlers.c Database handler routines for ndo2db daemon
3  */
4 /*
5  * Copyright 2009-2014 Nagios Core Development Team and Community Contributors
6  * Copyright 2005-2009 Ethan Galstad
7  *
8  * This file is part of NDOUtils.
9  *
10  * NDOUtils is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * NDOUtils is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with NDOUtils. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /* include our project's header files */
24 #include "../include/config.h"
25 #include "../include/common.h"
26 #include "../include/io.h"
27 #include "../include/utils.h"
28 #include "../include/protoapi.h"
29 #include "../include/ndo2db.h"
30 #include "../include/db.h"
31 #include "../include/dbhandlers.h"
32 
33 #include <pthread.h>
34 
35 /* Nagios header files */
36 
37 #ifdef BUILD_NAGIOS_2X
38 #include "../include/nagios-2x/nagios.h"
39 #include "../include/nagios-2x/broker.h"
40 #include "../include/nagios-2x/comments.h"
41 #endif
42 #ifdef BUILD_NAGIOS_3X
43 #include "../include/nagios-3x/nagios.h"
44 #include "../include/nagios-3x/broker.h"
45 #include "../include/nagios-3x/comments.h"
46 #endif
47 #ifdef BUILD_NAGIOS_4X
48 #include "../include/nagios-4x/nagios.h"
49 #include "../include/nagios-4x/broker.h"
50 #include "../include/nagios-4x/comments.h"
51 #endif
52 
53 
54 extern int errno;
55 
56 extern char *ndo2db_db_tablenames[NDO2DB_MAX_DBTABLES];
57 
58 
59 
60 /****************************************************************************/
61 /* OBJECT ROUTINES                                                          */
62 /****************************************************************************/
63 
ndo2db_get_object_id(ndo2db_idi * idi,int object_type,char * n1,char * n2,unsigned long * object_id)64 int ndo2db_get_object_id(ndo2db_idi *idi, int object_type, char *n1, char *n2, unsigned long *object_id){
65 	int result=NDO_OK;
66 	int x = 0;
67 	unsigned long cached_object_id=0L;
68 	int found_object=NDO_FALSE;
69 	char *name1=NULL;
70 	char *name2=NULL;
71 	char *buf=NULL;
72 	char *buf1=NULL;
73 	char *buf2=NULL;
74 	char *es[2];
75 
76 	/* make sure empty strings are set to null */
77 	name1=n1;
78 	name2=n2;
79 	if(name1 && !strcmp(name1,""))
80 		name1=NULL;
81 	if(name2 && !strcmp(name2,""))
82 		name2=NULL;
83 
84 	/* null names mean no object id */
85 	if(name1==NULL && name2==NULL){
86 		*object_id=0L;
87 		return NDO_OK;
88 	        }
89 
90 	/* see if the object already exists in cached lookup table */
91 	if(ndo2db_get_cached_object_id(idi,object_type,name1,name2,&cached_object_id)==NDO_OK){
92 		*object_id=cached_object_id;
93 		return NDO_OK;
94 	        }
95 
96 	if(name1==NULL){
97 		es[0]=NULL;
98 		if(asprintf(&buf1,"name1 IS NULL")==-1)
99 			buf1=NULL;
100 	        }
101 	else{
102 		es[0]=ndo2db_db_escape_string(idi,name1);
103 		/* HINT: HB 10/27/2009
104 		 * BINARY operator is just a MySQL special to provide case sensitive queries
105 		 * Think about it in the future if not only MySQL is supported
106 		 */
107 		if(asprintf(&buf1,"BINARY name1='%s'",es[0])==-1)
108 			buf1=NULL;
109 	        }
110 
111 	if(name2==NULL){
112 		es[1]=NULL;
113 		if(asprintf(&buf2,"name2 IS NULL")==-1)
114 			buf2=NULL;
115 	        }
116 	else{
117 		es[1]=ndo2db_db_escape_string(idi,name2);
118 		/* HINT: HB 10/27/2009
119 		 * BINARY operator is just a MySQL special to provide case sensitive queries
120 		 * Think about it in the future if not only MySQL is supported
121 		 */
122 		if(asprintf(&buf2,"BINARY name2='%s'",es[1])==-1)
123 			buf2=NULL;
124 	        }
125 
126 	if(asprintf(&buf,"SELECT * FROM %s WHERE instance_id='%lu' AND objecttype_id='%d' AND %s AND %s"
127 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS]
128 		    ,idi->dbinfo.instance_id
129 		    ,object_type
130 		    ,buf1
131 		    ,buf2
132 		   )==-1)
133 		buf=NULL;
134 	if((result=ndo2db_db_query(idi,buf))==NDO_OK){
135 		idi->dbinfo.mysql_result=mysql_store_result(&idi->dbinfo.mysql_conn);
136 		if((idi->dbinfo.mysql_row=mysql_fetch_row(idi->dbinfo.mysql_result))!=NULL){
137 			ndo2db_convert_string_to_unsignedlong(idi->dbinfo.mysql_row[0],object_id);
138 			found_object=NDO_TRUE;
139 		}
140 		mysql_free_result(idi->dbinfo.mysql_result);
141 		idi->dbinfo.mysql_result=NULL;
142 	}
143 	free(buf);
144 
145 	/* free memory */
146 	free(buf1);
147 	free(buf2);
148 
149 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
150 		free(es[x]);
151 
152 	if(found_object==NDO_FALSE)
153 		result=NDO_ERROR;
154 
155 	return result;
156         }
157 
158 
ndo2db_get_object_id_with_insert(ndo2db_idi * idi,int object_type,char * n1,char * n2,unsigned long * object_id)159 int ndo2db_get_object_id_with_insert(ndo2db_idi *idi, int object_type, char *n1, char *n2, unsigned long *object_id){
160 	int x = 0;
161 	int result=NDO_OK;
162 	char *buf=NULL;
163 	char *buf1=NULL;
164 	char *buf2=NULL;
165 	char *name1=NULL;
166 	char *name2=NULL;
167 	char *es[2];
168 
169 	/* make sure empty strings are set to null */
170 	name1=n1;
171 	name2=n2;
172 	if(name1 && !strcmp(name1,""))
173 		name1=NULL;
174 	if(name2 && !strcmp(name2,""))
175 		name2=NULL;
176 
177 	/* null names mean no object id */
178 	if(name1==NULL && name2==NULL){
179 		*object_id=0L;
180 		return NDO_OK;
181 	        }
182 
183 	/* object already exists */
184 	if((result=ndo2db_get_object_id(idi,object_type,name1,name2,object_id))==NDO_OK)
185 		return NDO_OK;
186 
187 	if(name1!=NULL){
188 		es[0]=ndo2db_db_escape_string(idi,name1);
189 		if(asprintf(&buf1,", name1='%s'",es[0])==-1)
190 			buf1=NULL;
191 	        }
192 	else
193 		es[0]=NULL;
194 	if(name2!=NULL){
195 		es[1]=ndo2db_db_escape_string(idi,name2);
196 		if(asprintf(&buf2,", name2='%s'",es[1])==-1)
197 			buf2=NULL;
198 	        }
199 	else
200 		es[1]=NULL;
201 
202 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', objecttype_id='%d' %s %s"
203 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS]
204 		    ,idi->dbinfo.instance_id
205 		    ,object_type
206 		    ,(buf1==NULL)?"":buf1
207 		    ,(buf2==NULL)?"":buf2
208 		   )==-1)
209 		buf=NULL;
210 	if((result=ndo2db_db_query(idi,buf))==NDO_OK){
211 		*object_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
212 	}
213 	free(buf);
214 
215 	/* cache object id for later lookups */
216 	ndo2db_add_cached_object_id(idi,object_type,name1,name2,*object_id);
217 
218 	/* free memory */
219 	free(buf1);
220 	free(buf2);
221 
222         /* free memory */
223 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
224 		free(es[x]);
225 
226 	return result;
227         }
228 
229 
230 
ndo2db_get_cached_object_ids(ndo2db_idi * idi)231 int ndo2db_get_cached_object_ids(ndo2db_idi *idi){
232 	int result=NDO_OK;
233 	unsigned long object_id=0L;
234 	int objecttype_id=0;
235 	char *buf=NULL;
236 
237 	/* find all the object definitions we already have */
238 	if(asprintf(&buf,"SELECT object_id, objecttype_id, name1, name2 FROM %s WHERE instance_id='%lu'"
239 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS]
240 		    ,idi->dbinfo.instance_id
241 		   )==-1)
242 		buf=NULL;
243 
244 	if((result=ndo2db_db_query(idi,buf))==NDO_OK){
245 		idi->dbinfo.mysql_result=mysql_store_result(&idi->dbinfo.mysql_conn);
246 		if(NULL != idi->dbinfo.mysql_result) {
247 			while((idi->dbinfo.mysql_row=mysql_fetch_row(idi->dbinfo.mysql_result))!=NULL){
248 
249 				ndo2db_convert_string_to_unsignedlong(idi->dbinfo.mysql_row[0],&object_id);
250 				ndo2db_convert_string_to_int(idi->dbinfo.mysql_row[1],&objecttype_id);
251 
252 				/* add object to cached list */
253 				ndo2db_add_cached_object_id(idi,objecttype_id,idi->dbinfo.mysql_row[2],idi->dbinfo.mysql_row[3],object_id);
254 				}
255 			mysql_free_result(idi->dbinfo.mysql_result);
256 			}
257 		else if(mysql_errno(&idi->dbinfo.mysql_conn) != 0) {
258 			syslog(LOG_USER|LOG_INFO,
259 					"Error: mysql_store_result() failed for '%s'\n", buf);
260 		}
261 		idi->dbinfo.mysql_result=NULL;
262 	}
263 	free(buf);
264 
265 	return result;
266         }
267 
268 
269 
ndo2db_get_cached_object_id(ndo2db_idi * idi,int object_type,char * name1,char * name2,unsigned long * object_id)270 int ndo2db_get_cached_object_id(ndo2db_idi *idi, int object_type, char *name1, char *name2, unsigned long *object_id){
271 	int result=NDO_ERROR;
272 	int hashslot=0;
273 	int compare=0;
274 	ndo2db_dbobject *temp_object=NULL;
275 	int y=0;
276 
277 	hashslot=ndo2db_object_hashfunc(name1,name2,NDO2DB_OBJECT_HASHSLOTS);
278 #ifdef NDO2DB_DEBUG_CACHING
279 	printf("OBJECT LOOKUP: type=%d, name1=%s, name2=%s\n",object_type,(name1==NULL)?"NULL":name1,(name2==NULL)?"NULL":name2);
280 #endif
281 
282 	if(idi->dbinfo.object_hashlist==NULL)
283 		return NDO_ERROR;
284 
285 	for(temp_object=idi->dbinfo.object_hashlist[hashslot],y=0;temp_object!=NULL;temp_object=temp_object->nexthash,y++){
286 #ifdef NDO2DB_DEBUG_CACHING
287 		printf("OBJECT LOOKUP LOOPING [%d][%d]: type=%d, id=%lu, name1=%s, name2=%s\n",hashslot,y,temp_object->object_type,temp_object->object_id,(temp_object->name1==NULL)?"NULL":temp_object->name1,(temp_object->name2==NULL)?"NULL":temp_object->name2);
288 #endif
289 		compare=ndo2db_compare_object_hashdata(temp_object->name1,temp_object->name2,name1,name2);
290 		if(compare==0 && temp_object->object_type==object_type)
291 			break;
292 	        }
293 
294 	/* we have a match! */
295 	if(temp_object && (ndo2db_compare_object_hashdata(temp_object->name1,temp_object->name2,name1,name2)==0) && temp_object->object_type==object_type){
296 #ifdef NDO2DB_DEBUG_CACHING
297 		printf("OBJECT CACHE HIT [%d][%d]: type=%d, id=%lu, name1=%s, name2=%s\n",hashslot,y,object_type,temp_object->object_id,(name1==NULL)?"NULL":name1,(name2==NULL)?"NULL":name2);
298 #endif
299 		*object_id=temp_object->object_id;
300 		result=NDO_OK;
301 	        }
302 #ifdef NDO2DB_DEBUG_CACHING
303 	else{
304 		printf("OBJECT CACHE MISS: type=%d, name1=%s, name2=%s\n",object_type,(name1==NULL)?"NULL":name1,(name2==NULL)?"NULL":name2);
305 	        }
306 #endif
307 
308 	return result;
309         }
310 
311 
312 
ndo2db_add_cached_object_id(ndo2db_idi * idi,int object_type,char * n1,char * n2,unsigned long object_id)313 int ndo2db_add_cached_object_id(ndo2db_idi *idi, int object_type, char *n1, char *n2, unsigned long object_id){
314 	int result=NDO_OK;
315 	ndo2db_dbobject *temp_object=NULL;
316 	ndo2db_dbobject *lastpointer=NULL;
317 	ndo2db_dbobject *new_object=NULL;
318 	int x=0;
319 	int y=0;
320 	int hashslot=0;
321 	int compare=0;
322 	char *name1=NULL;
323 	char *name2=NULL;
324 
325 	/* make sure empty strings are set to null */
326 	name1=n1;
327 	name2=n2;
328 	if(name1 && !strcmp(name1,""))
329 		name1=NULL;
330 	if(name2 && !strcmp(name2,""))
331 		name2=NULL;
332 
333 	/* null names mean no object id, so don't cache */
334 	if(name1==NULL && name2==NULL){
335 		return NDO_OK;
336 	        }
337 
338 #ifdef NDO2DB_DEBUG_CACHING
339 	printf("OBJECT CACHE ADD: type=%d, id=%lu, name1=%s, name2=%s\n",object_type,object_id,(name1==NULL)?"NULL":name1,(name2==NULL)?"NULL":name2);
340 #endif
341 
342 	/* initialize hash list if necessary */
343 	if(idi->dbinfo.object_hashlist==NULL){
344 
345 		idi->dbinfo.object_hashlist=(ndo2db_dbobject **)malloc(sizeof(ndo2db_dbobject *)*NDO2DB_OBJECT_HASHSLOTS);
346 		if(idi->dbinfo.object_hashlist==NULL)
347 			return NDO_ERROR;
348 
349 		for(x=0;x<NDO2DB_OBJECT_HASHSLOTS;x++)
350 			idi->dbinfo.object_hashlist[x]=NULL;
351 	        }
352 
353 	/* allocate and populate new object */
354 	if((new_object=(ndo2db_dbobject *)malloc(sizeof(ndo2db_dbobject)))==NULL)
355 		return NDO_ERROR;
356 	new_object->object_type=object_type;
357 	new_object->object_id=object_id;
358 	new_object->name1=NULL;
359 	if(name1)
360 		new_object->name1=strdup(name1);
361 	new_object->name2=NULL;
362 	if(name2)
363 		new_object->name2=strdup(name2);
364 
365 	hashslot=ndo2db_object_hashfunc(new_object->name1,new_object->name2,NDO2DB_OBJECT_HASHSLOTS);
366 
367 	lastpointer=NULL;
368 	for(temp_object=idi->dbinfo.object_hashlist[hashslot],y=0;temp_object!=NULL;temp_object=temp_object->nexthash,y++){
369 		compare=ndo2db_compare_object_hashdata(temp_object->name1,temp_object->name2,new_object->name1,new_object->name2);
370 		if(compare<0)
371 			break;
372 		lastpointer=temp_object;
373 	        }
374 
375 	if(lastpointer)
376 		lastpointer->nexthash=new_object;
377 	else
378 		idi->dbinfo.object_hashlist[hashslot]=new_object;
379 	new_object->nexthash=temp_object;
380 
381 	return result;
382         }
383 
384 
385 
ndo2db_object_hashfunc(const char * name1,const char * name2,int hashslots)386 int ndo2db_object_hashfunc(const char *name1,const char *name2,int hashslots){
387 	unsigned int i,result;
388 
389 	result=0;
390 	if(name1)
391 		for(i=0;i<strlen(name1);i++)
392 			result+=name1[i];
393 
394 	if(name2)
395 		for(i=0;i<strlen(name2);i++)
396 			result+=name2[i];
397 
398 	result=result%hashslots;
399 
400 	return result;
401         }
402 
403 
404 
ndo2db_compare_object_hashdata(const char * val1a,const char * val1b,const char * val2a,const char * val2b)405 int ndo2db_compare_object_hashdata(const char *val1a, const char *val1b, const char *val2a, const char *val2b){
406 	int result=0;
407 
408 	/* check first name */
409 	if(val1a==NULL && val2a==NULL)
410 		result=0;
411 	else if(val1a==NULL)
412 		result=1;
413 	else if(val2a==NULL)
414 		result=-1;
415 	else
416 		result=strcmp(val1a,val2a);
417 
418 	/* check second name if necessary */
419 	if(result==0){
420 		if(val1b==NULL && val2b==NULL)
421 			result=0;
422 		else if(val1b==NULL)
423 			result=1;
424 		else if(val2b==NULL)
425 			result=-1;
426 		else
427 			return strcmp(val1b,val2b);
428 	        }
429 
430 	return result;
431         }
432 
433 
434 
ndo2db_free_cached_object_ids(ndo2db_idi * idi)435 int ndo2db_free_cached_object_ids(ndo2db_idi *idi){
436 	int x=0;
437 	ndo2db_dbobject *temp_object=NULL;
438 	ndo2db_dbobject *next_object=NULL;
439 
440 	if(idi==NULL)
441 		return NDO_OK;
442 
443 	if(idi->dbinfo.object_hashlist){
444 
445 		for(x=0;x<NDO2DB_OBJECT_HASHSLOTS;x++){
446 			for(temp_object=idi->dbinfo.object_hashlist[x];temp_object!=NULL;temp_object=next_object){
447 				next_object=temp_object->nexthash;
448 				free(temp_object->name1);
449 				free(temp_object->name2);
450 				free(temp_object);
451 			        }
452 		        }
453 
454 		free(idi->dbinfo.object_hashlist);
455 		idi->dbinfo.object_hashlist=NULL;
456 	        }
457 
458 	return NDO_OK;
459         }
460 
461 
462 
ndo2db_set_all_objects_as_inactive(ndo2db_idi * idi)463 int ndo2db_set_all_objects_as_inactive(ndo2db_idi *idi){
464 	int result=NDO_OK;
465 	char *buf=NULL;
466 
467 	/* mark all objects as being inactive */
468 	if(asprintf(&buf,"UPDATE %s SET is_active='0' WHERE instance_id='%lu'"
469 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS]
470 		    ,idi->dbinfo.instance_id
471 		   )==-1)
472 		buf=NULL;
473 
474 	result=ndo2db_db_query(idi,buf);
475 	free(buf);
476 
477 	return result;
478          }
479 
480 
481 
ndo2db_set_object_as_active(ndo2db_idi * idi,int object_type,unsigned long object_id)482 int ndo2db_set_object_as_active(ndo2db_idi *idi, int object_type, unsigned long object_id){
483 	int result=NDO_OK;
484 	char *buf=NULL;
485 
486 	/* mark the object as being active */
487 	if(asprintf(&buf,"UPDATE %s SET is_active='1' WHERE instance_id='%lu' AND objecttype_id='%d' AND object_id='%lu'"
488 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS]
489 		    ,idi->dbinfo.instance_id
490 		    ,object_type
491 		    ,object_id
492 		   )==-1)
493 		buf=NULL;
494 
495 	result=ndo2db_db_query(idi,buf);
496 	free(buf);
497 
498 	return result;
499         }
500 
501 
502 
503 /****************************************************************************/
504 /* ARCHIVED LOG DATA HANDLER                                                */
505 /****************************************************************************/
506 
ndo2db_handle_logentry(ndo2db_idi * idi)507 int ndo2db_handle_logentry(ndo2db_idi *idi){
508 	char *ptr=NULL;
509 	char *buf=NULL;
510 	char *es[1];
511 	time_t etime=0L;
512 	char *ts[1];
513 	unsigned long type=0L;
514 	int result=NDO_OK;
515 	int duplicate_record=NDO_FALSE;
516 	int len=0;
517 	int x=0;
518 
519 	if(idi==NULL)
520 		return NDO_ERROR;
521 
522 	/* break log entry in pieces */
523 	if((ptr=strtok(idi->buffered_input[NDO_DATA_LOGENTRY],"]"))==NULL)
524 		return NDO_ERROR;
525 	if((ndo2db_convert_string_to_unsignedlong(ptr+1,(unsigned long *)&etime))==NDO_ERROR)
526 		return NDO_ERROR;
527 	ts[0]=ndo2db_db_timet_to_sql(idi,etime);
528 	if((ptr=strtok(NULL,"\x0"))==NULL)
529 		return NDO_ERROR;
530 	es[0]=ndo2db_db_escape_string(idi,(ptr+1));
531 
532 	/* strip newline chars from end */
533 	len=strlen(es[0]);
534 	for(x=len-1;x>=0;x--){
535 		if(es[0][x]=='\n')
536 			es[0][x]='\x0';
537 		else
538 			break;
539 	        }
540 
541 	/* what type of log entry is this? */
542 	type=0;
543 
544 	/* make sure we aren't importing a duplicate log entry... */
545 	if(asprintf(&buf,"SELECT * FROM %s WHERE instance_id='%lu' AND logentry_time=%s AND logentry_data='%s'"
546 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_LOGENTRIES]
547 		    ,idi->dbinfo.instance_id
548 		    ,ts[0]
549 		    ,es[0]
550 		   )==-1)
551 		buf=NULL;
552 	if((result=ndo2db_db_query(idi,buf))==NDO_OK){
553 		idi->dbinfo.mysql_result=mysql_store_result(&idi->dbinfo.mysql_conn);
554 		if((idi->dbinfo.mysql_row=mysql_fetch_row(idi->dbinfo.mysql_result))!=NULL)
555 			duplicate_record=NDO_TRUE;
556 		mysql_free_result(idi->dbinfo.mysql_result);
557 		idi->dbinfo.mysql_result=NULL;
558 	}
559 	free(buf);
560 
561 	/*if(duplicate_record==NDO_TRUE && idi->last_logentry_time!=etime){*/
562 	/*if(duplicate_record==NDO_TRUE && strcmp((es[0]==NULL)?"":es[0],idi->dbinfo.last_logentry_data)){*/
563 	if(duplicate_record==NDO_TRUE){
564 #ifdef NDO2DB_DEBUG
565 		printf("IGNORING DUPLICATE LOG RECORD!\n");
566 #endif
567 		return NDO_OK;
568 	        }
569 
570 	/* save entry to db */
571 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', logentry_time=%s, entry_time=%s, entry_time_usec='0', logentry_type='%lu', logentry_data='%s', realtime_data='0', inferred_data_extracted='0'"
572 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_LOGENTRIES]
573 		    ,idi->dbinfo.instance_id
574 		    ,ts[0]
575 		    ,ts[0]
576 		    ,type
577 		    ,(es[0]==NULL)?"":es[0]
578 		   )==-1)
579 		buf=NULL;
580 	result=ndo2db_db_query(idi,buf);
581 	free(buf);
582 
583 	/* record timestamp of last log entry */
584 	idi->dbinfo.last_logentry_time=etime;
585 
586 	/* save last log entry (for detecting duplicates) */
587 	if(idi->dbinfo.last_logentry_data)
588 		free(idi->dbinfo.last_logentry_data);
589 	idi->dbinfo.last_logentry_data=strdup((es[0]==NULL)?"":es[0]);
590 
591         /* free memory */
592 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
593 		free(ts[x]);
594 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
595 		free(es[x]);
596 
597 	/* TODO - further processing of log entry to expand archived data... */
598 
599 
600 	return result;
601         }
602 
603 
604 
605 /****************************************************************************/
606 /* REALTIME DATA HANDLERS                                                   */
607 /****************************************************************************/
608 
ndo2db_handle_processdata(ndo2db_idi * idi)609 int ndo2db_handle_processdata(ndo2db_idi *idi){
610 	int type,flags,attr;
611 	struct timeval tstamp;
612 	unsigned long process_id;
613 	int result=NDO_OK;
614 	char *ts[1];
615 	char *es[3];
616 	int x=0;
617 	char *buf=NULL;
618 
619 	if(idi==NULL)
620 		return NDO_ERROR;
621 
622 	/* convert timestamp, etc */
623 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
624 
625 	/* convert vars */
626 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_PROCESSID],&process_id);
627 
628 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
629 
630 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PROGRAMNAME]);
631 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PROGRAMVERSION]);
632 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PROGRAMDATE]);
633 
634 	/* save entry to db */
635 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', event_type='%d', event_time=%s, event_time_usec='%lu', process_id='%lu', program_name='%s', program_version='%s', program_date='%s'"
636 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_PROCESSEVENTS]
637 		    ,idi->dbinfo.instance_id
638 		    ,type
639 		    ,ts[0]
640 		    ,tstamp.tv_usec
641 		    ,process_id
642 		    ,es[0]
643 		    ,es[1]
644 		    ,es[2]
645 		   )==-1)
646 		buf=NULL;
647 	result=ndo2db_db_query(idi,buf);
648 	free(buf);
649 
650 	/* MORE PROCESSING.... */
651 
652 	/* if process is starting up, clearstatus data, event queue, etc. */
653 	if(type==NEBTYPE_PROCESS_PRELAUNCH && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
654 
655 		/* clear realtime data */
656 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_PROGRAMSTATUS]);
657 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTSTATUS]);
658 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICESTATUS]);
659 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTSTATUS]);
660 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTQUEUE]);
661 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTS]);
662 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SCHEDULEDDOWNTIME]);
663 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_RUNTIMEVARIABLES]);
664 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CUSTOMVARIABLESTATUS]);
665 
666 		/* clear config data */
667 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONFIGFILES]);
668 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONFIGFILEVARIABLES]);
669 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CUSTOMVARIABLES]);
670 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMANDS]);
671 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEPERIODS]);
672 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEPERIODTIMERANGES]);
673 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTGROUPS]);
674 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTGROUPMEMBERS]);
675 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTGROUPS]);
676 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTGROUPMEMBERS]);
677 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEGROUPS]);
678 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEGROUPMEMBERS]);
679 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTESCALATIONS]);
680 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTESCALATIONCONTACTS]);
681 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEESCALATIONS]);
682 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEESCALATIONCONTACTS]);
683 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTDEPENDENCIES]);
684 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEDEPENDENCIES]);
685 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTS]);
686 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTADDRESSES]);
687 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONCOMMANDS]);
688 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTS]);
689 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTPARENTHOSTS]);
690 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTCONTACTS]);
691 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICES]);
692 #ifdef BUILD_NAGIOS_4X
693 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEPARENTSERVICES]);
694 #endif
695 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICECONTACTS]);
696 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICECONTACTGROUPS]);
697 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTCONTACTGROUPS]);
698 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTESCALATIONCONTACTGROUPS]);
699 		ndo2db_db_clear_table(idi,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEESCALATIONCONTACTGROUPS]);
700 
701 		/* flag all objects as being inactive */
702 		ndo2db_set_all_objects_as_inactive(idi);
703 
704 #ifdef BAD_IDEA
705 		/* record a fake log entry to indicate that Nagios is starting - this normally occurs during the module's "blackout period" */
706 		if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', logentry_time=%s, logentry_type='%lu', logentry_data='Nagios %s starting... (PID=%lu)'"
707 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_LOGENTRIES]
708 			    ,idi->dbinfo.instance_id
709 			    ,ts[0]
710 			    ,NSLOG_PROCESS_INFO
711 			    ,es[1]
712 			    ,process_id
713 			   )==-1)
714 			buf=NULL;
715 		result=ndo2db_db_query(idi,buf);
716 		free(buf);
717 #endif
718 	        }
719 
720 	/* if process is shutting down or restarting, update process status data */
721 	if((type==NEBTYPE_PROCESS_SHUTDOWN || type==NEBTYPE_PROCESS_RESTART) && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
722 
723 		if(asprintf(&buf,"UPDATE %s SET program_end_time=%s, is_currently_running='0' WHERE instance_id='%lu'"
724 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_PROGRAMSTATUS]
725 			    ,ts[0]
726 			    ,idi->dbinfo.instance_id
727 			   )==-1)
728 			buf=NULL;
729 		result=ndo2db_db_query(idi,buf);
730 		free(buf);
731 	        }
732 
733         /* free memory */
734 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
735 		free(ts[x]);
736 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
737 		free(es[x]);
738 
739 	return NDO_OK;
740         }
741 
742 
ndo2db_handle_timedeventdata(ndo2db_idi * idi)743 int ndo2db_handle_timedeventdata(ndo2db_idi *idi){
744 	int type,flags,attr;
745 	struct timeval tstamp;
746 	int event_type=0;
747 	unsigned long run_time=0L;
748 	int recurring_event=0;
749 	unsigned long object_id=0L;
750 	int result=NDO_OK;
751 	char *ts[2];
752 	int x=0;
753 	char *buf=NULL;
754 	char *buf1=NULL;
755 
756 	if(idi==NULL)
757 		return NDO_ERROR;
758 
759 	/* convert timestamp, etc */
760 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
761 
762 	/* convert vars */
763 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EVENTTYPE],&event_type);
764 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RECURRING],&recurring_event);
765 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_RUNTIME],&run_time);
766 
767 	/* skip sleep events.... */
768 	if(type==NEBTYPE_TIMEDEVENT_SLEEP){
769 
770 		/* we could do some maintenance here if we wanted.... */
771 
772 		return NDO_OK;
773 	        }
774 
775 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
776 	ts[1]=ndo2db_db_timet_to_sql(idi,run_time);
777 
778 	/* get the object id (if applicable) */
779 	if(event_type==EVENT_SERVICE_CHECK || (event_type==EVENT_SCHEDULED_DOWNTIME && idi->buffered_input[NDO_DATA_SERVICE]!=NULL && strcmp(idi->buffered_input[NDO_DATA_SERVICE],"")))
780 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
781 	if(event_type==EVENT_HOST_CHECK || (event_type==EVENT_SCHEDULED_DOWNTIME && (idi->buffered_input[NDO_DATA_SERVICE]==NULL || !strcmp(idi->buffered_input[NDO_DATA_SERVICE],""))))
782 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
783 
784 
785 	/* HISTORICAL TIMED EVENTS */
786 
787 	/* save a record of timed events that get added */
788 	if(type==NEBTYPE_TIMEDEVENT_ADD && 0){
789 
790 		/* save entry to db */
791 		if(asprintf(&buf,"instance_id='%lu', event_type='%d', queued_time=%s, queued_time_usec='%lu', scheduled_time=%s, recurring_event='%d', object_id='%lu'"
792 			    ,idi->dbinfo.instance_id
793 			    ,event_type
794 			    ,ts[0]
795 			    ,tstamp.tv_usec
796 			    ,ts[1]
797 			    ,recurring_event
798 			    ,object_id
799 			   )==-1)
800 			buf=NULL;
801 
802 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
803 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTS]
804 			    ,buf
805 			    ,buf
806 			   )==-1)
807 			buf1=NULL;
808 
809 		result=ndo2db_db_query(idi,buf1);
810 		free(buf);
811 		free(buf1);
812 	        }
813 
814 	/* save a record of timed events that get executed.... */
815 	if(type==NEBTYPE_TIMEDEVENT_EXECUTE && 0){
816 
817 		/* save entry to db */
818 		if(asprintf(&buf,"instance_id='%lu', event_type='%d', event_time=%s, event_time_usec='%lu', scheduled_time=%s, recurring_event='%d', object_id='%lu'"
819 			    ,idi->dbinfo.instance_id
820 			    ,event_type
821 			    ,ts[0]
822 			    ,tstamp.tv_usec
823 			    ,ts[1]
824 			    ,recurring_event
825 			    ,object_id
826 			   )==-1)
827 			buf=NULL;
828 
829 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
830 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTS]
831 			    ,buf
832 			    ,buf
833 			   )==-1)
834 			buf1=NULL;
835 
836 		result=ndo2db_db_query(idi,buf1);
837 		free(buf);
838 		free(buf1);
839 	        }
840 
841 	/* save a record of timed events that get removed.... */
842 	if(type==NEBTYPE_TIMEDEVENT_REMOVE && 0){
843 
844 		/* save entry to db */
845 		if(asprintf(&buf,"UPDATE %s SET deletion_time=%s, deletion_time_usec='%lu' WHERE instance_id='%lu' AND event_type='%d' AND scheduled_time=%s AND recurring_event='%d' AND object_id='%lu'"
846 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTS]
847 			    ,ts[0]
848 			    ,tstamp.tv_usec
849 			    ,idi->dbinfo.instance_id
850 			    ,event_type
851 			    ,ts[1]
852 			    ,recurring_event
853 			    ,object_id
854 			   )==-1)
855 			buf=NULL;
856 
857 		result=ndo2db_db_query(idi,buf);
858 		free(buf);
859 	        }
860 
861 	/* CURRENT TIMED EVENTS */
862 
863 	/* remove (probably) expired events from the queue if client just connected */
864 	if(idi->dbinfo.clean_event_queue==NDO_TRUE && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
865 
866 		idi->dbinfo.clean_event_queue=NDO_FALSE;
867 
868 		/* clear old entries from db */
869 		if(asprintf(&buf,"DELETE FROM %s WHERE instance_id='%lu' AND scheduled_time<=%s"
870 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTQUEUE]
871 			    ,idi->dbinfo.instance_id
872 			    ,ts[0]
873 			   )==-1)
874 			buf=NULL;
875 		result=ndo2db_db_query(idi,buf);
876 		free(buf);
877 	        }
878 
879 	/* ADD QUEUED TIMED EVENTS */
880 	if(type==NEBTYPE_TIMEDEVENT_ADD  && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
881 
882 		/* save entry to db */
883 		if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', event_type='%d', queued_time=%s, queued_time_usec='%lu', scheduled_time=%s, recurring_event='%d', object_id='%lu'"
884 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTQUEUE]
885 			    ,idi->dbinfo.instance_id
886 			    ,event_type
887 			    ,ts[0]
888 			    ,tstamp.tv_usec
889 			    ,ts[1]
890 			    ,recurring_event
891 			    ,object_id
892 			   )==-1)
893 			buf=NULL;
894 		result=ndo2db_db_query(idi,buf);
895 		free(buf);
896 	        }
897 
898 	/* REMOVE QUEUED TIMED EVENTS */
899 	if((type==NEBTYPE_TIMEDEVENT_REMOVE || type==NEBTYPE_TIMEDEVENT_EXECUTE)  && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
900 
901 		/* clear entry from db */
902 		if(asprintf(&buf,"DELETE FROM %s WHERE instance_id='%lu' AND event_type='%d' AND scheduled_time=%s AND recurring_event='%d' AND object_id='%lu'"
903 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTQUEUE]
904 			    ,idi->dbinfo.instance_id
905 			    ,event_type
906 			    ,ts[1]
907 			    ,recurring_event
908 			    ,object_id
909 			   )==-1)
910 			buf=NULL;
911 		result=ndo2db_db_query(idi,buf);
912 		free(buf);
913 
914 		/* if we are executing a low-priority event, remove older events from the queue, as we know they've already been executed */
915 		/* THIS IS A HACK!  It shouldn't be necessary, but for some reason it is...  Otherwise not all events are removed from the queue. :-( */
916 		if(type==NEBTYPE_TIMEDEVENT_EXECUTE && (event_type==EVENT_SERVICE_CHECK || event_type==EVENT_HOST_CHECK)){
917 
918 			/* clear entries from db */
919 			if(asprintf(&buf,"DELETE FROM %s WHERE instance_id='%lu' AND scheduled_time<%s"
920 				    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEDEVENTQUEUE]
921 				    ,idi->dbinfo.instance_id
922 				    ,ts[1]
923 				   )==-1)
924 				buf=NULL;
925 			result=ndo2db_db_query(idi,buf);
926 			free(buf);
927 		        }
928 
929 	        }
930 
931         /* free memory */
932 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
933 		free(ts[x]);
934 
935 	return NDO_OK;
936         }
937 
938 
ndo2db_handle_logdata(ndo2db_idi * idi)939 int ndo2db_handle_logdata(ndo2db_idi *idi){
940 	int type,flags,attr;
941 	struct timeval tstamp;
942 	time_t etime=0L;
943 	unsigned long letype=0L;
944 	int result=NDO_OK;
945 	char *ts[2];
946 	char *es[1];
947 	char *buf=NULL;
948 	int len=0;
949 	int x=0;
950 
951 	if(idi==NULL)
952 		return NDO_ERROR;
953 
954 	/* convert timestamp, etc */
955 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
956 
957 	/* convert data */
958 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LOGENTRYTYPE],&letype);
959 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LOGENTRYTIME],(unsigned long *)&etime);
960 
961 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
962 	ts[1]=ndo2db_db_timet_to_sql(idi,etime);
963 
964 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LOGENTRY]);
965 
966 	/* strip newline chars from end */
967 	len=strlen(es[0]);
968 	for(x=len-1;x>=0;x--){
969 		if(es[0][x]=='\n')
970 			es[0][x]='\x0';
971 		else
972 			break;
973 	        }
974 
975 	/* save entry to db */
976 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', logentry_time=%s, entry_time=%s, entry_time_usec='%lu', logentry_type='%lu', logentry_data='%s', realtime_data='1', inferred_data_extracted='1'"
977 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_LOGENTRIES]
978 		    ,idi->dbinfo.instance_id
979 		    ,ts[1]
980 		    ,ts[0]
981 		    ,tstamp.tv_usec
982 		    ,letype
983 		    ,es[0]
984 		   )==-1)
985 		buf=NULL;
986 	result=ndo2db_db_query(idi,buf);
987 	free(buf);
988 
989         /* free memory */
990 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
991 		free(ts[x]);
992 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
993 		free(es[x]);
994 
995 	return NDO_OK;
996         }
997 
998 
ndo2db_handle_systemcommanddata(ndo2db_idi * idi)999 int ndo2db_handle_systemcommanddata(ndo2db_idi *idi){
1000 	int x = 0;
1001 	int type,flags,attr;
1002 	struct timeval tstamp;
1003 	struct timeval start_time;
1004 	struct timeval end_time;
1005 	int timeout=0;
1006 	int early_timeout=0;
1007 	double execution_time=0.0;
1008 	int return_code=0;
1009 	char *ts[2];
1010 	char *es[3];
1011 	char *buf=NULL;
1012 	char *buf1=NULL;
1013 	int result=NDO_OK;
1014 
1015 	if(idi==NULL)
1016 		return NDO_ERROR;
1017 
1018 	/* convert timestamp, etc */
1019 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1020 
1021 	/* covert vars */
1022 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_TIMEOUT],&timeout);
1023 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EARLYTIMEOUT],&early_timeout);
1024 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETURNCODE],&return_code);
1025 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_EXECUTIONTIME],&execution_time);
1026 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1027 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1028 
1029 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDLINE]);
1030 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
1031 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
1032 
1033 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1034 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1035 
1036 	/* save entry to db */
1037 	if(asprintf(&buf,"instance_id='%lu', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', command_line='%s', timeout='%d', early_timeout='%d', execution_time='%lf', return_code='%d', output='%s', long_output='%s'"
1038 		    ,idi->dbinfo.instance_id
1039 		    ,ts[0]
1040 		    ,start_time.tv_usec
1041 		    ,ts[1]
1042 		    ,end_time.tv_usec
1043 		    ,es[0]
1044 		    ,timeout
1045 		    ,early_timeout
1046 		    ,execution_time
1047 		    ,return_code
1048 		    ,es[1]
1049 		    ,es[2]
1050 		   )==-1)
1051 		buf=NULL;
1052 
1053 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1054 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SYSTEMCOMMANDS]
1055 		    ,buf
1056 		    ,buf
1057 		   )==-1)
1058 		buf1=NULL;
1059 
1060 	result=ndo2db_db_query(idi,buf1);
1061 	free(buf);
1062 	free(buf1);
1063 
1064         /* free memory */
1065 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1066 		free(ts[x]);
1067 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1068 		free(es[x]);
1069 
1070 	return NDO_OK;
1071         }
1072 
1073 
ndo2db_handle_eventhandlerdata(ndo2db_idi * idi)1074 int ndo2db_handle_eventhandlerdata(ndo2db_idi *idi){
1075 	int type,flags,attr;
1076 	struct timeval tstamp;
1077 	char *ts[2];
1078 	char *es[4];
1079 	int x=0;
1080 	int eventhandler_type=0;
1081 	int state=0;
1082 	int state_type=0;
1083 	struct timeval start_time;
1084 	struct timeval end_time;
1085 	int timeout=0;
1086 	int early_timeout=0;
1087 	double execution_time=0.0;
1088 	int return_code=0;
1089 	unsigned long object_id=0L;
1090 	unsigned long command_id=0L;
1091 	char *buf=NULL;
1092 	char *buf1=NULL;
1093 	int result=NDO_OK;
1094 
1095 	if(idi==NULL)
1096 		return NDO_ERROR;
1097 
1098 	/* convert timestamp, etc */
1099 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1100 
1101 	/* covert vars */
1102 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EVENTHANDLERTYPE],&eventhandler_type);
1103 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATE],&state);
1104 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATETYPE],&state_type);
1105 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_TIMEOUT],&timeout);
1106 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EARLYTIMEOUT],&early_timeout);
1107 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETURNCODE],&return_code);
1108 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_EXECUTIONTIME],&execution_time);
1109 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1110 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1111 
1112 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDARGS]);
1113 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDLINE]);
1114 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
1115 	es[3]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
1116 
1117 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1118 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1119 
1120 	/* get the object id */
1121 	if(eventhandler_type==SERVICE_EVENTHANDLER || eventhandler_type==GLOBAL_SERVICE_EVENTHANDLER)
1122 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
1123 	else
1124 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
1125 
1126 	/* get the command id */
1127 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,idi->buffered_input[NDO_DATA_COMMANDNAME],NULL,&command_id);
1128 
1129 	/* save entry to db */
1130 	if(asprintf(&buf,"instance_id='%lu', eventhandler_type='%d', object_id='%lu', state='%d', state_type='%d', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', command_object_id='%lu', command_args='%s', command_line='%s', timeout='%d', early_timeout='%d', execution_time='%lf', return_code='%d', output='%s', long_output='%s'"
1131 		    ,idi->dbinfo.instance_id
1132 		    ,eventhandler_type
1133 		    ,object_id
1134 		    ,state
1135 		    ,state_type
1136 		    ,ts[0]
1137 		    ,start_time.tv_usec
1138 		    ,ts[1]
1139 		    ,end_time.tv_usec
1140 		    ,command_id
1141 		    ,es[0]
1142 		    ,es[1]
1143 		    ,timeout
1144 		    ,early_timeout
1145 		    ,execution_time
1146 		    ,return_code
1147 		    ,es[2]
1148 		    ,es[3]
1149 		   )==-1)
1150 		buf=NULL;
1151 
1152 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1153 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_EVENTHANDLERS]
1154 		    ,buf
1155 		    ,buf
1156 		   )==-1)
1157 		buf1=NULL;
1158 
1159 	result=ndo2db_db_query(idi,buf1);
1160 	free(buf);
1161 	free(buf1);
1162 
1163         /* free memory */
1164 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1165 		free(ts[x]);
1166 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1167 		free(es[x]);
1168 
1169 	return NDO_OK;
1170         }
1171 
1172 
ndo2db_handle_notificationdata(ndo2db_idi * idi)1173 int ndo2db_handle_notificationdata(ndo2db_idi *idi){
1174 	int type,flags,attr;
1175 	struct timeval tstamp;
1176 	int notification_type=0;
1177 	int notification_reason=0;
1178 	unsigned long object_id=0L;
1179 	struct timeval start_time;
1180 	struct timeval end_time;
1181 	int state=0;
1182 	int escalated=0;
1183 	int contacts_notified=0;
1184 	int result=NDO_OK;
1185 	char *ts[2];
1186 	char *es[2];
1187 	int x=0;
1188 	char *buf=NULL;
1189 	char *buf1=NULL;
1190 
1191 	if(idi==NULL)
1192 		return NDO_ERROR;
1193 
1194 	/* convert timestamp, etc */
1195 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1196 
1197 	/* convert vars */
1198 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFICATIONTYPE],&notification_type);
1199 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFICATIONREASON],&notification_reason);
1200 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATE],&state);
1201 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATED],&escalated);
1202 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CONTACTSNOTIFIED],&contacts_notified);
1203 
1204 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1205 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1206 
1207 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
1208 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
1209 
1210 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1211 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1212 
1213 	/* get the object id */
1214 	if(notification_type==SERVICE_NOTIFICATION)
1215 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
1216 	if(notification_type==HOST_NOTIFICATION)
1217 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
1218 
1219 	/* save entry to db */
1220 	if(asprintf(&buf,"instance_id='%lu', notification_type='%d', notification_reason='%d', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', object_id='%lu', state='%d', output='%s', long_output='%s', escalated='%d', contacts_notified='%d'"
1221 		    ,idi->dbinfo.instance_id
1222 		    ,notification_type
1223 		    ,notification_reason
1224 		    ,ts[0]
1225 		    ,start_time.tv_usec
1226 		    ,ts[1]
1227 		    ,end_time.tv_usec
1228 		    ,object_id
1229 		    ,state
1230 		    ,es[0]
1231 		    ,es[1]
1232 		    ,escalated
1233 		    ,contacts_notified
1234 		   )==-1)
1235 		buf=NULL;
1236 
1237 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1238 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_NOTIFICATIONS]
1239 		    ,buf
1240 		    ,buf
1241 		   )==-1)
1242 		buf1=NULL;
1243 
1244 	/* run the query */
1245 	result=ndo2db_db_query(idi,buf1);
1246 
1247 	/* save the notification id for later use... */
1248 	if(type==NEBTYPE_NOTIFICATION_START)
1249 		idi->dbinfo.last_notification_id=0L;
1250 	if(result==NDO_OK && type==NEBTYPE_NOTIFICATION_START){
1251 		idi->dbinfo.last_notification_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
1252 	}
1253 	free(buf);
1254 	free(buf1);
1255 
1256         /* free memory */
1257 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1258 		free(ts[x]);
1259 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1260 		free(es[x]);
1261 
1262 	return NDO_OK;
1263         }
1264 
1265 
ndo2db_handle_contactnotificationdata(ndo2db_idi * idi)1266 int ndo2db_handle_contactnotificationdata(ndo2db_idi *idi){
1267 	int type,flags,attr;
1268 	struct timeval tstamp;
1269 	unsigned long contact_id=0L;
1270 	struct timeval start_time;
1271 	struct timeval end_time;
1272 	int result=NDO_OK;
1273 	char *ts[2];
1274 	int x=0;
1275 	char *buf=NULL;
1276 	char *buf1=NULL;
1277 
1278 	if(idi==NULL)
1279 		return NDO_ERROR;
1280 
1281 	/* convert timestamp, etc */
1282 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1283 
1284 	/* convert vars */
1285 
1286 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1287 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1288 
1289 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1290 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1291 
1292 	/* get the contact id */
1293 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,idi->buffered_input[NDO_DATA_CONTACTNAME],NULL,&contact_id);
1294 
1295 	/* save entry to db */
1296 	if(asprintf(&buf,"instance_id='%lu', notification_id='%lu', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', contact_object_id='%lu'"
1297 		    ,idi->dbinfo.instance_id
1298 		    ,idi->dbinfo.last_notification_id
1299 		    ,ts[0]
1300 		    ,start_time.tv_usec
1301 		    ,ts[1]
1302 		    ,end_time.tv_usec
1303 		    ,contact_id
1304 		   )==-1)
1305 		buf=NULL;
1306 
1307 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1308 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONS]
1309 		    ,buf
1310 		    ,buf
1311 		   )==-1)
1312 		buf1=NULL;
1313 
1314 	/* run the query */
1315 	result=ndo2db_db_query(idi,buf1);
1316 
1317 	/* save the contact notification id for later use... */
1318 	if(type==NEBTYPE_CONTACTNOTIFICATION_START)
1319 		idi->dbinfo.last_contact_notification_id=0L;
1320 	if(result==NDO_OK && type==NEBTYPE_CONTACTNOTIFICATION_START){
1321 		idi->dbinfo.last_contact_notification_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
1322 	}
1323 	free(buf);
1324 	free(buf1);
1325 
1326         /* free memory */
1327 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1328 		free(ts[x]);
1329 
1330 	return NDO_OK;
1331         }
1332 
1333 
ndo2db_handle_contactnotificationmethoddata(ndo2db_idi * idi)1334 int ndo2db_handle_contactnotificationmethoddata(ndo2db_idi *idi){
1335 	int type,flags,attr;
1336 	struct timeval tstamp;
1337 	unsigned long command_id=0L;
1338 	struct timeval start_time;
1339 	struct timeval end_time;
1340 	int result=NDO_OK;
1341 	char *ts[2];
1342 	char *es[1];
1343 	int x=0;
1344 	char *buf=NULL;
1345 	char *buf1=NULL;
1346 
1347 	if(idi==NULL)
1348 		return NDO_ERROR;
1349 
1350 	/* convert timestamp, etc */
1351 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1352 
1353 	/* convert vars */
1354 
1355 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1356 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1357 
1358 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDARGS]);
1359 
1360 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1361 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1362 
1363 
1364 	/* get the command id */
1365 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,idi->buffered_input[NDO_DATA_COMMANDNAME],NULL,&command_id);
1366 
1367 	/* save entry to db */
1368 	if(asprintf(&buf,"instance_id='%lu', contactnotification_id='%lu', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', command_object_id='%lu', command_args='%s'"
1369 		    ,idi->dbinfo.instance_id
1370 		    ,idi->dbinfo.last_contact_notification_id
1371 		    ,ts[0]
1372 		    ,start_time.tv_usec
1373 		    ,ts[1]
1374 		    ,end_time.tv_usec
1375 		    ,command_id
1376 		    ,es[0]
1377 		   )==-1)
1378 		buf=NULL;
1379 
1380 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1381 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONMETHODS]
1382 		    ,buf
1383 		    ,buf
1384 		   )==-1)
1385 		buf1=NULL;
1386 
1387 	/* run the query */
1388 	result=ndo2db_db_query(idi,buf1);
1389 	free(buf);
1390 	free(buf1);
1391 
1392         /* free memory */
1393 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1394 		free(ts[x]);
1395 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1396 		free(es[x]);
1397 
1398 	return NDO_OK;
1399         }
1400 
1401 
ndo2db_handle_servicecheckdata(ndo2db_idi * idi)1402 int ndo2db_handle_servicecheckdata(ndo2db_idi *idi){
1403 	int type,flags,attr;
1404 	struct timeval tstamp;
1405 	char *ts[2];
1406 	char *es[5];
1407 	int check_type=0;
1408 	struct timeval start_time;
1409 	struct timeval end_time;
1410 	int current_check_attempt=0;
1411 	int max_check_attempts=0;
1412 	int state=0;
1413 	int state_type=0;
1414 	int timeout=0;
1415 	int early_timeout=0;
1416 	double execution_time=0.0;
1417 	double latency=0.0;
1418 	int return_code=0;
1419 	unsigned long object_id=0L;
1420 	unsigned long command_id=0L;
1421 	char *buf=NULL;
1422 	char *buf1=NULL;
1423 	int x=0;
1424 	int result=NDO_OK;
1425 
1426 	if(idi==NULL)
1427 		return NDO_ERROR;
1428 
1429 	/* convert timestamp, etc */
1430 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1431 
1432 	/* only process some types of service checks... */
1433 	if(type!=NEBTYPE_SERVICECHECK_INITIATE && type!=NEBTYPE_SERVICECHECK_PROCESSED)
1434 		return NDO_OK;
1435 
1436 #if ( defined( BUILD_NAGIOS_3X) || defined( BUILD_NAGIOS_4X))
1437 	/* skip precheck events - they aren't useful to us */
1438 	if(type==NEBTYPE_SERVICECHECK_ASYNC_PRECHECK)
1439 		return NDO_OK;
1440 #endif
1441 
1442 	/* covert vars */
1443 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CHECKTYPE],&check_type);
1444 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTCHECKATTEMPT],&current_check_attempt);
1445 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MAXCHECKATTEMPTS],&max_check_attempts);
1446 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATE],&state);
1447 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATETYPE],&state_type);
1448 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_TIMEOUT],&timeout);
1449 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EARLYTIMEOUT],&early_timeout);
1450 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETURNCODE],&return_code);
1451 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_EXECUTIONTIME],&execution_time);
1452 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LATENCY],&latency);
1453 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1454 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1455 
1456 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDARGS]);
1457 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDLINE]);
1458 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
1459 	es[3]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
1460 	es[4]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PERFDATA]);
1461 
1462 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1463 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1464 
1465 	/* get the object id */
1466 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
1467 
1468 	/* get the command id */
1469 	if(idi->buffered_input[NDO_DATA_COMMANDNAME]!=NULL && strcmp(idi->buffered_input[NDO_DATA_COMMANDNAME],""))
1470 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,idi->buffered_input[NDO_DATA_COMMANDNAME],NULL,&command_id);
1471 	else
1472 		command_id=0L;
1473 
1474 	/* save entry to db */
1475 	if(asprintf(&buf1,"instance_id='%lu', service_object_id='%lu', check_type='%d', current_check_attempt='%d', max_check_attempts='%d', state='%d', state_type='%d', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', timeout='%d', early_timeout='%d', execution_time='%lf', latency='%lf', return_code='%d', output='%s', long_output='%s', perfdata='%s'"
1476 		    ,idi->dbinfo.instance_id
1477 		    ,object_id
1478 		    ,check_type
1479 		    ,current_check_attempt
1480 		    ,max_check_attempts
1481 		    ,state
1482 		    ,state_type
1483 		    ,ts[0]
1484 		    ,start_time.tv_usec
1485 		    ,ts[1]
1486 		    ,end_time.tv_usec
1487 		    ,timeout
1488 		    ,early_timeout
1489 		    ,execution_time
1490 		    ,latency
1491 		    ,return_code
1492 		    ,es[2]
1493 		    ,es[3]
1494 		    ,es[4]
1495 		   )==-1)
1496 		buf1=NULL;
1497 
1498 	if(asprintf(&buf,"INSERT INTO %s SET %s, command_object_id='%lu', command_args='%s', command_line='%s' ON DUPLICATE KEY UPDATE %s"
1499 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICECHECKS]
1500 		    ,buf1
1501 		    ,command_id
1502 		    ,es[0]
1503 		    ,es[1]
1504 		    ,buf1
1505 		   )==-1)
1506 		buf=NULL;
1507 
1508 	result=ndo2db_db_query(idi,buf);
1509 	free(buf);
1510 	free(buf1);
1511 
1512         /* free memory */
1513 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1514 		free(ts[x]);
1515 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1516 		free(es[x]);
1517 
1518 	return NDO_OK;
1519         }
1520 
1521 
ndo2db_handle_hostcheckdata(ndo2db_idi * idi)1522 int ndo2db_handle_hostcheckdata(ndo2db_idi *idi){
1523 	int type,flags,attr;
1524 	struct timeval tstamp;
1525 	char *ts[2];
1526 	char *es[5];
1527 	int check_type=0;
1528 	int is_raw_check=0;
1529 	struct timeval start_time;
1530 	struct timeval end_time;
1531 	int current_check_attempt=0;
1532 	int max_check_attempts=0;
1533 	int state=0;
1534 	int state_type=0;
1535 	int timeout=0;
1536 	int early_timeout=0;
1537 	double execution_time=0.0;
1538 	double latency=0.0;
1539 	int return_code=0;
1540 	unsigned long object_id=0L;
1541 	unsigned long command_id=0L;
1542 	char *buf=NULL;
1543 	char *buf1=NULL;
1544 	int x=0;
1545 	int result=NDO_OK;
1546 
1547 	if(idi==NULL)
1548 		return NDO_ERROR;
1549 
1550 	/* convert timestamp, etc */
1551 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1552 
1553 	/* only process finished host checks... */
1554 	/*
1555 	if(type!=NEBTYPE_HOSTCHECK_PROCESSED)
1556 		return NDO_OK;
1557 	*/
1558 
1559 #if ( defined( BUILD_NAGIOS_3X) || defined( BUILD_NAGIOS_4X))
1560 	/* skip precheck events - they aren't useful to us */
1561 	if(type==NEBTYPE_HOSTCHECK_ASYNC_PRECHECK || type==NEBTYPE_HOSTCHECK_SYNC_PRECHECK)
1562 		return NDO_OK;
1563 #endif
1564 
1565 	/* covert vars */
1566 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CHECKTYPE],&check_type);
1567 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTCHECKATTEMPT],&current_check_attempt);
1568 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MAXCHECKATTEMPTS],&max_check_attempts);
1569 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATE],&state);
1570 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATETYPE],&state_type);
1571 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_TIMEOUT],&timeout);
1572 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EARLYTIMEOUT],&early_timeout);
1573 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETURNCODE],&return_code);
1574 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_EXECUTIONTIME],&execution_time);
1575 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LATENCY],&latency);
1576 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1577 	result=ndo2db_convert_string_to_timeval(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1578 
1579 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDARGS]);
1580 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDLINE]);
1581 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
1582 	es[3]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
1583 	es[4]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PERFDATA]);
1584 
1585 	ts[0]=ndo2db_db_timet_to_sql(idi,start_time.tv_sec);
1586 	ts[1]=ndo2db_db_timet_to_sql(idi,end_time.tv_sec);
1587 
1588 	/* get the object id */
1589 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
1590 
1591 	/* get the command id */
1592 	if(idi->buffered_input[NDO_DATA_COMMANDNAME]!=NULL && strcmp(idi->buffered_input[NDO_DATA_COMMANDNAME],""))
1593 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,idi->buffered_input[NDO_DATA_COMMANDNAME],NULL,&command_id);
1594 	else
1595 		command_id=0L;
1596 
1597 	/* is this a raw check? */
1598 	if(type==NEBTYPE_HOSTCHECK_RAW_START || type==NEBTYPE_HOSTCHECK_RAW_END)
1599 		is_raw_check=1;
1600 	else
1601 		is_raw_check=0;
1602 
1603 	/* save entry to db */
1604 	if(asprintf(&buf1,"instance_id='%lu', host_object_id='%lu', check_type='%d', is_raw_check='%d', current_check_attempt='%d', max_check_attempts='%d', state='%d', state_type='%d', start_time=%s, start_time_usec='%lu', end_time=%s, end_time_usec='%lu', timeout='%d', early_timeout='%d', execution_time='%lf', latency='%lf', return_code='%d', output='%s', long_output='%s', perfdata='%s'"
1605 		    ,idi->dbinfo.instance_id
1606 		    ,object_id
1607 		    ,check_type
1608 		    ,is_raw_check
1609 		    ,current_check_attempt
1610 		    ,max_check_attempts
1611 		    ,state
1612 		    ,state_type
1613 		    ,ts[0]
1614 		    ,start_time.tv_usec
1615 		    ,ts[1]
1616 		    ,end_time.tv_usec
1617 		    ,timeout
1618 		    ,early_timeout
1619 		    ,execution_time
1620 		    ,latency
1621 		    ,return_code
1622 		    ,es[2]
1623 		    ,es[3]
1624 		    ,es[4]
1625 		   )==-1)
1626 		buf1=NULL;
1627 
1628 	if(asprintf(&buf,"INSERT INTO %s SET %s, command_object_id='%lu', command_args='%s', command_line='%s' ON DUPLICATE KEY UPDATE %s"
1629 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTCHECKS]
1630 		    ,buf1
1631 		    ,command_id
1632 		    ,es[0]
1633 		    ,es[1]
1634 		    ,buf1
1635 		   )==-1)
1636 		buf=NULL;
1637 
1638 	result=ndo2db_db_query(idi,buf);
1639 	free(buf);
1640 	free(buf1);
1641 
1642         /* free memory */
1643 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1644 		free(ts[x]);
1645 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1646 		free(es[x]);
1647 
1648 	return NDO_OK;
1649         }
1650 
1651 
ndo2db_handle_commentdata(ndo2db_idi * idi)1652 int ndo2db_handle_commentdata(ndo2db_idi *idi){
1653 	int type,flags,attr;
1654 	struct timeval tstamp;
1655 	int comment_type=0;
1656 	int entry_type=0;
1657 	unsigned long object_id=0L;
1658 	unsigned long comment_time=0L;
1659 	unsigned long internal_comment_id=0L;
1660 	int is_persistent=0;
1661 	int comment_source=0;
1662 	int expires=0;
1663 	unsigned long expire_time=0L;
1664 	int result=NDO_OK;
1665 	char *ts[3];
1666 	char *es[2];
1667 	int x=0;
1668 	char *buf=NULL;
1669 	char *buf1=NULL;
1670 
1671 	if(idi==NULL)
1672 		return NDO_ERROR;
1673 
1674 	/* convert timestamp, etc */
1675 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1676 
1677 	/* convert vars */
1678 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_COMMENTTYPE],&comment_type);
1679 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ENTRYTYPE],&entry_type);
1680 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PERSISTENT],&is_persistent);
1681 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SOURCE],&comment_source);
1682 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EXPIRES],&expires);
1683 
1684 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_COMMENTID],&internal_comment_id);
1685 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_ENTRYTIME],&comment_time);
1686 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_EXPIRATIONTIME],&expire_time);
1687 
1688 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_AUTHORNAME]);
1689 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMENT]);
1690 
1691 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
1692 	ts[1]=ndo2db_db_timet_to_sql(idi,comment_time);
1693 	ts[2]=ndo2db_db_timet_to_sql(idi,expire_time);
1694 
1695 	/* get the object id */
1696 	if(comment_type==SERVICE_COMMENT)
1697 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
1698 	if(comment_type==HOST_COMMENT)
1699 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
1700 
1701 	/* ADD HISTORICAL COMMENTS */
1702 	/* save a record of comments that get added (or get loaded and weren't previously recorded).... */
1703 	if(type==NEBTYPE_COMMENT_ADD || type==NEBTYPE_COMMENT_LOAD){
1704 
1705 		/* save entry to db */
1706 		if(asprintf(&buf,"instance_id='%lu', comment_type='%d', entry_type='%d', object_id='%lu', comment_time=%s, internal_comment_id='%lu', author_name='%s', comment_data='%s', is_persistent='%d', comment_source='%d', expires='%d', expiration_time=%s"
1707 			    ,idi->dbinfo.instance_id
1708 			    ,comment_type
1709 			    ,entry_type
1710 			    ,object_id
1711 			    ,ts[1]
1712 			    ,internal_comment_id
1713 			    ,es[0]
1714 			    ,es[1]
1715 			    ,is_persistent
1716 			    ,comment_source
1717 			    ,expires
1718 			    ,ts[2]
1719 			   )==-1)
1720 			buf=NULL;
1721 
1722 		if(asprintf(&buf1,"INSERT INTO %s SET %s, entry_time=%s, entry_time_usec='%lu' ON DUPLICATE KEY UPDATE %s"
1723 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTHISTORY]
1724 			    ,buf
1725 			    ,ts[0]
1726 			    ,tstamp.tv_usec
1727 			    ,buf
1728 			   )==-1)
1729 			buf1=NULL;
1730 
1731 		result=ndo2db_db_query(idi,buf1);
1732 		free(buf);
1733 		free(buf1);
1734 	        }
1735 
1736 	/* UPDATE HISTORICAL COMMENTS */
1737 	/* mark records that have been deleted */
1738 	if(type==NEBTYPE_COMMENT_DELETE){
1739 
1740 		/* update db entry */
1741 		if(asprintf(&buf,"UPDATE %s SET deletion_time=%s, deletion_time_usec='%lu' WHERE instance_id='%lu' AND comment_time=%s AND internal_comment_id='%lu'"
1742 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTHISTORY]
1743 			    ,ts[0]
1744 			    ,tstamp.tv_usec
1745 			    ,idi->dbinfo.instance_id
1746 			    ,ts[1]
1747 			    ,internal_comment_id
1748 			   )==-1)
1749 			buf=NULL;
1750 		result=ndo2db_db_query(idi,buf);
1751 		free(buf);
1752 	        }
1753 
1754 	/* ADD CURRENT COMMENTS */
1755 	if((type==NEBTYPE_COMMENT_ADD || type==NEBTYPE_COMMENT_LOAD) && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
1756 
1757 		/* save entry to db */
1758 		if(asprintf(&buf,"instance_id='%lu', comment_type='%d', entry_type='%d', object_id='%lu', comment_time=%s, internal_comment_id='%lu', author_name='%s', comment_data='%s', is_persistent='%d', comment_source='%d', expires='%d', expiration_time=%s"
1759 			    ,idi->dbinfo.instance_id
1760 			    ,comment_type
1761 			    ,entry_type
1762 			    ,object_id
1763 			    ,ts[1]
1764 			    ,internal_comment_id
1765 			    ,es[0]
1766 			    ,es[1]
1767 			    ,is_persistent
1768 			    ,comment_source
1769 			    ,expires
1770 			    ,ts[2]
1771 			   )==-1)
1772 			buf=NULL;
1773 
1774 		if(asprintf(&buf1,"INSERT INTO %s SET %s, entry_time=%s, entry_time_usec='%lu' ON DUPLICATE KEY UPDATE %s"
1775 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTS]
1776 			    ,buf
1777 			    ,ts[0]
1778 			    ,tstamp.tv_usec
1779 			    ,buf
1780 			   )==-1)
1781 			buf1=NULL;
1782 
1783 		result=ndo2db_db_query(idi,buf1);
1784 		free(buf);
1785 		free(buf1);
1786 	        }
1787 
1788 	/* REMOVE CURRENT COMMENTS */
1789 	if(type==NEBTYPE_COMMENT_DELETE  && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
1790 
1791 		/* clear entry from db */
1792 		if(asprintf(&buf,"DELETE FROM %s WHERE instance_id='%lu' AND comment_time=%s AND internal_comment_id='%lu'"
1793 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMENTS]
1794 			    ,idi->dbinfo.instance_id
1795 			    ,ts[1]
1796 			    ,internal_comment_id
1797 			   )==-1)
1798 			buf=NULL;
1799 		result=ndo2db_db_query(idi,buf);
1800 		free(buf);
1801 	        }
1802 
1803         /* free memory */
1804 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
1805 		free(ts[x]);
1806 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
1807 		free(es[x]);
1808 
1809 	return NDO_OK;
1810         }
1811 
1812 
ndo2db_handle_downtimedata(ndo2db_idi * idi)1813 int ndo2db_handle_downtimedata(ndo2db_idi *idi){
1814 	int type,flags,attr;
1815 	struct timeval tstamp;
1816 	int downtime_type=0;
1817 	int fixed=0;
1818 	unsigned long duration=0L;
1819 	unsigned long internal_downtime_id=0L;
1820 	unsigned long triggered_by=0L;
1821 	unsigned long entry_time=0L;
1822 	unsigned long start_time=0L;
1823 	unsigned long end_time=0L;
1824 	unsigned long object_id=0L;
1825 	int result=NDO_OK;
1826 	char *ts[4];
1827 	char *es[2];
1828 	int x=0;
1829 	char *buf=NULL;
1830 	char *buf1=NULL;
1831 
1832 	if(idi==NULL)
1833 		return NDO_ERROR;
1834 
1835 	/* convert timestamp, etc */
1836 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
1837 
1838 	/* convert vars */
1839 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_DOWNTIMETYPE],&downtime_type);
1840 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FIXED],&fixed);
1841 
1842 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_DURATION],&duration);
1843 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_DOWNTIMEID],&internal_downtime_id);
1844 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_TRIGGEREDBY],&triggered_by);
1845 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_ENTRYTIME],&entry_time);
1846 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_STARTTIME],&start_time);
1847 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_ENDTIME],&end_time);
1848 
1849 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_AUTHORNAME]);
1850 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMENT]);
1851 
1852 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
1853 	ts[1]=ndo2db_db_timet_to_sql(idi,entry_time);
1854 	ts[2]=ndo2db_db_timet_to_sql(idi,start_time);
1855 	ts[3]=ndo2db_db_timet_to_sql(idi,end_time);
1856 
1857 	/* get the object id */
1858 	if(downtime_type==SERVICE_DOWNTIME)
1859 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
1860 	if(downtime_type==HOST_DOWNTIME)
1861 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
1862 
1863 	/* HISTORICAL DOWNTIME */
1864 
1865 	/* save a record of scheduled downtime that gets added (or gets loaded and wasn't previously recorded).... */
1866 	if(type==NEBTYPE_DOWNTIME_ADD || type==NEBTYPE_DOWNTIME_LOAD){
1867 
1868 		/* save entry to db */
1869 		if(asprintf(&buf,"instance_id='%lu', downtime_type='%d', object_id='%lu', entry_time=%s, author_name='%s', comment_data='%s', internal_downtime_id='%lu', triggered_by_id='%lu', is_fixed='%d', duration='%lu', scheduled_start_time=%s, scheduled_end_time=%s"
1870 			    ,idi->dbinfo.instance_id
1871 			    ,downtime_type
1872 			    ,object_id
1873 			    ,ts[1]
1874 			    ,es[0]
1875 			    ,es[1]
1876 			    ,internal_downtime_id
1877 			    ,triggered_by
1878 			    ,fixed
1879 			    ,duration
1880 			    ,ts[2]
1881 			    ,ts[3]
1882 			   )==-1)
1883 			buf=NULL;
1884 
1885 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1886 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_DOWNTIMEHISTORY]
1887 			    ,buf
1888 			    ,buf
1889 			   )==-1)
1890 			buf1=NULL;
1891 
1892 		result=ndo2db_db_query(idi,buf1);
1893 		free(buf);
1894 		free(buf1);
1895 	        }
1896 
1897 	/* save a record of scheduled downtime that starts */
1898 	if(type==NEBTYPE_DOWNTIME_START){
1899 
1900 		/* save entry to db */
1901 		if(asprintf(&buf,"UPDATE %s SET actual_start_time=%s, actual_start_time_usec='%lu', was_started='%d' WHERE instance_id='%lu' AND downtime_type='%d' AND object_id='%lu' AND entry_time=%s AND scheduled_start_time=%s AND scheduled_end_time=%s"
1902 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_DOWNTIMEHISTORY]
1903 			    ,ts[0]
1904 			    ,tstamp.tv_usec
1905 			    ,1
1906 			    ,idi->dbinfo.instance_id
1907 			    ,downtime_type
1908 			    ,object_id
1909 			    ,ts[1]
1910 			    ,ts[2]
1911 			    ,ts[3]
1912 			   )==-1)
1913 			buf=NULL;
1914 
1915 		result=ndo2db_db_query(idi,buf);
1916 		free(buf);
1917 	        }
1918 
1919 	/* save a record of scheduled downtime that ends */
1920 	if(type==NEBTYPE_DOWNTIME_STOP){
1921 
1922 		/* save entry to db */
1923 		if(asprintf(&buf,"UPDATE %s SET actual_end_time=%s, actual_end_time_usec='%lu', was_cancelled='%d' WHERE instance_id='%lu' AND downtime_type='%d' AND object_id='%lu' AND entry_time=%s AND scheduled_start_time=%s AND scheduled_end_time=%s"
1924 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_DOWNTIMEHISTORY]
1925 			    ,ts[0]
1926 			    ,tstamp.tv_usec
1927 			    ,(attr==NEBATTR_DOWNTIME_STOP_CANCELLED)?1:0
1928 			    ,idi->dbinfo.instance_id
1929 			    ,downtime_type
1930 			    ,object_id
1931 			    ,ts[1]
1932 			    ,ts[2]
1933 			    ,ts[3]
1934 			   )==-1)
1935 			buf=NULL;
1936 
1937 		result=ndo2db_db_query(idi,buf);
1938 		free(buf);
1939 	        }
1940 
1941 
1942 	/* CURRENT DOWNTIME */
1943 
1944 	/* save a record of scheduled downtime that gets added (or gets loaded and wasn't previously recorded).... */
1945 	if((type==NEBTYPE_DOWNTIME_ADD || type==NEBTYPE_DOWNTIME_LOAD) && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
1946 
1947 		/* save entry to db */
1948 		if(asprintf(&buf,"instance_id='%lu', downtime_type='%d', object_id='%lu', entry_time=%s, author_name='%s', comment_data='%s', internal_downtime_id='%lu', triggered_by_id='%lu', is_fixed='%d', duration='%lu', scheduled_start_time=%s, scheduled_end_time=%s"
1949 			    ,idi->dbinfo.instance_id
1950 			    ,downtime_type
1951 			    ,object_id
1952 			    ,ts[1]
1953 			    ,es[0]
1954 			    ,es[1]
1955 			    ,internal_downtime_id
1956 			    ,triggered_by
1957 			    ,fixed
1958 			    ,duration
1959 			    ,ts[2]
1960 			    ,ts[3]
1961 			   )==-1)
1962 			buf=NULL;
1963 
1964 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
1965 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SCHEDULEDDOWNTIME]
1966 			    ,buf
1967 			    ,buf
1968 			   )==-1)
1969 			buf1=NULL;
1970 
1971 		result=ndo2db_db_query(idi,buf1);
1972 		free(buf);
1973 		free(buf1);
1974 	        }
1975 
1976 	/* save a record of scheduled downtime that starts */
1977 	if(type==NEBTYPE_DOWNTIME_START && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
1978 
1979 		/* save entry to db */
1980 		if(asprintf(&buf,"UPDATE %s SET actual_start_time=%s, actual_start_time_usec='%lu', was_started='%d' WHERE instance_id='%lu' AND downtime_type='%d' AND object_id='%lu' AND entry_time=%s AND scheduled_start_time=%s AND scheduled_end_time=%s"
1981 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SCHEDULEDDOWNTIME]
1982 			    ,ts[0]
1983 			    ,tstamp.tv_usec
1984 			    ,1
1985 			    ,idi->dbinfo.instance_id
1986 			    ,downtime_type
1987 			    ,object_id
1988 			    ,ts[1]
1989 			    ,ts[2]
1990 			    ,ts[3]
1991 			   )==-1)
1992 			buf=NULL;
1993 
1994 		result=ndo2db_db_query(idi,buf);
1995 		free(buf);
1996 	        }
1997 
1998 	/* remove completed or deleted downtime */
1999 	if((type==NEBTYPE_DOWNTIME_STOP || type==NEBTYPE_DOWNTIME_DELETE) && tstamp.tv_sec>=idi->dbinfo.latest_realtime_data_time){
2000 
2001 		/* save entry to db */
2002 		if(asprintf(&buf,"DELETE FROM %s WHERE instance_id='%lu' AND downtime_type='%d' AND object_id='%lu' AND entry_time=%s AND scheduled_start_time=%s AND scheduled_end_time=%s"
2003 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SCHEDULEDDOWNTIME]
2004 			    ,idi->dbinfo.instance_id
2005 			    ,downtime_type
2006 			    ,object_id
2007 			    ,ts[1]
2008 			    ,ts[2]
2009 			    ,ts[3]
2010 			   )==-1)
2011 			buf=NULL;
2012 
2013 		result=ndo2db_db_query(idi,buf);
2014 		free(buf);
2015 	        }
2016 
2017         /* free memory */
2018 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2019 		free(ts[x]);
2020 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2021 		free(es[x]);
2022 
2023 	return NDO_OK;
2024         }
2025 
2026 
ndo2db_handle_flappingdata(ndo2db_idi * idi)2027 int ndo2db_handle_flappingdata(ndo2db_idi *idi){
2028 	int x = 0;
2029 	int type,flags,attr;
2030 	struct timeval tstamp;
2031 	int flapping_type=0;
2032 	unsigned long object_id=0L;
2033 	double percent_state_change=0.0;
2034 	double low_threshold=0.0;
2035 	double high_threshold=0.0;
2036 	unsigned long comment_time=0L;
2037 	unsigned long internal_comment_id=0L;
2038 	int result=NDO_OK;
2039 	char *ts[2];
2040 	char *buf=NULL;
2041 
2042 	if(idi==NULL)
2043 		return NDO_ERROR;
2044 
2045 	/* convert timestamp, etc */
2046 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2047 
2048 	/* convert vars */
2049 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPPINGTYPE],&flapping_type);
2050 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_PERCENTSTATECHANGE],&percent_state_change);
2051 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LOWTHRESHOLD],&low_threshold);
2052 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_HIGHTHRESHOLD],&high_threshold);
2053 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_COMMENTTIME],&comment_time);
2054 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_COMMENTID],&internal_comment_id);
2055 
2056 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2057 	ts[1]=ndo2db_db_timet_to_sql(idi,comment_time);
2058 
2059 	/* get the object id (if applicable) */
2060 	if(flapping_type==SERVICE_FLAPPING)
2061 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
2062 	if(flapping_type==HOST_FLAPPING)
2063 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
2064 
2065 	/* save entry to db */
2066 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', event_time=%s, event_time_usec='%lu', event_type='%d', reason_type='%d', flapping_type='%d', object_id='%lu', percent_state_change='%lf', low_threshold='%lf', high_threshold='%lf', comment_time=%s, internal_comment_id='%lu'"
2067 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_FLAPPINGHISTORY]
2068 		    ,idi->dbinfo.instance_id
2069 		    ,ts[0]
2070 		    ,tstamp.tv_usec
2071 		    ,type
2072 		    ,attr
2073 		    ,flapping_type
2074 		    ,object_id
2075 		    ,percent_state_change
2076 		    ,low_threshold
2077 		    ,high_threshold
2078 		    ,ts[1]
2079 		    ,internal_comment_id
2080 		   )==-1)
2081 		buf=NULL;
2082 	result=ndo2db_db_query(idi,buf);
2083 	free(buf);
2084 
2085         /* free memory */
2086 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2087 		free(ts[x]);
2088 
2089 	return NDO_OK;
2090         }
2091 
2092 
ndo2db_handle_programstatusdata(ndo2db_idi * idi)2093 int ndo2db_handle_programstatusdata(ndo2db_idi *idi){
2094 	int x = 0;
2095 	int type,flags,attr;
2096 	struct timeval tstamp;
2097 	unsigned long program_start_time=0L;
2098 	unsigned long process_id=0L;
2099 	int daemon_mode=0;
2100 	unsigned long last_command_check=0L;
2101 	unsigned long last_log_rotation=0L;
2102 	int notifications_enabled=0;
2103 	int active_service_checks_enabled=0;
2104 	int passive_service_checks_enabled=0;
2105 	int active_host_checks_enabled=0;
2106 	int passive_host_checks_enabled=0;
2107 	int event_handlers_enabled=0;
2108 	int flap_detection_enabled=0;
2109 	int failure_prediction_enabled=0;
2110 	int process_performance_data=0;
2111 	int obsess_over_hosts=0;
2112 	int obsess_over_services=0;
2113 	unsigned long modified_host_attributes=0L;
2114 	unsigned long modified_service_attributes=0L;
2115 	char *ts[4];
2116 	char *es[2];
2117 	char *buf=NULL;
2118 	char *buf1=NULL;
2119 	int result=NDO_OK;
2120 
2121 	if(idi==NULL)
2122 		return NDO_ERROR;
2123 
2124 	/* convert timestamp, etc */
2125 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2126 
2127 	/* don't store old data */
2128 	if(tstamp.tv_sec < idi->dbinfo.latest_realtime_data_time)
2129 		return NDO_OK;
2130 
2131 	/* covert vars */
2132 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_PROGRAMSTARTTIME],&program_start_time);
2133 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_PROCESSID],&process_id);
2134 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_DAEMONMODE],&daemon_mode);
2135 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTCOMMANDCHECK],&last_command_check);
2136 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTLOGROTATION],&last_log_rotation);
2137 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFICATIONSENABLED],&notifications_enabled);
2138 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVESERVICECHECKSENABLED],&active_service_checks_enabled);
2139 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PASSIVESERVICECHECKSENABLED],&passive_service_checks_enabled);
2140 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVEHOSTCHECKSENABLED],&active_host_checks_enabled);
2141 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PASSIVEHOSTCHECKSENABLED],&passive_host_checks_enabled);
2142 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EVENTHANDLERSENABLED],&event_handlers_enabled);
2143 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONENABLED],&flap_detection_enabled);
2144 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILUREPREDICTIONENABLED],&failure_prediction_enabled);
2145 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROCESSPERFORMANCEDATA],&process_performance_data);
2146 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_OBSESSOVERHOSTS],&obsess_over_hosts);
2147 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_OBSESSOVERSERVICES],&obsess_over_services);
2148 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDHOSTATTRIBUTES],&modified_host_attributes);
2149 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDSERVICEATTRIBUTES],&modified_service_attributes);
2150 
2151 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_GLOBALHOSTEVENTHANDLER]);
2152 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_GLOBALSERVICEEVENTHANDLER]);
2153 
2154 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2155 	ts[1]=ndo2db_db_timet_to_sql(idi,program_start_time);
2156 	ts[2]=ndo2db_db_timet_to_sql(idi,last_command_check);
2157 	ts[3]=ndo2db_db_timet_to_sql(idi,last_log_rotation);
2158 
2159 	/* generate query string */
2160 	if(asprintf(&buf1,"instance_id='%lu', status_update_time=%s, program_start_time=%s, is_currently_running='1', process_id='%lu', daemon_mode='%d', last_command_check=%s, last_log_rotation=%s, notifications_enabled='%d', active_service_checks_enabled='%d', passive_service_checks_enabled='%d', active_host_checks_enabled='%d', passive_host_checks_enabled='%d', event_handlers_enabled='%d', flap_detection_enabled='%d', failure_prediction_enabled='%d', process_performance_data='%d', obsess_over_hosts='%d', obsess_over_services='%d', modified_host_attributes='%lu', modified_service_attributes='%lu', global_host_event_handler='%s', global_service_event_handler='%s'"
2161 		    ,idi->dbinfo.instance_id
2162 		    ,ts[0]
2163 		    ,ts[1]
2164 		    ,process_id
2165 		    ,daemon_mode
2166 		    ,ts[2]
2167 		    ,ts[3]
2168 		    ,notifications_enabled
2169 		    ,active_service_checks_enabled
2170 		    ,passive_service_checks_enabled
2171 		    ,active_host_checks_enabled
2172 		    ,passive_host_checks_enabled
2173 		    ,event_handlers_enabled
2174 		    ,flap_detection_enabled
2175 		    ,failure_prediction_enabled
2176 		    ,process_performance_data
2177 		    ,obsess_over_hosts
2178 		    ,obsess_over_services
2179 		    ,modified_host_attributes
2180 		    ,modified_service_attributes
2181 		    ,es[0]
2182 		    ,es[1]
2183 		   )==-1)
2184 		buf1=NULL;
2185 
2186 	if(asprintf(&buf,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
2187 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_PROGRAMSTATUS]
2188 		    ,buf1
2189 		    ,buf1
2190 		   )==-1)
2191 		buf=NULL;
2192 
2193 	/* save entry to db */
2194 	result=ndo2db_db_query(idi,buf);
2195 	free(buf);
2196 	free(buf1);
2197 
2198         /* free memory */
2199 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2200 		free(ts[x]);
2201 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2202 		free(es[x]);
2203 
2204 	return NDO_OK;
2205         }
2206 
2207 
ndo2db_handle_hoststatusdata(ndo2db_idi * idi)2208 int ndo2db_handle_hoststatusdata(ndo2db_idi *idi){
2209 	int type,flags,attr;
2210 	struct timeval tstamp;
2211 	unsigned long last_check=0L;
2212 	unsigned long next_check=0L;
2213 	unsigned long last_state_change=0L;
2214 	unsigned long last_hard_state_change=0L;
2215 	unsigned long last_time_up=0L;
2216 	unsigned long last_time_down=0L;
2217 	unsigned long last_time_unreachable=0L;
2218 	unsigned long last_notification=0L;
2219 	unsigned long next_notification=0L;
2220 	unsigned long modified_host_attributes=0L;
2221 	double percent_state_change=0.0;
2222 	double latency=0.0;
2223 	double execution_time=0.0;
2224 	int current_state=0;
2225 	int has_been_checked=0;
2226 	int should_be_scheduled=0;
2227 	int current_check_attempt=0;
2228 	int max_check_attempts=0;
2229 	int check_type=0;
2230 	int last_hard_state=0;
2231 	int state_type=0;
2232 	int no_more_notifications=0;
2233 	int notifications_enabled=0;
2234 	int problem_has_been_acknowledged=0;
2235 	int acknowledgement_type=0;
2236 	int current_notification_number=0;
2237 	int passive_checks_enabled=0;
2238 	int active_checks_enabled=0;
2239 	int event_handler_enabled;
2240 	int flap_detection_enabled=0;
2241 	int is_flapping=0;
2242 	int scheduled_downtime_depth=0;
2243 	int failure_prediction_enabled=0;
2244 	int process_performance_data;
2245 	int obsess_over_host=0;
2246 	double normal_check_interval=0.0;
2247 	double retry_check_interval=0.0;
2248 	char *ts[10];
2249 	char *es[5];
2250 	char *buf=NULL;
2251 	char *buf1=NULL;
2252 	unsigned long object_id=0L;
2253 	unsigned long check_timeperiod_object_id=0L;
2254 	int x=0;
2255 	int result=NDO_OK;
2256 
2257 	if(idi==NULL)
2258 		return NDO_ERROR;
2259 
2260 	/* convert timestamp, etc */
2261 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2262 
2263 	/* don't store old data */
2264 	if(tstamp.tv_sec < idi->dbinfo.latest_realtime_data_time)
2265 		return NDO_OK;
2266 
2267 	/* covert vars */
2268 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTHOSTCHECK],&last_check);
2269 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_NEXTHOSTCHECK],&next_check);
2270 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTSTATECHANGE],&last_state_change);
2271 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTHARDSTATECHANGE],&last_hard_state_change);
2272 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMEUP],&last_time_up);
2273 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMEDOWN],&last_time_down);
2274 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMEUNREACHABLE],&last_time_unreachable);
2275 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTHOSTNOTIFICATION],&last_notification);
2276 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_NEXTHOSTNOTIFICATION],&next_notification);
2277 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDHOSTATTRIBUTES],&modified_host_attributes);
2278 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_PERCENTSTATECHANGE],&percent_state_change);
2279 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LATENCY],&latency);
2280 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_EXECUTIONTIME],&execution_time);
2281 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTSTATE],&current_state);
2282 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HASBEENCHECKED],&has_been_checked);
2283 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SHOULDBESCHEDULED],&should_be_scheduled);
2284 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTCHECKATTEMPT],&current_check_attempt);
2285 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MAXCHECKATTEMPTS],&max_check_attempts);
2286 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CHECKTYPE],&check_type);
2287 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_LASTHARDSTATE],&last_hard_state);
2288 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATETYPE],&state_type);
2289 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOMORENOTIFICATIONS],&no_more_notifications);
2290 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFICATIONSENABLED],&notifications_enabled);
2291 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROBLEMHASBEENACKNOWLEDGED],&problem_has_been_acknowledged);
2292 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACKNOWLEDGEMENTTYPE],&acknowledgement_type);
2293 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTNOTIFICATIONNUMBER],&current_notification_number);
2294 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PASSIVEHOSTCHECKSENABLED],&passive_checks_enabled);
2295 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVEHOSTCHECKSENABLED],&active_checks_enabled);
2296 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EVENTHANDLERENABLED],&event_handler_enabled);
2297 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONENABLED],&flap_detection_enabled);
2298 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ISFLAPPING],&is_flapping);
2299 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SCHEDULEDDOWNTIMEDEPTH],&scheduled_downtime_depth);
2300 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILUREPREDICTIONENABLED],&failure_prediction_enabled);
2301 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROCESSPERFORMANCEDATA],&process_performance_data);
2302 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_OBSESSOVERHOST],&obsess_over_host);
2303 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_NORMALCHECKINTERVAL],&normal_check_interval);
2304 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_RETRYCHECKINTERVAL],&retry_check_interval);
2305 
2306 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
2307 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
2308 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PERFDATA]);
2309 	es[3]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_EVENTHANDLER]);
2310 	es[4]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_CHECKCOMMAND]);
2311 
2312 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2313 	ts[1]=ndo2db_db_timet_to_sql(idi,last_check);
2314 	ts[2]=ndo2db_db_timet_to_sql(idi,next_check);
2315 	ts[3]=ndo2db_db_timet_to_sql(idi,last_state_change);
2316 	ts[4]=ndo2db_db_timet_to_sql(idi,last_hard_state_change);
2317 	ts[5]=ndo2db_db_timet_to_sql(idi,last_time_up);
2318 	ts[6]=ndo2db_db_timet_to_sql(idi,last_time_down);
2319 	ts[7]=ndo2db_db_timet_to_sql(idi,last_time_unreachable);
2320 	ts[8]=ndo2db_db_timet_to_sql(idi,last_notification);
2321 	ts[9]=ndo2db_db_timet_to_sql(idi,next_notification);
2322 
2323 	/* get the object id */
2324 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
2325 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_HOSTCHECKPERIOD],NULL,&check_timeperiod_object_id);
2326 
2327 	/* generate query string */
2328 	if(asprintf(&buf1,"instance_id='%lu', host_object_id='%lu', status_update_time=%s, output='%s', long_output='%s', perfdata='%s', current_state='%d', has_been_checked='%d', should_be_scheduled='%d', current_check_attempt='%d', max_check_attempts='%d', last_check=%s, next_check=%s, check_type='%d', last_state_change=%s, last_hard_state_change=%s, last_hard_state='%d', last_time_up=%s, last_time_down=%s, last_time_unreachable=%s, state_type='%d', last_notification=%s, next_notification=%s, no_more_notifications='%d', notifications_enabled='%d', problem_has_been_acknowledged='%d', acknowledgement_type='%d', current_notification_number='%d', passive_checks_enabled='%d', active_checks_enabled='%d', event_handler_enabled='%d', flap_detection_enabled='%d', is_flapping='%d', percent_state_change='%lf', latency='%lf', execution_time='%lf', scheduled_downtime_depth='%d', failure_prediction_enabled='%d', process_performance_data='%d', obsess_over_host='%d', modified_host_attributes='%lu', event_handler='%s', check_command='%s', normal_check_interval='%lf', retry_check_interval='%lf', check_timeperiod_object_id='%lu'"
2329 		    ,idi->dbinfo.instance_id
2330 		    ,object_id
2331 		    ,ts[0]
2332 		    ,es[0]
2333 		    ,es[1]
2334 		    ,es[2]
2335 		    ,current_state
2336 		    ,has_been_checked
2337 		    ,should_be_scheduled
2338 		    ,current_check_attempt
2339 		    ,max_check_attempts
2340 		    ,ts[1]
2341 		    ,ts[2]
2342 		    ,check_type
2343 		    ,ts[3]
2344 		    ,ts[4]
2345 		    ,last_hard_state
2346 		    ,ts[5]
2347 		    ,ts[6]
2348 		    ,ts[7]
2349 		    ,state_type
2350 		    ,ts[8]
2351 		    ,ts[9]
2352 		    ,no_more_notifications
2353 		    ,notifications_enabled
2354 		    ,problem_has_been_acknowledged
2355 		    ,acknowledgement_type
2356 		    ,current_notification_number
2357 		    ,passive_checks_enabled
2358 		    ,active_checks_enabled
2359 		    ,event_handler_enabled
2360 		    ,flap_detection_enabled
2361 		    ,is_flapping
2362 		    ,percent_state_change
2363 		    ,latency
2364 		    ,execution_time
2365 		    ,scheduled_downtime_depth
2366 		    ,failure_prediction_enabled
2367 		    ,process_performance_data
2368 		    ,obsess_over_host
2369 		    ,modified_host_attributes
2370 		    ,es[3]
2371 		    ,es[4]
2372 		    ,normal_check_interval
2373 		    ,retry_check_interval
2374 		    ,check_timeperiod_object_id
2375 		   )==-1)
2376 		buf1=NULL;
2377 
2378 	if(asprintf(&buf,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
2379 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTSTATUS]
2380 		    ,buf1
2381 		    ,buf1
2382 		   )==-1)
2383 		buf=NULL;
2384 
2385 	/* save entry to db */
2386 	result=ndo2db_db_query(idi,buf);
2387 	free(buf);
2388 	free(buf1);
2389 
2390         /* free memory */
2391 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2392 		free(es[x]);
2393 
2394 	/* save custom variables to db */
2395 	result=ndo2db_save_custom_variables(idi,NDO2DB_DBTABLE_CUSTOMVARIABLESTATUS,object_id,ts[0]);
2396 
2397 
2398         /* free memory */
2399 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2400 		free(ts[x]);
2401 
2402 	return NDO_OK;
2403         }
2404 
2405 
ndo2db_handle_servicestatusdata(ndo2db_idi * idi)2406 int ndo2db_handle_servicestatusdata(ndo2db_idi *idi){
2407 	int type,flags,attr;
2408 	struct timeval tstamp;
2409 	unsigned long last_check=0L;
2410 	unsigned long next_check=0L;
2411 	unsigned long last_state_change=0L;
2412 	unsigned long last_hard_state_change=0L;
2413 	unsigned long last_time_ok=0L;
2414 	unsigned long last_time_warning=0L;
2415 	unsigned long last_time_unknown=0L;
2416 	unsigned long last_time_critical=0L;
2417 	unsigned long last_notification=0L;
2418 	unsigned long next_notification=0L;
2419 	unsigned long modified_service_attributes=0L;
2420 	double percent_state_change=0.0;
2421 	double latency=0.0;
2422 	double execution_time=0.0;
2423 	int current_state=0;
2424 	int has_been_checked=0;
2425 	int should_be_scheduled=0;
2426 	int current_check_attempt=0;
2427 	int max_check_attempts=0;
2428 	int check_type=0;
2429 	int last_hard_state=0;
2430 	int state_type=0;
2431 	int no_more_notifications=0;
2432 	int notifications_enabled=0;
2433 	int problem_has_been_acknowledged=0;
2434 	int acknowledgement_type=0;
2435 	int current_notification_number=0;
2436 	int passive_checks_enabled=0;
2437 	int active_checks_enabled=0;
2438 	int event_handler_enabled;
2439 	int flap_detection_enabled=0;
2440 	int is_flapping=0;
2441 	int scheduled_downtime_depth=0;
2442 	int failure_prediction_enabled=0;
2443 	int process_performance_data;
2444 	int obsess_over_service=0;
2445 	double normal_check_interval=0.0;
2446 	double retry_check_interval=0.0;
2447 	char *ts[11];
2448 	char *es[5];
2449 	char *buf=NULL;
2450 	char *buf1=NULL;
2451 	unsigned long object_id=0L;
2452 	unsigned long check_timeperiod_object_id=0L;
2453 	int x=0;
2454 	int result=NDO_OK;
2455 
2456 	if(idi==NULL)
2457 		return NDO_ERROR;
2458 
2459 	/* convert timestamp, etc */
2460 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2461 
2462 	/* don't store old data */
2463 	if(tstamp.tv_sec < idi->dbinfo.latest_realtime_data_time)
2464 		return NDO_OK;
2465 
2466 	/* covert vars */
2467 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTSERVICECHECK],&last_check);
2468 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_NEXTSERVICECHECK],&next_check);
2469 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTSTATECHANGE],&last_state_change);
2470 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTHARDSTATECHANGE],&last_hard_state_change);
2471 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMEOK],&last_time_ok);
2472 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMEWARNING],&last_time_warning);
2473 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMEUNKNOWN],&last_time_unknown);
2474 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTTIMECRITICAL],&last_time_critical);
2475 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTSERVICENOTIFICATION],&last_notification);
2476 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_NEXTSERVICENOTIFICATION],&next_notification);
2477 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDSERVICEATTRIBUTES],&modified_service_attributes);
2478 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_PERCENTSTATECHANGE],&percent_state_change);
2479 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LATENCY],&latency);
2480 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_EXECUTIONTIME],&execution_time);
2481 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTSTATE],&current_state);
2482 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HASBEENCHECKED],&has_been_checked);
2483 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SHOULDBESCHEDULED],&should_be_scheduled);
2484 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTCHECKATTEMPT],&current_check_attempt);
2485 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MAXCHECKATTEMPTS],&max_check_attempts);
2486 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CHECKTYPE],&check_type);
2487 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_LASTHARDSTATE],&last_hard_state);
2488 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATETYPE],&state_type);
2489 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOMORENOTIFICATIONS],&no_more_notifications);
2490 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFICATIONSENABLED],&notifications_enabled);
2491 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROBLEMHASBEENACKNOWLEDGED],&problem_has_been_acknowledged);
2492 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACKNOWLEDGEMENTTYPE],&acknowledgement_type);
2493 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTNOTIFICATIONNUMBER],&current_notification_number);
2494 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PASSIVESERVICECHECKSENABLED],&passive_checks_enabled);
2495 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVESERVICECHECKSENABLED],&active_checks_enabled);
2496 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_EVENTHANDLERENABLED],&event_handler_enabled);
2497 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONENABLED],&flap_detection_enabled);
2498 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ISFLAPPING],&is_flapping);
2499 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SCHEDULEDDOWNTIMEDEPTH],&scheduled_downtime_depth);
2500 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILUREPREDICTIONENABLED],&failure_prediction_enabled);
2501 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROCESSPERFORMANCEDATA],&process_performance_data);
2502 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_OBSESSOVERSERVICE],&obsess_over_service);
2503 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_NORMALCHECKINTERVAL],&normal_check_interval);
2504 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_RETRYCHECKINTERVAL],&retry_check_interval);
2505 
2506 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
2507 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
2508 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PERFDATA]);
2509 	es[3]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_EVENTHANDLER]);
2510 	es[4]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_CHECKCOMMAND]);
2511 
2512 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2513 	ts[1]=ndo2db_db_timet_to_sql(idi,last_check);
2514 	ts[2]=ndo2db_db_timet_to_sql(idi,next_check);
2515 	ts[3]=ndo2db_db_timet_to_sql(idi,last_state_change);
2516 	ts[4]=ndo2db_db_timet_to_sql(idi,last_hard_state_change);
2517 	ts[5]=ndo2db_db_timet_to_sql(idi,last_time_ok);
2518 	ts[6]=ndo2db_db_timet_to_sql(idi,last_time_warning);
2519 	ts[7]=ndo2db_db_timet_to_sql(idi,last_time_unknown);
2520 	ts[8]=ndo2db_db_timet_to_sql(idi,last_time_critical);
2521 	ts[9]=ndo2db_db_timet_to_sql(idi,last_notification);
2522 	ts[10]=ndo2db_db_timet_to_sql(idi,next_notification);
2523 
2524 	/* get the object id */
2525 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
2526 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_SERVICECHECKPERIOD],NULL,&check_timeperiod_object_id);
2527 
2528 	/* generate query string */
2529 	if(asprintf(&buf1,"instance_id='%lu', service_object_id='%lu', status_update_time=%s, output='%s', long_output='%s', perfdata='%s', current_state='%d', has_been_checked='%d', should_be_scheduled='%d', current_check_attempt='%d', max_check_attempts='%d', last_check=%s, next_check=%s, check_type='%d', last_state_change=%s, last_hard_state_change=%s, last_hard_state='%d', last_time_ok=%s, last_time_warning=%s, last_time_unknown=%s, last_time_critical=%s, state_type='%d', last_notification=%s, next_notification=%s, no_more_notifications='%d', notifications_enabled='%d', problem_has_been_acknowledged='%d', acknowledgement_type='%d', current_notification_number='%d', passive_checks_enabled='%d', active_checks_enabled='%d', event_handler_enabled='%d', flap_detection_enabled='%d', is_flapping='%d', percent_state_change='%lf', latency='%lf', execution_time='%lf', scheduled_downtime_depth='%d', failure_prediction_enabled='%d', process_performance_data='%d', obsess_over_service='%d', modified_service_attributes='%lu', event_handler='%s', check_command='%s', normal_check_interval='%lf', retry_check_interval='%lf', check_timeperiod_object_id='%lu'"
2530 		    ,idi->dbinfo.instance_id
2531 		    ,object_id
2532 		    ,ts[0]
2533 		    ,es[0]
2534 		    ,es[1]
2535 		    ,es[2]
2536 		    ,current_state
2537 		    ,has_been_checked
2538 		    ,should_be_scheduled
2539 		    ,current_check_attempt
2540 		    ,max_check_attempts
2541 		    ,ts[1]
2542 		    ,ts[2]
2543 		    ,check_type
2544 		    ,ts[3]
2545 		    ,ts[4]
2546 		    ,last_hard_state
2547 		    ,ts[5]
2548 		    ,ts[6]
2549 		    ,ts[7]
2550 		    ,ts[8]
2551 		    ,state_type
2552 		    ,ts[9]
2553 		    ,ts[10]
2554 		    ,no_more_notifications
2555 		    ,notifications_enabled
2556 		    ,problem_has_been_acknowledged
2557 		    ,acknowledgement_type
2558 		    ,current_notification_number
2559 		    ,passive_checks_enabled
2560 		    ,active_checks_enabled
2561 		    ,event_handler_enabled
2562 		    ,flap_detection_enabled
2563 		    ,is_flapping
2564 		    ,percent_state_change
2565 		    ,latency
2566 		    ,execution_time
2567 		    ,scheduled_downtime_depth
2568 		    ,failure_prediction_enabled
2569 		    ,process_performance_data
2570 		    ,obsess_over_service
2571 		    ,modified_service_attributes
2572 		    ,es[3]
2573 		    ,es[4]
2574 		    ,normal_check_interval
2575 		    ,retry_check_interval
2576 		    ,check_timeperiod_object_id
2577 		   )==-1)
2578 		buf1=NULL;
2579 
2580 	if(asprintf(&buf,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
2581 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICESTATUS]
2582 		    ,buf1
2583 		    ,buf1
2584 		   )==-1)
2585 		buf=NULL;
2586 
2587 	/* save entry to db */
2588 	result=ndo2db_db_query(idi,buf);
2589 	free(buf);
2590 	free(buf1);
2591 
2592         /* free memory */
2593 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2594 		free(es[x]);
2595 
2596 	/* save custom variables to db */
2597 	result=ndo2db_save_custom_variables(idi,NDO2DB_DBTABLE_CUSTOMVARIABLESTATUS,object_id,ts[0]);
2598 
2599         /* free memory */
2600 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2601 		free(ts[x]);
2602 
2603 	return NDO_OK;
2604         }
2605 
2606 
ndo2db_handle_contactstatusdata(ndo2db_idi * idi)2607 int ndo2db_handle_contactstatusdata(ndo2db_idi *idi){
2608 	int type,flags,attr;
2609 	struct timeval tstamp;
2610 	unsigned long last_host_notification=0L;
2611 	unsigned long last_service_notification=0L;
2612 	unsigned long modified_attributes=0L;
2613 	unsigned long modified_host_attributes=0L;
2614 	unsigned long modified_service_attributes=0L;
2615 	int host_notifications_enabled=0;
2616 	int service_notifications_enabled=0;
2617 	char *ts[3];
2618 	char *buf=NULL;
2619 	char *buf1=NULL;
2620 	unsigned long object_id=0L;
2621 	int x=0;
2622 	int result=NDO_OK;
2623 
2624 	if(idi==NULL)
2625 		return NDO_ERROR;
2626 
2627 	/* convert timestamp, etc */
2628 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2629 
2630 	/* don't store old data */
2631 	if(tstamp.tv_sec < idi->dbinfo.latest_realtime_data_time)
2632 		return NDO_OK;
2633 
2634 	/* covert vars */
2635 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTHOSTNOTIFICATION],&last_host_notification);
2636 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_LASTSERVICENOTIFICATION],&last_service_notification);
2637 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDCONTACTATTRIBUTES],&modified_attributes);
2638 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDHOSTATTRIBUTES],&modified_host_attributes);
2639 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_MODIFIEDSERVICEATTRIBUTES],&modified_service_attributes);
2640 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTNOTIFICATIONSENABLED],&host_notifications_enabled);
2641 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICENOTIFICATIONSENABLED],&service_notifications_enabled);
2642 
2643 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2644 	ts[1]=ndo2db_db_timet_to_sql(idi,last_host_notification);
2645 	ts[2]=ndo2db_db_timet_to_sql(idi,last_service_notification);
2646 
2647 	/* get the object id */
2648 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,idi->buffered_input[NDO_DATA_CONTACTNAME],NULL,&object_id);
2649 
2650 	/* generate query string */
2651 	if(asprintf(&buf1,"instance_id='%lu', contact_object_id='%lu', status_update_time=%s, host_notifications_enabled='%d', service_notifications_enabled='%d', last_host_notification=%s, last_service_notification=%s, modified_attributes='%lu', modified_host_attributes='%lu', modified_service_attributes='%lu'"
2652 		    ,idi->dbinfo.instance_id
2653 		    ,object_id
2654 		    ,ts[0]
2655 		    ,host_notifications_enabled
2656 		    ,service_notifications_enabled
2657 		    ,ts[1]
2658 		    ,ts[2]
2659 		    ,modified_attributes
2660 		    ,modified_host_attributes
2661 		    ,modified_service_attributes
2662 		   )==-1)
2663 		buf1=NULL;
2664 
2665 	if(asprintf(&buf,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
2666 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTSTATUS]
2667 		    ,buf1
2668 		    ,buf1
2669 		   )==-1)
2670 		buf=NULL;
2671 
2672 	/* save entry to db */
2673 	result=ndo2db_db_query(idi,buf);
2674 	free(buf);
2675 	free(buf1);
2676 
2677 	/* save custom variables to db */
2678 	result=ndo2db_save_custom_variables(idi,NDO2DB_DBTABLE_CUSTOMVARIABLESTATUS,object_id,ts[0]);
2679 
2680 
2681         /* free memory */
2682 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2683 		free(ts[x]);
2684 
2685 	return NDO_OK;
2686         }
2687 
2688 
ndo2db_handle_adaptiveprogramdata(ndo2db_idi * idi)2689 int ndo2db_handle_adaptiveprogramdata(ndo2db_idi *idi){
2690 
2691 	if(idi==NULL)
2692 		return NDO_ERROR;
2693 
2694 	/* IGNORED */
2695 
2696 	return NDO_OK;
2697         }
2698 
2699 
ndo2db_handle_adaptivehostdata(ndo2db_idi * idi)2700 int ndo2db_handle_adaptivehostdata(ndo2db_idi *idi){
2701 
2702 	if(idi==NULL)
2703 		return NDO_ERROR;
2704 
2705 	/* IGNORED */
2706 
2707 	return NDO_OK;
2708         }
2709 
2710 
ndo2db_handle_adaptiveservicedata(ndo2db_idi * idi)2711 int ndo2db_handle_adaptiveservicedata(ndo2db_idi *idi){
2712 
2713 	if(idi==NULL)
2714 		return NDO_ERROR;
2715 
2716 	/* IGNORED */
2717 
2718 	return NDO_OK;
2719         }
2720 
2721 
ndo2db_handle_adaptivecontactdata(ndo2db_idi * idi)2722 int ndo2db_handle_adaptivecontactdata(ndo2db_idi *idi){
2723 
2724 	if(idi==NULL)
2725 		return NDO_ERROR;
2726 
2727 	/* IGNORED */
2728 
2729 	return NDO_OK;
2730         }
2731 
2732 
ndo2db_handle_externalcommanddata(ndo2db_idi * idi)2733 int ndo2db_handle_externalcommanddata(ndo2db_idi *idi){
2734 	int x = 0;
2735 	int type,flags,attr;
2736 	struct timeval tstamp;
2737 	char *ts=NULL;
2738 	char *es[2];
2739 	int command_type=0;
2740 	unsigned long entry_time=0L;
2741 	char *buf=NULL;
2742 	int result=NDO_OK;
2743 
2744 	if(idi==NULL)
2745 		return NDO_ERROR;
2746 
2747 	/* convert timestamp, etc */
2748 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2749 
2750 	/* only handle start events */
2751 	if(type!=NEBTYPE_EXTERNALCOMMAND_START)
2752 		return NDO_OK;
2753 
2754 	/* covert vars */
2755 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_COMMANDTYPE],&command_type);
2756 	result=ndo2db_convert_string_to_unsignedlong(idi->buffered_input[NDO_DATA_ENTRYTIME],&entry_time);
2757 
2758 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDSTRING]);
2759 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDARGS]);
2760 
2761 	ts=ndo2db_db_timet_to_sql(idi,entry_time);
2762 
2763 	/* save entry to db */
2764 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', command_type='%d', entry_time=%s, command_name='%s', command_args='%s'"
2765 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_EXTERNALCOMMANDS]
2766 		    ,idi->dbinfo.instance_id
2767 		    ,command_type
2768 		    ,ts
2769 		    ,es[0]
2770 		    ,es[1]
2771 		   )==-1)
2772 		buf=NULL;
2773 	result=ndo2db_db_query(idi,buf);
2774 	free(buf);
2775 
2776         /* free memory */
2777 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2778 		free(es[x]);
2779 	free(ts);
2780 
2781 	return NDO_OK;
2782         }
2783 
2784 
ndo2db_handle_aggregatedstatusdata(ndo2db_idi * idi)2785 int ndo2db_handle_aggregatedstatusdata(ndo2db_idi *idi){
2786 
2787 	if(idi==NULL)
2788 		return NDO_ERROR;
2789 
2790 	/* IGNORED */
2791 
2792 	return NDO_OK;
2793         }
2794 
2795 
ndo2db_handle_retentiondata(ndo2db_idi * idi)2796 int ndo2db_handle_retentiondata(ndo2db_idi *idi){
2797 
2798 	if(idi==NULL)
2799 		return NDO_ERROR;
2800 
2801 	/* IGNORED */
2802 
2803 	return NDO_OK;
2804         }
2805 
2806 
ndo2db_handle_acknowledgementdata(ndo2db_idi * idi)2807 int ndo2db_handle_acknowledgementdata(ndo2db_idi *idi){
2808 	int type,flags,attr;
2809 	struct timeval tstamp;
2810 	int acknowledgement_type=0;
2811 	int state=0;
2812 	int is_sticky=0;
2813 	int persistent_comment=0;
2814 	int notify_contacts=0;
2815 	unsigned long object_id=0L;
2816 	int result=NDO_OK;
2817 	char *ts[1];
2818 	char *es[2];
2819 	int x=0;
2820 	char *buf=NULL;
2821 	char *buf1=NULL;
2822 
2823 	if(idi==NULL)
2824 		return NDO_ERROR;
2825 
2826 	/* convert timestamp, etc */
2827 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2828 
2829 	/* convert vars */
2830 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACKNOWLEDGEMENTTYPE],&acknowledgement_type);
2831 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATE],&state);
2832 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STICKY],&is_sticky);
2833 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PERSISTENT],&persistent_comment);
2834 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYCONTACTS],&notify_contacts);
2835 
2836 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_AUTHORNAME]);
2837 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMENT]);
2838 
2839 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2840 
2841 	/* get the object id */
2842 	if(acknowledgement_type==SERVICE_ACKNOWLEDGEMENT)
2843 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
2844 	if(acknowledgement_type==HOST_ACKNOWLEDGEMENT)
2845 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
2846 
2847 	/* save entry to db */
2848 	if(asprintf(&buf,"instance_id='%lu', entry_time=%s, entry_time_usec='%lu', acknowledgement_type='%d', object_id='%lu', state='%d', author_name='%s', comment_data='%s', is_sticky='%d', persistent_comment='%d', notify_contacts='%d'"
2849 		    ,idi->dbinfo.instance_id
2850 		    ,ts[0]
2851 		    ,tstamp.tv_usec
2852 		    ,acknowledgement_type
2853 		    ,object_id
2854 		    ,state
2855 		    ,es[0]
2856 		    ,es[1]
2857 		    ,is_sticky
2858 		    ,persistent_comment
2859 		    ,notify_contacts
2860 		   )==-1)
2861 		buf=NULL;
2862 
2863 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
2864 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_ACKNOWLEDGEMENTS]
2865 		    ,buf
2866 		    ,buf
2867 		   )==-1)
2868 		buf1=NULL;
2869 
2870 	result=ndo2db_db_query(idi,buf1);
2871 	free(buf);
2872 	free(buf1);
2873 
2874         /* free memory */
2875 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2876 		free(ts[x]);
2877 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2878 		free(es[x]);
2879 
2880 	return NDO_OK;
2881         }
2882 
2883 
ndo2db_handle_statechangedata(ndo2db_idi * idi)2884 int ndo2db_handle_statechangedata(ndo2db_idi *idi){
2885 	int x = 0;
2886 	int type,flags,attr;
2887 	struct timeval tstamp;
2888 	int statechange_type=0;
2889 	int state_change_occurred=0;
2890 	int state=0;
2891 	int state_type=0;
2892 	int current_attempt=0;
2893 	int max_attempts=0;
2894 	int last_state=-1;
2895 	int last_hard_state=-1;
2896 	unsigned long object_id=0L;
2897 	int result=NDO_OK;
2898 	char *ts[1];
2899 	char *es[2];
2900 	char *buf=NULL;
2901 
2902 	if(idi==NULL)
2903 		return NDO_ERROR;
2904 
2905 	/* convert timestamp, etc */
2906 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2907 
2908 	/* only process completed state changes */
2909 	if(type!=NEBTYPE_STATECHANGE_END)
2910 		return NDO_OK;
2911 
2912 	/* convert vars */
2913 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATECHANGETYPE],&statechange_type);
2914 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATECHANGE],&state_change_occurred);
2915 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATE],&state);
2916 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STATETYPE],&state_type);
2917 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CURRENTCHECKATTEMPT],&current_attempt);
2918 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MAXCHECKATTEMPTS],&max_attempts);
2919 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_LASTHARDSTATE],&last_hard_state);
2920 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_LASTSTATE],&last_state);
2921 
2922 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_OUTPUT]);
2923 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_LONGOUTPUT]);
2924 
2925 	ts[0]=ndo2db_db_timet_to_sql(idi,tstamp.tv_sec);
2926 
2927 	/* get the object id */
2928 	if(statechange_type==SERVICE_STATECHANGE)
2929 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOST],idi->buffered_input[NDO_DATA_SERVICE],&object_id);
2930 	else
2931 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOST],NULL,&object_id);
2932 
2933 	/* save entry to db */
2934 	if(asprintf(&buf,"INSERT INTO %s SET instance_id='%lu', state_time=%s, state_time_usec='%lu', object_id='%lu', state_change='%d', state='%d', state_type='%d', current_check_attempt='%d', max_check_attempts='%d', last_state='%d', last_hard_state='%d', output='%s', long_output='%s'"
2935 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_STATEHISTORY]
2936 		    ,idi->dbinfo.instance_id
2937 		    ,ts[0]
2938 		    ,tstamp.tv_usec
2939 		    ,object_id
2940 		    ,state_change_occurred
2941 		    ,state
2942 		    ,state_type
2943 		    ,current_attempt
2944 		    ,max_attempts
2945 		    ,last_state
2946 		    ,last_hard_state
2947 		    ,es[0]
2948 		    ,es[1]
2949 		   )==-1)
2950 		buf=NULL;
2951 
2952 	result=ndo2db_db_query(idi,buf);
2953 	free(buf);
2954 
2955         /* free memory */
2956 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(ts); x++)
2957 		free(ts[x]);
2958 	for (x = 0; x < NAGIOS_SIZEOF_ARRAY(es); x++)
2959 		free(es[x]);
2960 
2961 	return NDO_OK;
2962         }
2963 
2964 
2965 
2966 /****************************************************************************/
2967 /* VARIABLE DATA HANDLERS                                                   */
2968 /****************************************************************************/
2969 
ndo2db_handle_configfilevariables(ndo2db_idi * idi,int configfile_type)2970 int ndo2db_handle_configfilevariables(ndo2db_idi *idi, int configfile_type){
2971 	int type,flags,attr;
2972 	struct timeval tstamp;
2973 	unsigned long configfile_id=0L;
2974 	int result=NDO_OK;
2975 	char *es[3];
2976 	int x=0;
2977 	char *buf=NULL;
2978 	char *buf1=NULL;
2979 	char *varname=NULL;
2980 	char *varvalue=NULL;
2981 	ndo2db_mbuf mbuf;
2982 
2983 	ndo2db_log_debug_info(NDO2DB_DEBUGL_SQL,0,"HANDLE_CONFIGFILEVARS [1]\n");
2984 
2985 	if(idi==NULL)
2986 		return NDO_ERROR;
2987 
2988 	/* convert timestamp, etc */
2989 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
2990 
2991 	ndo2db_log_debug_info(NDO2DB_DEBUGL_SQL,0,"HANDLE_CONFIGFILEVARS [2]\n");
2992 	ndo2db_log_debug_info(NDO2DB_DEBUGL_SQL,0,"TSTAMP: %lu   LATEST: %lu\n",tstamp.tv_sec,idi->dbinfo.latest_realtime_data_time);
2993 
2994 	/* don't store old data */
2995 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
2996 		return NDO_OK;
2997 
2998 	ndo2db_log_debug_info(NDO2DB_DEBUGL_SQL,0,"HANDLE_CONFIGFILEVARS [3]\n");
2999 
3000 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_CONFIGFILENAME]);
3001 
3002 	/* add config file to db */
3003 	if(asprintf(&buf,"instance_id='%lu', configfile_type='%d', configfile_path='%s'"
3004 		    ,idi->dbinfo.instance_id
3005 		    ,configfile_type
3006 		    ,es[0]
3007 		   )==-1)
3008 		buf=NULL;
3009 
3010 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3011 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONFIGFILES]
3012 		    ,buf
3013 		    ,buf
3014 		   )==-1)
3015 		buf1=NULL;
3016 
3017 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
3018 		configfile_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
3019 	}
3020 	free(buf);
3021 	free(buf1);
3022 
3023 	free(es[0]);
3024 
3025 	/* save config file variables to db */
3026 	mbuf=idi->mbuf[NDO2DB_MBUF_CONFIGFILEVARIABLE];
3027 	for(x=0;x<mbuf.used_lines;x++){
3028 
3029 		if(mbuf.buffer[x]==NULL)
3030 			continue;
3031 
3032 		/* get var name/val pair */
3033 		varname=strtok(mbuf.buffer[x],"=");
3034 		varvalue=strtok(NULL,"\x0");
3035 
3036 		es[1]=ndo2db_db_escape_string(idi,varname);
3037 		es[2]=ndo2db_db_escape_string(idi,varvalue);
3038 
3039 		if(asprintf(&buf,"instance_id='%lu', configfile_id='%lu', varname='%s', varvalue='%s'"
3040 			    ,idi->dbinfo.instance_id
3041 			    ,configfile_id
3042 			    ,es[1]
3043 			    ,es[2]
3044 			   )==-1)
3045 			buf=NULL;
3046 
3047 		if(asprintf(&buf1,"INSERT INTO %s SET %s"
3048 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONFIGFILEVARIABLES]
3049 			    ,buf
3050 			   )==-1)
3051 			buf1=NULL;
3052 #ifdef REMOVED_10182007
3053 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3054 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONFIGFILEVARIABLES]
3055 			    ,buf
3056 			    ,buf
3057 			   )==-1)
3058 			buf1=NULL;
3059 #endif
3060 
3061 		result=ndo2db_db_query(idi,buf1);
3062 		free(buf);
3063 		free(buf1);
3064 
3065 		free(es[1]);
3066 		free(es[2]);
3067 	        }
3068 
3069 	return NDO_OK;
3070         }
3071 
3072 
3073 
ndo2db_handle_configvariables(ndo2db_idi * idi)3074 int ndo2db_handle_configvariables(ndo2db_idi *idi){
3075 
3076 	if(idi==NULL)
3077 		return NDO_ERROR;
3078 
3079 	return NDO_OK;
3080         }
3081 
3082 
ndo2db_handle_runtimevariables(ndo2db_idi * idi)3083 int ndo2db_handle_runtimevariables(ndo2db_idi *idi){
3084 	int type,flags,attr;
3085 	struct timeval tstamp;
3086 	int result=NDO_OK;
3087 	char *es[2];
3088 	int x=0;
3089 	char *buf=NULL;
3090 	char *buf1=NULL;
3091 	char *varname=NULL;
3092 	char *varvalue=NULL;
3093 	ndo2db_mbuf mbuf;
3094 
3095 	if(idi==NULL)
3096 		return NDO_ERROR;
3097 
3098 	/* convert timestamp, etc */
3099 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
3100 
3101 	/* don't store old data */
3102 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
3103 		return NDO_OK;
3104 
3105 	/* save config file variables to db */
3106 	mbuf=idi->mbuf[NDO2DB_MBUF_RUNTIMEVARIABLE];
3107 	for(x=0;x<mbuf.used_lines;x++){
3108 
3109 		if(mbuf.buffer[x]==NULL)
3110 			continue;
3111 
3112 		/* get var name/val pair */
3113 		varname=strtok(mbuf.buffer[x],"=");
3114 		varvalue=strtok(NULL,"\x0");
3115 
3116 		es[0]=ndo2db_db_escape_string(idi,varname);
3117 		es[1]=ndo2db_db_escape_string(idi,varvalue);
3118 
3119 		if(asprintf(&buf,"instance_id='%lu', varname='%s', varvalue='%s'"
3120 			    ,idi->dbinfo.instance_id
3121 			    ,es[0]
3122 			    ,es[1]
3123 			   )==-1)
3124 			buf=NULL;
3125 
3126 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3127 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_RUNTIMEVARIABLES]
3128 			    ,buf
3129 			    ,buf
3130 			   )==-1)
3131 			buf1=NULL;
3132 
3133 		result=ndo2db_db_query(idi,buf1);
3134 		free(buf);
3135 		free(buf1);
3136 
3137 		free(es[0]);
3138 		free(es[1]);
3139 	        }
3140 
3141 	return NDO_OK;
3142         }
3143 
3144 
3145 
3146 /****************************************************************************/
3147 /* OBJECT DEFINITION DATA HANDLERS                                          */
3148 /****************************************************************************/
3149 
ndo2db_handle_configdumpstart(ndo2db_idi * idi)3150 int ndo2db_handle_configdumpstart(ndo2db_idi *idi){
3151 	int type,flags,attr;
3152 	struct timeval tstamp;
3153 	int result=NDO_OK;
3154 
3155 	/* convert timestamp, etc */
3156 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
3157 
3158 	/* set config dump type */
3159 	if(idi->buffered_input[NDO_DATA_CONFIGDUMPTYPE]!=NULL && !strcmp(idi->buffered_input[NDO_DATA_CONFIGDUMPTYPE],NDO_API_CONFIGDUMP_RETAINED))
3160 		idi->current_object_config_type=1;
3161 	else
3162 		idi->current_object_config_type=0;
3163 
3164 	return NDO_OK;
3165         }
3166 
3167 
ndo2db_handle_configdumpend(ndo2db_idi * idi)3168 int ndo2db_handle_configdumpend(ndo2db_idi *idi){
3169 
3170 	return NDO_OK;
3171         }
3172 
3173 
ndo2db_handle_hostdefinition(ndo2db_idi * idi)3174 int ndo2db_handle_hostdefinition(ndo2db_idi *idi){
3175 	int type,flags,attr;
3176 	struct timeval tstamp;
3177 	unsigned long object_id=0L;
3178 	unsigned long check_timeperiod_id=0L;
3179 	unsigned long notification_timeperiod_id=0L;
3180 	unsigned long check_command_id=0L;
3181 	unsigned long eventhandler_command_id=0L;
3182 	double check_interval=0.0;
3183 	double retry_interval=0.0;
3184 	int max_check_attempts=0;
3185 	double first_notification_delay=0.0;
3186 	double notification_interval=0.0;
3187 	int notify_on_down=0;
3188 	int notify_on_unreachable=0;
3189 	int notify_on_recovery=0;
3190 	int notify_on_flapping=0;
3191 	int notify_on_downtime=0;
3192 	int stalk_on_up=0;
3193 	int stalk_on_down=0;
3194 	int stalk_on_unreachable=0;
3195 	int flap_detection_enabled=0;
3196 	int flap_detection_on_up=0;
3197 	int flap_detection_on_down=0;
3198 	int flap_detection_on_unreachable=0;
3199 	int process_performance_data=0;
3200 	int freshness_checks_enabled=0;
3201 	int freshness_threshold=0;
3202 	int passive_checks_enabled=0;
3203 	int event_handler_enabled=0;
3204 	int active_checks_enabled=0;
3205 	int retain_status_information=0;
3206 	int retain_nonstatus_information=0;
3207 	int notifications_enabled=0;
3208 	int obsess_over_host=0;
3209 	int failure_prediction_enabled=0;
3210 	double low_flap_threshold=0.0;
3211 	double high_flap_threshold=0.0;
3212 	int have_2d_coords=0;
3213 	int x_2d=0;
3214 	int y_2d=0;
3215 	int have_3d_coords=0;
3216 	double x_3d=0.0;
3217 	double y_3d=0.0;
3218 	double z_3d=0.0;
3219 	unsigned long host_id=0L;
3220 	unsigned long member_id=0L;
3221 	int result=NDO_OK;
3222 	char *es[13];
3223 	int x=0;
3224 	char *buf=NULL;
3225 	char *buf1=NULL;
3226 	ndo2db_mbuf mbuf;
3227 	char *cmdptr=NULL;
3228 	char *argptr=NULL;
3229 #ifdef BUILD_NAGIOS_4X
3230 	int	importance=0;
3231 #endif
3232 
3233 	if(idi==NULL)
3234 		return NDO_ERROR;
3235 
3236 	/* convert timestamp, etc */
3237 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
3238 
3239 	/* don't store old data */
3240 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
3241 		return NDO_OK;
3242 
3243 	/* convert vars */
3244 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_HOSTCHECKINTERVAL],&check_interval);
3245 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_HOSTRETRYINTERVAL],&retry_interval);
3246 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTMAXCHECKATTEMPTS],&max_check_attempts);
3247 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_FIRSTNOTIFICATIONDELAY],&first_notification_delay);
3248 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_HOSTNOTIFICATIONINTERVAL],&notification_interval);
3249 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTDOWN],&notify_on_down);
3250 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTUNREACHABLE],&notify_on_unreachable);
3251 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTRECOVERY],&notify_on_recovery);
3252 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTFLAPPING],&notify_on_flapping);
3253 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTDOWNTIME],&notify_on_downtime);
3254 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKHOSTONUP],&stalk_on_up);
3255 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKHOSTONDOWN],&stalk_on_down);
3256 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKHOSTONUNREACHABLE],&stalk_on_unreachable);
3257 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTFLAPDETECTIONENABLED],&flap_detection_enabled);
3258 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONUP],&flap_detection_on_up);
3259 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONDOWN],&flap_detection_on_down);
3260 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONUNREACHABLE],&flap_detection_on_unreachable);
3261 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROCESSHOSTPERFORMANCEDATA],&process_performance_data);
3262 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTFRESHNESSCHECKSENABLED],&freshness_checks_enabled);
3263 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTFRESHNESSTHRESHOLD],&freshness_threshold);
3264 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PASSIVEHOSTCHECKSENABLED],&passive_checks_enabled);
3265 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTEVENTHANDLERENABLED],&event_handler_enabled);
3266 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVEHOSTCHECKSENABLED],&active_checks_enabled);
3267 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETAINHOSTSTATUSINFORMATION],&retain_status_information);
3268 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETAINHOSTNONSTATUSINFORMATION],&retain_nonstatus_information);
3269 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTNOTIFICATIONSENABLED],&notifications_enabled);
3270 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_OBSESSOVERHOST],&obsess_over_host);
3271 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTFAILUREPREDICTIONENABLED],&failure_prediction_enabled);
3272 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LOWHOSTFLAPTHRESHOLD],&low_flap_threshold);
3273 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_HIGHHOSTFLAPTHRESHOLD],&high_flap_threshold);
3274 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HAVE2DCOORDS],&have_2d_coords);
3275 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_X2D],&x_2d);
3276 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_Y3D],&y_2d);
3277 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HAVE3DCOORDS],&have_3d_coords);
3278 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_X3D],&x_3d);
3279 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_Y3D],&y_3d);
3280 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_Z3D],&z_3d);
3281 #ifdef BUILD_NAGIOS_4X
3282 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_IMPORTANCE],&importance);
3283 #endif
3284 
3285 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_HOSTADDRESS]);
3286 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_HOSTFAILUREPREDICTIONOPTIONS]);
3287 
3288 	/* get the check command */
3289 	cmdptr=strtok(idi->buffered_input[NDO_DATA_HOSTCHECKCOMMAND],"!");
3290 	argptr=strtok(NULL,"\x0");
3291 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,cmdptr,NULL,&check_command_id);
3292 	es[2]=ndo2db_db_escape_string(idi,argptr);
3293 
3294 	/* get the event handler command */
3295 	cmdptr=strtok(idi->buffered_input[NDO_DATA_HOSTEVENTHANDLER],"!");
3296 	argptr=strtok(NULL,"\x0");
3297 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,cmdptr,NULL,&eventhandler_command_id);
3298 	es[3]=ndo2db_db_escape_string(idi,argptr);
3299 
3300 	es[4]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_NOTES]);
3301 	es[5]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_NOTESURL]);
3302 	es[6]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_ACTIONURL]);
3303 	es[7]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_ICONIMAGE]);
3304 	es[8]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_ICONIMAGEALT]);
3305 	es[9]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_VRMLIMAGE]);
3306 	es[10]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_STATUSMAPIMAGE]);
3307 	es[11]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_DISPLAYNAME]);
3308 	es[12]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_HOSTALIAS]);
3309 
3310 	/* get the object id */
3311 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOSTNAME],NULL,&object_id);
3312 
3313 	/* flag the object as being active */
3314 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_HOST,object_id);
3315 
3316 	/* get the timeperiod ids */
3317 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_HOSTCHECKPERIOD],NULL,&check_timeperiod_id);
3318 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_HOSTNOTIFICATIONPERIOD],NULL,&notification_timeperiod_id);
3319 
3320  	/* add definition to db */
3321 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', host_object_id='%lu', alias='%s', display_name='%s', address='%s', check_command_object_id='%lu', check_command_args='%s', eventhandler_command_object_id='%lu', eventhandler_command_args='%s', check_timeperiod_object_id='%lu', notification_timeperiod_object_id='%lu', failure_prediction_options='%s', check_interval='%lf', retry_interval='%lf', max_check_attempts='%d', first_notification_delay='%lf', notification_interval='%lf', notify_on_down='%d', notify_on_unreachable='%d', notify_on_recovery='%d', notify_on_flapping='%d', notify_on_downtime='%d', stalk_on_up='%d', stalk_on_down='%d', stalk_on_unreachable='%d', flap_detection_enabled='%d', flap_detection_on_up='%d', flap_detection_on_down='%d', flap_detection_on_unreachable='%d', low_flap_threshold='%lf', high_flap_threshold='%lf', process_performance_data='%d', freshness_checks_enabled='%d', freshness_threshold='%d', passive_checks_enabled='%d', event_handler_enabled='%d', active_checks_enabled='%d', retain_status_information='%d', retain_nonstatus_information='%d', notifications_enabled='%d', obsess_over_host='%d', failure_prediction_enabled='%d', notes='%s', notes_url='%s', action_url='%s', icon_image='%s', icon_image_alt='%s', vrml_image='%s', statusmap_image='%s', have_2d_coords='%d', x_2d='%d', y_2d='%d', have_3d_coords='%d', x_3d='%lf', y_3d='%lf', z_3d='%lf'"
3322 #ifdef BUILD_NAGIOS_4X
3323 			", importance='%d'"
3324 #endif
3325 		    ,idi->dbinfo.instance_id
3326 		    ,idi->current_object_config_type
3327 		    ,object_id
3328 		    ,(es[12]==NULL)?"":es[12]
3329 		    ,(es[11]==NULL)?"":es[11]
3330 		    ,(es[0]==NULL)?"":es[0]
3331 		    ,check_command_id
3332 		    ,(es[2]==NULL)?"":es[2]
3333 		    ,eventhandler_command_id
3334 		    ,(es[3]==NULL)?"":es[3]
3335 		    ,check_timeperiod_id
3336 		    ,notification_timeperiod_id
3337 		    ,(es[1]==NULL)?"":es[1]
3338 		    ,check_interval
3339 		    ,retry_interval
3340 		    ,max_check_attempts
3341 		    ,first_notification_delay
3342 		    ,notification_interval
3343 		    ,notify_on_down
3344 		    ,notify_on_unreachable
3345 		    ,notify_on_recovery
3346 		    ,notify_on_flapping
3347 		    ,notify_on_downtime
3348 		    ,stalk_on_up
3349 		    ,stalk_on_down
3350 		    ,stalk_on_unreachable
3351 		    ,flap_detection_enabled
3352 		    ,flap_detection_on_up
3353 		    ,flap_detection_on_down
3354 		    ,flap_detection_on_unreachable
3355 		    ,low_flap_threshold
3356 		    ,high_flap_threshold
3357 		    ,process_performance_data
3358 		    ,freshness_checks_enabled
3359 		    ,freshness_threshold
3360 		    ,passive_checks_enabled
3361 		    ,event_handler_enabled
3362 		    ,active_checks_enabled
3363 		    ,retain_status_information
3364 		    ,retain_nonstatus_information
3365 		    ,notifications_enabled
3366 		    ,obsess_over_host
3367 		    ,failure_prediction_enabled
3368 		    ,(es[4]==NULL)?"":es[4]
3369 		    ,(es[5]==NULL)?"":es[5]
3370 		    ,(es[6]==NULL)?"":es[6]
3371 		    ,(es[7]==NULL)?"":es[7]
3372 		    ,(es[8]==NULL)?"":es[8]
3373 		    ,(es[9]==NULL)?"":es[9]
3374 		    ,(es[10]==NULL)?"":es[10]
3375 		    ,have_2d_coords
3376 		    ,x_2d
3377 		    ,y_2d
3378 		    ,have_3d_coords
3379 		    ,x_3d
3380 		    ,y_3d
3381 		    ,z_3d
3382 #ifdef BUILD_NAGIOS_4X
3383 			,importance
3384 #endif
3385 		   )==-1)
3386 		buf=NULL;
3387 
3388 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3389 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTS]
3390 		    ,buf
3391 		    ,buf
3392 		   )==-1)
3393 		buf1=NULL;
3394 
3395 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
3396 		host_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
3397 	}
3398 	free(buf);
3399 	free(buf1);
3400 
3401 	for(x=0;x<13;x++)
3402 		free(es[x]);
3403 
3404 	/* save parent hosts to db */
3405 	mbuf=idi->mbuf[NDO2DB_MBUF_PARENTHOST];
3406 	for(x=0;x<mbuf.used_lines;x++){
3407 
3408 		if(mbuf.buffer[x]==NULL)
3409 			continue;
3410 
3411 		/* get the object id of the member */
3412 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,mbuf.buffer[x],NULL,&member_id);
3413 
3414 		if(asprintf(&buf,"instance_id='%d', host_id='%lu', parent_host_object_id='%lu'"
3415 			    ,idi->dbinfo.instance_id
3416 			    ,host_id
3417 			    ,member_id
3418 			   )==-1)
3419 			buf=NULL;
3420 
3421 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3422 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTPARENTHOSTS]
3423 			    ,buf
3424 			    ,buf
3425 			   )==-1)
3426 			buf1=NULL;
3427 
3428 		result=ndo2db_db_query(idi,buf1);
3429 		free(buf);
3430 		free(buf1);
3431 	        }
3432 
3433 	/* save contact groups to db */
3434 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTGROUP];
3435 	for(x=0;x<mbuf.used_lines;x++){
3436 
3437 		if(mbuf.buffer[x]==NULL)
3438 			continue;
3439 
3440 		/* get the object id of the member */
3441 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACTGROUP,mbuf.buffer[x],NULL,&member_id);
3442 
3443 		if(asprintf(&buf,"instance_id='%d', host_id='%lu', contactgroup_object_id='%lu'"
3444 			    ,idi->dbinfo.instance_id
3445 			    ,host_id
3446 			    ,member_id
3447 			   )==-1)
3448 			buf=NULL;
3449 
3450 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3451 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTCONTACTGROUPS]
3452 			    ,buf
3453 			    ,buf
3454 			   )==-1)
3455 			buf1=NULL;
3456 
3457 		result=ndo2db_db_query(idi,buf1);
3458 		free(buf);
3459 		free(buf1);
3460 	        }
3461 
3462 	/* save contacts to db */
3463 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACT];
3464 	for(x=0;x<mbuf.used_lines;x++){
3465 
3466 		if(mbuf.buffer[x]==NULL)
3467 			continue;
3468 
3469 		/* get the object id of the member */
3470 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,mbuf.buffer[x],NULL,&member_id);
3471 
3472 		if(asprintf(&buf,"instance_id='%d', host_id='%lu', contact_object_id='%lu'"
3473 			    ,idi->dbinfo.instance_id
3474 			    ,host_id
3475 			    ,member_id
3476 			   )==-1)
3477 			buf=NULL;
3478 
3479 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3480 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTCONTACTS]
3481 			    ,buf
3482 			    ,buf
3483 			   )==-1)
3484 			buf1=NULL;
3485 
3486 		result=ndo2db_db_query(idi,buf1);
3487 		free(buf);
3488 		free(buf1);
3489 	}
3490 
3491 	/* save custom variables to db */
3492 	result=ndo2db_save_custom_variables(idi,NDO2DB_DBTABLE_CUSTOMVARIABLES,object_id,NULL);
3493 
3494 	return NDO_OK;
3495         }
3496 
3497 
ndo2db_handle_hostgroupdefinition(ndo2db_idi * idi)3498 int ndo2db_handle_hostgroupdefinition(ndo2db_idi *idi){
3499 	int type,flags,attr;
3500 	struct timeval tstamp;
3501 	unsigned long object_id=0L;
3502 	unsigned long group_id=0L;
3503 	unsigned long member_id=0L;
3504 	int result=NDO_OK;
3505 	char *es[1];
3506 	int x=0;
3507 	char *buf=NULL;
3508 	char *buf1=NULL;
3509 	ndo2db_mbuf mbuf;
3510 
3511 	if(idi==NULL)
3512 		return NDO_ERROR;
3513 
3514 	/* convert timestamp, etc */
3515 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
3516 
3517 	/* don't store old data */
3518 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
3519 		return NDO_OK;
3520 
3521 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_HOSTGROUPALIAS]);
3522 
3523 	/* get the object id */
3524 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOSTGROUP,idi->buffered_input[NDO_DATA_HOSTGROUPNAME],NULL,&object_id);
3525 
3526 	/* flag the object as being active */
3527 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_HOSTGROUP,object_id);
3528 
3529 	/* add definition to db */
3530 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', hostgroup_object_id='%lu', alias='%s'"
3531 		    ,idi->dbinfo.instance_id
3532 		    ,idi->current_object_config_type
3533 		    ,object_id
3534 		    ,es[0]
3535 		   )==-1)
3536 		buf=NULL;
3537 
3538 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3539 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTGROUPS]
3540 		    ,buf
3541 		    ,buf
3542 		   )==-1)
3543 		buf1=NULL;
3544 
3545 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
3546 		group_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
3547 	}
3548 	free(buf);
3549 	free(buf1);
3550 
3551 	free(es[0]);
3552 
3553 	/* save hostgroup members to db */
3554 	mbuf=idi->mbuf[NDO2DB_MBUF_HOSTGROUPMEMBER];
3555 	for(x=0;x<mbuf.used_lines;x++){
3556 
3557 		if(mbuf.buffer[x]==NULL)
3558 			continue;
3559 
3560 		/* get the object id of the member */
3561 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,mbuf.buffer[x],NULL,&member_id);
3562 
3563 		if(asprintf(&buf,"instance_id='%d', hostgroup_id='%lu', host_object_id='%lu'"
3564 			    ,idi->dbinfo.instance_id
3565 			    ,group_id
3566 			    ,member_id
3567 			   )==-1)
3568 			buf=NULL;
3569 
3570 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3571 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTGROUPMEMBERS]
3572 			    ,buf
3573 			    ,buf
3574 			   )==-1)
3575 			buf1=NULL;
3576 
3577 		result=ndo2db_db_query(idi,buf1);
3578 		free(buf);
3579 		free(buf1);
3580 	        }
3581 
3582 	return NDO_OK;
3583         }
3584 
3585 
ndo2db_handle_servicedefinition(ndo2db_idi * idi)3586 int ndo2db_handle_servicedefinition(ndo2db_idi *idi){
3587 	int type,flags,attr;
3588 	struct timeval tstamp;
3589 	unsigned long object_id=0L;
3590 	unsigned long host_id=0L;
3591 	unsigned long check_timeperiod_id=0L;
3592 	unsigned long notification_timeperiod_id=0L;
3593 	unsigned long check_command_id=0L;
3594 	unsigned long eventhandler_command_id=0L;
3595 	double check_interval=0.0;
3596 	double retry_interval=0.0;
3597 	int max_check_attempts=0;
3598 	double first_notification_delay=0.0;
3599 	double notification_interval=0.0;
3600 	int notify_on_warning=0;
3601 	int notify_on_unknown=0;
3602 	int notify_on_critical=0;
3603 	int notify_on_recovery=0;
3604 	int notify_on_flapping=0;
3605 	int notify_on_downtime=0;
3606 	int stalk_on_ok=0;
3607 	int stalk_on_warning=0;
3608 	int stalk_on_unknown=0;
3609 	int stalk_on_critical=0;
3610 	int is_volatile=0;
3611 	int flap_detection_enabled=0;
3612 	int flap_detection_on_ok=0;
3613 	int flap_detection_on_warning=0;
3614 	int flap_detection_on_unknown=0;
3615 	int flap_detection_on_critical=0;
3616 	int process_performance_data=0;
3617 	int freshness_checks_enabled=0;
3618 	int freshness_threshold=0;
3619 	int passive_checks_enabled=0;
3620 	int event_handler_enabled=0;
3621 	int active_checks_enabled=0;
3622 	int retain_status_information=0;
3623 	int retain_nonstatus_information=0;
3624 	int notifications_enabled=0;
3625 	int obsess_over_service=0;
3626 	int failure_prediction_enabled=0;
3627 	double low_flap_threshold=0.0;
3628 	double high_flap_threshold=0.0;
3629 	unsigned long service_id=0L;
3630 	unsigned long member_id=0L;
3631 	int result=NDO_OK;
3632 	char *es[9];
3633 	int x=0;
3634 	char *buf=NULL;
3635 	char *buf1=NULL;
3636 	ndo2db_mbuf mbuf;
3637 	char *cmdptr=NULL;
3638 	char *argptr=NULL;
3639 #ifdef BUILD_NAGIOS_4X
3640 	int	importance=0;
3641 	char *hptr=NULL;
3642 	char *sptr=NULL;
3643 #endif
3644 
3645 	if(idi==NULL)
3646 		return NDO_ERROR;
3647 
3648 	/* convert timestamp, etc */
3649 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
3650 
3651 	/* don't store old data */
3652 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
3653 		return NDO_OK;
3654 
3655 	/* convert vars */
3656 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_SERVICECHECKINTERVAL],&check_interval);
3657 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_SERVICERETRYINTERVAL],&retry_interval);
3658 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MAXSERVICECHECKATTEMPTS],&max_check_attempts);
3659 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_FIRSTNOTIFICATIONDELAY],&first_notification_delay);
3660 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_SERVICENOTIFICATIONINTERVAL],&notification_interval);
3661 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEWARNING],&notify_on_warning);
3662 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEUNKNOWN],&notify_on_unknown);
3663 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICECRITICAL],&notify_on_critical);
3664 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICERECOVERY],&notify_on_recovery);
3665 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEFLAPPING],&notify_on_flapping);
3666 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEDOWNTIME],&notify_on_downtime);
3667 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKSERVICEONOK],&stalk_on_ok);
3668 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKSERVICEONWARNING],&stalk_on_warning);
3669 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKSERVICEONUNKNOWN],&stalk_on_unknown);
3670 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_STALKSERVICEONCRITICAL],&stalk_on_critical);
3671 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICEISVOLATILE],&is_volatile);
3672 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICEFLAPDETECTIONENABLED],&flap_detection_enabled);
3673 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONOK],&flap_detection_on_ok);
3674 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONWARNING],&flap_detection_on_warning);
3675 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONUNKNOWN],&flap_detection_on_unknown);
3676 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FLAPDETECTIONONCRITICAL],&flap_detection_on_critical);
3677 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PROCESSSERVICEPERFORMANCEDATA],&process_performance_data);
3678 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICEFRESHNESSCHECKSENABLED],&freshness_checks_enabled);
3679 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICEFRESHNESSTHRESHOLD],&freshness_threshold);
3680 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_PASSIVESERVICECHECKSENABLED],&passive_checks_enabled);
3681 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICEEVENTHANDLERENABLED],&event_handler_enabled);
3682 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVESERVICECHECKSENABLED],&active_checks_enabled);
3683 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETAINSERVICESTATUSINFORMATION],&retain_status_information);
3684 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_RETAINSERVICENONSTATUSINFORMATION],&retain_nonstatus_information);
3685 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICENOTIFICATIONSENABLED],&notifications_enabled);
3686 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_OBSESSOVERSERVICE],&obsess_over_service);
3687 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICEFAILUREPREDICTIONENABLED],&failure_prediction_enabled);
3688 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_LOWSERVICEFLAPTHRESHOLD],&low_flap_threshold);
3689 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_HIGHSERVICEFLAPTHRESHOLD],&high_flap_threshold);
3690 #ifdef BUILD_NAGIOS_4X
3691 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_IMPORTANCE],&importance);
3692 #endif
3693 
3694 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_SERVICEFAILUREPREDICTIONOPTIONS]);
3695 
3696 	/* get the check command */
3697 	cmdptr=strtok(idi->buffered_input[NDO_DATA_SERVICECHECKCOMMAND],"!");
3698 	argptr=strtok(NULL,"\x0");
3699 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,cmdptr,NULL,&check_command_id);
3700 	es[1]=ndo2db_db_escape_string(idi,argptr);
3701 
3702 	/* get the event handler command */
3703 	cmdptr=strtok(idi->buffered_input[NDO_DATA_SERVICEEVENTHANDLER],"!");
3704 	argptr=strtok(NULL,"\x0");
3705 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,cmdptr,NULL,&eventhandler_command_id);
3706 	es[2]=ndo2db_db_escape_string(idi,argptr);
3707 
3708 	es[3]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_NOTES]);
3709 	es[4]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_NOTESURL]);
3710 	es[5]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_ACTIONURL]);
3711 	es[6]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_ICONIMAGE]);
3712 	es[7]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_ICONIMAGEALT]);
3713 	es[8]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_DISPLAYNAME]);
3714 
3715 	/* get the object ids */
3716 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOSTNAME],idi->buffered_input[NDO_DATA_SERVICEDESCRIPTION],&object_id);
3717 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOSTNAME],NULL,&host_id);
3718 
3719 	/* flag the object as being active */
3720 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_SERVICE,object_id);
3721 
3722 	/* get the timeperiod ids */
3723 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_SERVICECHECKPERIOD],NULL,&check_timeperiod_id);
3724 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_SERVICENOTIFICATIONPERIOD],NULL,&notification_timeperiod_id);
3725 
3726 	/* add definition to db */
3727 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', host_object_id='%lu', service_object_id='%lu', display_name='%s', check_command_object_id='%lu', check_command_args='%s', eventhandler_command_object_id='%lu', eventhandler_command_args='%s', check_timeperiod_object_id='%lu', notification_timeperiod_object_id='%lu', failure_prediction_options='%s', check_interval='%lf', retry_interval='%lf', max_check_attempts='%d', first_notification_delay='%lf', notification_interval='%lf', notify_on_warning='%d', notify_on_unknown='%d', notify_on_critical='%d', notify_on_recovery='%d', notify_on_flapping='%d', notify_on_downtime='%d', stalk_on_ok='%d', stalk_on_warning='%d', stalk_on_unknown='%d', stalk_on_critical='%d', is_volatile='%d', flap_detection_enabled='%d', flap_detection_on_ok='%d', flap_detection_on_warning='%d', flap_detection_on_unknown='%d', flap_detection_on_critical='%d', low_flap_threshold='%lf', high_flap_threshold='%lf', process_performance_data='%d', freshness_checks_enabled='%d', freshness_threshold='%d', passive_checks_enabled='%d', event_handler_enabled='%d', active_checks_enabled='%d', retain_status_information='%d', retain_nonstatus_information='%d', notifications_enabled='%d', obsess_over_service='%d', failure_prediction_enabled='%d', notes='%s', notes_url='%s', action_url='%s', icon_image='%s', icon_image_alt='%s'"
3728 #ifdef BUILD_NAGIOS_4X
3729 			", importance='%d'"
3730 #endif
3731 		    ,idi->dbinfo.instance_id
3732 		    ,idi->current_object_config_type
3733 		    ,host_id
3734 		    ,object_id
3735 		    ,(es[8]==NULL)?"":es[8]
3736 		    ,check_command_id
3737 		    ,(es[1]==NULL)?"":es[1]
3738 		    ,eventhandler_command_id
3739 		    ,(es[2]==NULL)?"":es[2]
3740 		    ,check_timeperiod_id
3741 		    ,notification_timeperiod_id
3742 		    ,(es[0]==NULL)?"":es[0]
3743 		    ,check_interval
3744 		    ,retry_interval
3745 		    ,max_check_attempts
3746 		    ,first_notification_delay
3747 		    ,notification_interval
3748 		    ,notify_on_warning
3749 		    ,notify_on_unknown
3750 		    ,notify_on_critical
3751 		    ,notify_on_recovery
3752 		    ,notify_on_flapping
3753 		    ,notify_on_downtime
3754 		    ,stalk_on_ok
3755 		    ,stalk_on_warning
3756 		    ,stalk_on_unknown
3757 		    ,stalk_on_critical
3758 		    ,is_volatile
3759 		    ,flap_detection_enabled
3760 		    ,flap_detection_on_ok
3761 		    ,flap_detection_on_warning
3762 		    ,flap_detection_on_unknown
3763 		    ,flap_detection_on_critical
3764 		    ,low_flap_threshold
3765 		    ,high_flap_threshold
3766 		    ,process_performance_data
3767 		    ,freshness_checks_enabled
3768 		    ,freshness_threshold
3769 		    ,passive_checks_enabled
3770 		    ,event_handler_enabled
3771 		    ,active_checks_enabled
3772 		    ,retain_status_information
3773 		    ,retain_nonstatus_information
3774 		    ,notifications_enabled
3775 		    ,obsess_over_service
3776 		    ,failure_prediction_enabled
3777 		    ,(es[3]==NULL)?"":es[3]
3778 		    ,(es[4]==NULL)?"":es[4]
3779 		    ,(es[5]==NULL)?"":es[5]
3780 		    ,(es[6]==NULL)?"":es[6]
3781 		    ,(es[7]==NULL)?"":es[7]
3782 #ifdef BUILD_NAGIOS_4X
3783 			,importance
3784 #endif
3785 		   )==-1)
3786 		buf=NULL;
3787 
3788 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3789 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICES]
3790 		    ,buf
3791 		    ,buf
3792 		   )==-1)
3793 		buf1=NULL;
3794 
3795 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
3796 		service_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
3797 	}
3798 	free(buf);
3799 	free(buf1);
3800 
3801 	for(x=0;x<9;x++)
3802 		free(es[x]);
3803 
3804 #ifdef BUILD_NAGIOS_4X
3805 	/* save parent services to db */
3806 	mbuf = idi->mbuf[NDO2DB_MBUF_PARENTSERVICE];
3807 	for(x = 0; x < mbuf.used_lines; x++) {
3808 
3809 		if(mbuf.buffer[x] == NULL) continue;
3810 
3811 		/* split the host/service name */
3812 		hptr=strtok(mbuf.buffer[x],";");
3813 		sptr=strtok(NULL,"\x0");
3814 
3815 		/* get the object id of the member */
3816 		result = ndo2db_get_object_id_with_insert(idi,
3817 				NDO2DB_OBJECTTYPE_SERVICE, hptr, sptr, &member_id);
3818 
3819 		if(asprintf(&buf,
3820 				"instance_id='%d', service_id='%lu', parent_service_object_id='%lu'",
3821 				idi->dbinfo.instance_id, service_id, member_id) == -1) {
3822 			buf = NULL;
3823 			}
3824 
3825 		if(asprintf(&buf1, "INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s",
3826 				ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEPARENTSERVICES],
3827 				buf, buf) == -1) {
3828 			buf1=NULL;
3829 			}
3830 
3831 		result = ndo2db_db_query(idi, buf1);
3832 		free(buf);
3833 		free(buf1);
3834 		}
3835 #endif
3836 
3837 	/* save contact groups to db */
3838 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTGROUP];
3839 	for(x=0;x<mbuf.used_lines;x++){
3840 
3841 		if(mbuf.buffer[x]==NULL)
3842 			continue;
3843 
3844 		/* get the object id of the member */
3845 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACTGROUP,mbuf.buffer[x],NULL,&member_id);
3846 
3847 		if(asprintf(&buf,"instance_id='%d', service_id='%lu', contactgroup_object_id='%lu'"
3848 			    ,idi->dbinfo.instance_id
3849 			    ,service_id
3850 			    ,member_id
3851 			   )==-1)
3852 			buf=NULL;
3853 
3854 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3855 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICECONTACTGROUPS]
3856 			    ,buf
3857 			    ,buf
3858 			   )==-1)
3859 			buf1=NULL;
3860 
3861 		result=ndo2db_db_query(idi,buf1);
3862 		free(buf);
3863 		free(buf1);
3864 	        }
3865 
3866 	/* save contacts to db */
3867 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACT];
3868 	for(x=0;x<mbuf.used_lines;x++){
3869 
3870 		if(mbuf.buffer[x]==NULL)
3871 			continue;
3872 
3873 		/* get the object id of the member */
3874 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,mbuf.buffer[x],NULL,&member_id);
3875 
3876 		if(asprintf(&buf,"instance_id='%d', service_id='%lu', contact_object_id='%lu'"
3877 			    ,idi->dbinfo.instance_id
3878 			    ,service_id
3879 			    ,member_id
3880 			   )==-1)
3881 			buf=NULL;
3882 
3883 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3884 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICECONTACTS]
3885 			    ,buf
3886 			    ,buf
3887 			   )==-1)
3888 			buf1=NULL;
3889 
3890 		result=ndo2db_db_query(idi,buf1);
3891 		free(buf);
3892 		free(buf1);
3893 	}
3894 
3895 	/* save custom variables to db */
3896 	result=ndo2db_save_custom_variables(idi,NDO2DB_DBTABLE_CUSTOMVARIABLES,object_id,NULL);
3897 
3898 	return NDO_OK;
3899         }
3900 
3901 
ndo2db_handle_servicegroupdefinition(ndo2db_idi * idi)3902 int ndo2db_handle_servicegroupdefinition(ndo2db_idi *idi){
3903 	int type,flags,attr;
3904 	struct timeval tstamp;
3905 	unsigned long object_id=0L;
3906 	unsigned long group_id=0L;
3907 	unsigned long member_id=0L;
3908 	int result=NDO_OK;
3909 	char *es[1];
3910 	int x=0;
3911 	char *buf=NULL;
3912 	char *buf1=NULL;
3913 	ndo2db_mbuf mbuf;
3914 	char *hptr=NULL;
3915 	char *sptr=NULL;
3916 
3917 	if(idi==NULL)
3918 		return NDO_ERROR;
3919 
3920 	/* convert timestamp, etc */
3921 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
3922 
3923 	/* don't store old data */
3924 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
3925 		return NDO_OK;
3926 
3927 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_SERVICEGROUPALIAS]);
3928 
3929 	/* get the object id */
3930 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICEGROUP,idi->buffered_input[NDO_DATA_SERVICEGROUPNAME],NULL,&object_id);
3931 
3932 	/* flag the object as being active */
3933 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_SERVICEGROUP,object_id);
3934 
3935 	/* add definition to db */
3936 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', servicegroup_object_id='%lu', alias='%s'"
3937 		    ,idi->dbinfo.instance_id
3938 		    ,idi->current_object_config_type
3939 		    ,object_id
3940 		    ,es[0]
3941 		   )==-1)
3942 		buf=NULL;
3943 
3944 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3945 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEGROUPS]
3946 		    ,buf
3947 		    ,buf
3948 		   )==-1)
3949 		buf1=NULL;
3950 
3951 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
3952 		group_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
3953 	}
3954 	free(buf);
3955 	free(buf1);
3956 
3957 	free(es[0]);
3958 
3959 	/* save members to db */
3960 	mbuf=idi->mbuf[NDO2DB_MBUF_SERVICEGROUPMEMBER];
3961 	for(x=0;x<mbuf.used_lines;x++){
3962 
3963 		if(mbuf.buffer[x]==NULL)
3964 			continue;
3965 
3966 		/* split the host/service name */
3967 		hptr=strtok(mbuf.buffer[x],";");
3968 		sptr=strtok(NULL,"\x0");
3969 
3970 		/* get the object id of the member */
3971 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,hptr,sptr,&member_id);
3972 
3973 		if(asprintf(&buf,"instance_id='%d', servicegroup_id='%lu', service_object_id='%lu'"
3974 			    ,idi->dbinfo.instance_id
3975 			    ,group_id
3976 			    ,member_id
3977 			   )==-1)
3978 			buf=NULL;
3979 
3980 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
3981 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEGROUPMEMBERS]
3982 			    ,buf
3983 			    ,buf
3984 			   )==-1)
3985 			buf1=NULL;
3986 
3987 		result=ndo2db_db_query(idi,buf1);
3988 		free(buf);
3989 		free(buf1);
3990 	        }
3991 
3992 	return NDO_OK;
3993         }
3994 
3995 
ndo2db_handle_hostdependencydefinition(ndo2db_idi * idi)3996 int ndo2db_handle_hostdependencydefinition(ndo2db_idi *idi){
3997 	int type,flags,attr;
3998 	struct timeval tstamp;
3999 	unsigned long object_id=0L;
4000 	unsigned long dependent_object_id=0L;
4001 	unsigned long timeperiod_object_id=0L;
4002 	int dependency_type=0;
4003 	int inherits_parent=0;
4004 	int fail_on_up=0;
4005 	int fail_on_down=0;
4006 	int fail_on_unreachable=0;
4007 	int result=NDO_OK;
4008 	char *buf=NULL;
4009 	char *buf1=NULL;
4010 
4011 	if(idi==NULL)
4012 		return NDO_ERROR;
4013 
4014 	/* convert timestamp, etc */
4015 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4016 
4017 	/* don't store old data */
4018 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4019 		return NDO_OK;
4020 
4021 	/* convert vars */
4022 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_DEPENDENCYTYPE],&dependency_type);
4023 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_INHERITSPARENT],&inherits_parent);
4024 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONUP],&fail_on_up);
4025 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONDOWN],&fail_on_down);
4026 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONUNREACHABLE],&fail_on_unreachable);
4027 
4028 	/* get the object ids */
4029 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOSTNAME],NULL,&object_id);
4030 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_DEPENDENTHOSTNAME],NULL,&dependent_object_id);
4031 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_DEPENDENCYPERIOD],NULL,&timeperiod_object_id);
4032 
4033 	/* add definition to db */
4034 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', host_object_id='%lu', dependent_host_object_id='%lu', dependency_type='%d', inherits_parent='%d', timeperiod_object_id='%lu', fail_on_up='%d', fail_on_down='%d', fail_on_unreachable='%d'"
4035 		    ,idi->dbinfo.instance_id
4036 		    ,idi->current_object_config_type
4037 		    ,object_id
4038 		    ,dependent_object_id
4039 		    ,dependency_type
4040 		    ,inherits_parent
4041 		    ,timeperiod_object_id
4042 		    ,fail_on_up
4043 		    ,fail_on_down
4044 		    ,fail_on_unreachable
4045 		   )==-1)
4046 		buf=NULL;
4047 
4048 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4049 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTDEPENDENCIES]
4050 		    ,buf
4051 		    ,buf
4052 		   )==-1)
4053 		buf1=NULL;
4054 
4055 	result=ndo2db_db_query(idi,buf1);
4056 	free(buf);
4057 	free(buf1);
4058 
4059 	return NDO_OK;
4060         }
4061 
4062 
ndo2db_handle_servicedependencydefinition(ndo2db_idi * idi)4063 int ndo2db_handle_servicedependencydefinition(ndo2db_idi *idi){
4064 	int type,flags,attr;
4065 	struct timeval tstamp;
4066 	unsigned long object_id=0L;
4067 	unsigned long dependent_object_id=0L;
4068 	unsigned long timeperiod_object_id=0L;
4069 	int dependency_type=0;
4070 	int inherits_parent=0;
4071 	int fail_on_ok=0;
4072 	int fail_on_warning=0;
4073 	int fail_on_unknown=0;
4074 	int fail_on_critical=0;
4075 	int result=NDO_OK;
4076 	char *buf=NULL;
4077 	char *buf1=NULL;
4078 
4079 	if(idi==NULL)
4080 		return NDO_ERROR;
4081 
4082 	/* convert timestamp, etc */
4083 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4084 
4085 	/* don't store old data */
4086 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4087 		return NDO_OK;
4088 
4089 	/* convert vars */
4090 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_DEPENDENCYTYPE],&dependency_type);
4091 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_INHERITSPARENT],&inherits_parent);
4092 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONOK],&fail_on_ok);
4093 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONWARNING],&fail_on_warning);
4094 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONUNKNOWN],&fail_on_unknown);
4095 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FAILONCRITICAL],&fail_on_critical);
4096 
4097 	/* get the object ids */
4098 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOSTNAME],idi->buffered_input[NDO_DATA_SERVICEDESCRIPTION],&object_id);
4099 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_DEPENDENTHOSTNAME],idi->buffered_input[NDO_DATA_DEPENDENTSERVICEDESCRIPTION],&dependent_object_id);
4100 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_DEPENDENCYPERIOD],NULL,&timeperiod_object_id);
4101 
4102 	/* add definition to db */
4103 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', service_object_id='%lu', dependent_service_object_id='%lu', dependency_type='%d', inherits_parent='%d', timeperiod_object_id='%lu', fail_on_ok='%d', fail_on_warning='%d', fail_on_unknown='%d', fail_on_critical='%d'"
4104 		    ,idi->dbinfo.instance_id
4105 		    ,idi->current_object_config_type
4106 		    ,object_id
4107 		    ,dependent_object_id
4108 		    ,dependency_type
4109 		    ,inherits_parent
4110 		    ,timeperiod_object_id
4111 		    ,fail_on_ok
4112 		    ,fail_on_warning
4113 		    ,fail_on_unknown
4114 		    ,fail_on_critical
4115 		   )==-1)
4116 		buf=NULL;
4117 
4118 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4119 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEDEPENDENCIES]
4120 		    ,buf
4121 		    ,buf
4122 		   )==-1)
4123 		buf1=NULL;
4124 
4125 	result=ndo2db_db_query(idi,buf1);
4126 	free(buf);
4127 	free(buf1);
4128 
4129 	return NDO_OK;
4130         }
4131 
4132 
ndo2db_handle_hostescalationdefinition(ndo2db_idi * idi)4133 int ndo2db_handle_hostescalationdefinition(ndo2db_idi *idi){
4134 	int type,flags,attr;
4135 	struct timeval tstamp;
4136 	unsigned long object_id=0L;
4137 	unsigned long timeperiod_id=0L;
4138 	unsigned long escalation_id=0L;
4139 	unsigned long member_id=0L;
4140 	int first_notification=0;
4141 	int last_notification=0;
4142 	double notification_interval=0.0;
4143 	int escalate_recovery=0;
4144 	int escalate_down=0;
4145 	int escalate_unreachable=0;
4146 	int result=NDO_OK;
4147 	int x=0;
4148 	char *buf=NULL;
4149 	char *buf1=NULL;
4150 	ndo2db_mbuf mbuf;
4151 
4152 	if(idi==NULL)
4153 		return NDO_ERROR;
4154 
4155 	/* convert timestamp, etc */
4156 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4157 
4158 	/* don't store old data */
4159 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4160 		return NDO_OK;
4161 
4162 	/* convert vars */
4163 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FIRSTNOTIFICATION],&first_notification);
4164 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_LASTNOTIFICATION],&last_notification);
4165 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_NOTIFICATIONINTERVAL],&notification_interval);
4166 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONRECOVERY],&escalate_recovery);
4167 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONDOWN],&escalate_down);
4168 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONUNREACHABLE],&escalate_unreachable);
4169 
4170 	/* get the object id */
4171 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_HOST,idi->buffered_input[NDO_DATA_HOSTNAME],NULL,&object_id);
4172 
4173 	/* get the timeperiod id */
4174 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_ESCALATIONPERIOD],NULL,&timeperiod_id);
4175 
4176 	/* add definition to db */
4177 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', host_object_id='%lu', timeperiod_object_id='%lu', first_notification='%d', last_notification='%d', notification_interval='%lf', escalate_on_recovery='%d', escalate_on_down='%d', escalate_on_unreachable='%d'"
4178 		    ,idi->dbinfo.instance_id
4179 		    ,idi->current_object_config_type
4180 		    ,object_id
4181 		    ,timeperiod_id
4182 		    ,first_notification
4183 		    ,last_notification
4184 		    ,notification_interval
4185 		    ,escalate_recovery
4186 		    ,escalate_down
4187 		    ,escalate_unreachable
4188 		   )==-1)
4189 		buf=NULL;
4190 
4191 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4192 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTESCALATIONS]
4193 		    ,buf
4194 		    ,buf
4195 		   )==-1)
4196 		buf1=NULL;
4197 
4198 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
4199 		escalation_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
4200 	}
4201 	free(buf);
4202 	free(buf1);
4203 
4204 	/* save contact groups to db */
4205 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTGROUP];
4206 	for(x=0;x<mbuf.used_lines;x++){
4207 
4208 		if(mbuf.buffer[x]==NULL)
4209 			continue;
4210 
4211 		/* get the object id of the member */
4212 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACTGROUP,mbuf.buffer[x],NULL,&member_id);
4213 
4214 		if(asprintf(&buf,"instance_id='%d', hostescalation_id='%lu', contactgroup_object_id='%lu'"
4215 			    ,idi->dbinfo.instance_id
4216 			    ,escalation_id
4217 			    ,member_id
4218 			   )==-1)
4219 			buf=NULL;
4220 
4221 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4222 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTESCALATIONCONTACTGROUPS]
4223 			    ,buf
4224 			    ,buf
4225 			   )==-1)
4226 			buf1=NULL;
4227 
4228 		result=ndo2db_db_query(idi,buf1);
4229 		free(buf);
4230 		free(buf1);
4231 	        }
4232 
4233 	/* save contacts to db */
4234 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACT];
4235 	for(x=0;x<mbuf.used_lines;x++){
4236 
4237 		if(mbuf.buffer[x]==NULL)
4238 			continue;
4239 
4240 		/* get the object id of the member */
4241 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,mbuf.buffer[x],NULL,&member_id);
4242 
4243 		if(asprintf(&buf,"instance_id='%d', hostescalation_id='%lu', contact_object_id='%lu'"
4244 			    ,idi->dbinfo.instance_id
4245 			    ,escalation_id
4246 			    ,member_id
4247 			   )==-1)
4248 			buf=NULL;
4249 
4250 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4251 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_HOSTESCALATIONCONTACTS]
4252 			    ,buf
4253 			    ,buf
4254 			   )==-1)
4255 			buf1=NULL;
4256 
4257 		result=ndo2db_db_query(idi,buf1);
4258 		free(buf);
4259 		free(buf1);
4260 	        }
4261 
4262 	return NDO_OK;
4263         }
4264 
4265 
ndo2db_handle_serviceescalationdefinition(ndo2db_idi * idi)4266 int ndo2db_handle_serviceescalationdefinition(ndo2db_idi *idi){
4267 	int type,flags,attr;
4268 	struct timeval tstamp;
4269 	unsigned long object_id=0L;
4270 	unsigned long timeperiod_id=0L;
4271 	unsigned long escalation_id=0L;
4272 	unsigned long member_id=0L;
4273 	int first_notification=0;
4274 	int last_notification=0;
4275 	double notification_interval=0.0;
4276 	int escalate_recovery=0;
4277 	int escalate_warning=0;
4278 	int escalate_unknown=0;
4279 	int escalate_critical=0;
4280 	int result=NDO_OK;
4281 	int x=0;
4282 	char *buf=NULL;
4283 	char *buf1=NULL;
4284 	ndo2db_mbuf mbuf;
4285 
4286 	if(idi==NULL)
4287 		return NDO_ERROR;
4288 
4289 	/* convert timestamp, etc */
4290 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4291 
4292 	/* don't store old data */
4293 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4294 		return NDO_OK;
4295 
4296 	/* convert vars */
4297 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_FIRSTNOTIFICATION],&first_notification);
4298 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_LASTNOTIFICATION],&last_notification);
4299 	result=ndo2db_convert_string_to_double(idi->buffered_input[NDO_DATA_NOTIFICATIONINTERVAL],&notification_interval);
4300 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONRECOVERY],&escalate_recovery);
4301 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONWARNING],&escalate_warning);
4302 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONUNKNOWN],&escalate_unknown);
4303 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ESCALATEONCRITICAL],&escalate_critical);
4304 
4305 	/* get the object id */
4306 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_SERVICE,idi->buffered_input[NDO_DATA_HOSTNAME],idi->buffered_input[NDO_DATA_SERVICEDESCRIPTION],&object_id);
4307 
4308 	/* get the timeperiod id */
4309 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_ESCALATIONPERIOD],NULL,&timeperiod_id);
4310 
4311 	/* add definition to db */
4312 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', service_object_id='%lu', timeperiod_object_id='%lu', first_notification='%d', last_notification='%d', notification_interval='%lf', escalate_on_recovery='%d', escalate_on_warning='%d', escalate_on_unknown='%d', escalate_on_critical='%d'"
4313 		    ,idi->dbinfo.instance_id
4314 		    ,idi->current_object_config_type
4315 		    ,object_id
4316 		    ,timeperiod_id
4317 		    ,first_notification
4318 		    ,last_notification
4319 		    ,notification_interval
4320 		    ,escalate_recovery
4321 		    ,escalate_warning
4322 		    ,escalate_unknown
4323 		    ,escalate_critical
4324 		   )==-1)
4325 		buf=NULL;
4326 
4327 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4328 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEESCALATIONS]
4329 		    ,buf
4330 		    ,buf
4331 		   )==-1)
4332 		buf1=NULL;
4333 
4334 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
4335 		escalation_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
4336 	}
4337 	free(buf);
4338 	free(buf1);
4339 
4340 	/* save contact groups to db */
4341 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTGROUP];
4342 	for(x=0;x<mbuf.used_lines;x++){
4343 
4344 		if(mbuf.buffer[x]==NULL)
4345 			continue;
4346 
4347 		/* get the object id of the member */
4348 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACTGROUP,mbuf.buffer[x],NULL,&member_id);
4349 
4350 		if(asprintf(&buf,"instance_id='%d', serviceescalation_id='%lu', contactgroup_object_id='%lu'"
4351 			    ,idi->dbinfo.instance_id
4352 			    ,escalation_id
4353 			    ,member_id
4354 			   )==-1)
4355 			buf=NULL;
4356 
4357 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4358 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEESCALATIONCONTACTGROUPS]
4359 			    ,buf
4360 			    ,buf
4361 			   )==-1)
4362 			buf1=NULL;
4363 
4364 		result=ndo2db_db_query(idi,buf1);
4365 		free(buf);
4366 		free(buf1);
4367 	        }
4368 
4369 	/* save contacts to db */
4370 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACT];
4371 	for(x=0;x<mbuf.used_lines;x++){
4372 
4373 		if(mbuf.buffer[x]==NULL)
4374 			continue;
4375 
4376 		/* get the object id of the member */
4377 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,mbuf.buffer[x],NULL,&member_id);
4378 
4379 		if(asprintf(&buf,"instance_id='%d', serviceescalation_id='%lu', contact_object_id='%lu'"
4380 			    ,idi->dbinfo.instance_id
4381 			    ,escalation_id
4382 			    ,member_id
4383 			   )==-1)
4384 			buf=NULL;
4385 
4386 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4387 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_SERVICEESCALATIONCONTACTS]
4388 			    ,buf
4389 			    ,buf
4390 			   )==-1)
4391 			buf1=NULL;
4392 
4393 		result=ndo2db_db_query(idi,buf1);
4394 		free(buf);
4395 		free(buf1);
4396 	        }
4397 
4398 	return NDO_OK;
4399         }
4400 
4401 
ndo2db_handle_commanddefinition(ndo2db_idi * idi)4402 int ndo2db_handle_commanddefinition(ndo2db_idi *idi){
4403 	int type,flags,attr;
4404 	struct timeval tstamp;
4405 	unsigned long object_id=0L;
4406 	int result=NDO_OK;
4407 	char *es[1];
4408 	int x=0;
4409 	char *buf=NULL;
4410 	char *buf1=NULL;
4411 
4412 	if(idi==NULL)
4413 		return NDO_ERROR;
4414 
4415 	/* convert timestamp, etc */
4416 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4417 
4418 	/* don't store old data */
4419 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4420 		return NDO_OK;
4421 
4422 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_COMMANDLINE]);
4423 
4424 	/* get the object id */
4425 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,idi->buffered_input[NDO_DATA_COMMANDNAME],NULL,&object_id);
4426 
4427 	/* flag the object as being active */
4428 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_COMMAND,object_id);
4429 
4430 	/* add definition to db */
4431 	if(asprintf(&buf,"instance_id='%lu', object_id='%lu', config_type='%d', command_line='%s'"
4432 		    ,idi->dbinfo.instance_id
4433 		    ,object_id
4434 		    ,idi->current_object_config_type
4435 		    ,es[0]
4436 		   )==-1)
4437 		buf=NULL;
4438 
4439 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4440 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_COMMANDS]
4441 		    ,buf
4442 		    ,buf
4443 		   )==-1)
4444 		buf1=NULL;
4445 
4446 	result=ndo2db_db_query(idi,buf1);
4447 	free(buf);
4448 	free(buf1);
4449 
4450 	for(x=0;x<1;x++)
4451 		free(es[x]);
4452 
4453 	return NDO_OK;
4454         }
4455 
4456 
ndo2db_handle_timeperiodefinition(ndo2db_idi * idi)4457 int ndo2db_handle_timeperiodefinition(ndo2db_idi *idi){
4458 	int type,flags,attr;
4459 	struct timeval tstamp;
4460 	unsigned long object_id=0L;
4461 	unsigned long timeperiod_id=0L;
4462 	char *dayptr=NULL;
4463 	char *startptr=NULL;
4464 	char *endptr=NULL;
4465 	int day=0;
4466 	unsigned long start_sec=0L;
4467 	unsigned long end_sec=0L;
4468 	int result=NDO_OK;
4469 	char *es[1];
4470 	int x=0;
4471 	char *buf=NULL;
4472 	char *buf1=NULL;
4473 	ndo2db_mbuf mbuf;
4474 
4475 	if(idi==NULL)
4476 		return NDO_ERROR;
4477 
4478 	/* convert timestamp, etc */
4479 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4480 
4481 	/* don't store old data */
4482 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4483 		return NDO_OK;
4484 
4485 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_TIMEPERIODALIAS]);
4486 
4487 	/* get the object id */
4488 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_TIMEPERIODNAME],NULL,&object_id);
4489 
4490 	/* flag the object as being active */
4491 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,object_id);
4492 
4493 	/* add definition to db */
4494 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', timeperiod_object_id='%lu', alias='%s'"
4495 		    ,idi->dbinfo.instance_id
4496 		    ,idi->current_object_config_type
4497 		    ,object_id
4498 		    ,es[0]
4499 		   )==-1)
4500 		buf=NULL;
4501 
4502 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4503 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEPERIODS]
4504 		    ,buf
4505 		    ,buf
4506 		   )==-1)
4507 		buf1=NULL;
4508 
4509 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
4510 		timeperiod_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
4511 	}
4512 	free(buf);
4513 	free(buf1);
4514 
4515 	free(es[0]);
4516 
4517 	/* save timeranges to db */
4518 	mbuf=idi->mbuf[NDO2DB_MBUF_TIMERANGE];
4519 	for(x=0;x<mbuf.used_lines;x++){
4520 
4521 		if(mbuf.buffer[x]==NULL)
4522 			continue;
4523 
4524 		/* get var name/val pair */
4525 		dayptr=strtok(mbuf.buffer[x],":");
4526 		startptr=strtok(NULL,"-");
4527 		endptr=strtok(NULL,"\x0");
4528 
4529 		if(startptr==NULL || endptr==NULL)
4530 			continue;
4531 
4532 		day=atoi(dayptr);
4533 		start_sec=strtoul(startptr,NULL,0);
4534 		end_sec=strtoul(endptr,NULL,0);
4535 
4536 		if(asprintf(&buf,"instance_id='%d', timeperiod_id='%lu', day='%d', start_sec='%lu', end_sec='%lu'"
4537 			    ,idi->dbinfo.instance_id
4538 			    ,timeperiod_id
4539 			    ,day
4540 			    ,start_sec
4541 			    ,end_sec
4542 			   )==-1)
4543 			buf=NULL;
4544 
4545 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4546 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_TIMEPERIODTIMERANGES]
4547 			    ,buf
4548 			    ,buf
4549 			   )==-1)
4550 			buf1=NULL;
4551 
4552 		result=ndo2db_db_query(idi,buf1);
4553 		free(buf);
4554 		free(buf1);
4555 	        }
4556 
4557 	return NDO_OK;
4558         }
4559 
4560 
ndo2db_handle_contactdefinition(ndo2db_idi * idi)4561 int ndo2db_handle_contactdefinition(ndo2db_idi *idi){
4562 	int type,flags,attr;
4563 	struct timeval tstamp;
4564 	unsigned long contact_id=0L;
4565 	unsigned long host_timeperiod_id=0L;
4566 	unsigned long service_timeperiod_id=0L;
4567 	int host_notifications_enabled=0;
4568 	int service_notifications_enabled=0;
4569 	int can_submit_commands=0;
4570 	int notify_service_recovery=0;
4571 	int notify_service_warning=0;
4572 	int notify_service_unknown=0;
4573 	int notify_service_critical=0;
4574 	int notify_service_flapping=0;
4575 	int notify_service_downtime=0;
4576 	int notify_host_recovery=0;
4577 	int notify_host_down=0;
4578 	int notify_host_unreachable=0;
4579 	int notify_host_flapping=0;
4580 	int notify_host_downtime=0;
4581 	unsigned long command_id=0L;
4582 	int result=NDO_OK;
4583 	char *es[3];
4584 	int x=0;
4585 	char *buf=NULL;
4586 	char *buf1=NULL;
4587 	ndo2db_mbuf mbuf;
4588 	char *numptr=NULL;
4589 	char *addressptr=NULL;
4590 	int address_number=0;
4591 	char *cmdptr=NULL;
4592 	char *argptr=NULL;
4593 #ifdef BUILD_NAGIOS_4X
4594 	int minimum_importance=0;
4595 #endif
4596 
4597 	if(idi==NULL)
4598 		return NDO_ERROR;
4599 
4600 	/* convert timestamp, etc */
4601 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4602 
4603 	/* don't store old data */
4604 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4605 		return NDO_OK;
4606 
4607 	/* convert vars */
4608 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_HOSTNOTIFICATIONSENABLED],&host_notifications_enabled);
4609 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_SERVICENOTIFICATIONSENABLED],&service_notifications_enabled);
4610 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_CANSUBMITCOMMANDS],&can_submit_commands);
4611 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEWARNING],&notify_service_warning);
4612 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEUNKNOWN],&notify_service_unknown);
4613 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICECRITICAL],&notify_service_critical);
4614 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICERECOVERY],&notify_service_recovery);
4615 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEFLAPPING],&notify_service_flapping);
4616 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYSERVICEDOWNTIME],&notify_service_downtime);
4617 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTDOWN],&notify_host_down);
4618 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTUNREACHABLE],&notify_host_unreachable);
4619 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTRECOVERY],&notify_host_recovery);
4620 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTFLAPPING],&notify_host_flapping);
4621 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_NOTIFYHOSTDOWNTIME],&notify_host_downtime);
4622 #ifdef BUILD_NAGIOS_4X
4623 	result=ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_MINIMUMIMPORTANCE],&minimum_importance);
4624 #endif
4625 
4626 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_CONTACTALIAS]);
4627 	es[1]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_EMAILADDRESS]);
4628 	es[2]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_PAGERADDRESS]);
4629 
4630 	/* get the object id */
4631 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,idi->buffered_input[NDO_DATA_CONTACTNAME],NULL,&contact_id);
4632 
4633 	/* get the timeperiod ids */
4634 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_HOSTNOTIFICATIONPERIOD],NULL,&host_timeperiod_id);
4635 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_TIMEPERIOD,idi->buffered_input[NDO_DATA_SERVICENOTIFICATIONPERIOD],NULL,&service_timeperiod_id);
4636 
4637 	/* flag the object as being active */
4638 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_CONTACT,contact_id);
4639 
4640 	/* add definition to db */
4641 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', contact_object_id='%lu', alias='%s', email_address='%s', pager_address='%s', host_timeperiod_object_id='%lu', service_timeperiod_object_id='%lu', host_notifications_enabled='%d', service_notifications_enabled='%d', can_submit_commands='%d', notify_service_recovery='%d', notify_service_warning='%d', notify_service_unknown='%d', notify_service_critical='%d', notify_service_flapping='%d', notify_service_downtime='%d', notify_host_recovery='%d', notify_host_down='%d', notify_host_unreachable='%d', notify_host_flapping='%d', notify_host_downtime='%d'"
4642 #ifdef BUILD_NAGIOS_4X
4643 			", minimum_importance='%d'"
4644 #endif
4645 		    ,idi->dbinfo.instance_id
4646 		    ,idi->current_object_config_type
4647 		    ,contact_id
4648 		    ,es[0]
4649 		    ,es[1]
4650 		    ,es[2]
4651 		    ,host_timeperiod_id
4652 		    ,service_timeperiod_id
4653 		    ,host_notifications_enabled
4654 		    ,service_notifications_enabled
4655 		    ,can_submit_commands
4656 		    ,notify_service_recovery
4657 		    ,notify_service_warning
4658 		    ,notify_service_unknown
4659 		    ,notify_service_critical
4660 		    ,notify_service_flapping
4661 		    ,notify_service_downtime
4662 		    ,notify_host_recovery
4663 		    ,notify_host_down
4664 		    ,notify_host_unreachable
4665 		    ,notify_host_flapping
4666 		    ,notify_host_downtime
4667 #ifdef BUILD_NAGIOS_4X
4668 			,minimum_importance
4669 #endif
4670 		   )==-1)
4671 		buf=NULL;
4672 
4673 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4674 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTS]
4675 		    ,buf
4676 		    ,buf
4677 		   )==-1)
4678 		buf1=NULL;
4679 
4680 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
4681 		contact_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
4682 	}
4683 	free(buf);
4684 	free(buf1);
4685 
4686 	for(x=0;x<3;x++)
4687 		free(es[x]);
4688 
4689 	/* save addresses to db */
4690 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTADDRESS];
4691 	for(x=0;x<mbuf.used_lines;x++){
4692 
4693 		if(mbuf.buffer[x]==NULL)
4694 			continue;
4695 
4696 		numptr=strtok(mbuf.buffer[x],":");
4697 		addressptr=strtok(NULL,"\x0");
4698 
4699 		if(numptr==NULL || addressptr==NULL)
4700 			continue;
4701 
4702 		address_number=atoi(numptr);
4703 		es[0]=ndo2db_db_escape_string(idi,addressptr);
4704 
4705 		if(asprintf(&buf,"instance_id='%d', contact_id='%lu', address_number='%d', address='%s'"
4706 			    ,idi->dbinfo.instance_id
4707 			    ,contact_id
4708 			    ,address_number
4709 			    ,es[0]
4710 			   )==-1)
4711 			buf=NULL;
4712 
4713 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4714 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTADDRESSES]
4715 			    ,buf
4716 			    ,buf
4717 			   )==-1)
4718 			buf1=NULL;
4719 
4720 		result=ndo2db_db_query(idi,buf1);
4721 		free(buf);
4722 		free(buf1);
4723 
4724 		free(es[0]);
4725 	        }
4726 
4727 	/* save host notification commands to db */
4728 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTADDRESS];
4729 	for(x=0;x<mbuf.used_lines;x++){
4730 
4731 		if(mbuf.buffer[x]==NULL)
4732 			continue;
4733 
4734 		cmdptr=strtok(mbuf.buffer[x],"!");
4735 		argptr=strtok(NULL,"\x0");
4736 
4737 		if(numptr==NULL)
4738 			continue;
4739 
4740 		/* find the command */
4741 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,cmdptr,NULL,&command_id);
4742 
4743 		es[0]=ndo2db_db_escape_string(idi,argptr);
4744 
4745 		if(asprintf(&buf,"instance_id='%d', contact_id='%lu', notification_type='%d', command_object_id='%lu', command_args='%s'"
4746 			    ,idi->dbinfo.instance_id
4747 			    ,contact_id
4748 			    ,HOST_NOTIFICATION
4749 			    ,command_id
4750 			    ,(es[0]==NULL)?"":es[0]
4751 			   )==-1)
4752 			buf=NULL;
4753 
4754 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4755 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONCOMMANDS]
4756 			    ,buf
4757 			    ,buf
4758 			   )==-1)
4759 			buf1=NULL;
4760 
4761 		result=ndo2db_db_query(idi,buf1);
4762 		free(buf);
4763 		free(buf1);
4764 
4765 		free(es[0]);
4766 	        }
4767 
4768 	/* save service notification commands to db */
4769 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTADDRESS];
4770 	for(x=0;x<mbuf.used_lines;x++){
4771 
4772 		if(mbuf.buffer[x]==NULL)
4773 			continue;
4774 
4775 		cmdptr=strtok(mbuf.buffer[x],"!");
4776 		argptr=strtok(NULL,"\x0");
4777 
4778 		if(numptr==NULL)
4779 			continue;
4780 
4781 		/* find the command */
4782 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_COMMAND,cmdptr,NULL,&command_id);
4783 
4784 		es[0]=ndo2db_db_escape_string(idi,argptr);
4785 
4786 		if(asprintf(&buf,"instance_id='%d', contact_id='%lu', notification_type='%d', command_object_id='%lu', command_args='%s'"
4787 			    ,idi->dbinfo.instance_id
4788 			    ,contact_id
4789 			    ,SERVICE_NOTIFICATION
4790 			    ,command_id
4791 			    ,(es[0]==NULL)?"":es[0]
4792 			   )==-1)
4793 			buf=NULL;
4794 
4795 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4796 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTNOTIFICATIONCOMMANDS]
4797 			    ,buf
4798 			    ,buf
4799 			   )==-1)
4800 			buf1=NULL;
4801 
4802 		result=ndo2db_db_query(idi,buf1);
4803 		free(buf);
4804 		free(buf1);
4805 
4806 		free(es[0]);
4807 	}
4808 
4809 	/* save custom variables to db */
4810 	result=ndo2db_save_custom_variables(idi,NDO2DB_DBTABLE_CUSTOMVARIABLES,contact_id,NULL);
4811 
4812 	return NDO_OK;
4813         }
4814 
4815 
ndo2db_handle_contactgroupdefinition(ndo2db_idi * idi)4816 int ndo2db_handle_contactgroupdefinition(ndo2db_idi *idi){
4817 	int type,flags,attr;
4818 	struct timeval tstamp;
4819 	unsigned long object_id=0L;
4820 	unsigned long group_id=0L;
4821 	unsigned long member_id=0L;
4822 	int result=NDO_OK;
4823 	char *es[1];
4824 	int x=0;
4825 	char *buf=NULL;
4826 	char *buf1=NULL;
4827 	ndo2db_mbuf mbuf;
4828 
4829 	if(idi==NULL)
4830 		return NDO_ERROR;
4831 
4832 	/* convert timestamp, etc */
4833 	result=ndo2db_convert_standard_data_elements(idi,&type,&flags,&attr,&tstamp);
4834 
4835 	/* don't store old data */
4836 	if(tstamp.tv_sec<idi->dbinfo.latest_realtime_data_time)
4837 		return NDO_OK;
4838 
4839 	es[0]=ndo2db_db_escape_string(idi,idi->buffered_input[NDO_DATA_CONTACTGROUPALIAS]);
4840 
4841 	/* get the object id */
4842 	result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACTGROUP,idi->buffered_input[NDO_DATA_CONTACTGROUPNAME],NULL,&object_id);
4843 
4844 	/* flag the object as being active */
4845 	ndo2db_set_object_as_active(idi,NDO2DB_OBJECTTYPE_CONTACTGROUP,object_id);
4846 
4847 	/* add definition to db */
4848 	if(asprintf(&buf,"instance_id='%lu', config_type='%d', contactgroup_object_id='%lu', alias='%s'"
4849 		    ,idi->dbinfo.instance_id
4850 		    ,idi->current_object_config_type
4851 		    ,object_id
4852 		    ,es[0]
4853 		   )==-1)
4854 		buf=NULL;
4855 
4856 	if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4857 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTGROUPS]
4858 		    ,buf
4859 		    ,buf
4860 		   )==-1)
4861 		buf1=NULL;
4862 
4863 	if((result=ndo2db_db_query(idi,buf1))==NDO_OK){
4864 		group_id=mysql_insert_id(&idi->dbinfo.mysql_conn);
4865 	}
4866 	free(buf);
4867 	free(buf1);
4868 
4869 	free(es[0]);
4870 
4871 	/* save contact group members to db */
4872 	mbuf=idi->mbuf[NDO2DB_MBUF_CONTACTGROUPMEMBER];
4873 	for(x=0;x<mbuf.used_lines;x++){
4874 
4875 		if(mbuf.buffer[x]==NULL)
4876 			continue;
4877 
4878 		/* get the object id of the member */
4879 		result=ndo2db_get_object_id_with_insert(idi,NDO2DB_OBJECTTYPE_CONTACT,mbuf.buffer[x],NULL,&member_id);
4880 
4881 		if(asprintf(&buf,"instance_id='%d', contactgroup_id='%lu', contact_object_id='%lu'"
4882 			    ,idi->dbinfo.instance_id
4883 			    ,group_id
4884 			    ,member_id
4885 			   )==-1)
4886 			buf=NULL;
4887 
4888 		if(asprintf(&buf1,"INSERT INTO %s SET %s ON DUPLICATE KEY UPDATE %s"
4889 			    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_CONTACTGROUPMEMBERS]
4890 			    ,buf
4891 			    ,buf
4892 			   )==-1)
4893 			buf1=NULL;
4894 
4895 		result=ndo2db_db_query(idi,buf1);
4896 		free(buf);
4897 		free(buf1);
4898 	        }
4899 
4900 	return NDO_OK;
4901         }
4902 
4903 /*
4904  * In this function, we get a list of up to 250 objects, with the object type
4905  * stored in idi->buffered_input[NDO_DATA_ACTIVEOBJECTSTYPE]. The list of
4906  * object names are in the idi->buffered_input with indexes from 1 to 250.
4907  * We generate an UPDATE query to set them all active at one time.
4908  */
ndo2db_handle_activeobjectlist(ndo2db_idi * idi)4909 int ndo2db_handle_activeobjectlist(ndo2db_idi *idi)
4910 {
4911 	ndo_dbuf	dbuf;
4912 	char		*buf = NULL, *name1, *name2 = NULL;
4913 	int			rc, i, object_type, num_objs = 0, sz, first = 1;
4914 
4915 	if(idi==NULL)
4916 		return NDO_ERROR;
4917 
4918 	/* What type of object are we dealing with? */
4919 	ndo2db_convert_string_to_int(idi->buffered_input[NDO_DATA_ACTIVEOBJECTSTYPE], &object_type);
4920 
4921 	/* Find out how many objects we're dealing with */
4922 	while (idi->buffered_input[num_objs])
4923 		++num_objs;
4924 
4925 	/* Estimate the number of bytes we'll need for the SQL statement */
4926 	sz = ((num_objs * 25) & !0x3ff) | 0x800;
4927 
4928 	/* Convert object type into correct value for the table */
4929 	switch (object_type) {
4930 		case NDO_API_HOSTDEFINITION:
4931 			object_type = NDO2DB_OBJECTTYPE_HOST;
4932 			break;
4933 		case NDO_API_HOSTGROUPDEFINITION:
4934 			object_type = NDO2DB_OBJECTTYPE_HOSTGROUP;
4935 			break;
4936 		case NDO_API_SERVICEDEFINITION:
4937 			object_type = NDO2DB_OBJECTTYPE_SERVICE;
4938 			break;
4939 		case NDO_API_SERVICEGROUPDEFINITION:
4940 			object_type = NDO2DB_OBJECTTYPE_SERVICEGROUP;
4941 			break;
4942 		case NDO_API_COMMANDDEFINITION:
4943 			object_type = NDO2DB_OBJECTTYPE_COMMAND;
4944 			break;
4945 		case NDO_API_TIMEPERIODDEFINITION:
4946 			object_type = NDO2DB_OBJECTTYPE_TIMEPERIOD;
4947 			break;
4948 		case NDO_API_CONTACTDEFINITION:
4949 			object_type = NDO2DB_OBJECTTYPE_CONTACT;
4950 			break;
4951 		case NDO_API_CONTACTGROUPDEFINITION:
4952 			object_type = NDO2DB_OBJECTTYPE_CONTACTGROUP;
4953 			break;
4954 		default:
4955 			return NDO_ERROR;
4956 	}
4957 
4958 	ndo_dbuf_init(&dbuf, sz);
4959 
4960 	/* Build the first part of the query */
4961 	rc = asprintf(
4962 			&buf, "UPDATE %s SET is_active='1' WHERE instance_id='%lu' "
4963 			"AND objecttype_id='%d' AND ("
4964 		    ,ndo2db_db_tablenames[NDO2DB_DBTABLE_OBJECTS],
4965 			idi->dbinfo.instance_id, object_type);
4966 	if (rc == -1) {
4967 		ndo_dbuf_free(&dbuf);
4968 		syslog(LOG_ERR, "Error: memory allocation error in ndo2db_handle_activeobjectlist()");
4969 		return NDO_ERROR;
4970 	}
4971 	if (ndo_dbuf_strcat(&dbuf, buf) == NDO_ERROR) {
4972 		ndo_dbuf_free(&dbuf);
4973 		free(buf);
4974 		syslog(LOG_ERR, "Error: memory allocation error in ndo2db_handle_activeobjectlist()");
4975 		return NDO_ERROR;
4976 	}
4977 	free(buf);
4978 
4979 	while (num_objs) {
4980 		if (first)
4981 			first = 0;
4982 		else
4983 			ndo_dbuf_strcat(&dbuf, "OR ");
4984 
4985 		if (object_type == NDO2DB_OBJECTTYPE_SERVICE) {
4986 			name2 = ndo2db_db_escape_string(idi, idi->buffered_input[num_objs--]);
4987 			name1 = ndo2db_db_escape_string(idi, idi->buffered_input[num_objs--]);
4988 			rc = asprintf(&buf, "(name1='%s' AND name2='%s')", name1, name2);
4989 
4990 		} else {
4991 			name1 = ndo2db_db_escape_string(idi, idi->buffered_input[num_objs--]);
4992 			rc = asprintf(&buf, "name1='%s'", name1);
4993 		}
4994 
4995 		free(name1);
4996 		free(name2);
4997 
4998 		if (rc == -1) {
4999 			ndo_dbuf_free(&dbuf);
5000 			free(buf);
5001 			syslog(LOG_ERR, "Error: memory allocation error in ndo2db_handle_activeobjectlist()");
5002 			return NDO_ERROR;
5003 		}
5004 		if (ndo_dbuf_strcat(&dbuf, buf) == NDO_ERROR) {
5005 			ndo_dbuf_free(&dbuf);
5006 			free(buf);
5007 			syslog(LOG_ERR, "Error: memory allocation error in ndo2db_handle_activeobjectlist()");
5008 			return NDO_ERROR;
5009 		}
5010 
5011 		free(buf);
5012 	}
5013 
5014 	if (ndo_dbuf_strcat(&dbuf, ")") == NDO_ERROR) {
5015 		ndo_dbuf_free(&dbuf);
5016 		syslog(LOG_ERR, "Error: memory allocation error in ndo2db_handle_activeobjectlist()");
5017 		return NDO_ERROR;
5018 	}
5019 
5020 	if (!first)
5021 		rc = ndo2db_db_query(idi, dbuf.buf);
5022 	ndo_dbuf_free(&dbuf);
5023 	return rc;
5024 }
5025 
ndo2db_save_custom_variables(ndo2db_idi * idi,int table_idx,unsigned long o_id,char * ts)5026 int ndo2db_save_custom_variables(ndo2db_idi *idi,int table_idx, unsigned long o_id, char *ts ){
5027 	char *buf=NULL;
5028 	char *buf1=NULL;
5029 	ndo2db_mbuf mbuf;
5030 	char *es[2];
5031 	char *ptr1=NULL;
5032 	char *ptr2=NULL;
5033 	char *ptr3=NULL;
5034 	int result=NDO_OK;
5035 	int has_been_modified=0;
5036 	int x=0;
5037 
5038 	/* save custom variables to db */
5039 	mbuf=idi->mbuf[NDO2DB_MBUF_CUSTOMVARIABLE];
5040 	for(x=0;x<mbuf.used_lines;x++){
5041 
5042 		if(mbuf.buffer[x]==NULL)
5043 			continue;
5044 
5045 		if((ptr1=strtok(mbuf.buffer[x],":"))==NULL)
5046 			continue;
5047 
5048 		es[0]=strdup(ptr1);
5049 		if((ptr2=strtok(NULL,":"))==NULL)
5050 			continue;
5051 		has_been_modified=atoi(ptr2);
5052 		ptr3=strtok(NULL,"\n");
5053 
5054 		buf1=strdup((ptr3==NULL)?"":ptr3);
5055 		es[1]=ndo2db_db_escape_string(idi,buf1);
5056 		free(buf1);
5057 
5058 		if (table_idx==NDO2DB_DBTABLE_CUSTOMVARIABLES) {
5059 			if(asprintf(&buf,"instance_id='%d', object_id='%lu', config_type='%d', has_been_modified='%d', varname='%s', varvalue='%s'"
5060 					,idi->dbinfo.instance_id
5061 					,o_id
5062 					,idi->current_object_config_type
5063 					,has_been_modified
5064 					,(es[0]==NULL)?"":es[0]
5065 					,(es[1]==NULL)?"":es[1]
5066 				)==-1)
5067 				buf=NULL;
5068 		}
5069 		if (table_idx==NDO2DB_DBTABLE_CUSTOMVARIABLESTATUS) {
5070 			if(asprintf(&buf,"instance_id='%d', object_id='%lu',status_update_time=%s, has_been_modified='%d', varname='%s', varvalue='%s'"
5071 					,idi->dbinfo.instance_id
5072 					,o_id
5073 					,(ts==NULL)?"NULL":ts
5074 					,has_been_modified
5075 					,(es[0]==NULL)?"":es[0]
5076 					,(es[1]==NULL)?"":es[1]
5077 				)==-1)
5078 				buf=NULL;
5079 		}
5080 		free(es[0]);
5081 		free(es[1]);
5082 
5083 		if(asprintf(&buf1,"INSERT INtO %s SET %s ON DUPLICATE KEY UPDATE %s"
5084 			    ,ndo2db_db_tablenames[table_idx]
5085 			    ,buf
5086 			    ,buf
5087 			   )==-1)
5088 			buf1=NULL;
5089 
5090 		result=ndo2db_db_query(idi,buf1);
5091 		free(buf);
5092 		free(buf1);
5093 	}
5094 	return result;
5095 }
5096