forked from Cacti/spine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller.c
2038 lines (1759 loc) · 83.2 KB
/
poller.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-2020 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"
extern int pending_threads;
/*! \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) {
int host_id;
int host_thread;
int last_host_thread;
int host_data_ids;
int host_errors;
double host_time_double;
char host_time[SMALL_BUFSIZE];
int a_threads_value;
host_errors = 0;
poller_thread_t poller_details = *(poller_thread_t*) arg;
host_id = poller_details.host_id;
host_thread = poller_details.host_thread;
last_host_thread = poller_details.last_host_thread;
host_data_ids = poller_details.host_data_ids;
host_time_double = poller_details.host_time_double;
snprintf(host_time, SMALL_BUFSIZE, "%s", poller_details.host_time);
/* Allows main thread to proceed with creation of other threads */
sem_post(poller_details.thread_init_sem);
if (is_debug_device(host_id)) {
SPINE_LOG(("DEBUG: In Poller, About to Start Polling of Device for Device ID %i", host_id));
} else {
SPINE_LOG_DEBUG(("DEBUG: In Poller, About to Start Polling of Device for Device ID %i", host_id));
}
poll_host(host_id, host_thread, last_host_thread, host_data_ids, host_time, &host_errors, host_time_double);
sem_post(&active_threads);
sem_getvalue(&active_threads, &a_threads_value);
if (is_debug_device(host_id)) {
SPINE_LOG(("DEBUG: The Value of Active Threads is %i for Device ID %i", set.threads - a_threads_value, host_id));
} else {
SPINE_LOG_DEBUG(("DEBUG: The Value of Active Threads is %i for Device ID %i", set.threads - a_threads_value, host_id));
}
thread_mutex_lock(LOCK_PEND);
pending_threads--;
poller_details.complete = TRUE;
SPINE_LOG_MEDIUM(("POLLER: Active Threads is %i, Pending is %i", set.threads - a_threads_value, pending_threads));
thread_mutex_unlock(LOCK_PEND);
/* end the thread */
pthread_exit(0);
exit(0);
}
/*! \fn void poll_host(int host_id, int host_thread, int last_host_thread, int host_data_ids, char *host_time, int *host_errors, double *host_time_double)
* \brief core Spine function that polls a host
* \param host_id integer value for the host_id from the hosts table in Cacti
*
* This function is core to Spine. It will take a host_id and then poll it.
*
* Prior to the poll, the system will ping the host to verifiy that it is up.
* In addition, the system will check to see if any reindexing of data query's
* is required.
*
* If reindexing is required, the Cacti poller.php function will spawn that
* reindexing process.
*
* In the case of hosts that require reindexing because of a sysUptime
* rollback, Spine will store an unknown (NaN) value for all objects to prevent
* spikes in the graphs.
*
* With regard to snmp calls, if the host has multiple snmp agents running
* Spine will re-initialize the snmp session and poll under those new ports
* as the host poller_items table dictates.
*
*/
void poll_host(int host_id, int host_thread, int last_host_thread, int host_data_ids, char *host_time, int *host_errors, double host_time_double) {
char query1[BUFSIZE];
char query2[BIG_BUFSIZE];
char *query3 = NULL;
char query4[BUFSIZE];
char query5[BUFSIZE];
char query6[BUFSIZE];
char query8[BUFSIZE];
char query9[BUFSIZE];
char query10[BUFSIZE];
char query11[BUFSIZE];
char *query12 = NULL;
char posuffix[BUFSIZE];
char sysUptime[BUFSIZE];
char result_string[RESULTS_BUFFER+SMALL_BUFSIZE];
int result_length;
char temp_result[RESULTS_BUFFER];
int errors = 0;
int *buf_errors;
int *buf_size;
char *error_string;
int error_len = 0;
int num_rows;
int assert_fail = FALSE;
int reindex_err = FALSE;
int spike_kill = FALSE;
int rows_processed = 0;
int i = 0;
int j = 0;
int k = 0;
int num_oids = 0;
int snmp_poller_items = 0;
size_t out_buffer;
int php_process;
char *poll_result = NULL;
char update_sql[BIG_BUFSIZE];
char temp_poll_result[BUFSIZE];
char temp_arg1[BUFSIZE];
char limits[SMALL_BUFSIZE];
int num_snmp_agents = 0;
int last_snmp_version = 0;
int last_snmp_port = 0;
char last_snmp_community[50];
char last_snmp_username[50];
char last_snmp_password[50];
char last_snmp_auth_protocol[5];
char last_snmp_priv_passphrase[200];
char last_snmp_priv_protocol[7];
char last_snmp_context[65];
char last_snmp_engine_id[30];
double poll_time = get_time_as_double();
/* reindex shortcuts to speed polling */
int previous_assert_failure = FALSE;
int last_data_query_id = 0;
int perform_assert = TRUE;
int new_buffer = TRUE;
int ignore_sysinfo = TRUE;
reindex_t *reindex;
host_t *host;
ping_t *ping;
target_t *poller_items;
snmp_oids_t *snmp_oids;
error_string = malloc(DBL_BUFSIZE);
buf_size = malloc(sizeof(int));
buf_errors = malloc(sizeof(int));
*buf_size = 0;
*buf_errors = 0;
MYSQL mysql;
MYSQL mysqlr;
MYSQL mysqlt;
MYSQL_RES *result;
MYSQL_ROW row;
db_connect(LOCAL, &mysql);
/* Since MySQL 5.7 the sql_mode defaults are too strict for cacti */
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE', ''))");
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE', ''))");
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''))");
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_AUTO_VALUE_ON_ZERO', ''))");
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'TRADITIONAL', ''))");
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_ALL_TABLES', ''))");
db_insert(&mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_TRANS_TABLES', ''))");
if (set.mode == REMOTE_ONLINE) {
db_connect(REMOTE, &mysqlr);
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE', ''))");
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE', ''))");
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''))");
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_AUTO_VALUE_ON_ZERO', ''))");
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'TRADITIONAL', ''))");
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_ALL_TABLES', ''))");
db_insert(&mysqlr, REMOTE, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_TRANS_TABLES', ''))");
}
/* allocate host and ping structures with appropriate values */
if (!(host = (host_t *) malloc(sizeof(host_t)))) {
die("ERROR: Fatal malloc error: poller.c host struct!");
}
memset(host, 0, sizeof(host_t));
if (!(ping = (ping_t *) malloc(sizeof(ping_t)))) {
die("ERROR: Fatal malloc error: poller.c ping struct!");
}
memset(ping, 0, sizeof(ping_t));
if (!(reindex = (reindex_t *) malloc(sizeof(reindex_t)))) {
die("ERROR: Fatal malloc error: poller.c reindex poll!");
}
memset(reindex, 0, sizeof(reindex_t));
/* determine the SQL limits using the poller instructions */
if (host_data_ids > 0) {
snprintf(limits, SMALL_BUFSIZE, "LIMIT %i, %i", host_data_ids * (host_thread - 1), host_data_ids);
} else {
limits[0] = '\0';
}
/* single polling interval query for items */
if (set.poller_id == 0) {
snprintf(query1, BUFSIZE,
"SELECT 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 "
" FROM poller_item"
" WHERE host_id = %i"
" AND deleted = ''"
" ORDER BY snmp_port %s", host_id, limits);
/* host structure for uptime checks */
snprintf(query2, BIG_BUFSIZE,
"SELECT 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, status_fail_date, "
"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);
/* data query structure for reindex detection */
snprintf(query4, BUFSIZE,
"SELECT data_query_id, action, op, assert_value, arg1"
" FROM poller_reindex"
" WHERE host_id=%i", host_id);
/* multiple polling interval query for items */
snprintf(query5, BUFSIZE,
"SELECT 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 "
" FROM poller_item"
" WHERE host_id=%i AND rrd_next_step <=0"
" ORDER by snmp_port %s", host_id, limits);
/* query to setup the next polling interval in cacti */
snprintf(query6, BUFSIZE,
"UPDATE poller_item"
" SET rrd_next_step=IF((rrd_next_step-%i)>=0, (rrd_next_step-%i), (rrd_step-%i))"
" WHERE host_id=%i", set.poller_interval, set.poller_interval, set.poller_interval, host_id);
/* query to add output records to the poller output table */
snprintf(query8, BUFSIZE,
"INSERT INTO poller_output"
" (local_data_id, rrd_name, time, output) VALUES");
/* query suffix to add rows to the poller output table */
snprintf(posuffix, BUFSIZE,
" ON DUPLICATE KEY UPDATE output=VALUES(output)");
/* number of agent's count for single polling interval */
snprintf(query9, BUFSIZE,
"SELECT snmp_port, count(snmp_port)"
" FROM poller_item"
" WHERE host_id=%i"
" GROUP BY snmp_port %s", host_id, limits);
/* number of agent's count for multiple polling intervals */
snprintf(query10, BUFSIZE,
"SELECT snmp_port, count(snmp_port)"
" FROM poller_item"
" WHERE host_id=%i"
" AND rrd_next_step <=0"
" GROUP BY snmp_port %s", host_id, limits);
} else {
snprintf(query1, BUFSIZE,
"SELECT 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 "
" FROM poller_item"
" WHERE host_id=%i AND poller_id=%i"
" ORDER BY snmp_port %s", host_id, set.poller_id, limits);
/* host structure for uptime checks */
snprintf(query2, BIG_BUFSIZE,
"SELECT 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, status_fail_date, "
"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);
/* data query structure for reindex detection */
snprintf(query4, BUFSIZE,
"SELECT data_query_id, action, op, assert_value, arg1"
" FROM poller_reindex"
" WHERE host_id=%i", host_id);
/* multiple polling interval query for items */
snprintf(query5, BUFSIZE,
"SELECT 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 "
" FROM poller_item"
" WHERE host_id=%i AND rrd_next_step <=0 AND poller_id=%i"
" ORDER by snmp_port %s", host_id, set.poller_id, limits);
/* query to setup the next polling interval in cacti */
snprintf(query6, BUFSIZE,
"UPDATE poller_item"
" SET rrd_next_step=IF((rrd_next_step-%i)>=0, (rrd_next_step-%i), (rrd_step-%i))"
" WHERE host_id=%i AND poller_id=%i", set.poller_interval, set.poller_interval, set.poller_interval, host_id, set.poller_id);
/* query to add output records to the poller output table */
snprintf(query8, BUFSIZE,
"INSERT INTO poller_output"
" (local_data_id, rrd_name, time, output) VALUES");
/* query suffix to add rows to the poller output table */
snprintf(posuffix, BUFSIZE,
" ON DUPLICATE KEY UPDATE output=VALUES(output)");
/* number of agent's count for single polling interval */
snprintf(query9, BUFSIZE,
"SELECT snmp_port, count(snmp_port)"
" FROM poller_item"
" WHERE host_id=%i"
" AND poller_id=%i"
" GROUP BY snmp_port %s", host_id, set.poller_id, limits);
/* number of agent's count for multiple polling intervals */
snprintf(query10, BUFSIZE,
"SELECT snmp_port, count(snmp_port)"
" FROM poller_item"
" WHERE host_id=%i"
" AND rrd_next_step <=0"
" AND poller_id=%i"
" GROUP BY snmp_port %s", host_id, set.poller_id, limits);
}
/* query to add output records to the poller output table */
snprintf(query11, BUFSIZE,
"INSERT INTO poller_output_boost"
" (local_data_id, rrd_name, time, output) VALUES");
/* initialize the ping structure variables */
snprintf(ping->ping_status, 50, "down");
snprintf(ping->ping_response, SMALL_BUFSIZE, "Ping not performed due to setting.");
snprintf(ping->snmp_status, 50, "down");
snprintf(ping->snmp_response, SMALL_BUFSIZE, "SNMP not performed due to setting or ping result");
/* if the host is a real host. Note host_id=0 is not host based data source */
if (host_id) {
/* get data about this host */
if ((result = db_query(&mysql, LOCAL, query2)) != 0) {
num_rows = mysql_num_rows(result);
if (num_rows != 1) {
mysql_free_result(result);
db_disconnect(&mysql);
if (set.mode == REMOTE_ONLINE) {
db_disconnect(&mysqlr);
}
#ifndef OLD_MYSQL
mysql_thread_end();
#endif
return;
}
/* fetch the result */
row = mysql_fetch_row(result);
if (row) {
/* initialize variables first */
host->id = 0; // 0
host->hostname[0] = '\0'; // 1
host->snmp_session = NULL; // -
host->snmp_community[0] = '\0'; // 2
host->snmp_version = 1; // 3
host->snmp_username[0] = '\0'; // 4
host->snmp_password[0] = '\0'; // 5
host->snmp_auth_protocol[0] = '\0'; // 6
host->snmp_priv_passphrase[0] = '\0'; // 7
host->snmp_priv_protocol[0] = '\0'; // 8
host->snmp_context[0] = '\0'; // 9
host->snmp_engine_id[0] = '\0'; // 10
host->snmp_port = 161; // 11
host->snmp_timeout = 500; // 12
host->snmp_retries = set.snmp_retries; // -
host->max_oids = 10; // 13
host->availability_method = 0; // 14
host->ping_method = 0; // 15
host->ping_port = 23; // 16
host->ping_timeout = 500; // 17
host->ping_retries = 2; // 18
host->status = HOST_UP; // 19
host->status_event_count = 0; // 20
host->status_fail_date[0] = '\0'; // 21
host->status_rec_date[0] = '\0'; // 22
host->status_last_error[0] = '\0'; // 23
host->min_time = 0; // 24
host->max_time = 0; // 25
host->cur_time = 0; // 26
host->avg_time = 0; // 27
host->total_polls = 0; // 28
host->failed_polls = 0; // 29
host->availability = 100; // 30
host->snmp_sysUpTimeInstance = 0; // 31
host->snmp_sysDescr[0] = '\0'; // 32
host->snmp_sysObjectID[0] = '\0'; // 33
host->snmp_sysContact[0] = '\0'; // 34
host->snmp_sysName[0] = '\0'; // 35
host->snmp_sysLocation[0] = '\0'; // 36
/* populate host structure */
host->ignore_host = FALSE;
if (row[0] != NULL) host->id = atoi(row[0]);
if (row[1] != NULL) STRNCOPY(host->hostname, row[1]);
if (row[2] != NULL) STRNCOPY(host->snmp_community, row[2]);
if (row[3] != NULL) host->snmp_version = atoi(row[3]);
if (row[4] != NULL) STRNCOPY(host->snmp_username, row[4]);
if (row[5] != NULL) STRNCOPY(host->snmp_password, row[5]);
if (row[6] != NULL) STRNCOPY(host->snmp_auth_protocol, row[6]);
if (row[7] != NULL) STRNCOPY(host->snmp_priv_passphrase, row[7]);
if (row[8] != NULL) STRNCOPY(host->snmp_priv_protocol, row[8]);
if (row[9] != NULL) STRNCOPY(host->snmp_context, row[9]);
if (row[10] != NULL) STRNCOPY(host->snmp_engine_id, row[10]);
if (row[11] != NULL) host->snmp_port = atoi(row[11]);
if (row[12] != NULL) host->snmp_timeout = atoi(row[12]);
if (row[13] != NULL) host->max_oids = atoi(row[13]);
if (row[14] != NULL) host->availability_method = atoi(row[14]);
if (row[15] != NULL) host->ping_method = atoi(row[15]);
if (row[16] != NULL) host->ping_port = atoi(row[16]);
if (row[17] != NULL) host->ping_timeout = atoi(row[17]);
if (row[18] != NULL) host->ping_retries = atoi(row[18]);
if (row[19] != NULL) host->status = atoi(row[19]);
if (row[20] != NULL) host->status_event_count = atoi(row[20]);
if (row[21] != NULL) STRNCOPY(host->status_fail_date, row[21]);
if (row[22] != NULL) STRNCOPY(host->status_rec_date, row[22]);
if (row[23] != NULL) STRNCOPY(host->status_last_error, row[23]);
if (row[24] != NULL) host->min_time = atof(row[24]);
if (row[25] != NULL) host->max_time = atof(row[25]);
if (row[26] != NULL) host->cur_time = atof(row[26]);
if (row[27] != NULL) host->avg_time = atof(row[27]);
if (row[28] != NULL) host->total_polls = atoi(row[28]);
if (row[29] != NULL) host->failed_polls = atoi(row[29]);
if (row[30] != NULL) host->availability = atof(row[30]);
if (row[31] != NULL) host->snmp_sysUpTimeInstance=atoi(row[31]);
if (row[32] != NULL) db_escape(&mysql, host->snmp_sysDescr, sizeof(host->snmp_sysDescr), row[32]);
if (row[33] != NULL) db_escape(&mysql, host->snmp_sysObjectID, sizeof(host->snmp_sysObjectID), row[33]);
if (row[34] != NULL) db_escape(&mysql, host->snmp_sysContact, sizeof(host->snmp_sysContact), row[34]);
if (row[35] != NULL) db_escape(&mysql, host->snmp_sysName, sizeof(host->snmp_sysName), row[35]);
if (row[36] != NULL) db_escape(&mysql, host->snmp_sysLocation, sizeof(host->snmp_sysLocation), row[36]);
/* correct max_oid bounds issues */
if ((host->max_oids == 0) || (host->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", host_id, host_thread, host->max_oids));
host->max_oids = 5;
}
/* free the host result */
mysql_free_result(result);
if (((host->snmp_version >= 1) && (host->snmp_version <= 2) &&
(strlen(host->snmp_community) > 0)) ||
(host->snmp_version == 3)) {
host->snmp_session = snmp_host_init(host->id,
host->hostname,
host->snmp_version,
host->snmp_community,
host->snmp_username,
host->snmp_password,
host->snmp_auth_protocol,
host->snmp_priv_passphrase,
host->snmp_priv_protocol,
host->snmp_context,
host->snmp_engine_id,
host->snmp_port,
host->snmp_timeout);
} else {
host->snmp_session = NULL;
}
/* perform a check to see if the host is alive by polling it's SysDesc
* if the host down from an snmp perspective, don't poll it.
* function sets the ignore_host bit */
if ((host->availability_method == AVAIL_SNMP) &&
(strlen(host->snmp_community) == 0) &&
(host->snmp_version < 3)) {
host->ignore_host = FALSE;
update_host_status(HOST_UP, host, ping, host->availability_method);
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] No host availability check possible for '%s'", host->id, host_thread, host->hostname));
} else {
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] No host availability check possible for '%s'", host->id, host_thread, host->hostname));
}
} else {
if (ping_host(host, ping) == HOST_UP) {
host->ignore_host = FALSE;
if (host_thread == 1) {
update_host_status(HOST_UP, host, ping, host->availability_method);
if ((host->availability_method != AVAIL_PING) && (host->availability_method != AVAIL_NONE)) {
if (host->snmp_session != NULL) {
get_system_information(host, &mysql, 1);
ignore_sysinfo = FALSE;
}
}
}
} else {
host->ignore_host = TRUE;
if (host_thread == 1) {
update_host_status(HOST_DOWN, host, ping, host->availability_method);
}
}
}
/* update host table */
if (host_thread == 1) {
if (!ignore_sysinfo) {
if (host->ignore_host != TRUE) {
snprintf(update_sql, BIG_BUFSIZE, "UPDATE host "
"SET status='%i', status_event_count='%i', status_fail_date='%s',"
" status_rec_date='%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='%u', "
" snmp_sysContact='%s', snmp_sysName='%s', snmp_sysLocation='%s' "
"WHERE id='%i'",
host->status,
host->status_event_count,
host->status_fail_date,
host->status_rec_date,
host->status_last_error,
host->min_time,
host->max_time,
host->cur_time,
host->avg_time,
host->total_polls,
host->failed_polls,
host->availability,
host->snmp_sysDescr,
host->snmp_sysObjectID,
host->snmp_sysUpTimeInstance,
host->snmp_sysContact,
host->snmp_sysName,
host->snmp_sysLocation,
host->id);
} else {
snprintf(update_sql, BIG_BUFSIZE, "UPDATE host "
"SET status='%i', status_event_count='%i', status_fail_date='%s',"
" status_rec_date='%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->status,
host->status_event_count,
host->status_fail_date,
host->status_rec_date,
host->status_last_error,
host->min_time,
host->max_time,
host->cur_time,
host->avg_time,
host->total_polls,
host->failed_polls,
host->availability,
host->id);
}
} else {
snprintf(update_sql, BIG_BUFSIZE, "UPDATE host "
"SET status='%i', status_event_count='%i', status_fail_date='%s',"
" status_rec_date='%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->status,
host->status_event_count,
host->status_fail_date,
host->status_rec_date,
host->status_last_error,
host->min_time,
host->max_time,
host->cur_time,
host->avg_time,
host->total_polls,
host->failed_polls,
host->availability,
host->id);
}
db_insert(&mysql, LOCAL, update_sql);
}
} else {
SPINE_LOG(("Device[%i] HT[%i] ERROR: MySQL Returned a Null Device Result", host->id, host_thread));
num_rows = 0;
host->ignore_host = TRUE;
}
} else {
num_rows = 0;
host->ignore_host = TRUE;
}
} else {
host->id = 0;
host->max_oids = 1;
host->snmp_session = NULL;
host->ignore_host = FALSE;
}
/* do the reindex check for this host if not script based */
if ((!host->ignore_host) && (host_id)) {
if ((result = db_query(&mysql, LOCAL, query4)) != 0) {
num_rows = mysql_num_rows(result);
if (num_rows > 0) {
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] RECACHE: Processing %i items in the auto reindex cache for '%s'", host->id, host_thread, num_rows, host->hostname));
} else {
SPINE_LOG_DEBUG(("Device[%i] HT[%i] RECACHE: Processing %i items in the auto reindex cache for '%s'", host->id, host_thread, num_rows, host->hostname));
}
// Cache uptime in case we need it again
sysUptime[0] = '\0';
while ((row = mysql_fetch_row(result))) {
assert_fail = FALSE;
reindex_err = FALSE;
/* 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]);
/* shortcut assertion checks if a data query reindex has already been queued */
if ((last_data_query_id == reindex->data_query_id) &&
(!previous_assert_failure)) {
perform_assert = TRUE;
} else if (last_data_query_id != reindex->data_query_id) {
last_data_query_id = reindex->data_query_id;
perform_assert = TRUE;
previous_assert_failure = FALSE;
} else {
perform_assert = FALSE;
}
poll_result = NULL;
if (perform_assert) {
switch(reindex->action) {
case POLLER_ACTION_SNMP: /* snmp */
/* if there is no snmp session, don't probe */
if (!host->snmp_session) {
reindex_err = TRUE;
}
/* check to see if you are checking uptime */
if (!reindex_err) {
if (strstr(reindex->arg1, ".1.3.6.1.2.1.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")) {
poll_result = snmp_get(host, reindex->arg1);
snprintf(sysUptime, BUFSIZE, "%s", poll_result);
} else {
poll_result = snmp_get(host, reindex->arg1);
}
if (is_debug_device(host->id)) {
SPINE_LOG(("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));
} else {
SPINE_LOG_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));
}
} else {
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));
}
break;
case POLLER_ACTION_SCRIPT: /* script (popen) */
poll_result = trim(exec_poll(host, reindex->arg1, reindex->data_query_id, "DQ"));
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE CMD: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
} else {
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] DQ[%i] RECACHE CMD: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
}
break;
case POLLER_ACTION_PHP_SCRIPT_SERVER: /* script (php script server) */
php_process = php_get_process();
poll_result = trim(php_cmd(reindex->arg1, php_process));
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE SERVER: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
} else {
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] DQ[%i] RECACHE SERVER: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, 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';
snprintf(poll_result, BUFSIZE, "%d", snmp_count(host, reindex->arg1));
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE OID COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
} else {
SPINE_LOG_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 items by counting line feeds */
if (!(poll_result = (char *) malloc(BUFSIZE))) {
die("ERROR: Fatal malloc error: poller.c poll_result");
}
poll_result[0] = '\0';
snprintf(poll_result, BUFSIZE, "%d", char_count(exec_poll(host, reindex->arg1, reindex->data_query_id, "DQ"), '\n'));
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE CMD COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
} else {
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] DQ[%i] RECACHE CMD COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, 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();
sprintf(poll_result, "%d", char_count(php_cmd(reindex->arg1, php_process), '\n'));
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE SERVER COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
} else {
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] DQ[%i] RECACHE SERVER COUNT: %s, output: %s", host->id, host_thread, reindex->data_query_id, reindex->arg1, poll_result));
}
break;
default:
SPINE_LOG(("Device[%i] HT[%i] ERROR: Unknown Assert Action!", host->id, host_thread));
}
if (!reindex_err) {
if (!(query3 = (char *)malloc(LRG_BUFSIZE))) {
die("ERROR: Fatal malloc error: poller.c reindex insert!");
}
query3[0] = '\0';
/* assume ok if host is up and result wasn't obtained */
if (poll_result == NULL || (IS_UNDEFINED(poll_result)) || (STRIMATCH(poll_result, "No Such Instance"))) {
if (is_debug_device(host->id) || set.spine_log_level == 2) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s=%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
}
assert_fail = FALSE;
} else if ((!strcmp(reindex->op, "=")) && (strcmp(reindex->assert_value, poll_result))) {
if (is_debug_device(host->id) || set.spine_log_level == 2) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s=%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
} else {
if (set.spine_log_level == 1) {
errors++;
}
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s=%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
}
if (host_thread == 1) {
snprintf(query3, LRG_BUFSIZE, "REPLACE INTO poller_command (poller_id, time, action,command) values (%i, NOW(), %i, '%i:%i')", set.poller_id, POLLER_COMMAND_REINDEX, host->id, reindex->data_query_id);
if (set.mode == REMOTE_ONLINE) {
db_insert(&mysqlr, REMOTE, query3);
} else {
db_insert(&mysql, LOCAL, query3);
}
}
assert_fail = TRUE;
previous_assert_failure = TRUE;
} else if ((!strcmp(reindex->op, ">")) && (strtoll(reindex->assert_value, (char **)NULL, 10) < strtoll(poll_result, (char **)NULL, 10))) {
if (is_debug_device(host->id) || set.spine_log_level == 2) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s>%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
} else {
if (set.spine_log_level == 1) {
errors++;
}
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s>%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
}
if (host_thread == 1) {
snprintf(query3, LRG_BUFSIZE, "REPLACE INTO poller_command (poller_id, time, action, command) values (%i, NOW(), %i, '%i:%i')", set.poller_id, POLLER_COMMAND_REINDEX, host->id, reindex->data_query_id);
if (set.mode == REMOTE_ONLINE) {
db_insert(&mysqlr, REMOTE, query3);
} else {
db_insert(&mysql, LOCAL, query3);
}
}
assert_fail = TRUE;
previous_assert_failure = TRUE;
/* if uptime is set to '0' don't fail out */
} else if (strcmp(reindex->assert_value, "0")) {
if ((!strcmp(reindex->op, "<")) && (strtoll(reindex->assert_value, (char **)NULL, 10) > strtoll(poll_result, (char **)NULL, 10))) {
if (is_debug_device(host->id) || set.spine_log_level == 2) {
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s<%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
} else {
if (set.spine_log_level == 1) {
errors++;
}
SPINE_LOG(("Device[%i] HT[%i] DQ[%i] RECACHE ASSERT FAILED: '%s<%s'", host->id, host_thread, reindex->data_query_id, reindex->assert_value, poll_result));
}
if (host_thread == 1) {
snprintf(query3, LRG_BUFSIZE, "REPLACE INTO poller_command (poller_id, time, action, command) values (%i, NOW(), %i, '%i:%i')", set.poller_id, POLLER_COMMAND_REINDEX, host->id, reindex->data_query_id);
if (set.mode == REMOTE_ONLINE) {
db_insert(&mysqlr, REMOTE, query3);
} else {
db_insert(&mysql, LOCAL, query3);
}
}
assert_fail = TRUE;
previous_assert_failure = TRUE;
}
}
/* update 'poller_reindex' with the correct information if:
* 1) the assert fails
* 2) the OP code is > or < meaning the current value could have changed without causing
* the assert to fail */
if ((assert_fail) || (!strcmp(reindex->op, ">")) || (!strcmp(reindex->op, "<"))) {
if (host_thread == 1) {
db_escape(&mysql, temp_poll_result, sizeof(temp_poll_result), poll_result);
db_escape(&mysql, temp_arg1, sizeof(temp_arg1), reindex->arg1);
snprintf(query3, LRG_BUFSIZE, "UPDATE poller_reindex SET assert_value='%s' WHERE host_id='%i' AND data_query_id='%i' AND arg1='%s'", temp_poll_result, host_id, reindex->data_query_id, temp_arg1);
db_insert(&mysql, LOCAL, query3);
}
if ((assert_fail) &&
((!strcmp(reindex->op, "<")) || (!strcmp(reindex->arg1,".1.3.6.1.2.1.1.3.0")))) {
spike_kill = TRUE;
if (is_debug_device(host->id) || set.spine_log_level == 2) {
SPINE_LOG(("Device[%i] HT[%i] NOTICE: Spike Kill in Effect for '%s'", host_id, host_thread, host->hostname));
} else {
if (set.spine_log_level == 1) {
errors++;
}
SPINE_LOG_MEDIUM(("Device[%i] HT[%i] NOTICE: Spike Kill in Effect for '%s'", host_id, host_thread, host->hostname));
}
}
}
free(query3);
if (poll_result != NULL) {
free(poll_result);
}
}
}
}
} else {
if (is_debug_device(host->id)) {
SPINE_LOG(("Device[%i] HT[%i] Device has no information for recache.", host->id, host_thread));
} else {
SPINE_LOG_HIGH(("Device[%i] HT[%i] Device has no information for recache.", host->id, host_thread));
}
}
/* free the host result */
mysql_free_result(result);
} else {
SPINE_LOG(("Device[%i] HT[%i] ERROR: RECACHE Query Returned Null Result!", host->id, host_thread));
}
/* close the host snmp session, we will create again momentarily */
if (host->snmp_session) {
snmp_host_cleanup(host->snmp_session);
host->snmp_session = NULL;
}
}
/* calculate the number of poller items to poll this cycle */
num_rows = 0;
if (set.poller_interval == 0) {
/* get the number of agents */
if ((result = db_query(&mysql, LOCAL, query9)) != 0) {
num_snmp_agents = mysql_num_rows(result);
mysql_free_result(result);
/* get the poller items */
if ((result = db_query(&mysql, LOCAL, query1)) != 0) {
num_rows = mysql_num_rows(result);
} else {
SPINE_LOG(("Device[%i] HT[%i] ERROR: Unable to Retrieve Rows due to Null Result!", host->id, host_thread));
}
} else {
SPINE_LOG(("Device[%i] HT[%i] ERROR: Agent Count Query Returned Null Result!", host->id, host_thread));
}
} else {
/* get the number of agents */
if ((result = db_query(&mysql, LOCAL, query10)) != 0) {
num_snmp_agents = (int)mysql_num_rows(result);
mysql_free_result(result);
/* get the poller items */
if ((result = db_query(&mysql, LOCAL, query5)) != 0) {
num_rows = mysql_num_rows(result);
} else {
SPINE_LOG(("Device[%i] HT[%i] ERROR: Unable to Retrieve Rows due to Null Result!", host->id, host_thread));
}
} else {
SPINE_LOG(("Device[%i] HT[%i] ERROR: Agent Count Query Returned Null Result!", host->id, host_thread));