Repository navigation
Expand file tree
/
Copy pathpoller.c
More file actions
1842 lines (1592 loc) · 74.9 KB
/
Copy pathpoller.c
File metadata and controls
1842 lines (1592 loc) · 74.9 KB
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-2026 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, implementation details) |
+-------------------------------------------------------------------------+
| - Cacti - http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
#include "common.h"
#include "spine.h"
int format_poller_output_row(char *output, size_t output_size,
int local_data_id, const char *escaped_rrd_name,
const char *host_time, const char *escaped_result) {
const char *timep;
int decimal_points = 0;
int digits = 0;
int written;
if (output == NULL || output_size == 0 || escaped_rrd_name == NULL ||
host_time == NULL || host_time[0] == '\0' || escaped_result == NULL) {
return FALSE;
}
/* host_time is emitted outside SQL quotes. Accept the integer timestamps
* Spine generates and a single fractional part, but no SQL syntax. */
for (timep = host_time; *timep != '\0'; timep++) {
if (*timep == '.') {
decimal_points++;
if (decimal_points > 1) {
return FALSE;
}
} else if (!isdigit((unsigned char)*timep)) {
return FALSE;
} else {
digits++;
}
}
if (digits == 0) {
return FALSE;
}
written = snprintf(output, output_size,
" (%i, '%s', FROM_UNIXTIME(%s), '%s')",
local_data_id, escaped_rrd_name, host_time, escaped_result);
return written >= 0 && (size_t)written < output_size;
}
void child_cleanup(void *arg) {
poller_thread_t poller_details = *(poller_thread_t*) arg;
SPINE_LOG_DEVDBG(("Device[%i] HT[%i] DEBUG: The Device Thread has cleaned up.", poller_details.host_id, poller_details.host_thread));
free(arg);
child_cleanup_thread(NULL);
}
void child_cleanup_thread(void *arg) {
(void)arg;
int a_threads_value;
a_threads_value = spine_permits_available(&available_threads) + 1;
SPINE_LOG_DEVDBG(("DEBUG: Available Threads is %i (%i outstanding)", a_threads_value, set.poller.threads - a_threads_value));
/* Releasing the permit publishes completion: no shared state is used after it. */
spine_permits_release(&available_threads);
}
void child_cleanup_script(void *arg) {
(void)arg;
spine_permits_release(&available_scripts);
int a_scripts_value;
a_scripts_value = spine_permits_available(&available_scripts);
SPINE_LOG_DEVDBG(("DEBUG: Available Scripts is %i (%i outstanding)", a_scripts_value, MAX_SIMULTANEOUS_SCRIPTS - a_scripts_value));
}
/*! \fn void *child(void *arg)
* \brief function is called via the fork command and initiates a poll of a host
* \param arg a pointer to an integer point to the host_id to be polled
*
* This function will call the primary Spine polling function to poll a host
* and then reduce the number of active threads by one so that the next host
* can be polled.
*
*/
void *child(void *arg) {
pthread_cleanup_push(child_cleanup, arg);
int host_errors = 0;
poller_thread_t poller_details = *(poller_thread_t*)arg;
/* Allows main thread to proceed with creation of other threads. */
spine_permits_release(poller_details.thread_init_sem);
SPINE_LOG_DEVICE(poller_details.host_id, POLLER_VERBOSITY_DEBUG, ("Device[%i] HT[%i] DEBUG: In Poller, About to Start Polling", poller_details.host_id, poller_details.host_thread));
if (mysql_thread_init() != 0) die("ERROR: Unable to initialize polling thread database state");
poll_host(&poller_details, &host_errors);
pthread_cleanup_pop(1);
/* end the thread */
pthread_exit(0);
}
/*! \fn void poll_host(const poller_thread_t *work, int *host_errors)
* \brief core Spine function that polls a host
* \param work borrowed polling instructions, valid until the synchronous call returns
* \param host_errors receives the count of invalid polling results
*
* This function is core to Spine. It takes a host_id and polls it, first
* checking reachability and any required data-query reindexing.
*/
enum poll_result_status normalize_poll_result(char *result, bool snmp) {
classified_result_t classified;
bool unknown_marker;
if (IS_UNDEFINED(result)) return POLL_RESULT_UNDEFINED;
unknown_marker = snmp && (STRIMATCH(result, "U") || STRIMATCH(result, "Nan"));
if (classify_result(result, &classified) != RESULT_UNKNOWN) {
strncopy(result, classified.text, RESULTS_BUFFER);
return POLL_RESULT_VALID;
}
/* An agent's "u" or "NaN" is still unknown; store the U RRDtool expects. */
SET_UNDEFINED(result);
return unknown_marker ? POLL_RESULT_UNDEFINED : POLL_RESULT_INVALID;
}
typedef struct poll_error_context {
char *buffer;
int *size;
int *count;
int *errors;
int host_id;
int thread_id;
} poll_error_context_t;
static void record_result_error(const poll_error_context_t *context, const host_t *host,
const target_t *item, const char *result, bool snmp) {
buffer_output_errors(context->buffer, context->size, context->count,
context->host_id, context->thread_id, item->local_data_id, false);
(*context->errors)++;
if (set.logging.spine_log_level != 2) return;
if (snmp) {
SPINE_LOG(("WARNING: Invalid Response, Device[%i] HT[%i] DS[%i] SNMP: v%i: %s, dsname: %s, oid: %s, value: %s",
context->host_id, context->thread_id, item->local_data_id,
host->snmp.profile.version, host->hostname, item->rrd_name, item->arg1, result));
} else {
char script[SMALL_BUFSIZE];
php_command_script(item->arg1, script, sizeof(script));
SPINE_LOG(("WARNING: Invalid Response, Device[%i] HT[%i] DS[%i] SCRIPT: %s, output: %s",
context->host_id, context->thread_id, item->local_data_id, script, result));
}
}
static void normalize_snmp_item(const host_t *host, const target_t *item,
char *result, const poll_error_context_t *errors) {
if (host->ignore_host) {
SPINE_LOG(("Device[%i] HT[%i] DS[%i] WARNING: SNMP timeout detected [%i ms], ignoring host '%s'",
errors->host_id, errors->thread_id, item->local_data_id, host->snmp.profile.timeout, host->hostname));
SET_UNDEFINED(result);
return;
}
enum poll_result_status status = normalize_poll_result(result, true);
if (status == POLL_RESULT_VALID) return;
record_result_error(errors, host, item, result, true);
if (status == POLL_RESULT_INVALID) SET_UNDEFINED(result);
}
static void store_snmp_results(const host_t *host, target_t *poller_items, snmp_oids_t *snmp_oids,
int num_oids, const poll_error_context_t *errors, double thread_start, bool spike_kill) {
for (int j = 0; j < num_oids; j++) {
const target_t *item = &poller_items[snmp_oids[j].array_position];
normalize_snmp_item(host, item, snmp_oids[j].result, errors);
if (strlen(item->output_regex)) {
char temp_result[RESULTS_BUFFER];
snprintf(temp_result, sizeof(temp_result), "%s", regex_replace(item->output_regex, snmp_oids[j].result));
snprintf(snmp_oids[j].result, RESULTS_BUFFER, "%s", temp_result);
}
snprintf(poller_items[snmp_oids[j].array_position].result, RESULTS_BUFFER, "%s", snmp_oids[j].result);
double thread_end = get_time_as_double();
SPINE_LOG_DEVICE(errors->host_id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DS[%i] TT[%.2f] SNMP: v%i: %s, dsname: %s, oid: %s, value: %s", errors->host_id, errors->thread_id, poller_items[snmp_oids[j].array_position].local_data_id, (float) ((thread_end - thread_start) * 1000), host->snmp.profile.version, host->hostname, poller_items[snmp_oids[j].array_position].rrd_name, poller_items[snmp_oids[j].array_position].arg1, poller_items[snmp_oids[j].array_position].result));
if ((!IS_UNDEFINED(poller_items[snmp_oids[j].array_position].result)) && (spike_kill && (!strstr(poller_items[snmp_oids[j].array_position].result,":")))) {
SET_UNDEFINED(poller_items[snmp_oids[j].array_position].result);
}
}
}
typedef struct {
int host_id;
const char *limits;
bool due_only;
bool group_ports;
} poller_query_filter_t;
static void poller_item_query(char *buffer, size_t capacity, const char *columns, const poller_query_filter_t *filter) {
char *cursor = buffer;
cursor += spine_snprintf(cursor, capacity, "SELECT SQL_NO_CACHE %s FROM poller_item WHERE host_id = %i", columns, filter->host_id);
if (set.poller.poller_id != 0) cursor += spine_snprintf(cursor, capacity - (size_t)(cursor - buffer), " AND poller_id = %i", set.poller.poller_id);
if (filter->due_only) cursor += spine_snprintf(cursor, capacity - (size_t)(cursor - buffer), " AND rrd_next_step <= 0");
if (filter->group_ports) {
cursor += spine_snprintf(cursor, capacity - (size_t)(cursor - buffer), " GROUP BY snmp_port");
} else if (set.snmp.total_snmp_ports != 1) {
cursor += spine_snprintf(cursor, capacity - (size_t)(cursor - buffer), " ORDER BY snmp_port");
}
spine_snprintf(cursor, capacity - (size_t)(cursor - buffer), " %s", filter->limits);
}
void poller_prepare_queries(poller_queries_t *queries, int host_id, int host_thread, int host_data_ids) {
char limits[SMALL_BUFSIZE] = "";
if (host_data_ids > 0) {
if (host_thread < 1) die("ERROR: Invalid device thread for poller item selection");
long long offset = (long long)host_data_ids * ((long long)host_thread - 1);
spine_snprintf(limits, sizeof(limits), "LIMIT %lld, %i", offset, host_data_ids);
}
char columns[BUFSIZE];
/* optional output_regex column (added in Cacti 1.3.1) */
spine_snprintf(columns, sizeof(columns), "%s%s",
"action, hostname, snmp_community, snmp_version, snmp_username, snmp_password, "
"rrd_name, rrd_path, arg1, arg2, arg3, local_data_id, rrd_num, snmp_port, snmp_timeout, "
"snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_engine_id",
set.hosts.has_output_regex ? ", output_regex" : "");
poller_query_filter_t filter = {host_id, limits, FALSE, FALSE};
poller_item_query(queries->items, sizeof(queries->items), columns, &filter);
filter.due_only = set.poller.active_profiles != 1;
poller_item_query(queries->due_items, sizeof(queries->due_items), columns, &filter);
filter.group_ports = TRUE;
filter.due_only = FALSE;
poller_item_query(queries->agents, sizeof(queries->agents), "snmp_port, count(snmp_port)", &filter);
filter.due_only = set.poller.active_profiles != 1;
poller_item_query(queries->due_agents, sizeof(queries->due_agents), "snmp_port, count(snmp_port)", &filter);
spine_snprintf(queries->host, sizeof(queries->host),
"SELECT SQL_NO_CACHE id, hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, "
"snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_engine_id, snmp_port, snmp_timeout, max_oids, "
"availability_method, ping_method, ping_port, ping_timeout, ping_retries, status, status_event_count, "
"UNIX_TIMESTAMP(status_fail_date), UNIX_TIMESTAMP(status_rec_date), status_last_error, min_time, max_time, "
"cur_time, avg_time, total_polls, failed_polls, availability, snmp_sysUpTimeInstance, snmp_sysDescr, snmp_sysObjectID, "
"snmp_sysContact, snmp_sysName, snmp_sysLocation FROM host WHERE id = %i AND deleted = ''", host_id);
spine_snprintf(queries->reindex, sizeof(queries->reindex),
"SELECT SQL_NO_CACHE data_query_id, action, op, assert_value, arg1 FROM poller_reindex WHERE host_id = %i", host_id);
spine_snprintf(queries->schedule, sizeof(queries->schedule),
"UPDATE poller_item SET rrd_next_step = IF(rrd_step = %i, 0, IF(rrd_next_step - %i < 0, rrd_step - %i, rrd_next_step - %i)) WHERE host_id = %i",
set.poller.poller_interval, set.poller.poller_interval, set.poller.poller_interval, set.poller.poller_interval, host_id);
if (set.poller.poller_id != 0) {
size_t length = strlen(queries->schedule);
spine_snprintf(queries->schedule + length, sizeof(queries->schedule) - length, " AND poller_id = %i", set.poller.poller_id);
}
strncopy(queries->output, "INSERT INTO poller_output (local_data_id, rrd_name, time, output) VALUES", sizeof(queries->output));
strncopy(queries->boost_output, "INSERT INTO poller_output_boost (local_data_id, rrd_name, time, output) VALUES", sizeof(queries->boost_output));
/* The cached upsert capability describes the local connection, while output can
* go to the remote one. VALUES() is accepted by both MySQL and MariaDB. */
strncopy(queries->suffix, " ON DUPLICATE KEY UPDATE output=VALUES(output)", sizeof(queries->suffix));
}
static void host_metadata_defaults(host_t *host) {
/* initialize variables first */
host->id = 0; // 0
host->hostname[0] = '\0'; // 1
host->snmp.session = NULL; // -
host->snmp.profile.community[0] = '\0'; // 2
host->snmp.profile.version = 1; // 3
host->snmp.profile.username[0] = '\0'; // 4
host->snmp.profile.password[0] = '\0'; // 5
host->snmp.profile.auth_protocol[0] = '\0'; // 6
host->snmp.profile.priv_passphrase[0] = '\0'; // 7
host->snmp.profile.priv_protocol[0] = '\0'; // 8
host->snmp.profile.context[0] = '\0'; // 9
host->snmp.profile.engine_id[0] = '\0'; // 10
host->snmp.profile.port = 161; // 11
host->snmp.profile.timeout = 500; // 12
host->snmp.retries = set.snmp.snmp_retries; // -
host->snmp.max_oids = 10; // 13
host->availability.method = 0; // 14
host->availability.ping_method = 0; // 15
host->availability.port = 23; // 16
host->availability.timeout = 500; // 17
host->availability.retries = 2; // 18
host->state.status = HOST_UP; // 19
host->state.status_event_count = 0; // 20
host->state.status_fail_date[0] = '\0'; // 21
host->state.status_rec_date[0] = '\0'; // 22
host->state.status_last_error[0] = '\0'; // 23
host->statistics.min_time = 0; // 24
host->statistics.max_time = 0; // 25
host->statistics.cur_time = 0; // 26
host->statistics.avg_time = 0; // 27
host->statistics.total_polls = 0; // 28
host->statistics.failed_polls = 0; // 29
host->statistics.availability = 100; // 30
host->system.snmp_sysUpTimeInstance = 0; // 31
host->system.snmp_sysDescr[0] = '\0'; // 32
host->system.snmp_sysObjectID[0] = '\0'; // 33
host->system.snmp_sysContact[0] = '\0'; // 34
host->system.snmp_sysName[0] = '\0'; // 35
host->system.snmp_sysLocation[0] = '\0'; // 36
}
static void host_metadata_connection(host_t *host, MYSQL_ROW row) {
/* populate host structure */
host->ignore_host = FALSE;
if (row[0] != NULL) host->id = atoi(row[0]);
if (row[1] != NULL) {
name_t *name = get_namebyhost(row[1], NULL);
STRNCOPY(host->hostname, name->hostname);
host->availability.port = name->port;
SPINE_FREE(name);
}
if (row[2] != NULL) STRNCOPY(host->snmp.profile.community, row[2]);
if (row[3] != NULL) host->snmp.profile.version = atoi(row[3]);
if (row[4] != NULL) STRNCOPY(host->snmp.profile.username, row[4]);
if (row[5] != NULL) STRNCOPY(host->snmp.profile.password, row[5]);
if (row[6] != NULL) STRNCOPY(host->snmp.profile.auth_protocol, row[6]);
if (row[7] != NULL) STRNCOPY(host->snmp.profile.priv_passphrase, row[7]);
if (row[8] != NULL) STRNCOPY(host->snmp.profile.priv_protocol, row[8]);
if (row[9] != NULL) STRNCOPY(host->snmp.profile.context, row[9]);
if (row[10] != NULL) STRNCOPY(host->snmp.profile.engine_id, row[10]);
if (row[11] != NULL) host->snmp.profile.port = atoi(row[11]);
if (row[12] != NULL) host->snmp.profile.timeout = atoi(row[12]);
if (row[13] != NULL) host->snmp.max_oids = atoi(row[13]);
}
static void host_metadata_status(host_t *host, MYSQL_ROW row) {
if (row[14] != NULL) host->availability.method = atoi(row[14]);
if (row[15] != NULL) host->availability.ping_method = atoi(row[15]);
if (row[16] != NULL) host->availability.port = atoi(row[16]);
if (row[17] != NULL) host->availability.timeout = atoi(row[17]);
if (row[18] != NULL) host->availability.retries = atoi(row[18]);
if (row[19] != NULL) host->state.status = atoi(row[19]);
if (row[20] != NULL) host->state.status_event_count = atoi(row[20]);
if (row[21] != NULL) STRNCOPY(host->state.status_fail_date, row[21]);
if (row[22] != NULL) STRNCOPY(host->state.status_rec_date, row[22]);
if (row[23] != NULL) STRNCOPY(host->state.status_last_error, row[23]);
}
static void host_metadata_statistics(host_t *host, MYSQL_ROW row) {
if (row[24] != NULL) host->statistics.min_time = atof(row[24]);
if (row[25] != NULL) host->statistics.max_time = atof(row[25]);
if (row[26] != NULL) host->statistics.cur_time = atof(row[26]);
if (row[27] != NULL) host->statistics.avg_time = atof(row[27]);
if (row[28] != NULL) host->statistics.total_polls = atoi(row[28]);
if (row[29] != NULL) host->statistics.failed_polls = atoi(row[29]);
if (row[30] != NULL) host->statistics.availability = atof(row[30]);
}
static void host_metadata_system(host_t *host, MYSQL_ROW row, MYSQL *mysql) {
if (row[31] != NULL) host->system.snmp_sysUpTimeInstance=atoll(row[31]);
if (row[32] != NULL) db_escape(mysql, host->system.snmp_sysDescr, sizeof(host->system.snmp_sysDescr), row[32]);
if (row[33] != NULL) db_escape(mysql, host->system.snmp_sysObjectID, sizeof(host->system.snmp_sysObjectID), row[33]);
if (row[34] != NULL) db_escape(mysql, host->system.snmp_sysContact, sizeof(host->system.snmp_sysContact), row[34]);
if (row[35] != NULL) db_escape(mysql, host->system.snmp_sysName, sizeof(host->system.snmp_sysName), row[35]);
if (row[36] != NULL) db_escape(mysql, host->system.snmp_sysLocation, sizeof(host->system.snmp_sysLocation), row[36]);
}
static void persist_host_status(MYSQL *mysql, const host_t *host, bool include_system_information) {
char update_sql[BIG_BUFSIZE];
char escaped_last_error[BUFSIZE];
db_escape(mysql, escaped_last_error, sizeof(escaped_last_error), host->state.status_last_error);
if (include_system_information) {
snprintf(update_sql, BIG_BUFSIZE, "UPDATE host "
"SET status='%i', status_event_count='%i', status_fail_date=FROM_UNIXTIME(%s),"
" status_rec_date=FROM_UNIXTIME(%s), status_last_error='%s', min_time='%f',"
" max_time='%f', cur_time='%f', avg_time='%f', total_polls='%i',"
" failed_polls='%i', availability='%.4f', snmp_sysDescr='%s', "
" snmp_sysObjectID='%s', snmp_sysUpTimeInstance='%llu', "
" snmp_sysContact='%s', snmp_sysName='%s', snmp_sysLocation='%s' "
"WHERE id='%i'",
host->state.status,
host->state.status_event_count,
host->state.status_fail_date,
host->state.status_rec_date,
escaped_last_error,
host->statistics.min_time,
host->statistics.max_time,
host->statistics.cur_time,
host->statistics.avg_time,
host->statistics.total_polls,
host->statistics.failed_polls,
host->statistics.availability,
host->system.snmp_sysDescr,
host->system.snmp_sysObjectID,
host->system.snmp_sysUpTimeInstance,
host->system.snmp_sysContact,
host->system.snmp_sysName,
host->system.snmp_sysLocation,
host->id);
} else {
snprintf(update_sql, BIG_BUFSIZE, "UPDATE host "
"SET status='%i', status_event_count='%i', status_fail_date=FROM_UNIXTIME(%s),"
" status_rec_date=FROM_UNIXTIME(%s), status_last_error='%s', min_time='%f',"
" max_time='%f', cur_time='%f', avg_time='%f', total_polls='%i',"
" failed_polls='%i', availability='%.4f' "
"WHERE id='%i'",
host->state.status,
host->state.status_event_count,
host->state.status_fail_date,
host->state.status_rec_date,
escaped_last_error,
host->statistics.min_time,
host->statistics.max_time,
host->statistics.cur_time,
host->statistics.avg_time,
host->statistics.total_polls,
host->statistics.failed_polls,
host->statistics.availability,
host->id);
}
db_insert(mysql, LOCAL, update_sql);
}
static void load_host_metadata(MYSQL *mysql, MYSQL_ROW row, host_t *host, const poller_thread_t *work) {
host_metadata_defaults(host);
host_metadata_connection(host, row);
host_metadata_status(host, row);
host_metadata_statistics(host, row);
host_metadata_system(host, row, mysql);
/* correct max_oid bounds issues */
if ((host->snmp.max_oids == 0) || (host->snmp.max_oids > 100)) {
SPINE_LOG(("Device[%i] HT[%i] WARNING: Max OIDS is out of range with value of '%i'. Resetting to default of 5", work->host_id, work->host_thread, host->snmp.max_oids));
host->snmp.max_oids = 5;
}
}
static void initialize_host_snmp(host_t *host) {
if (((host->snmp.profile.version >= 1) && (host->snmp.profile.version <= 2) &&
(strlen(host->snmp.profile.community) > 0)) ||
(host->snmp.profile.version == 3)) {
host->snmp.session = snmp_host_init(&(snmp_connection_t){
.host_id = host->id,
.hostname = host->hostname,
.snmp_version = host->snmp.profile.version,
.snmp_community = host->snmp.profile.community,
.snmp_username = host->snmp.profile.username,
.snmp_password = host->snmp.profile.password,
.snmp_auth_protocol = host->snmp.profile.auth_protocol,
.snmp_priv_passphrase = host->snmp.profile.priv_passphrase,
.snmp_priv_protocol = host->snmp.profile.priv_protocol,
.snmp_context = host->snmp.profile.context,
.snmp_engine_id = host->snmp.profile.engine_id,
.snmp_port = host->snmp.profile.port,
.snmp_timeout = host->snmp.profile.timeout,
});
} else {
host->snmp.session = NULL;
}
}
static bool refresh_host_availability(MYSQL *mysql, host_t *host, ping_t *ping, int host_thread) {
if (host->availability.method == AVAIL_SNMP && strlen(host->snmp.profile.community) == 0 && host->snmp.profile.version < 3) {
host->ignore_host = FALSE;
update_host_status(HOST_UP, host, ping, host->availability.method);
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] No host availability check possible for '%s'", host->id, host_thread, host->hostname));
return FALSE;
}
if (host->availability.method == AVAIL_STREAM) {
update_host_status(HOST_UP, host, ping, host->availability.method);
return FALSE;
}
bool alive = ping_host(host, ping) == HOST_UP;
host->ignore_host = !alive;
if (host_thread != 1) return FALSE;
update_host_status(alive ? HOST_UP : HOST_DOWN, host, ping, host->availability.method);
if (!alive || host->availability.method == AVAIL_PING || host->availability.method == AVAIL_NONE || host->snmp.session == NULL || !set.snmp.mibs) return FALSE;
get_system_information(host, mysql, 1);
return TRUE;
}
typedef struct {
MYSQL *local;
MYSQL *remote;
const host_t *host;
const poller_thread_t *work;
int *errors;
int *spike_kill;
} reindex_evaluation_t;
static bool reindex_assertion_failed(const reindex_t *reindex, const char *value) {
if (value == NULL || IS_UNDEFINED(value) || STRIMATCH(value, "No Such Instance")) return FALSE;
if (STRMATCH(reindex->op, "=")) return strcmp(reindex->assert_value, value) != 0;
if (STRMATCH(reindex->op, ">")) return atoll(reindex->assert_value) < atoll(value);
if (STRMATCH(reindex->op, "<")) return !STRMATCH(reindex->assert_value, "0") && atoll(reindex->assert_value) > atoll(value);
return FALSE;
}
static void log_reindex_assertion(const reindex_evaluation_t *evaluation, const reindex_t *reindex, const char *value, bool failed) {
bool highlighted = is_debug_device(evaluation->host->id) || set.logging.spine_log_level == 2;
if (!failed && !highlighted) return;
if (failed && !highlighted && set.logging.spine_log_level == 1) (*evaluation->errors)++;
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s%s%s'", evaluation->host->id, evaluation->work->host_thread, reindex->data_query_id, reindex->assert_value, failed ? reindex->op : "=", value != NULL ? value : "(null)"));
}
/* FALSE only when the queue write failed; other partitions never queue. */
static bool queue_reindex(const reindex_evaluation_t *evaluation, const reindex_t *reindex) {
if (evaluation->work->host_thread != 1) return TRUE;
char query[LRG_BUFSIZE];
snprintf(query, sizeof(query), "REPLACE INTO poller_command (poller_id, time, action, command) VALUES (%i, NOW(), %i, '%i:%i')", set.poller.poller_id, POLLER_COMMAND_REINDEX, evaluation->host->id, reindex->data_query_id);
if (set.poller.poller_id > 1 && set.poller.mode == REMOTE_ONLINE) return db_insert(evaluation->remote, REMOTE, query);
return db_insert(evaluation->local, LOCAL, query);
}
static void update_reindex_value(const reindex_evaluation_t *evaluation, const reindex_t *reindex, const char *value) {
/* An unavailable result cannot replace the stored assertion with uninitialized bytes. */
if (evaluation->work->host_thread != 1 || value == NULL) return;
char escaped_value[BUFSIZE];
char escaped_argument[BUFSIZE];
char query[LRG_BUFSIZE];
db_escape(evaluation->local, escaped_value, sizeof(escaped_value), value);
db_escape(evaluation->local, escaped_argument, sizeof(escaped_argument), reindex->arg1);
snprintf(query, sizeof(query), "UPDATE poller_reindex SET assert_value='%s' WHERE host_id='%i' AND data_query_id='%i' AND arg1='%s'", escaped_value, evaluation->work->host_id, reindex->data_query_id, escaped_argument);
db_insert(evaluation->local, LOCAL, query);
}
static void discard_reindex_spike(const reindex_evaluation_t *evaluation) {
*evaluation->spike_kill = TRUE;
if (is_debug_device(evaluation->host->id) || set.logging.spine_log_level == 2) {
SPINE_LOG(("Device[%i] HT[%i] NOTICE: Spike Kill in Effect for '%s'", evaluation->work->host_id, evaluation->work->host_thread, evaluation->host->hostname));
} else {
if (set.logging.spine_log_level == 1) (*evaluation->errors)++;
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] NOTICE: Spike Kill in Effect for '%s'", evaluation->work->host_id, evaluation->work->host_thread, evaluation->host->hostname));
}
}
static bool evaluate_reindex_assertion(const reindex_evaluation_t *evaluation, const reindex_t *reindex, const char *value) {
bool failed = reindex_assertion_failed(reindex, value);
bool unavailable = value == NULL || IS_UNDEFINED(value) || STRIMATCH(value, "No Such Instance");
if (failed || unavailable) log_reindex_assertion(evaluation, reindex, value, failed);
/* Advancing the stored value without a queued reindex would lose the
* reindex for good: the next poll compares against the new value. */
bool queued = !failed || queue_reindex(evaluation, reindex);
/* Keep the last good baseline. U compares as 0, so storing it would hide
* the next reboot under "<" and fire a false reindex under ">". */
if (queued && !unavailable && (failed || STRMATCH(reindex->op, ">") || STRMATCH(reindex->op, "<"))) {
update_reindex_value(evaluation, reindex, value);
}
/* A failed uptime assertion means the counters reset, so the sample is a spike. */
if (failed && (STRMATCH(reindex->op, "<") ||
STRMATCH(reindex->arg1, ".1.3.6.1.2.1.1.3.0") ||
STRMATCH(reindex->arg1, ".1.3.6.1.6.3.10.2.1.3.0"))) {
discard_reindex_spike(evaluation);
}
return failed;
}
/* An uptime is a non-negative integer; anything else cannot be compared. */
static bool parse_uptime(const char *text, unsigned long long *ticks) {
unsigned long long value = 0;
unsigned int digit;
if (text == NULL || ticks == NULL) return FALSE;
while (isspace((unsigned char)*text)) text++;
if (*text < '0' || *text > '9') return FALSE;
while (*text >= '0' && *text <= '9') {
digit = (unsigned int)(*text++ - '0');
if (value > (ULLONG_MAX - digit) / 10) return FALSE;
value = value * 10 + digit;
}
while (isspace((unsigned char)*text)) text++;
if (*text != '\0') return FALSE;
*ticks = value;
return TRUE;
}
static char *poll_reindex_snmp(host_t *host, reindex_t *reindex, int host_thread, char *sysUptime, bool *unavailable) {
if (host->snmp.session == NULL) {
*unavailable = TRUE;
SPINE_LOG(("WARNING: Device[%i] HT[%i] DQ[%i] Reindex Check FAILED: No SNMP Session. If not an SNMP host, don't use Uptime Goes Backwards!", host->id, host_thread, reindex->data_query_id));
return NULL;
}
char *poll_result = NULL;
if ((strstr(reindex->arg1, ".1.3.6.1.2.1.1.3.0") ||
strstr(reindex->arg1, ".1.3.6.1.6.3.10.2.1.3.0")) && strlen(sysUptime) > 0) {
if (!(poll_result = (char *) malloc(BUFSIZE))) {
die("ERROR: Fatal malloc error: poller.c poll_result");
}
poll_result[0] = '\0';
snprintf(poll_result, BUFSIZE, "%s", sysUptime);
} else if (strstr(reindex->arg1, ".1.3.6.1.2.1.1.3.0") ||
strstr(reindex->arg1, ".1.3.6.1.6.3.10.2.1.3.0")) {
unsigned long long ticks;
bool uptime_use_engine_oid;
// Ensure uptime is empty to start with
sysUptime[0] = '\0';
/* Pin the uptime-goes-backward calculation to a single OID for this poll.
The legacy (centisecond) and modern (second) OIDs are different counters,
and letting either supply the value on different rows or cycles caused
false "uptime went backward" detections and constant reindexing. Prefer
the modern engine OID and fall back to the legacy OID only when the
engine OID is not present with numeric data. */
poll_result = snmp_get_base(host, ".1.3.6.1.6.3.10.2.1.3.0", false);
uptime_use_engine_oid = parse_uptime(poll_result, &ticks) && ticks <= ULLONG_MAX / 100;
if (uptime_use_engine_oid) {
snprintf(sysUptime, BUFSIZE, "%llu", ticks * 100);
}
SPINE_FREE(poll_result);
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] Engine Uptime OID Present: %d", host->id, host_thread, reindex->data_query_id, uptime_use_engine_oid));
if (!uptime_use_engine_oid) {
// Engine OID unavailable, fall back to the legacy sysUpTime OID
poll_result = snmp_get(host, ".1.3.6.1.2.1.1.3.0");
if (parse_uptime(poll_result, &ticks)) {
snprintf(sysUptime, BUFSIZE, "%llu", ticks);
}
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] Legacy Uptime Result: %s, Is Numeric: %d", host->id, host_thread, reindex->data_query_id, poll_result != NULL ? poll_result : "U", sysUptime[0] != '\0'));
SPINE_FREE(poll_result);
}
// Use the primed uptime to repopulate the poll_result
// This ensures whichever response was valid gets used
/* With neither OID readable there is nothing to compare. An empty
* value reads as 0, which looks like a reboot and queues a reindex. */
poll_result = strdup(sysUptime[0] != '\0' ? sysUptime : "U");
if (poll_result == NULL) {
die("ERROR: Fatal malloc error: poller.c uptime result");
}
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] Extended Uptime Result: %s, Is Numeric: %d", host->id, host_thread, reindex->data_query_id, poll_result, sysUptime[0] != '\0'));
} else {
poll_result = snmp_get(host, reindex->arg1);
}
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] RECACHE OID: %s, (assert: %s %s output: %s)", host->id, host_thread, reindex->data_query_id, reindex->arg1, reindex->assert_value, reindex->op, poll_result));
return poll_result;
}
/* trim() can return a pointer past the start of the allocation. Move the
* trimmed text to the front so the caller still frees what malloc returned. */
static char *trim_owned(char *owned) {
char *trimmed;
if (owned == NULL) return NULL;
trimmed = trim(owned);
if (trimmed != owned) memmove(owned, trimmed, strlen(trimmed) + 1);
return owned;
}
static char *poll_reindex_action(host_t *host, reindex_t *reindex, int host_thread, char *sysUptime, bool *unavailable) {
char *poll_result = NULL;
int php_process;
char script[SMALL_BUFSIZE];
switch(reindex->action) {
case POLLER_ACTION_SNMP:
poll_result = poll_reindex_snmp(host, reindex, host_thread, sysUptime, unavailable);
break;
case POLLER_ACTION_SCRIPT: /* script (popen) */
/* Reject empty script commands that could cause unexpected behavior */
if (strlen(reindex->arg1) == 0) {
SPINE_LOG(("WARNING: Device[%i] HT[%i] DQ[%i] empty script command, skipping",
host->id, host_thread, reindex->data_query_id));
break;
}
poll_result = trim_owned(exec_poll(host, reindex->arg1, reindex->data_query_id, "DQ"));
php_command_script(reindex->arg1, script, sizeof(script));
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] RECACHE CMD: %s, output: %s", host->id, host_thread, reindex->data_query_id, script, poll_result));
break;
case POLLER_ACTION_PHP_SCRIPT_SERVER: /* script (php script server) */
php_process = php_get_process();
poll_result = trim_owned(php_cmd(reindex->arg1, php_process));
php_command_script(reindex->arg1, script, sizeof(script));
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] RECACHE SERVER: %s, output: %s", host->id, host_thread, reindex->data_query_id, script, poll_result));
break;
case POLLER_ACTION_SNMP_COUNT: { /* snmp; count items */
if (!(poll_result = (char *) malloc(BUFSIZE))) {
die("ERROR: Fatal malloc error: poller.c poll_result");
}
poll_result[0] = '\0';
int snmp_items = snmp_count(host, reindex->arg1);
if (snmp_items < 0) {
SET_UNDEFINED(poll_result);
} else {
snprintf(poll_result, BUFSIZE, "%d", snmp_items);
}
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] RECACHE OID COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
break;
}
case POLLER_ACTION_SCRIPT_COUNT: { /* script (popen); count line feeds */
if (!(poll_result = (char *) malloc(BUFSIZE))) {
die("ERROR: Fatal malloc error: poller.c poll_result");
}
poll_result[0] = '\0';
char *count_result = exec_poll(host, reindex->arg1, reindex->data_query_id, "DQ");
snprintf(poll_result, BUFSIZE, "%d", char_count(count_result, '\n'));
SPINE_FREE(count_result);
php_command_script(reindex->arg1, script, sizeof(script));
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] RECACHE CMD COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, script, poll_result));
break;
}
case POLLER_ACTION_PHP_SCRIPT_SERVER_COUNT: { /* script (php script server); count number of lines */
if (!(poll_result = (char *) malloc(BUFSIZE))) {
die("ERROR: Fatal malloc error: poller.c poll_result");
}
poll_result[0] = '\0';
php_process = php_get_process();
char *count_result = php_cmd(reindex->arg1, php_process);
spine_snprintf(poll_result, BUFSIZE, "%d", char_count(count_result, '\n'));
SPINE_FREE(count_result);
php_command_script(reindex->arg1, script, sizeof(script));
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DQ[%i] RECACHE SERVER COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, script, poll_result));
break;
}
default:
SPINE_LOG(("Device[%i] HT[%i] ERROR: Unknown Assert Action!", host->id, host_thread));
}
return poll_result;
}
static void load_reindex_item(reindex_t *reindex, MYSQL_ROW row) {
/* initialize the reindex struction */
reindex->data_query_id = 0;
reindex->action = -1;
reindex->op[0] = '\0';
reindex->assert_value[0] = '\0';
reindex->arg1[0] = '\0';
if (row[0] != NULL) reindex->data_query_id = atoi(row[0]);
if (row[1] != NULL) reindex->action = atoi(row[1]);
if (row[2] != NULL) snprintf(reindex->op, sizeof(reindex->op), "%s", row[2]);
if (row[3] != NULL) snprintf(reindex->assert_value, sizeof(reindex->assert_value), "%s", row[3]);
if (row[4] != NULL) snprintf(reindex->arg1, sizeof(reindex->arg1), "%s", row[4]);
}
static bool poll_host_reindex(host_t *host, reindex_t *reindex, const char *query,
const reindex_evaluation_t *evaluation) {
if (host->ignore_host || evaluation->work->host_id == 0) return TRUE;
int thread = evaluation->work->host_thread;
MYSQL_RES *result = db_query(evaluation->local, LOCAL, query);
if (result == NULL) {
SPINE_LOG(("Device[%i] HT[%i] ERROR: RECACHE Query Returned Null Result!", host->id, thread));
} else {
int count = spine_count_to_int(mysql_num_rows(result));
if (count == 0) {
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_HIGH, ("Device[%i] HT[%i] Device has no information for recache.", host->id, thread));
} else {
SPINE_LOG_DEVICE(host->id, POLLER_VERBOSITY_DEBUG, ("Device[%i] HT[%i] DEBUG: RECACHE: Processing %i items in the auto reindex cache for '%s'", host->id, thread, count, host->hostname));
}
char sysUptime[BUFSIZE] = "";
int last_data_query_id = 0;
bool previous_assert_failure = FALSE;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
load_reindex_item(reindex, row);
if (last_data_query_id != reindex->data_query_id) {
last_data_query_id = reindex->data_query_id;
previous_assert_failure = FALSE;
}
if (previous_assert_failure) continue;
bool unavailable = FALSE;
char *value = poll_reindex_action(host, reindex, thread, sysUptime, &unavailable);
if (unavailable) continue;
if (evaluate_reindex_assertion(evaluation, reindex, value)) previous_assert_failure = TRUE;
SPINE_FREE(value);
}
db_free_result(result);
}
/* Item polling opens a new session after reindex work has finished. */
if (host->snmp.session != NULL) {
snmp_host_cleanup(host->snmp.session);
host->snmp.session = NULL;
}
return result != NULL;
}
static void load_poll_item(target_t *item, MYSQL_ROW row) {
/* initialize monitored object */
item->target_id = 0;
item->action = -1;
item->hostname[0] = '\0';
item->snmp.community[0] = '\0';
item->snmp.version = 1;
item->snmp.username[0] = '\0';
item->snmp.password[0] = '\0';
item->snmp.auth_protocol[0] = '\0';
item->snmp.priv_passphrase[0] = '\0';
item->snmp.priv_protocol[0] = '\0';
item->snmp.context[0] = '\0';
item->snmp.engine_id[0] = '\0';
item->snmp.port = 161;
item->snmp.timeout = 500;
item->rrd_name[0] = '\0';
item->rrd_path[0] = '\0';
item->arg1[0] = '\0';
item->arg2[0] = '\0';
item->arg3[0] = '\0';
item->local_data_id = 0;
item->rrd_num = 0;
item->output_regex[0] = '\0';
if (row[0] != NULL) item->action = atoi(row[0]);
if (row[1] != NULL) snprintf(item->hostname, sizeof(item->hostname), "%s", row[1]);
if (row[2] != NULL) snprintf(item->snmp.community, sizeof(item->snmp.community), "%s", row[2]);
if (row[3] != NULL) item->snmp.version = atoi(row[3]);
if (row[4] != NULL) snprintf(item->snmp.username, sizeof(item->snmp.username), "%s", row[4]);
if (row[5] != NULL) snprintf(item->snmp.password, sizeof(item->snmp.password), "%s", row[5]);
if (row[6] != NULL) snprintf(item->rrd_name, sizeof(item->rrd_name), "%s", row[6]);
if (row[7] != NULL) snprintf(item->rrd_path, sizeof(item->rrd_path), "%s", row[7]);
if (row[8] != NULL) snprintf(item->arg1, sizeof(item->arg1), "%s", row[8]);
if (row[9] != NULL) snprintf(item->arg2, sizeof(item->arg2), "%s", row[9]);
if (row[10] != NULL) snprintf(item->arg3, sizeof(item->arg3), "%s", row[10]);
if (row[11] != NULL) item->local_data_id = atoi(row[11]);
if (row[12] != NULL) item->rrd_num = atoi(row[12]);
if (row[13] != NULL) item->snmp.port = atoi(row[13]);
if (row[14] != NULL) item->snmp.timeout = atoi(row[14]);
if (row[15] != NULL) snprintf(item->snmp.auth_protocol,
sizeof(item->snmp.auth_protocol), "%s", row[15]);
if (row[16] != NULL) snprintf(item->snmp.priv_passphrase,
sizeof(item->snmp.priv_passphrase), "%s", row[16]);
if (row[17] != NULL) snprintf(item->snmp.priv_protocol,
sizeof(item->snmp.priv_protocol), "%s", row[17]);
if (row[18] != NULL) snprintf(item->snmp.context,
sizeof(item->snmp.context), "%s", row[18]);
if (row[19] != NULL) snprintf(item->snmp.engine_id,
sizeof(item->snmp.engine_id), "%s", row[19]);
if (set.hosts.has_output_regex && row[20] != NULL)
snprintf(item->output_regex, sizeof(item->output_regex), "%s", row[20]);
SET_UNDEFINED(item->result);
}
static void poll_script_item(host_t *host, target_t *item,
const poll_error_context_t *errors, double thread_start, bool spike_kill,
bool script_server) {
int php_process = -1;
char *poll_result;
/* Reject empty script commands that could cause unexpected behavior */
if (strlen(item->arg1) == 0) {
SPINE_LOG(("WARNING: Device[%i] HT[%i] DS[%i] empty script%s command, skipping",
errors->host_id, errors->thread_id, item->local_data_id, script_server ? " server" : ""));
SET_UNDEFINED(item->result);
return;
}
if (script_server) {
php_process = php_get_process();
poll_result = php_cmd(item->arg1, php_process);
} else {
poll_result = exec_poll(host, item->arg1, item->local_data_id, "DS");
}
strncopy(item->result, poll_result, sizeof(item->result));
enum poll_result_status status = normalize_poll_result(item->result, false);
if (status != POLL_RESULT_VALID) {
record_result_error(errors, host, item, item->result, false);
if (status == POLL_RESULT_INVALID) SET_UNDEFINED(item->result);
}
SPINE_FREE(poll_result);
double thread_end = get_time_as_double();
char script[SMALL_BUFSIZE];
php_command_script(item->arg1, script, sizeof(script));
if (script_server) {
SPINE_LOG_DEVICE(errors->host_id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DS[%i] TT[%.2f] SS[%i] SERVER: %s, output: %s", errors->host_id, errors->thread_id, item->local_data_id, (float) ((thread_end - thread_start) * 1000), php_process, script, item->result));
} else {
SPINE_LOG_DEVICE(errors->host_id, POLLER_VERBOSITY_MEDIUM, ("Device[%i] HT[%i] DS[%i] TT[%.2f] SCRIPT: %s, output: %s", errors->host_id, errors->thread_id, item->local_data_id, (float) ((thread_end - thread_start) * 1000), script, item->result));
}
/* insert a NaN in place of the actual value if the snmp agent restarts */
if (!IS_UNDEFINED(item->result) && spike_kill && !strstr(item->result, ":")) SET_UNDEFINED(item->result);
}
typedef struct {
char *output;
char *boost;
bool failed;
} poll_output_buffers_t;
static char *allocate_output_query(const char *prefix, const char *failure_message) {
char *query = malloc(MAX_MYSQL_BUF_SIZE + RESULTS_BUFFER);
if (query == NULL) die("%s", failure_message);
memset(query, 0, MAX_MYSQL_BUF_SIZE + RESULTS_BUFFER);
strncat(query, prefix, spine_count_to_int(strlen(prefix)));
return query;
}
/* Attempt each destination independently. Reset only early-flush buffers;
* a failed write must not suppress the other destination or later batches. */
static bool flush_output_query(MYSQL *mysql, int mode, char *query,
const char *suffix, const char *prefix) {
if (query == NULL) return TRUE;
strncat(query, suffix, spine_count_to_int(strlen(suffix)));
bool success = db_insert(mysql, mode, query);
if (prefix != NULL) {
memset(query, 0, MAX_MYSQL_BUF_SIZE + RESULTS_BUFFER);
strncat(query, prefix, spine_count_to_int(strlen(prefix)));
}
return success;
}
static poll_output_buffers_t write_poll_results(MYSQL *mysql, MYSQL *mysqlr,
const poller_queries_t *queries, const target_t *poller_items,
int rows_processed, const char *host_time) {
char *query3 = NULL;
char *query12 = NULL;
bool failed = FALSE;
/* Escaping can double the result and the data source name. */
char result_string[RESULTS_BUFFER * 2 + DBL_BUFSIZE + SMALL_BUFSIZE];
int result_length;
int new_buffer = TRUE;
size_t out_buffer;
MYSQL *mysqlt;
int i;
/* insert the query results into the database */
query3 = allocate_output_query(queries->output, "ERROR: Fatal malloc error: poller.c query3 output buffer!");
out_buffer = strlen(query3);
if (set.boost.boost_redirect && set.boost.boost_enabled) {
query12 = allocate_output_query(queries->boost_output, "ERROR: Fatal malloc error: poller.c query12 boost output buffer!");
}
int mode;
if (set.poller.poller_id > 1 && set.poller.mode == REMOTE_ONLINE) {
SPINE_LOG_DEBUG(("DEBUG: Setting up writes to remote database"));
mysqlt = mysqlr;
mode = REMOTE;
} else {
SPINE_LOG_DEBUG(("DEBUG: Setting up writes to local database"));
mysqlt = mysql;
mode = LOCAL;
}
i = 0;
while (i < rows_processed) {
char escaped_result[RESULTS_BUFFER * 2 + 1];
char escaped_rrd_name[DBL_BUFSIZE];
db_escape(mysqlt, escaped_result, sizeof(escaped_result), poller_items[i].result);