forked from Cacti/spine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
1568 lines (1321 loc) · 40.5 KB
/
util.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
/*
ex: set tabstop=4 shiftwidth=4 autoindent:
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2018 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU Lesser General Public |
| License as published by the Free Software Foundation; either |
| version 2.1 of the License, or (at your option) any later version. |
| |
| 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 Lesser General Public License for more details. |
| |
| You should have received a copy of the GNU Lesser General Public |
| License along with this library; if not, write to the Free Software |
| Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 02110-1301, USA |
| |
+-------------------------------------------------------------------------+
| spine: a backend data gatherer for cacti |
+-------------------------------------------------------------------------+
| This poller would not have been possible without: |
| - Larry Adams (current development and enhancements) |
| - Rivo Nurges (rrd support, mysql poller cache, misc functions) |
| - RTG (core poller code, pthreads, snmp, autoconf examples) |
| - Brady Alleman/Doug Warner (threading ideas, implimentation details) |
+-------------------------------------------------------------------------+
| - Cacti - http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
#include "common.h"
#include "spine.h"
static int nopts = 0;
/*! Override Options Structure
*
* When we fetch a setting from the database, we allow the user to override
* it from the command line. These overrides are provided with the --option
* parameter and stored in this table: we *use* them when the config code
* reads from the DB.
*
* It's not an error to set an option which is unknown, but maybe should be.
*
*/
static struct {
const char *opt;
const char *val;
} opttable[256];
/*! \fn void set_option(const char *option, const char *value)
* \brief Override spine setting from the Cacti settings table.
*
* Called from the command-line processing code, this provides a value
* to replace any DB-stored option settings.
*
*/
void set_option(const char *option, const char *value) {
opttable[nopts ].opt = option;
opttable[nopts++].val = value;
}
/*! \fn static const char *getsetting(MYSQL *psql, const char *setting)
* \brief Returns a character pointer to a Cacti setting.
*
* Given a pointer to a database and the name of a setting, return the string
* which represents the value from the settings table. Return NULL if we
* can't find a setting for whatever reason.
*
* NOTE: if the user has provided one of these options on the command line,
* it's intercepted here and returned, overriding the database setting.
*
* \return the database option setting
*
*/
static const char *getsetting(MYSQL *psql, const char *setting) {
char qstring[256];
char *retval;
MYSQL_RES *result;
MYSQL_ROW mysql_row;
int i;
assert(psql != 0);
assert(setting != 0);
/* see if it's in the option table */
for (i=0; i<nopts; i++) {
if (STRIMATCH(setting, opttable[i].opt)) {
/* FOUND IT! */
retval = strdup(opttable[i].val);
return retval;
}
}
sprintf(qstring, "SELECT value FROM settings WHERE name = '%s'", setting);
result = db_query(psql, qstring);
if (result != 0) {
if (mysql_num_rows(result) > 0) {
mysql_row = mysql_fetch_row(result);
if (mysql_row != NULL) {
retval = strdup(mysql_row[0]);
db_free_result(result);
return retval;
}else{
return 0;
}
}else{
db_free_result(result);
return 0;
}
}else{
return 0;
}
}
/*! \fn static const char *getpsetting(MYSQL *psql, const char *setting)
* \brief Returns a character pointer to a Cacti poller setting.
*
* Given a pointer to a database and the name of a setting,
* return the string which represents the value from the poller table.
* Return NULL if we can't find a setting for whatever reason.
*
* NOTE: if the user has provided one of these options on the command line,
* it's intercepted here and returned, overriding the database setting.
*
* \return the database option setting
*
*/
static const char *getpsetting(MYSQL *psql, const char *setting) {
char qstring[256];
char *retval;
MYSQL_RES *result;
MYSQL_ROW mysql_row;
int i;
assert(psql != 0);
assert(setting != 0);
/* see if it's in the option table */
for (i=0; i<nopts; i++) {
if (STRIMATCH(setting, opttable[i].opt)) {
/* FOUND IT! */
retval = strdup(opttable[i].val);
return retval;
}
}
sprintf(qstring, "SELECT %s FROM poller WHERE id = '%d'", setting, set.poller_id);
result = db_query(psql, qstring);
if (result != 0) {
if (mysql_num_rows(result) > 0) {
mysql_row = mysql_fetch_row(result);
if (mysql_row != NULL) {
retval = strdup(mysql_row[0]);
db_free_result(result);
return retval;
} else {
return 0;
}
} else {
db_free_result(result);
return 0;
}
} else {
return 0;
}
}
/*! \fn static int getboolsetting(MYSQL *psql, const char *setting, int dflt)
* \brief Obtains a boolean option from the database.
*
* Given the parameters for fetching a setting from the database,
* do so for a *Boolean* value. We parse the usual set of words
* meaning true/false, and if we don't get a value, or if we don't
* understand what we fetched, we use the default value provided.
*
* \return boolean TRUE or FALSE based upon database setting or the DEFAULT if not found
*/
static int getboolsetting(MYSQL *psql, const char *setting, int dflt) {
const char *rc;
assert(psql != 0);
assert(setting != 0);
rc = getsetting(psql, setting);
if (rc == 0) return dflt;
if (STRIMATCH(rc, "on" ) ||
STRIMATCH(rc, "yes" ) ||
STRIMATCH(rc, "true") ||
STRIMATCH(rc, "1" ) ) {
free((char *)rc);
return TRUE;
}
if (STRIMATCH(rc, "off" ) ||
STRIMATCH(rc, "no" ) ||
STRIMATCH(rc, "false") ||
STRIMATCH(rc, "0" ) ) {
free((char *)rc);
return FALSE;
}
/* doesn't really match one of our keywords: what to do? */
free((char *)rc);
return dflt;
}
/*! \fn static const char *getglobalvariable(MYSQL *psql, const char *setting)
* \brief Returns a character pointer to a MySQL global variable setting.
*
* Given a pointer to a database and the name of a global variable, return the string
* which represents that value from the settings table. Return NULL if we
* can't find a variable for whatever reason.
*
* \return the database global variable setting
*
*/
static const char *getglobalvariable(MYSQL *psql, const char *setting) {
char qstring[256];
char *retval;
MYSQL_RES *result;
MYSQL_ROW mysql_row;
int i;
assert(psql != 0);
assert(setting != 0);
/* see if it's in the option table */
for (i=0; i<nopts; i++) {
if (STRIMATCH(setting, opttable[i].opt)) {
/* FOUND IT! */
return opttable[i].val;
}
}
sprintf(qstring, "SHOW GLOBAL VARIABLES LIKE '%s'", setting);
result = db_query(psql, qstring);
if (result != 0) {
if (mysql_num_rows(result) > 0) {
mysql_row = mysql_fetch_row(result);
if (mysql_row != NULL) {
retval = strdup(mysql_row[1]);
db_free_result(result);
return retval;
} else {
return 0;
}
} else {
db_free_result(result);
return 0;
}
} else {
return 0;
}
}
/*! \fn int is_debug_device(int device_id)
* \brief Determine if a device is a debug device
*
*/
int is_debug_device(int device_id) {
extern int *debug_devices;
int i = 0;
while (i < 100) {
if (debug_devices[i] == '\0') break;
if (debug_devices[i] == device_id) {
return TRUE;
}
i++;
}
return FALSE;
}
/*! \fn void read_config_options(void)
* \brief Reads the default Spine runtime parameters from the database and set's the global array
*
* load default values from the database for poller processing
*
*/
void read_config_options() {
MYSQL mysql;
MYSQL_RES *result;
int num_rows;
char web_root[BUFSIZE];
char sqlbuf[SMALL_BUFSIZE], *sqlp = sqlbuf;
const char *res;
db_connect(set.dbdb, &mysql);
/* get the mysql server version */
set.dbversion = 0;
if ((res = getglobalvariable(&mysql, "version")) != 0) {
set.dbversion = atoi(res);
free((char *)res);
}
/* get logging level from database - overrides spine.conf */
if ((res = getsetting(&mysql, "log_verbosity")) != 0) {
const int n = atoi(res);
free((char *)res);
if (n != 0) set.log_level = n;
}
/* determine script server path operation and default log file processing */
if ((res = getsetting(&mysql, "path_webroot")) != 0) {
snprintf(set.path_php_server, SMALL_BUFSIZE, "%s/script_server.php", res);
snprintf(web_root, BUFSIZE, "%s", res);
free((char *)res);
}
/* determine logfile path */
if ((res = getsetting(&mysql, "path_cactilog")) != 0) {
if (strlen(res) != 0) {
snprintf(set.path_logfile, SMALL_BUFSIZE, "%s", res);
} else {
if (strlen(web_root) != 0) {
snprintf(set.path_logfile, SMALL_BUFSIZE, "%s/log/cacti.log", web_root);
} else {
set.path_logfile[0] ='\0';
}
}
free((char *)res);
} else {
snprintf(set.path_logfile, SMALL_BUFSIZE, "%s/log/cacti.log", web_root);
}
/* get log separator */
if ((res = getsetting(&mysql, "default_datechar")) != 0) {
set.log_datetime_separator = atoi(res);
free((char *)res);
if (set.log_datetime_separator < GDC_MIN || set.log_datetime_separator > GDC_MAX) {
set.log_datetime_separator = GDC_DEFAULT;
}
}
/* get log separator */
if ((res = getsetting(&mysql, "default_datechar")) != 0) {
set.log_datetime_separator = atoi(res);
free((char *)res);
if (set.log_datetime_separator < GDC_MIN || set.log_datetime_separator > GDC_MAX) {
set.log_datetime_separator = GDC_DEFAULT;
}
}
/* log the path_webroot variable */
SPINE_LOG_DEBUG(("DEBUG: The path_php_server variable is %s", set.path_php_server));
/* log the path_cactilog variable */
SPINE_LOG_DEBUG(("DEBUG: The path_cactilog variable is %s", set.path_logfile));
/* determine log file, syslog or both, default is 1 or log file only */
if ((res = getsetting(&mysql, "log_destination")) != 0) {
set.log_destination = parse_logdest(res, LOGDEST_FILE);
free((char *)res);
} else {
set.log_destination = LOGDEST_FILE;
}
/* log the log_destination variable */
SPINE_LOG_DEBUG(("DEBUG: The log_destination variable is %i (%s)",
set.log_destination,
printable_logdest(set.log_destination)));
set.logfile_processed = TRUE;
/* get PHP Path Information for Scripting */
if ((res = getsetting(&mysql, "path_php_binary")) != 0) {
STRNCOPY(set.path_php, res);
free((char *)res);
}
/* log the path_php variable */
SPINE_LOG_DEBUG(("DEBUG: The path_php variable is %s", set.path_php));
/* set availability_method */
if ((res = getsetting(&mysql, "availability_method")) != 0) {
set.availability_method = atoi(res);
free((char *)res);
}
/* log the availability_method variable */
SPINE_LOG_DEBUG(("DEBUG: The availability_method variable is %i", set.availability_method));
/* set ping_recovery_count */
if ((res = getsetting(&mysql, "ping_recovery_count")) != 0) {
set.ping_recovery_count = atoi(res);
free((char *)res);
}
/* log the ping_recovery_count variable */
SPINE_LOG_DEBUG(("DEBUG: The ping_recovery_count variable is %i", set.ping_recovery_count));
/* set ping_failure_count */
if ((res = getsetting(&mysql, "ping_failure_count")) != 0) {
set.ping_failure_count = atoi(res);
free((char *)res);
}
/* log the ping_failure_count variable */
SPINE_LOG_DEBUG(("DEBUG: The ping_failure_count variable is %i", set.ping_failure_count));
/* set ping_method */
if ((res = getsetting(&mysql, "ping_method")) != 0) {
set.ping_method = atoi(res);
free((char *)res);
}
/* log the ping_method variable */
SPINE_LOG_DEBUG(("DEBUG: The ping_method variable is %i", set.ping_method));
/* set ping_retries */
if ((res = getsetting(&mysql, "ping_retries")) != 0) {
set.ping_retries = atoi(res);
free((char *)res);
}
/* log the ping_retries variable */
SPINE_LOG_DEBUG(("DEBUG: The ping_retries variable is %i", set.ping_retries));
/* set ping_timeout */
if ( (res = getsetting(&mysql, "ping_timeout")) != 0) {
set.ping_timeout = atoi(res);
free((char *)res);
}
/* log the ping_timeout variable */
SPINE_LOG_DEBUG(("DEBUG: The ping_timeout variable is %i", set.ping_timeout));
/* set snmp_retries */
if ( (res = getsetting(&mysql, "snmp_retries")) != 0) {
set.snmp_retries = atoi(res);
free((char *)res);
}
/* log the snmp_retries variable */
SPINE_LOG_DEBUG(("DEBUG: The snmp_retries variable is %i", set.snmp_retries));
/* set logging option for errors */
set.log_perror = getboolsetting(&mysql, "log_perror", FALSE);
/* log the log_perror variable */
SPINE_LOG_DEBUG(("DEBUG: The log_perror variable is %i", set.log_perror));
/* set logging option for errors */
set.log_pwarn = getboolsetting(&mysql, "log_pwarn", FALSE);
/* log the log_pwarn variable */
SPINE_LOG_DEBUG(("DEBUG: The log_pwarn variable is %i", set.log_pwarn));
/* set option to increase insert performance */
set.boost_redirect = getboolsetting(&mysql, "boost_redirect", FALSE);
/* log the boost_redirect variable */
SPINE_LOG_DEBUG(("DEBUG: The boost_redirect variable is %i", set.boost_redirect));
/* set option for determining if boost is enabled */
set.boost_enabled = getboolsetting(&mysql, "boost_rrd_update_enable", FALSE);
/* log the boost_rrd_update_enable variable */
SPINE_LOG_DEBUG(("DEBUG: The boost_rrd_update_enable variable is %i", set.boost_enabled));
/* set logging option for statistics */
set.log_pstats = getboolsetting(&mysql, "log_pstats", FALSE);
/* log the log_pstats variable */
SPINE_LOG_DEBUG(("DEBUG: The log_pstats variable is %i", set.log_pstats));
/* get Cacti defined max threads override spine.conf */
if ((res = getpsetting(&mysql, "threads")) != 0) {
set.threads = atoi(res);
free((char *)res);
if (set.threads > MAX_THREADS) {
set.threads = MAX_THREADS;
}
}
/* log the threads variable */
SPINE_LOG_DEBUG(("DEBUG: The threads variable is %i", set.threads));
/* get the poller_interval for those who have elected to go with a 1 minute polling interval */
if ((res = getsetting(&mysql, "poller_interval")) != 0) {
set.poller_interval = atoi(res);
free((char *)res);
} else {
set.poller_interval = 0;
}
/* log the poller_interval variable */
if (set.poller_interval == 0) {
SPINE_LOG_DEBUG(("DEBUG: The polling interval is the system default"));
} else {
SPINE_LOG_DEBUG(("DEBUG: The polling interval is %i seconds", set.poller_interval));
}
/* get the concurrent_processes variable to determine thread sleep values */
if ((res = getsetting(&mysql, "concurrent_processes")) != 0) {
set.num_parent_processes = atoi(res);
free((char *)res);
} else {
set.num_parent_processes = 1;
}
/* log the concurrent processes variable */
SPINE_LOG_DEBUG(("DEBUG: The number of concurrent processes is %i", set.num_parent_processes));
/* get the script timeout to establish timeouts */
if ((res = getsetting(&mysql, "script_timeout")) != 0) {
set.script_timeout = atoi(res);
free((char *)res);
if (set.script_timeout < 5) {
set.script_timeout = 5;
}
} else {
set.script_timeout = 25;
}
/* log the script timeout value */
SPINE_LOG_DEBUG(("DEBUG: The script timeout is %i", set.script_timeout));
/* get selective_device_debug string */
if ((res = getsetting(&mysql, "selective_device_debug")) != 0) {
STRNCOPY(set.selective_device_debug, res);
free((char *)res);
}
/* log the selective_device_debug variable */
SPINE_LOG_DEBUG(("DEBUG: The selective_device_debug variable is %s", set.selective_device_debug));
/* get spine_log_level */
if ((res = getsetting(&mysql, "spine_log_level")) != 0) {
set.spine_log_level = atoi(res);
free((char *)res);
}
/* log the spine_log_level variable */
SPINE_LOG_DEBUG(("DEBUG: The spine_log_level variable is %i", set.spine_log_level));
/* get the number of script server processes to run */
if ((res = getsetting(&mysql, "php_servers")) != 0) {
set.php_servers = atoi(res);
free((char *)res);
if (set.php_servers > MAX_PHP_SERVERS) {
set.php_servers = MAX_PHP_SERVERS;
}
if (set.php_servers <= 0) {
set.php_servers = 1;
}
} else {
set.php_servers = 2;
}
/* log the script timeout value */
SPINE_LOG_DEBUG(("DEBUG: The number of php script servers to run is %i", set.php_servers));
/*----------------------------------------------------------------
* determine if the php script server is required by searching for
* all the host records for an action of POLLER_ACTION_PHP_SCRIPT_SERVER.
* If we get even one, it means we have to deal with the PHP script
* server.
*
*/
set.php_required = FALSE; /* assume no */
/* log the requirement for the script server */
if (!strlen(set.host_id_list)) {
sqlp = sqlbuf;
sqlp += sprintf(sqlp, "SELECT action FROM poller_item");
sqlp += sprintf(sqlp, " WHERE action=%d", POLLER_ACTION_PHP_SCRIPT_SERVER);
sqlp += append_hostrange(sqlp, "host_id");
if (set.poller_id_exists) {
sqlp += sprintf(sqlp, " AND poller_id=%i", set.poller_id);
}
sqlp += sprintf(sqlp, " LIMIT 1");
result = db_query(&mysql, sqlbuf);
num_rows = mysql_num_rows(result);
db_free_result(result);
if (num_rows > 0) set.php_required = TRUE;
SPINE_LOG_DEBUG(("DEBUG: StartDevice='%i', EndDevice='%i', TotalPHPScripts='%i'",
set.start_host_id,
set.end_host_id,
num_rows));
} else {
sqlp = sqlbuf;
sqlp += sprintf(sqlp, "SELECT action FROM poller_item");
sqlp += sprintf(sqlp, " WHERE action=%d", POLLER_ACTION_PHP_SCRIPT_SERVER);
sqlp += sprintf(sqlp, " AND host_id IN(%s)", set.host_id_list);
if (set.poller_id_exists) {
sqlp += sprintf(sqlp, " AND poller_id=%i", set.poller_id);
}
sqlp += sprintf(sqlp, " LIMIT 1");
result = db_query(&mysql, sqlbuf);
num_rows = mysql_num_rows(result);
db_free_result(result);
if (num_rows > 0) set.php_required = TRUE;
SPINE_LOG_DEBUG(("DEBUG: Device List to be polled='%s', TotalPHPScripts='%i'",
set.host_id_list,
num_rows));
}
SPINE_LOG_DEBUG(("DEBUG: The PHP Script Server is %sRequired",
set.php_required
? ""
: "Not "));
/* determine the maximum oid's to obtain in a single get request */
if ((res = getsetting(&mysql, "max_get_size")) != 0) {
set.snmp_max_get_size = atoi(res);
free((char *)res);
if (set.snmp_max_get_size > 128) {
set.snmp_max_get_size = 128;
}
} else {
set.snmp_max_get_size = 25;
}
/* log the snmp_max_get_size variable */
SPINE_LOG_DEBUG(("DEBUG: The Maximum SNMP OID Get Size is %i", set.snmp_max_get_size));
db_disconnect(&mysql);
}
/*! \fn int read_spine_config(char *file)
* \brief obtain default startup variables from the spine.conf file.
* \param file the spine config file
*
* \return 0 if successful or -1 if the file could not be opened
*/
int read_spine_config(char *file) {
FILE *fp;
char buff[BUFSIZE];
char *buffer;
char p1[BUFSIZE];
char p2[BUFSIZE];
if ((fp = fopen(file, "rb")) == NULL) {
if (set.log_level == POLLER_VERBOSITY_DEBUG) {
if (!set.stderr_notty) {
fprintf(stderr, "ERROR: Could not open config file [%s]\n", file);
}
}
return -1;
} else {
if (!set.stdout_notty) {
fprintf(stdout, "SPINE: Using spine config file [%s]\n", file);
}
while (!feof(fp)) {
buffer = fgets(buff, BUFSIZE, fp);
if (!feof(fp) && *buff != '#' && *buff != ' ' && *buff != '\n') {
sscanf(buff, "%15s %255s", p1, p2);
if (STRIMATCH(p1, "RDB_Host")) STRNCOPY(set.rdbhost, p2);
else if (STRIMATCH(p1, "RDB_Database")) STRNCOPY(set.rdbdb, p2);
else if (STRIMATCH(p1, "RDB_User")) STRNCOPY(set.rdbuser, p2);
else if (STRIMATCH(p1, "RDB_Pass")) STRNCOPY(set.rdbpass, p2);
else if (STRIMATCH(p1, "RDB_Port")) set.rdbport = atoi(p2);
else if (STRIMATCH(p1, "DB_Host")) STRNCOPY(set.dbhost, p2);
else if (STRIMATCH(p1, "DB_Database")) STRNCOPY(set.dbdb, p2);
else if (STRIMATCH(p1, "DB_User")) STRNCOPY(set.dbuser, p2);
else if (STRIMATCH(p1, "DB_Pass")) STRNCOPY(set.dbpass, p2);
else if (STRIMATCH(p1, "DB_Port")) set.dbport = atoi(p2);
else if (STRIMATCH(p1, "Poller")) set.poller_id = atoi(p2);
else if (STRIMATCH(p1, "DB_PreG")) {
if (!set.stderr_notty) {
fprintf(stderr,"WARNING: DB_PreG is no longer supported\n");
}
}
else if (STRIMATCH(p1, "SNMP_Clientaddr")) STRNCOPY(set.snmp_clientaddr, p2);
else if (!set.stderr_notty) {
fprintf(stderr,"WARNING: Unrecongized directive: %s=%s in %s\n", p1, p2, file);
}
*p1 = '\0';
*p2 = '\0';
}
}
if (strlen(set.dbpass) == 0) *set.dbpass = '\0';
return 0;
}
}
/*! \fn void config_defaults(void)
* \brief populates the global configuration structure with default spine.conf file settings
* \param *set global runtime parameters
*
*/
void config_defaults() {
set.threads = DEFAULT_THREADS;
/* default server */
set.dbport = DEFAULT_DB_PORT;
STRNCOPY(set.dbhost, DEFAULT_DB_HOST);
STRNCOPY(set.dbdb, DEFAULT_DB_DB );
STRNCOPY(set.dbuser, DEFAULT_DB_USER);
STRNCOPY(set.dbpass, DEFAULT_DB_PASS);
/* remote default server */
set.rdbport = DEFAULT_DB_PORT;
STRNCOPY(set.rdbhost, DEFAULT_DB_HOST);
STRNCOPY(set.rdbdb, DEFAULT_DB_DB );
STRNCOPY(set.rdbuser, DEFAULT_DB_USER);
STRNCOPY(set.rdbpass, DEFAULT_DB_PASS);
STRNCOPY(config_paths[0], CONFIG_PATH_1);
STRNCOPY(config_paths[1], CONFIG_PATH_2);
STRNCOPY(config_paths[2], CONFIG_PATH_3);
STRNCOPY(config_paths[3], CONFIG_PATH_4);
set.log_destination = LOGDEST_FILE;
}
/*! \fn void die(const char *format, ...)
* \brief a method to end Spine while returning the fatal error to stderr
*
* Given a printf-style argument list, format it to the standard
* error, append a newline, then exit Spine.
*
*/
void die(const char *format, ...) {
va_list args;
char logmessage[BUFSIZE];
char flogmessage[BUFSIZE];
int old_errno = errno;
va_start(args, format);
vsprintf(logmessage, format, args);
va_end(args);
if (set.log_perror) {
char perr[BUFSIZE];
snprintf(perr, BUFSIZE, " [%d, %s]", old_errno, strerror(old_errno));
strcat(logmessage,perr);
}
if (set.logfile_processed) {
if (set.parent_fork == SPINE_PARENT) {
snprintf(flogmessage, BUFSIZE, "%s (Spine parent)", logmessage);
} else {
snprintf(flogmessage, BUFSIZE, "%s (Spine thread)", logmessage);
}
} else {
snprintf(flogmessage, BUFSIZE, "%s (Spine init)", logmessage);
}
SPINE_LOG(("%s", flogmessage));
if (set.parent_fork == SPINE_PARENT) {
if (set.php_initialized) {
php_close(PHP_INIT);
}
}
exit(set.exit_code);
}
char * get_date_format() {
char *log_fmt;
if (!(log_fmt = (char *) malloc(GD_FMT_SIZE))) {
die("ERROR: Fatal malloc error: util.c get_date_format!");
}
char log_sep = '/';
if (set.log_datetime_separator < GDC_MIN || set.log_datetime_separator > GDC_MAX) {
set.log_datetime_separator = GDC_DEFAULT;
}
if (set.log_datetime_format < GD_MIN || set.log_datetime_format > GD_MAX) {
set.log_datetime_format = GD_DEFAULT;
}
switch (set.log_datetime_separator) {
case GDC_DOT:
log_sep = '.';
break;
case GDC_HYPHEN:
log_sep = '-';
break;
default:
log_sep = '/';
break;
}
switch (set.log_datetime_format) {
case GD_MO_D_Y:
snprintf(log_fmt, GD_FMT_SIZE, "%%m%c%%d%c%%Y %%H:%%M:%%S - ", log_sep, log_sep);
case GD_MN_D_Y:
snprintf(log_fmt, GD_FMT_SIZE, "%%b%c%%d%c%%Y %%H:%%M:%%S - ", log_sep, log_sep);
case GD_D_MO_Y:
snprintf(log_fmt, GD_FMT_SIZE, "%%d%c%%m%c%%Y %%H:%%M:%%S - ", log_sep, log_sep);
case GD_D_MN_Y:
snprintf(log_fmt, GD_FMT_SIZE, "%%d%c%%b%c%%Y %%H:%%M:%%S - ", log_sep, log_sep);
case GD_Y_MO_D:
snprintf(log_fmt, GD_FMT_SIZE, "%%Y%c%%m%c%%d %%H:%%M:%%S - ", log_sep, log_sep);
case GD_Y_MN_D:
snprintf(log_fmt, GD_FMT_SIZE, "%%Y%c%%b%c%%d %%H:%%M:%%S - ", log_sep, log_sep);
default:
snprintf(log_fmt, GD_FMT_SIZE, "%%Y%c%%m%c%%d %%H:%%M:%%S - ", log_sep, log_sep);
}
return (log_fmt);
}
/*! \fn void spine_log(const char *format, ...)
* \brief output's log information to the desired cacti logfile.
* \param *logmessage a pointer to the pre-formated log message.
*
*/
int spine_log(const char *format, ...) {
va_list args;
FILE *log_file = NULL;
FILE *fp = NULL;
/* variables for time display */
time_t nowbin;
struct tm now_time;
struct tm *now_ptr;
/* keep track of an errored log file */
static int log_error = FALSE;
char logprefix[SMALL_BUFSIZE]; /* Formatted Log Prefix */
char ulogmessage[LOGSIZE]; /* Un-Formatted Log Message */
char flogmessage[LOGSIZE]; /* Formatted Log Message */
va_start(args, format);
vsnprintf(ulogmessage, LOGSIZE - 1, format, args);
va_end(args);
/* default for "console" messages to go to stdout */
fp = stdout;
if (IS_LOGGING_TO_STDOUT()) {
puts(trim(ulogmessage));
return TRUE;
}
/* log message prefix */
snprintf(logprefix, SMALL_BUFSIZE, "SPINE: Poller[%i] ", set.poller_id);
/* get time for poller_output table */
nowbin = time(&nowbin);
localtime_r(&nowbin,&now_time);
now_ptr = &now_time;
char * log_fmt = get_date_format();
if (strlen(log_fmt) == 0) {
#ifdef DISABLE_STDERR
fp = stdout;
#else
fp = stderr;
#endif
if ((set.stderr_notty) && (fp == stderr)) {
/* do nothing stderr does not exist */
} else if ((set.stdout_notty) && (fp == stdout)) {
/* do nothing stdout does not exist */
} else {
fprintf(fp, "ERROR: Could not get format from get_date_format()\n");
}
}
if (strftime(flogmessage, 50, log_fmt, now_ptr) == (size_t) 0) {
#ifdef DISABLE_STDERR
fp = stdout;
#else
fp = stderr;
#endif
if ((set.stderr_notty) && (fp == stderr)) {
/* do nothing stderr does not exist */
} else if ((set.stdout_notty) && (fp == stdout)) {
/* do nothing stdout does not exist */
} else {
fprintf(fp, "ERROR: Could not get string from strftime()\n");
}
}
strncat(flogmessage, logprefix, sizeof(flogmessage) - 1);
strncat(flogmessage, ulogmessage, sizeof(flogmessage) - 50);
/* output to syslog/eventlog */
if (IS_LOGGING_TO_SYSLOG()) {
thread_mutex_lock(LOCK_SYSLOG);
openlog("Cacti", LOG_NDELAY | LOG_PID, LOG_SYSLOG);
if ((strstr(flogmessage,"ERROR") || (strstr(flogmessage, "FATAL"))) && (set.log_perror)) {
syslog(LOG_CRIT,"%s\n", flogmessage);
}
if ((strstr(flogmessage,"WARNING")) && (set.log_pwarn)){
syslog(LOG_WARNING,"%s\n", flogmessage);
}
if ((strstr(flogmessage,"STATS")) && (set.log_pstats)){
syslog(LOG_NOTICE,"%s\n", flogmessage);
}
closelog();
thread_mutex_unlock(LOCK_SYSLOG);
}
/* append a line feed to the log message if needed */
if (!strstr(flogmessage, "\n")) {
strncat(flogmessage, "\n", 1);
}
if ((IS_LOGGING_TO_FILE() &&
(set.log_level != POLLER_VERBOSITY_NONE) &&
(strlen(set.path_logfile) != 0))) {
if (set.logfile_processed) {
if (!file_exists(set.path_logfile)) {
log_file = fopen(set.path_logfile, "w");
} else {
log_file = fopen(set.path_logfile, "a");
}
if (log_file) {
fputs(flogmessage, log_file);
fclose(log_file);
} else {
if (!log_error) {
printf("ERROR: Spine Log File Could Not Be Opened/Created\n");
log_error = TRUE;
}
}
}
}
if (set.log_level >= POLLER_VERBOSITY_NONE) {
if ((strstr(flogmessage,"ERROR")) ||
(strstr(flogmessage,"WARNING")) ||
(strstr(flogmessage,"FATAL"))) {
#ifdef DISABLE_STDERR
fp = stdout;
#else
fp = stderr;
#endif
}
if ((set.stderr_notty) && (fp == stderr)) {
/* do nothing stderr does not exist */
} else if ((set.stdout_notty) && (fp == stdout)) {
/* do nothing stdout does not exist */
} else {
fprintf(fp, "%s", flogmessage);
}
}
free(log_fmt);
return TRUE;
}
/*! \fn int file_exists(const char *filename)
* \brief checks for the existance of a file.
* \param *filename the name of the file to check for.
*
* \return TRUE if found FALSE if not.
*
*/
int file_exists(const char *filename) {
struct stat file_stat;
if (stat(filename, &file_stat)) {
return FALSE;
} else {
return TRUE;