/
xodtemplate.c
10334 lines (8574 loc) · 377 KB
/
xodtemplate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* XODTEMPLATE.C - Template-based object configuration data input routines
*
*
* Description:
*
* Routines for parsing and resolving template-based object definitions.
* Basic steps involved in this in the daemon are as follows:
*
* 1) Read
* 2) Resolve
* 3) Duplicate
* 4) Recombobulate
* 5) Cache
* 7) Register
* 8) Cleanup
*
* The steps involved for the CGIs differ a bit, since they read the cached
* definitions which are already resolved, recombobulated and duplicated. In
* otherwords, they've already been "flattened"...
*
* 1) Read
* 2) Register
* 3) Cleanup
*
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/
/*********** COMMON HEADER FILES ***********/
#include "../include/config.h"
#include "../include/common.h"
#include "../include/objects.h"
#include "../include/locations.h"
#include "../include/macros.h"
/**** CORE OR CGI SPECIFIC HEADER FILES ****/
#ifdef NSCORE
#include "../include/nagios.h"
#endif
#ifdef NSCGI
#include "../include/cgiutils.h"
#endif
/**** DATA INPUT-SPECIFIC HEADER FILES ****/
#include "xodtemplate.h"
#define XOD_NEW 0 /* not seen */
#define XOD_SEEN 1 /* seen, but not yet loopy */
#define XOD_LOOPY 2 /* loopy */
#define XOD_OK 3 /* not loopy */
xodtemplate_timeperiod *xodtemplate_timeperiod_list = NULL;
xodtemplate_command *xodtemplate_command_list = NULL;
xodtemplate_contactgroup *xodtemplate_contactgroup_list = NULL;
xodtemplate_hostgroup *xodtemplate_hostgroup_list = NULL;
xodtemplate_servicegroup *xodtemplate_servicegroup_list = NULL;
xodtemplate_servicedependency *xodtemplate_servicedependency_list = NULL;
xodtemplate_serviceescalation *xodtemplate_serviceescalation_list = NULL;
xodtemplate_contact *xodtemplate_contact_list = NULL;
xodtemplate_host *xodtemplate_host_list = NULL;
xodtemplate_service *xodtemplate_service_list = NULL;
xodtemplate_hostdependency *xodtemplate_hostdependency_list = NULL;
xodtemplate_hostescalation *xodtemplate_hostescalation_list = NULL;
xodtemplate_hostextinfo *xodtemplate_hostextinfo_list = NULL;
xodtemplate_serviceextinfo *xodtemplate_serviceextinfo_list = NULL;
xodtemplate_timeperiod *xodtemplate_timeperiod_list_tail = NULL;
xodtemplate_command *xodtemplate_command_list_tail = NULL;
xodtemplate_contactgroup *xodtemplate_contactgroup_list_tail = NULL;
xodtemplate_hostgroup *xodtemplate_hostgroup_list_tail = NULL;
xodtemplate_servicegroup *xodtemplate_servicegroup_list_tail = NULL;
xodtemplate_servicedependency *xodtemplate_servicedependency_list_tail = NULL;
xodtemplate_serviceescalation *xodtemplate_serviceescalation_list_tail = NULL;
xodtemplate_contact *xodtemplate_contact_list_tail = NULL;
xodtemplate_host *xodtemplate_host_list_tail = NULL;
xodtemplate_service *xodtemplate_service_list_tail = NULL;
xodtemplate_hostdependency *xodtemplate_hostdependency_list_tail = NULL;
xodtemplate_hostescalation *xodtemplate_hostescalation_list_tail = NULL;
xodtemplate_hostextinfo *xodtemplate_hostextinfo_list_tail = NULL;
xodtemplate_serviceextinfo *xodtemplate_serviceextinfo_list_tail = NULL;
skiplist *xobject_template_skiplists[NUM_XOBJECT_SKIPLISTS];
skiplist *xobject_skiplists[NUM_XOBJECT_SKIPLISTS];
void *xodtemplate_current_object = NULL;
int xodtemplate_current_object_type = XODTEMPLATE_NONE;
int xodtemplate_current_config_file = 0;
char **xodtemplate_config_files = NULL;
int presorted_objects = FALSE;
/* xodtemplate id / object counter */
static struct object_count xodcount;
/* reusable bitmaps for expanding objects */
static bitmap *host_map = NULL, *contact_map = NULL;
static bitmap *service_map = NULL, *parent_map = NULL;
/* These variables are defined in base/utils.c, but as CGIs do not need these
we just fake the values for this file */
#ifdef NSCGI
#define use_precached_objects TRUE
#define use_regexp_matches FALSE
#define use_true_regexp_matching FALSE
#define test_scheduling FALSE
#endif
/*
* simple inheritance macros. o = object, t = template, v = variable
* Note that these can be used for inter-object inheritance as well,
* so long as the variable names are identical.
*/
#define xod_inherit(o, t, v) \
do { \
if(o->have_##v == FALSE && t->have_##v == TRUE) { \
o->v = t->v; \
o->have_##v = TRUE; \
} \
} while(0)
#define xod_inherit_str_nohave(o, t, v) \
do { \
if(o->v == NULL && t->v != NULL) { \
o->v = (char *)strdup(t->v); \
} \
} while(0)
#define xod_inherit_str(o, t, v) \
do { \
if(o->have_##v == FALSE && t->have_##v == TRUE) { \
xod_inherit_str_nohave(o, t, v); \
o->have_##v = TRUE; \
} \
} while(0)
/* returns the name of a numbered config file */
static const char *xodtemplate_config_file_name(int cfgfile) {
if(cfgfile <= xodtemplate_current_config_file)
return xodtemplate_config_files[cfgfile - 1];
return "?";
}
/******************************************************************/
/************* TOP-LEVEL CONFIG DATA INPUT FUNCTION ***************/
/******************************************************************/
#ifndef NSCGI
static void xodtemplate_free_template_skiplists(void) {
int x = 0;
for(x = 0; x < NUM_XOBJECT_SKIPLISTS; x++) {
skiplist_free(&xobject_template_skiplists[x]);
}
}
#endif
/* process all config files - both core and CGIs pass in name of main config file */
int xodtemplate_read_config_data(const char *main_config_file, int options) {
#ifdef NSCORE
char *cfgfile = NULL;
char *config_base_dir = NULL;
char *input = NULL;
char *var = NULL;
char *val = NULL;
double runtime[11];
mmapfile *thefile = NULL;
#endif
struct timeval tv[12];
int result = OK;
if(main_config_file == NULL) {
#ifdef NSCORE
printf("Error: No main config file passed to object routines!\n");
#endif
return ERROR;
}
timing_point("Reading config data from '%s'\n", main_config_file);
/* initialize variables */
xodtemplate_timeperiod_list = NULL;
xodtemplate_command_list = NULL;
xodtemplate_contactgroup_list = NULL;
xodtemplate_hostgroup_list = NULL;
xodtemplate_servicegroup_list = NULL;
xodtemplate_servicedependency_list = NULL;
xodtemplate_serviceescalation_list = NULL;
xodtemplate_contact_list = NULL;
xodtemplate_host_list = NULL;
xodtemplate_service_list = NULL;
xodtemplate_hostdependency_list = NULL;
xodtemplate_hostescalation_list = NULL;
xodtemplate_hostextinfo_list = NULL;
xodtemplate_serviceextinfo_list = NULL;
/* initialize skiplists */
xodtemplate_init_xobject_skiplists();
xodtemplate_current_object = NULL;
xodtemplate_current_object_type = XODTEMPLATE_NONE;
/* allocate memory for 256 config files (increased dynamically) */
xodtemplate_current_config_file = 0;
xodtemplate_config_files = (char **)malloc(256 * sizeof(char **));
if(xodtemplate_config_files == NULL) {
#ifdef NSCORE
printf("Unable to allocate memory!\n");
#endif
return ERROR;
}
/* are the objects we're reading already pre-sorted? */
presorted_objects = FALSE;
#ifdef NSCORE
presorted_objects = (use_precached_objects == TRUE) ? TRUE : FALSE;
#endif
#ifdef NSCORE
if(test_scheduling == TRUE)
gettimeofday(&tv[0], NULL);
/* only process the precached object file as long as we're not regenerating it and we're not verifying the config */
if(use_precached_objects == TRUE)
result = xodtemplate_process_config_file(object_precache_file, options);
/* process object config files normally... */
else {
/* determine the directory of the main config file */
if((cfgfile = (char *)strdup(main_config_file)) == NULL) {
my_free(xodtemplate_config_files);
printf("Unable to allocate memory!\n");
return ERROR;
}
config_base_dir = (char *)strdup(dirname(cfgfile));
my_free(cfgfile);
/* open the main config file for reading (we need to find all the config files to read) */
if((thefile = mmap_fopen(main_config_file)) == NULL) {
my_free(config_base_dir);
my_free(xodtemplate_config_files);
printf("Unable to open main config file '%s'\n", main_config_file);
return ERROR;
}
/* daemon reads all config files/dirs specified in the main config file */
/* read in all lines from the main config file */
while(1) {
/* free memory */
my_free(input);
/* get the next line */
if((input = mmap_fgets_multiline(thefile)) == NULL)
break;
/* strip input */
strip(input);
/* skip blank lines and comments */
if(input[0] == '#' || input[0] == ';' || input[0] == '\x0')
continue;
if((var = strtok(input, "=")) == NULL)
continue;
if((val = strtok(NULL, "\n")) == NULL)
continue;
/* process a single config file */
if(!strcmp(var, "xodtemplate_config_file") || !strcmp(var, "cfg_file")) {
if(config_base_dir != NULL && val[0] != '/') {
asprintf(&cfgfile, "%s/%s", config_base_dir, val);
}
else
cfgfile = strdup(val);
/* process the config file... */
result = xodtemplate_process_config_file(cfgfile, options);
my_free(cfgfile);
/* if there was an error processing the config file, break out of loop */
if(result == ERROR)
break;
}
/* process all files in a config directory */
else if(!strcmp(var, "xodtemplate_config_dir") || !strcmp(var, "cfg_dir")) {
if(config_base_dir != NULL && val[0] != '/') {
asprintf(&cfgfile, "%s/%s", config_base_dir, val);
}
else
cfgfile = strdup(val);
/* strip trailing / if necessary */
if(cfgfile != NULL && cfgfile[strlen(cfgfile) - 1] == '/')
cfgfile[strlen(cfgfile) - 1] = '\x0';
/* process the config directory... */
result = xodtemplate_process_config_dir(cfgfile, options);
my_free(cfgfile);
/* if there was an error processing the config file, break out of loop */
if(result == ERROR)
break;
}
}
/* free memory and close the file */
my_free(config_base_dir);
my_free(input);
mmap_fclose(thefile);
}
if(test_scheduling == TRUE)
gettimeofday(&tv[1], NULL);
#endif
#ifdef NSCGI
/* CGIs process only one file - the cached objects file */
result = xodtemplate_process_config_file(object_cache_file, options);
#endif
timing_point("Done parsing config files\n");
#ifdef NSCORE
/* only perform intensive operations if we're not using the precached object file */
if(use_precached_objects == FALSE) {
/* resolve objects definitions */
if(result == OK)
result = xodtemplate_resolve_objects();
timing_point("Done resolving objects\n");
/* these are no longer needed */
xodtemplate_free_template_skiplists();
if(test_scheduling == TRUE)
gettimeofday(&tv[2], NULL);
/* cleanup some additive inheritance stuff... */
xodtemplate_clean_additive_strings();
}
#endif
/* do the meat and potatoes stuff... */
host_map = bitmap_create(xodcount.hosts);
contact_map = bitmap_create(xodcount.contacts);
if(!host_map || !contact_map) {
logit(NSLOG_RUNTIME_ERROR, TRUE, "Error: Failed to create bitmaps for resolving objects\n");
return ERROR;
}
if(result == OK)
result = xodtemplate_recombobulate_contactgroups();
if(test_scheduling == TRUE)
gettimeofday(&tv[3], NULL);
timing_point("Done recombobulating contactgroups\n");
if(result == OK)
result = xodtemplate_recombobulate_hostgroups();
if(test_scheduling == TRUE)
gettimeofday(&tv[4], NULL);
timing_point("Done recombobulating hostgroups\n");
#ifndef NSCGI
if(use_precached_objects == FALSE) {
if(result == OK)
result = xodtemplate_duplicate_services();
if(test_scheduling == TRUE)
gettimeofday(&tv[5], NULL);
timing_point("Created %u services (dupes possible)\n", xodcount.services);
}
#endif
/* now we have an accurate service count */
service_map = bitmap_create(xodcount.services);
if(!service_map) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Failed to create service map\n");
return ERROR;
}
if(result == OK)
result = xodtemplate_recombobulate_servicegroups();
if(test_scheduling == TRUE)
gettimeofday(&tv[6], NULL);
timing_point("Done recombobulating servicegroups\n");
#ifndef NSCGI
if(use_precached_objects == FALSE) {
if(result == OK)
result = xodtemplate_duplicate_objects();
if(test_scheduling == TRUE)
gettimeofday(&tv[7], NULL);
/* NOTE: some missing defaults (notification options, etc.) are also applied here */
if(result == OK)
result = xodtemplate_inherit_object_properties();
timing_point("Done propagating inherited object properties\n");
if(test_scheduling == TRUE)
gettimeofday(&tv[8], NULL);
}
#endif
if(test_scheduling == TRUE)
gettimeofday(&tv[9], NULL);
/* register objects */
if(result == OK)
result = xodtemplate_register_objects();
if(test_scheduling == TRUE)
gettimeofday(&tv[10], NULL);
/* cleanup */
xodtemplate_free_memory();
#ifdef NSCORE
if(test_scheduling == TRUE) {
gettimeofday(&tv[11], NULL);
runtime[0] = (double)((double)(tv[1].tv_sec - tv[0].tv_sec) + (double)((tv[1].tv_usec - tv[0].tv_usec) / 1000.0) / 1000.0);
if(use_precached_objects == FALSE) {
runtime[1] = (double)((double)(tv[2].tv_sec - tv[1].tv_sec) + (double)((tv[2].tv_usec - tv[1].tv_usec) / 1000.0) / 1000.0);
runtime[2] = (double)((double)(tv[3].tv_sec - tv[2].tv_sec) + (double)((tv[3].tv_usec - tv[2].tv_usec) / 1000.0) / 1000.0);
runtime[3] = (double)((double)(tv[4].tv_sec - tv[3].tv_sec) + (double)((tv[4].tv_usec - tv[3].tv_usec) / 1000.0) / 1000.0);
runtime[4] = (double)((double)(tv[5].tv_sec - tv[4].tv_sec) + (double)((tv[5].tv_usec - tv[4].tv_usec) / 1000.0) / 1000.0);
runtime[5] = (double)((double)(tv[6].tv_sec - tv[5].tv_sec) + (double)((tv[6].tv_usec - tv[5].tv_usec) / 1000.0) / 1000.0);
runtime[6] = (double)((double)(tv[7].tv_sec - tv[6].tv_sec) + (double)((tv[7].tv_usec - tv[6].tv_usec) / 1000.0) / 1000.0);
runtime[7] = (double)((double)(tv[8].tv_sec - tv[7].tv_sec) + (double)((tv[8].tv_usec - tv[7].tv_usec) / 1000.0) / 1000.0);
runtime[8] = (double)((double)(tv[10].tv_sec - tv[9].tv_sec) + (double)((tv[10].tv_usec - tv[9].tv_usec) / 1000.0) / 1000.0);
}
else {
runtime[1] = 0.0;
runtime[2] = 0.0;
runtime[3] = 0.0;
runtime[4] = 0.0;
runtime[5] = 0.0;
runtime[6] = 0.0;
runtime[7] = 0.0;
runtime[8] = (double)((double)(tv[10].tv_sec - tv[1].tv_sec) + (double)((tv[10].tv_usec - tv[1].tv_usec) / 1000.0) / 1000.0);
}
runtime[9] = (double)((double)(tv[11].tv_sec - tv[10].tv_sec) + (double)((tv[11].tv_usec - tv[10].tv_usec) / 1000.0) / 1000.0);
runtime[10] = (double)((double)(tv[11].tv_sec - tv[0].tv_sec) + (double)((tv[11].tv_usec - tv[0].tv_usec) / 1000.0) / 1000.0);
printf("Timing information on object configuration processing is listed\n");
printf("below. You can use this information to see if precaching your\n");
printf("object configuration would be useful.\n\n");
printf("Object Config Source: %s\n\n", (use_precached_objects == TRUE) ? "Pre-cached config file" : "Config files (uncached)");
printf("OBJECT CONFIG PROCESSING TIMES (* = Potential for precache savings with -u option)\n");
printf("----------------------------------\n");
printf("Read: %.6lf sec\n", runtime[0]);
printf("Resolve: %.6lf sec *\n", runtime[1]);
printf("Recomb Contactgroups: %.6lf sec *\n", runtime[2]);
printf("Recomb Hostgroups: %.6lf sec *\n", runtime[3]);
printf("Dup Services: %.6lf sec *\n", runtime[4]);
printf("Recomb Servicegroups: %.6lf sec *\n", runtime[5]);
printf("Duplicate: %.6lf sec *\n", runtime[6]);
printf("Inherit: %.6lf sec *\n", runtime[7]);
printf("Register: %.6lf sec\n", runtime[8]);
printf("Free: %.6lf sec\n", runtime[9]);
printf(" ============\n");
printf("TOTAL: %.6lf sec ", runtime[10]);
if(use_precached_objects == FALSE)
printf("* = %.6lf sec (%.2f%%) estimated savings", (runtime[10] - runtime[9] - runtime[8] - runtime[0]) / 4, ((runtime[10] - runtime[9] - runtime[8] - runtime[0]) / runtime[10]) * 25.0);
printf("\n");
printf("\n\n");
}
#endif
bitmap_destroy(contact_map);
bitmap_destroy(host_map);
bitmap_destroy(service_map);
return result;
}
/* process all files in a specific config directory */
int xodtemplate_process_config_dir(char *dirname, int options) {
char file[MAX_FILENAME_LENGTH];
DIR *dirp = NULL;
struct dirent *dirfile = NULL;
int result = OK;
register int x = 0;
struct stat stat_buf;
#ifdef NSCORE
if(verify_config >= 2)
printf("Processing object config directory '%s'...\n", dirname);
#endif
/* open the directory for reading */
dirp = opendir(dirname);
if(dirp == NULL) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Could not open config directory '%s' for reading.\n", dirname);
return ERROR;
}
/* process all files in the directory... */
while((dirfile = readdir(dirp)) != NULL) {
/* skip hidden files and directories, and current and parent dir */
if(dirfile->d_name[0] == '.')
continue;
/* create /path/to/file */
snprintf(file, sizeof(file), "%s/%s", dirname, dirfile->d_name);
file[sizeof(file) - 1] = '\x0';
/* process this if it's a non-hidden config file... */
if(stat(file, &stat_buf) == -1) {
logit(NSLOG_RUNTIME_ERROR, TRUE, "Error: Could not open config directory member '%s' for reading.\n", file);
closedir(dirp);
return ERROR;
}
switch(stat_buf.st_mode & S_IFMT) {
case S_IFREG:
x = strlen(dirfile->d_name);
if(x <= 4 || strcmp(dirfile->d_name + (x - 4), ".cfg"))
break;
/* process the config file */
result = xodtemplate_process_config_file(file, options);
if(result == ERROR) {
closedir(dirp);
return ERROR;
}
break;
case S_IFDIR:
/* recurse into subdirectories... */
result = xodtemplate_process_config_dir(file, options);
if(result == ERROR) {
closedir(dirp);
return ERROR;
}
break;
default:
/* everything else we ignore */
break;
}
}
closedir(dirp);
return result;
}
/* process data in a specific config file */
int xodtemplate_process_config_file(char *filename, int options) {
mmapfile *thefile = NULL;
char *input = NULL;
register int in_definition = FALSE;
register int current_line = 0;
int result = OK;
register int x = 0;
register int y = 0;
char *ptr = NULL;
#ifdef NSCORE
if(verify_config >= 2)
printf("Processing object config file '%s'...\n", filename);
#endif
/* save config file name */
xodtemplate_config_files[xodtemplate_current_config_file++] = (char *)strdup(filename);
/* reallocate memory for config files */
if(!(xodtemplate_current_config_file % 256)) {
xodtemplate_config_files = (char **)realloc(xodtemplate_config_files, (xodtemplate_current_config_file + 256) * sizeof(char **));
if(xodtemplate_config_files == NULL)
return ERROR;
}
/* open the config file for reading */
if((thefile = mmap_fopen(filename)) == NULL) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Cannot open config file '%s' for reading: %s\n", filename, strerror(errno));
return ERROR;
}
/* read in all lines from the config file */
while(1) {
/* free memory */
my_free(input);
/* read the next line */
if((input = mmap_fgets_multiline(thefile)) == NULL)
break;
current_line = thefile->current_line;
/* grab data before comment delimiter - faster than a strtok() and strncpy()... */
for(x = 0; input[x] != '\x0'; x++) {
if(input[x] == ';') {
if(x == 0)
break;
else if(input[x - 1] != '\\')
break;
}
}
input[x] = '\x0';
/* strip input */
strip(input);
/* skip empty lines */
if(input[0] == '\x0' || input[0] == '#')
continue;
/* this is the start of an object definition */
if(strstr(input, "define") == input) {
/* get the type of object we're defining... */
for(x = 6; input[x] != '\x0'; x++)
if(input[x] != ' ' && input[x] != '\t')
break;
for(y = 0; input[x] != '\x0'; x++) {
if(input[x] == ' ' || input[x] == '\t' || input[x] == '{')
break;
else
input[y++] = input[x];
}
input[y] = '\x0';
/* make sure an object type is specified... */
if(input[0] == '\x0') {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: No object type specified in file '%s' on line %d.\n", filename, current_line);
result = ERROR;
break;
}
/* check validity of object type */
if(strcmp(input, "timeperiod") && strcmp(input, "command") && strcmp(input, "contact") && strcmp(input, "contactgroup") && strcmp(input, "host") && strcmp(input, "hostgroup") && strcmp(input, "servicegroup") && strcmp(input, "service") && strcmp(input, "servicedependency") && strcmp(input, "serviceescalation") && strcmp(input, "hostgroupescalation") && strcmp(input, "hostdependency") && strcmp(input, "hostescalation")) {
if(strcmp(input, "hostextinfo") && strcmp(input, "serviceextinfo")) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Invalid object definition type '%s' in file '%s' on line %d.\n", input, filename, current_line);
result = ERROR;
break;
}
logit(NSLOG_CONFIG_WARNING, TRUE, "WARNING: Extinfo objects are deprecated and will be removed in future versions\n");
}
/* we're already in an object definition... */
if(in_definition == TRUE) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Unexpected start of object definition in file '%s' on line %d. Make sure you close preceding objects before starting a new one.\n", filename, current_line);
result = ERROR;
break;
}
/* start a new definition */
if(xodtemplate_begin_object_definition(input, options, xodtemplate_current_config_file, current_line) == ERROR) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Could not add object definition in file '%s' on line %d.\n", filename, current_line);
result = ERROR;
break;
}
in_definition = TRUE;
}
/* we're currently inside an object definition */
else if(in_definition == TRUE) {
/* this is the close of an object definition */
if(!strcmp(input, "}")) {
in_definition = FALSE;
/* close out current definition */
if(xodtemplate_end_object_definition(options) == ERROR) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Could not complete object definition in file '%s' on line %d. Have you named all your objects?\n", filename, current_line);
result = ERROR;
break;
}
}
/* this is a directive inside an object definition */
else {
/* add directive to object definition */
if(xodtemplate_add_object_property(input, options) == ERROR) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Could not add object property in file '%s' on line %d.\n", filename, current_line);
result = ERROR;
break;
}
}
}
/* include another file */
else if(strstr(input, "include_file=") == input) {
ptr = strtok(input, "=");
ptr = strtok(NULL, "\n");
if(ptr != NULL) {
result = xodtemplate_process_config_file(ptr, options);
if(result == ERROR)
break;
}
}
/* include a directory */
else if(strstr(input, "include_dir") == input) {
ptr = strtok(input, "=");
ptr = strtok(NULL, "\n");
if(ptr != NULL) {
result = xodtemplate_process_config_dir(ptr, options);
if(result == ERROR)
break;
}
}
/* unexpected token or statement */
else {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Unexpected token or statement in file '%s' on line %d.\n", filename, current_line);
result = ERROR;
break;
}
}
/* free memory and close file */
my_free(input);
mmap_fclose(thefile);
/* whoops - EOF while we were in the middle of an object definition... */
if(in_definition == TRUE && result == OK) {
logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Unexpected EOF in file '%s' on line %d - check for a missing closing bracket.\n", filename, current_line);
result = ERROR;
}
return result;
}
/******************************************************************/
/***************** OBJECT DEFINITION FUNCTIONS ********************/
/******************************************************************/
/*
* all objects start the same way, so we can get rid of quite
* a lot of code with this struct-offset-insensitive macro
*/
#define xod_begin_def(type) \
do { \
new_##type = (xodtemplate_##type *)calloc(1, sizeof(*new_##type)); \
if (new_##type == NULL) \
return ERROR; \
new_##type->register_object=TRUE; \
new_##type->_config_file=cfgfile; \
new_##type->_start_line=start_line; \
\
/* precached object files are already sorted, so add to tail */ \
if(presorted_objects==TRUE){ \
\
if(xodtemplate_##type##_list==NULL){ \
xodtemplate_##type##_list=new_##type; \
xodtemplate_##type##_list_tail=xodtemplate_##type##_list; \
} else { \
xodtemplate_##type##_list_tail->next=new_##type; \
xodtemplate_##type##_list_tail=new_##type; \
} \
\
/* update current object pointer */ \
xodtemplate_current_object=xodtemplate_##type##_list_tail; \
} else { \
/* add new object to head of list in memory */ \
new_##type->next=xodtemplate_##type##_list; \
xodtemplate_##type##_list=new_##type; \
\
/* update current object pointer */ \
xodtemplate_current_object=xodtemplate_##type##_list; \
} \
} while (0)
/* starts a new object definition */
int xodtemplate_begin_object_definition(char *input, int options, int cfgfile, int start_line) {
int result = OK;
xodtemplate_timeperiod *new_timeperiod = NULL;
xodtemplate_command *new_command = NULL;
xodtemplate_contactgroup *new_contactgroup = NULL;
xodtemplate_hostgroup *new_hostgroup = NULL;
xodtemplate_servicegroup *new_servicegroup = NULL;
xodtemplate_servicedependency *new_servicedependency = NULL;
xodtemplate_serviceescalation *new_serviceescalation = NULL;
xodtemplate_contact *new_contact = NULL;
xodtemplate_host *new_host = NULL;
xodtemplate_service *new_service = NULL;
xodtemplate_hostdependency *new_hostdependency = NULL;
xodtemplate_hostescalation *new_hostescalation = NULL;
xodtemplate_hostextinfo *new_hostextinfo = NULL;
xodtemplate_serviceextinfo *new_serviceextinfo = NULL;
if(!strcmp(input, "service"))
xodtemplate_current_object_type = XODTEMPLATE_SERVICE;
else if(!strcmp(input, "host"))
xodtemplate_current_object_type = XODTEMPLATE_HOST;
else if(!strcmp(input, "command"))
xodtemplate_current_object_type = XODTEMPLATE_COMMAND;
else if(!strcmp(input, "contact"))
xodtemplate_current_object_type = XODTEMPLATE_CONTACT;
else if(!strcmp(input, "contactgroup"))
xodtemplate_current_object_type = XODTEMPLATE_CONTACTGROUP;
else if(!strcmp(input, "hostgroup"))
xodtemplate_current_object_type = XODTEMPLATE_HOSTGROUP;
else if(!strcmp(input, "servicegroup"))
xodtemplate_current_object_type = XODTEMPLATE_SERVICEGROUP;
else if(!strcmp(input, "timeperiod"))
xodtemplate_current_object_type = XODTEMPLATE_TIMEPERIOD;
else if(!strcmp(input, "servicedependency"))
xodtemplate_current_object_type = XODTEMPLATE_SERVICEDEPENDENCY;
else if(!strcmp(input, "serviceescalation"))
xodtemplate_current_object_type = XODTEMPLATE_SERVICEESCALATION;
else if(!strcmp(input, "hostdependency"))
xodtemplate_current_object_type = XODTEMPLATE_HOSTDEPENDENCY;
else if(!strcmp(input, "hostescalation"))
xodtemplate_current_object_type = XODTEMPLATE_HOSTESCALATION;
else if(!strcmp(input, "hostextinfo"))
xodtemplate_current_object_type = XODTEMPLATE_HOSTEXTINFO;
else if(!strcmp(input, "serviceextinfo"))
xodtemplate_current_object_type = XODTEMPLATE_SERVICEEXTINFO;
else
return ERROR;
/* check to see if we should process this type of object */
switch(xodtemplate_current_object_type) {
case XODTEMPLATE_TIMEPERIOD:
if(!(options & READ_TIMEPERIODS))
return OK;
break;
case XODTEMPLATE_COMMAND:
if(!(options & READ_COMMANDS))
return OK;
break;
case XODTEMPLATE_CONTACT:
if(!(options & READ_CONTACTS))
return OK;
break;
case XODTEMPLATE_CONTACTGROUP:
if(!(options & READ_CONTACTGROUPS))
return OK;
break;
case XODTEMPLATE_HOST:
if(!(options & READ_HOSTS))
return OK;
break;
case XODTEMPLATE_HOSTGROUP:
if(!(options & READ_HOSTGROUPS))
return OK;
break;
case XODTEMPLATE_SERVICEGROUP:
if(!(options & READ_SERVICEGROUPS))
return OK;
break;
case XODTEMPLATE_SERVICE:
if(!(options & READ_SERVICES))
return OK;
break;
case XODTEMPLATE_SERVICEDEPENDENCY:
if(!(options & READ_SERVICEDEPENDENCIES))
return OK;
break;
case XODTEMPLATE_SERVICEESCALATION:
if(!(options & READ_SERVICEESCALATIONS))
return OK;
break;
case XODTEMPLATE_HOSTDEPENDENCY:
if(!(options & READ_HOSTDEPENDENCIES))
return OK;
break;
case XODTEMPLATE_HOSTESCALATION:
if(!(options & READ_HOSTESCALATIONS))
return OK;
break;
case XODTEMPLATE_HOSTEXTINFO:
if(!(options & READ_HOSTEXTINFO))
return OK;
break;
case XODTEMPLATE_SERVICEEXTINFO:
if(!(options & READ_SERVICEEXTINFO))
return OK;
break;
default:
return ERROR;
break;
}
/* add a new (blank) object */
switch(xodtemplate_current_object_type) {
case XODTEMPLATE_TIMEPERIOD:
xod_begin_def(timeperiod);
break;
case XODTEMPLATE_COMMAND:
xod_begin_def(command);
break;
case XODTEMPLATE_CONTACTGROUP:
xod_begin_def(contactgroup);
break;
case XODTEMPLATE_HOSTGROUP:
xod_begin_def(hostgroup);
break;
case XODTEMPLATE_SERVICEGROUP:
xod_begin_def(servicegroup);
break;
case XODTEMPLATE_SERVICEDEPENDENCY:
xod_begin_def(servicedependency);
break;
case XODTEMPLATE_SERVICEESCALATION:
xod_begin_def(serviceescalation);
new_serviceescalation->first_notification = -2;
new_serviceescalation->last_notification = -2;
break;
case XODTEMPLATE_CONTACT:
xod_begin_def(contact);
new_contact->minimum_value = 0;
new_contact->host_notifications_enabled = TRUE;
new_contact->service_notifications_enabled = TRUE;
new_contact->can_submit_commands = TRUE;
new_contact->retain_status_information = TRUE;
new_contact->retain_nonstatus_information = TRUE;
break;
case XODTEMPLATE_HOST:
xod_begin_def(host);
new_host->hourly_value = 0;
new_host->check_interval = 5.0;
new_host->retry_interval = 1.0;
new_host->active_checks_enabled = TRUE;
new_host->passive_checks_enabled = TRUE;
new_host->obsess = TRUE;
new_host->max_check_attempts = -2;
new_host->event_handler_enabled = TRUE;
new_host->flap_detection_enabled = TRUE;
new_host->flap_detection_options = OPT_ALL;
new_host->notifications_enabled = TRUE;
new_host->notification_interval = 30.0;
new_host->process_perf_data = TRUE;
new_host->x_2d = -1;
new_host->y_2d = -1;
new_host->retain_status_information = TRUE;