forked from NETWAYS/check_interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_interfaces.c
1515 lines (1360 loc) · 43.6 KB
/
check_interfaces.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
// This one is needed on FreeBSD and it has to be before the others or at least some of them
#include <getopt.h>
#include "snmp_bulkget.h"
#include "utils.h"
#include <net-snmp/net-snmp-config.h>
#include <stdbool.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
/*
* text strings to output in the perfdata
*/
char *if_vars_default[] = {"inOctets", "outOctets", "inDiscards", "outDiscards",
"inErrors", "outErrors", "inUcast", "outUcast",
"speed", "inBitps", "outBitps"};
char *if_vars_cisco[] = {"inOctets", "outOctets", "inDiscards",
"outDiscards", "inCRCs", "outCollisions",
"inUcast", "outUcast", "speed",
"inBitps", "outBitps"};
/*
* OIDs, hardcoded to remove the dependency on MIBs
*/
char *oid_if_bulkget[] = {".1.3.6.1.2.1.1.3", ".1.3.6.1.2.1.2.1",
".1.3.6.1.2.1.2.2.1.2",
0}; /* "uptime", "ifNumber", "ifDescr" */
char *oid_if_get[] = {".1.3.6.1.2.1.1.3.0", ".1.3.6.1.2.1.2.1.0",
".1.3.6.1.2.1.2.2.1.2.1",
0}; /* "uptime", "ifNumber", "ifDescr" */
char *oid_if_bintec[] = {".1.3.6.1.2.1.1.3.0", ".1.3.6.1.2.1.2.1.0",
".1.3.6.1.2.1.2.2.1.2.0",
0}; /* "uptime", "ifNumber", "ifDescr" */
char *oid_extended[] = {".1.3.6.1.2.1.31.1.1.1.6", /* ifHCInOctets */
".1.3.6.1.2.1.31.1.1.1.10", /* ifHCOutOctets */
".1.3.6.1.2.1.2.2.1.11", /* ifInUcastPkts */
".1.3.6.1.2.1.2.2.1.17", /* ifOutUcastPkts */
".1.3.6.1.2.1.2.2.1.5", /* ifSpeed */
".1.3.6.1.2.1.31.1.1.1.15", /* ifHighSpeed */
".1.3.6.1.2.1.31.1.1.1.18", /* alias */
".1.3.6.1.2.1.31.1.1.1.1", /* name */
0};
char *oid_alias_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.18", 0}; /* "alias" */
char *oid_alias_get[] = {".1.3.6.1.2.1.31.1.1.1.18.1", 0}; /* "alias" */
char *oid_alias_bintec[] = {".1.3.6.1.2.1.31.1.1.1.18.0", 0}; /* "alias" */
char *oid_names_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.1", 0}; /* "name" */
char *oid_names_get[] = {".1.3.6.1.2.1.31.1.1.1.1.1", 0}; /* "name" */
char *oid_names_bintec[] = {".1.3.6.1.2.1.31.1.1.1.1.0",
0}; /* "name - NOT TESTED!" */
char *oid_extended_cisco[] = {
".1.3.6.1.4.1.9.5.1.4.1.1.23", /* portAdditionalOperStatus */
0};
char *oid_vals_default[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
".1.3.6.1.2.1.2.2.1.8", /* ifOperStatus */
".1.3.6.1.2.1.2.2.1.10", /* ifInOctets */
".1.3.6.1.2.1.2.2.1.13", /* ifInDiscards */
".1.3.6.1.2.1.2.2.1.14", /* ifInErrors */
".1.3.6.1.2.1.2.2.1.16", /* ifOutOctets */
".1.3.6.1.2.1.2.2.1.19", /* ifOutDiscards */
".1.3.6.1.2.1.2.2.1.20", /* ifOutErrors */
0};
char *oid_vals_cisco[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
".1.3.6.1.2.1.2.2.1.8", /* ifOperStatus */
".1.3.6.1.2.1.2.2.1.10", /* ifInOctets */
".1.3.6.1.2.1.2.2.1.13", /* ifInDiscards */
".1.3.6.1.4.1.9.2.2.1.1.12", /* locIfInCRC */
".1.3.6.1.2.1.2.2.1.16", /* ifOutOctets */
".1.3.6.1.2.1.2.2.1.19", /* ifOutDiscards */
".1.3.6.1.4.1.9.2.2.1.1.25", /* locIfCollisions */
0};
#define DEFAULT_COMMUNITY "public"
/*
* operating modes
*/
const char *modes[] = {"default", "cisco", "nonbulk", "bintec", NULL};
#ifdef DEBUG
static char *implode_result;
#endif
enum returncode {
OK = 0,
WARNING = 1,
CRITICAL = 2,
UNKNOWN = 3
};
typedef enum returncode returncode_t;
// Forward declarations
void parse_and_check_commandline(int argc, char **argv,
struct configuration_struct *config);
bool fetch_interface_aliases(struct configuration_struct* , char **, netsnmp_session *ss, netsnmp_session *session, struct ifStruct *interfaces, int ifNumber);
bool fetch_interface_names(struct configuration_struct* , char **oid_namesp, netsnmp_session *ss, netsnmp_session *session, struct ifStruct *interfaces, int ifNumber);
returncode_t print_output(struct configuration_struct *config, struct ifStruct *oldperfdata,
long double starttime, struct ifStruct *interfaces, String *out, char **if_vars,
unsigned int number_of_matched_interfaces, struct timeval *tv, int uptime,
int ifNumber);
int main(int argc, char *argv[]) {
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
netsnmp_variable_list *vars;
int status, status2;
int count = 0; /* used for: the number of interfaces we receive, the number
of regex matches */
size_t size;
/* uptime counter */
unsigned int uptime = 0;
int ifNumber = 0;
config_t config = {
.crit_on_down_flag = true,
.get_aliases_flag = false,
.match_aliases_flag = false,
.get_names_flag = false,
.print_all_flag = false,
.community = DEFAULT_COMMUNITY,
.bandwith = 0,
.oldperfdatap = 0,
.err_tolerance = 50,
.coll_tolerance = -1,
.hostname = 0,
.user = 0,
.auth_proto = 0,
.auth_pass = 0,
.priv_proto = 0,
.priv_pass = 0,
.trimdescr = 0,
.prefix = 0,
.iface_regex = 0,
.global_timeout = DFLT_TIMEOUT,
.exclude_list = 0,
.speed = 0,
.lastcheck = 0,
.sleep_usecs = 0,
.session_retries = 2,
.pdu_max_repetitions = 4096L,
.mode = DEFAULT,
};
struct ifStruct *interfaces = NULL; /* current interface data */
struct ifStruct *oldperfdata = NULL; /* previous check interface data */
struct OIDStruct *OIDp;
long double starttime;
int ignore_count = 0;
char outstr[MAX_STRING];
memset(outstr, 0, sizeof(outstr));
String out;
out.max = MAX_STRING;
out.len = 0;
out.text = outstr;
struct OIDStruct lastOid;
static char **oid_ifp;
static char **oid_vals;
static char **if_vars;
static char **oid_aliasp;
static char **oid_namesp;
oid_ifp = oid_if_bulkget;
oid_vals = oid_vals_default;
if_vars = if_vars_default;
parse_and_check_commandline(argc, argv, &config);
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
starttime = (long double)tv.tv_sec + (((long double)tv.tv_usec) / 1000000);
#ifdef DEBUG
benchmark_start("Start SNMP session");
#endif
if (config.user)
/* use snmpv3 */
ss = start_session_v3(&session, config.user, config.auth_proto,
config.auth_pass, config.priv_proto,
config.priv_pass, config.hostname,
config.global_timeout, config.session_retries);
else
ss = start_session(&session, config.community, config.hostname,
config.mode, config.global_timeout,
config.session_retries);
#ifdef DEBUG
benchmark_end();
#endif
if (config.mode == NONBULK) {
oid_ifp = oid_if_get;
size = (sizeof(oid_if_get) / sizeof(char *)) - 1;
oid_aliasp = oid_alias_get;
oid_namesp = oid_names_get;
} else if (config.mode == BINTEC) {
oid_ifp = oid_if_bintec;
size = (sizeof(oid_if_bintec) / sizeof(char *)) - 1;
oid_aliasp = oid_alias_bintec;
oid_namesp = oid_names_bintec;
} else {
oid_ifp = oid_if_bulkget;
size = (sizeof(oid_if_bulkget) / sizeof(char *)) - 1;
oid_aliasp = oid_alias_bulkget;
oid_namesp = oid_names_bulkget;
}
/* allocate the space for the interface OIDs */
OIDp = (struct OIDStruct *)calloc(size, sizeof(struct OIDStruct));
if (config.mode == CISCO) {
if_vars = if_vars_cisco;
oid_vals = oid_vals_cisco;
}
/* get the number of interfaces, and their index numbers
*
* We will attempt to get all the interfaces in a single packet
* - which should manage about 64 interfaces.
* If the end interface has not been reached, we fetch more packets - this
* is necessary to work around buggy switches that lie about the ifNumber
*/
for (bool lastifflag = false; lastifflag != true;) {
/* build our request depending on the mode */
if (count == 0)
create_pdu(config.mode, oid_ifp, &pdu, &OIDp, 2,
config.pdu_max_repetitions);
else {
/* we have not received all interfaces in the preceding packet, so
* fetch the next lot */
if (config.mode == BINTEC || config.mode == NONBULK)
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
else {
pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
pdu->non_repeaters = 0;
pdu->max_repetitions = config.pdu_max_repetitions;
}
snmp_add_null_var(pdu, lastOid.name, lastOid.name_len);
}
#ifdef DEBUG
implode_result = implode(", ", oid_ifp + count);
benchmark_start("Send SNMP request for OIDs: %s", implode_result);
#endif
/* send the request */
status = snmp_synch_response(ss, pdu, &response);
#ifdef DEBUG
benchmark_end();
free(implode_result);
#endif
if (config.sleep_usecs)
usleep(config.sleep_usecs);
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
vars = response->variables;
if (count == 0) {
/* assuming that the uptime and ifNumber come first */
/* on some devices the ifNumber is not available... */
while (!ifNumber) {
if (!(memcmp(OIDp[0].name, vars->name,
OIDp[0].name_len * sizeof(oid)))) {
/* uptime */
if (vars->type == ASN_TIMETICKS)
/* uptime is in 10ms units -> convert to seconds */
uptime = *(vars->val.integer) / 100;
} else if (!memcmp(OIDp[1].name, vars->name,
OIDp[1].name_len * sizeof(oid))) {
/* we received a valid IfNumber */
ifNumber = *(vars->val.integer);
if (ifNumber == 0) {
/* there are no interfaces! Stop here */
printf("No interfaces found");
exit(0);
}
} else {
addstr(
&out,
"(no IfNumber parameter, assuming 32 interfaces) ");
ifNumber = 32;
}
vars = vars->next_variable;
}
interfaces = (struct ifStruct *)calloc((size_t)ifNumber,
sizeof(struct ifStruct));
oldperfdata = (struct ifStruct *)calloc(
(size_t)ifNumber, sizeof(struct ifStruct));
#ifdef DEBUG
fprintf(stderr, "got %d interfaces\n", ifNumber);
#endif
} else {
/* subsequent replies have no ifNumber */
}
for (; vars; vars = vars->next_variable) {
/*
* if the next OID is shorter
* or if the next OID doesn't begin with our base OID
* then we have reached the end of the table :-)
* print_variable(vars->name, vars->name_length, vars);
*/
/* save the OID in case we need additional packets */
memcpy(lastOid.name, vars->name,
(vars->name_length * sizeof(oid)));
lastOid.name_len = vars->name_length;
if ((vars->name_length < OIDp[2].name_len) ||
(memcmp(OIDp[2].name, vars->name,
(vars->name_length - 1) * sizeof(oid)))) {
#ifdef DEBUG
fprintf(stderr, "reached end of interfaces\n");
#endif
lastifflag = true;
break;
}
/* now we fill our interfaces array with the index number and
* the description that we have received
*/
if (vars->type == ASN_OCTET_STR) {
if (config.trimdescr && config.trimdescr < vars->val_len) {
interfaces[count].index =
(int)vars->name[(vars->name_length - 1)];
MEMCPY(interfaces[count].descr,
(vars->val.string) + config.trimdescr,
vars->val_len - config.trimdescr);
TERMSTR(interfaces[count].descr,
vars->val_len - config.trimdescr);
} else {
interfaces[count].index =
(int)vars->name[(vars->name_length - 1)];
MEMCPY(interfaces[count].descr, vars->val.string,
vars->val_len);
TERMSTR(interfaces[count].descr, vars->val_len);
}
count++;
}
}
if (count < ifNumber) {
if (lastifflag) {
#ifdef DEBUG
fprintf(
stderr,
"Device says it has %d but really has %d interfaces\n",
ifNumber, count);
#endif
ifNumber = count;
} else {
#ifdef DEBUG
fprintf(stderr, "Sending another packet\n");
#endif
}
} else {
lastifflag = true;
if (count > ifNumber) {
#ifdef DEBUG
fprintf(
stderr,
"Device says it has %d but really has %d interfaces\n",
ifNumber, count);
#endif
ifNumber = count;
}
#ifdef DEBUG
fprintf(stderr, "%d interfaces found\n", ifNumber);
#endif
}
} else {
/*
* FAILURE: print what went wrong!
*/
if (status == STAT_SUCCESS)
printf("Error in packet\nReason: %s\n",
snmp_errstring(response->errstat));
else if (status == STAT_TIMEOUT) {
printf("Timeout while reading interface descriptions from %s\n",
session.peername);
exit(EXITCODE_TIMEOUT);
} else if (status == STAT_ERROR &&
ss->s_snmp_errno == SNMPERR_TIMEOUT) {
printf("Timeout\n");
exit(EXITCODE_TIMEOUT);
} else
snmp_sess_perror("snmp_bulkget", ss);
exit(2);
}
/*
* Clean up:
* free the response.
*/
if (response) {
snmp_free_pdu(response);
response = 0;
}
}
if (OIDp) {
free(OIDp);
OIDp = 0;
}
/* we should have all interface descriptions in our array */
/* If we want to match the regex with the aliases, we have to get them now.
* this allows us later to only request the interface counters of the
* desired interfaces.
*/
if (config.match_aliases_flag && config.iface_regex) {
fetch_interface_aliases(&config, oid_aliasp, ss, &session, interfaces, ifNumber);
}
/* If the get_names_flag is set, we also have to get the interface names so
* we can match the regex with them */
/* TODO: This is just a slightly changed copy from above. I think it could
* be solved better (i.e. by putting it into a function) but it works this
* way
* :-) */
if (config.get_names_flag && config.iface_regex) {
fetch_interface_names(&config, oid_namesp, ss, &session, interfaces, ifNumber);
}
if (config.iface_regex) {
/*
* a regex was given so we will go through our array
* and try and match it with what we received
*
* count is the number of matches
*/
count = 0;
for (int i = 0; i < ifNumber; i++) {
/* When --if-name is set ignore descr in favor of name, else use old
* behaviour */
if (config.get_names_flag)
status = !regexec(&config.re, interfaces[i].name, (size_t)0,
NULL, 0) ||
(config.match_aliases_flag &&
!(regexec(&config.re, interfaces[i].alias, (size_t)0,
NULL, 0)));
else
status = !regexec(&config.re, interfaces[i].descr, (size_t)0,
NULL, 0) ||
(config.match_aliases_flag &&
!(regexec(&config.re, interfaces[i].alias, (size_t)0,
NULL, 0)));
status2 = 0;
if (status && config.exclude_list) {
if (config.get_names_flag)
status2 = !regexec(&config.exclude_re, interfaces[i].name,
(size_t)0, NULL, 0) ||
(config.match_aliases_flag &&
!(regexec(&config.re, interfaces[i].alias,
(size_t)0, NULL, 0)));
else
status2 =
!regexec(&config.exclude_re, interfaces[i].descr,
(size_t)0, NULL, 0) ||
(config.match_aliases_flag &&
!(regexec(&config.exclude_re, interfaces[i].alias,
(size_t)0, NULL, 0)));
}
if (status && !status2) {
count++;
#ifdef DEBUG
fprintf(stderr, "Interface %d (%s) matched\n",
interfaces[i].index, interfaces[i].descr);
#endif
} else
interfaces[i].ignore = 1;
}
regfree(&config.re);
if (config.exclude_list)
regfree(&config.exclude_re);
if (count) {
#ifdef DEBUG
fprintf(stderr, "- %d interface%s found\n", count,
(count == 1) ? "" : "s");
#endif
} else {
printf("- no interfaces matched regex");
exit(0);
}
}
/* now retrieve the interface values in 2 GET requests
* N.B. if the interfaces are continuous we could try
* a bulk get instead
*/
for (int j = 0, k = 0; j < ifNumber; j++) {
/* add the interface to the oldperfdata list */
strcpy_nospaces(oldperfdata[j].descr, interfaces[j].descr);
if (!interfaces[j].ignore) {
/* fetch the standard values first */
if (create_request(ss, &OIDp, oid_vals, interfaces[j].index,
&response, config.sleep_usecs)) {
for (vars = response->variables; vars;
vars = vars->next_variable) {
k = -1;
/* compare the received value to the requested value */
for (int i = 0; oid_vals[i]; i++) {
if (!memcmp(OIDp[i].name, vars->name,
OIDp[i].name_len * sizeof(oid))) {
k = i;
break;
}
}
switch (k) /* the offset into oid_vals */
{
case 0: /* ifAdminStatus */
if (vars->type == ASN_INTEGER &&
*(vars->val.integer) == 2) {
/* ignore interfaces that are administratively down
*/
interfaces[j].admin_down = 1;
ignore_count++;
}
break;
case 1: /*ifOperStatus */
if (vars->type == ASN_INTEGER)
/* 1 is up(OK), 3 is testing (assume OK), 5 is
* dormant(assume OK)
*/
interfaces[j].status = (*(vars->val.integer) == 1 ||
*(vars->val.integer) == 5 ||
*(vars->val.integer) == 3)
? 1
: 0;
break;
case 2: /* ifInOctets */
if (vars->type == ASN_COUNTER)
interfaces[j].inOctets = *(vars->val.integer);
break;
case 3: /* ifInDiscards */
if (vars->type == ASN_COUNTER)
interfaces[j].inDiscards = *(vars->val.integer);
break;
case 4: /* ifInErrors or locIfInCRC */
if (vars->type == ASN_COUNTER ||
vars->type == ASN_INTEGER)
interfaces[j].inErrors = *(vars->val.integer);
break;
case 5: /* ifOutOctets */
if (vars->type == ASN_COUNTER)
interfaces[j].outOctets = *(vars->val.integer);
break;
case 6: /* ifOutDiscards */
if (vars->type == ASN_COUNTER)
interfaces[j].outDiscards = *(vars->val.integer);
break;
case 7: /* ifOutErrors or locIfCollisions */
if (vars->type == ASN_COUNTER ||
vars->type == ASN_INTEGER)
interfaces[j].outErrors = *(vars->val.integer);
break;
}
}
if (response) {
snmp_free_pdu(response);
response = 0;
}
}
/* now fetch the extended oids (64 bit counters etc.) */
if (create_request(ss, &OIDp, oid_extended, interfaces[j].index,
&response, config.sleep_usecs)) {
for (vars = response->variables; vars;
vars = vars->next_variable) {
k = -1;
/* compare the received value to the requested value */
for (int i = 0; oid_extended[i]; i++) {
if (!memcmp(OIDp[i].name, vars->name,
OIDp[i].name_len * sizeof(oid))) {
k = i;
break;
}
}
switch (k) /* the offset into oid_extended */
{
case 0: /* ifHCInOctets */
if (vars->type == ASN_COUNTER64)
interfaces[j].inOctets =
convertto64((vars->val.counter64), 0);
break;
case 1: /* ifHCOutOctets */
if (vars->type == ASN_COUNTER64)
interfaces[j].outOctets =
convertto64((vars->val.counter64), 0);
break;
case 2: /* ifInUcastPkts */
if (vars->type == ASN_COUNTER)
interfaces[j].inUcast = *(vars->val.integer);
break;
case 3: /* ifOutUcastPkts */
if (vars->type == ASN_COUNTER)
interfaces[j].outUcast = *(vars->val.integer);
break;
case 4: /* ifSpeed */
/* don't overwrite a high-speed value */
if (vars->type == ASN_GAUGE && !(interfaces[j].speed))
interfaces[j].speed = *(vars->val.integer);
break;
case 5: /* ifHighSpeed */
if (vars->type == ASN_GAUGE)
/* convert to bits / sec */
interfaces[j].speed =
((u64) * (vars->val.integer)) * 1000000ULL;
break;
case 6: /* alias */
if (vars->type == ASN_OCTET_STR)
MEMCPY(interfaces[j].alias, vars->val.string,
vars->val_len);
break;
case 7: /* name */
if (vars->type == ASN_OCTET_STR)
MEMCPY(interfaces[j].name, vars->val.string,
vars->val_len);
break;
}
}
if (response) {
snmp_free_pdu(response);
response = 0;
}
}
/* now fetch the Cisco-specific extended oids */
if (config.mode == CISCO &&
create_request(ss, &OIDp, oid_extended_cisco,
interfaces[j].index, &response,
config.sleep_usecs)) {
for (vars = response->variables; vars;
vars = vars->next_variable) {
k = -1;
/* compare the received value to the requested value */
for (int i = 0; oid_extended_cisco[i]; i++) {
if (!memcmp(OIDp[i].name, vars->name,
OIDp[i].name_len * sizeof(oid))) {
k = i;
break;
}
}
switch (k) /* the offset into oid_extended_cisco */
{
case 0: /* portAdditionalOperStatus */
if (vars->type == ASN_OCTET_STR)
interfaces[j].err_disable =
!!(vars->val.string[1] & (unsigned char)32u);
break;
}
}
if (response) {
snmp_free_pdu(response);
response = 0;
}
}
}
}
/* let the user know about interfaces that are down (and subsequently
* ignored)
*/
if (ignore_count)
addstr(&out, " - %d %s administratively down", ignore_count,
ignore_count != 1 ? "are" : "is");
if (OIDp) {
free(OIDp);
OIDp = 0;
}
/* calculate time taken, print perfdata */
gettimeofday(&tv, &tz);
returncode_t exit_code = print_output(&config, oldperfdata, starttime, interfaces, &out, if_vars, count, &tv, uptime, ifNumber);
#ifdef DEBUG
benchmark_start("Close SNMP session");
#endif
snmp_close(ss);
snmp_close(&session);
#ifdef DEBUG
benchmark_end();
#endif
SOCK_CLEANUP;
exit(exit_code);
}
returncode_t print_output(struct configuration_struct *config, struct ifStruct *oldperfdata,
long double starttime, struct ifStruct *interfaces, String *out, char **if_vars,
unsigned int number_of_matched_interfaces, struct timeval *tv, int uptime,
int ifNumber) {
unsigned int parsed_lastcheck = 0;
if (config->oldperfdatap && config->oldperfdatap[0])
parse_perfdata(config->oldperfdatap, oldperfdata, config->prefix,
&parsed_lastcheck, config->mode, ifNumber, if_vars);
if (config->lastcheck)
config->lastcheck = (starttime - config->lastcheck);
else if (parsed_lastcheck)
config->lastcheck = (starttime - parsed_lastcheck);
/* do not use old perfdata if the device has been reset recently
* Note that a switch will typically rollover the uptime counter every 497
* days which is infrequent enough to not bother about :-)
* UPTIME_TOLERANCE_IN_SECS doesn't need to be a big number
*/
if ((config->lastcheck + UPTIME_TOLERANCE_IN_SECS) > uptime)
config->lastcheck = 0;
char perfstr[MAX_STRING];
memset(perfstr, 0, sizeof(perfstr));
String perf;
perf.max = MAX_STRING;
perf.len = 0;
perf.text = perfstr;
bool errorflag = false;
bool warnflag = false;
for (int i = 0; i < ifNumber; i++) {
double inload = 0, outload = 0;
if (!interfaces[i].ignore) {
int warn = 0;
char *nameOrDescr =
config->get_names_flag && strlen(interfaces[i].name)
? interfaces[i].name
: interfaces[i].descr;
if ((!interfaces[i].status || interfaces[i].err_disable) &&
!interfaces[i].ignore && !interfaces[i].admin_down) {
if (config->crit_on_down_flag) {
addstr(&perf, "[CRITICAL] ");
errorflag = true;
/* show the alias if configured */
if (config->get_names_flag && strlen(interfaces[i].name)) {
addstr(out, ", %s", interfaces[i].name);
addstr(&perf, "%s", interfaces[i].name);
} else {
addstr(out, ", %s", interfaces[i].descr);
addstr(&perf, "%s", interfaces[i].descr);
}
if (!interfaces[i].admin_down) {
if (config->get_aliases_flag &&
strlen(interfaces[i].alias)) {
addstr(out, " (%s) down", interfaces[i].alias);
addstr(&perf, " (%s) down", interfaces[i].alias);
} else {
addstr(out, " down");
addstr(&perf, " down");
}
if (interfaces[i].err_disable) {
addstr(out, " (errdisable)");
addstr(&perf, " (errdisable)");
}
}
} else {
addstr(&perf, "[OK] ");
if (config->get_names_flag && strlen(interfaces[i].name))
addstr(&perf, "%s", interfaces[i].name);
else
addstr(&perf, "%s", interfaces[i].descr);
if (config->get_aliases_flag && strlen(interfaces[i].alias))
addstr(&perf, " (%s) down", interfaces[i].alias);
else
addstr(&perf, " down");
}
} else if (interfaces[i].admin_down && config->print_all_flag) {
addstr(&perf, "[OK] %s",
(config->get_names_flag && strlen(interfaces[i].name))
? interfaces[i].name
: interfaces[i].descr);
if (config->get_aliases_flag && strlen(interfaces[i].alias))
addstr(&perf, " (%s) is down (administrative down)",
interfaces[i].alias);
else
addstr(&perf, " is down (administrative down)");
}
/* check if errors on the interface are increasing faster than our
defined value */
else if ((oldperfdata[i].inErrors || oldperfdata[i].outErrors) &&
(interfaces[i].inErrors >
(oldperfdata[i].inErrors +
(unsigned long)config->err_tolerance) ||
interfaces[i].outErrors >
(oldperfdata[i].outErrors +
(unsigned long)config->coll_tolerance))) {
if (config->oldperfdatap && !interfaces[i].ignore) {
if (config->get_names_flag && strlen(interfaces[i].name))
addstr(&perf, "[WARNING] %s", interfaces[i].name);
else
addstr(&perf, "[WARNING] %s", interfaces[i].descr);
if (config->get_aliases_flag && strlen(interfaces[i].alias))
addstr(&perf, " (%s) has", interfaces[i].alias);
else
addstr(&perf, " has");
/* if we are not in cisco mode simply use "errors" */
if (config->mode != CISCO)
addstr(&perf, " errors\n");
else {
if (interfaces[i].inErrors >
(oldperfdata[i].inErrors +
(unsigned long)config->err_tolerance))
addstr(&perf, " %lu CRC errors since last check\n",
interfaces[i].inErrors -
oldperfdata[i].inErrors);
if (interfaces[i].outErrors >
(oldperfdata[i].outErrors +
(unsigned long)config->coll_tolerance))
addstr(&perf, " %lu collisions since last check\n",
interfaces[i].outErrors -
oldperfdata[i].outErrors);
}
if (config->get_names_flag && strlen(interfaces[i].name))
addstr(out, ", %s has %lu errors", interfaces[i].name,
(interfaces[i].inErrors +
interfaces[i].outErrors -
oldperfdata[i].inErrors -
oldperfdata[i].outErrors));
else
addstr(out, ", %s has %lu errors", interfaces[i].descr,
(interfaces[i].inErrors +
interfaces[i].outErrors -
oldperfdata[i].inErrors -
oldperfdata[i].outErrors));
warnflag = true;
// warn++; /* if you uncomment this you will get 2 rows with
// [warning]
// */
}
}
if (config->lastcheck && (interfaces[i].speed || config->speed) &&
!interfaces[i].admin_down &&
(oldperfdata[i].inOctets || oldperfdata[i].outOctets)) {
interfaces[i].inbitps =
(subtract64(interfaces[i].inOctets, oldperfdata[i].inOctets,
config->lastcheck, uptime) /
(u64)config->lastcheck) *
8ULL;
interfaces[i].outbitps =
(subtract64(interfaces[i].outOctets,
oldperfdata[i].outOctets, config->lastcheck, uptime) /
(u64)config->lastcheck) *
8ULL;
if (config->speed) {
inload = (long double)interfaces[i].inbitps /
((long double)config->speed / 100L);
outload = (long double)interfaces[i].outbitps /
((long double)config->speed / 100L);
} else {
/* use the interface speed if a speed is not given */
inload = (long double)interfaces[i].inbitps /
((long double)interfaces[i].speed / 100L);
outload = (long double)interfaces[i].outbitps /
((long double)interfaces[i].speed / 100L);
}
if ((config->bandwith > 0) && ((int)inload > config->bandwith ||
(int)outload > config->bandwith)) {
warn++;
warnflag = true;
}
}
if (interfaces[i].status && !interfaces[i].ignore) {
if (!(warn))
addstr(&perf, "[OK]");
else
addstr(&perf, "[WARNING]");
addstr(&perf, " %s", nameOrDescr);
if (config->get_aliases_flag && strlen(interfaces[i].alias))
addstr(&perf, " (%s)", interfaces[i].alias);
addstr(&perf, " is up");
}
if (config->lastcheck && (interfaces[i].speed || config->speed) &&
(interfaces[i].inbitps > 0ULL ||
interfaces[i].outbitps > 0ULL) &&
!interfaces[i].admin_down) {
char *ins, *outs;
gauge_to_si(interfaces[i].inbitps, &ins);
gauge_to_si(interfaces[i].outbitps, &outs);
addstr(&perf, " %sbps(%0.2f%%)/%sbps(%0.2f%%)", ins, inload,
outs, outload);
free(ins);
free(outs);
}
if (perf.len > 0u && perf.text[(perf.len - 1u)] != '\n') {
addstr(&perf, "\n");
}
}
}
if (errorflag)
printf("CRITICAL:");
else if (warnflag)
printf("WARNING:");
else
printf("OK:");
printf(" %d interface%s found", ifNumber, (ifNumber == 1) ? "" : "s");
if (config->iface_regex)
printf(", of which %d matched the regex", number_of_matched_interfaces);
/* now print performance data */
printf("%*s | interfaces::check_multi::plugins=%d time=%.2Lf checktime=%ld",
(int)out->len, out->text, number_of_matched_interfaces,
(((long double)tv->tv_sec + ((long double)tv->tv_usec / 1000000)) -
starttime),
tv->tv_sec);
if (uptime)
printf(" %sdevice::check_snmp::uptime=%us",
config->prefix ? config->prefix : "", uptime);
for (int i = 0; i < ifNumber; i++) {
if (!interfaces[i].ignore &&
(!interfaces[i].admin_down || config->print_all_flag)) {
printf(" %s%s::check_snmp::", config->prefix ? config->prefix : "",
oldperfdata[i].descr);
printf("%s=%lluc %s=%lluc", if_vars[0], interfaces[i].inOctets,
if_vars[1], interfaces[i].outOctets);
printf(" %s=%luc %s=%luc", if_vars[2], interfaces[i].inDiscards,
if_vars[3], interfaces[i].outDiscards);
printf(" %s=%luc %s=%luc", if_vars[4], interfaces[i].inErrors,
if_vars[5], interfaces[i].outErrors);
printf(" %s=%luc %s=%luc", if_vars[6], interfaces[i].inUcast,
if_vars[7], interfaces[i].outUcast);
if (config->speed)
printf(" %s=%llu", if_vars[8], config->speed);
else
printf(" %s=%llu", if_vars[8], interfaces[i].speed);
printf(" %s=%llub %s=%llub", if_vars[9], interfaces[i].inbitps,
if_vars[10], interfaces[i].outbitps);
}
}
printf("\n%*s", (int)perf.len, perf.text);
if (errorflag) {
return CRITICAL;
} else if (warnflag) {
return WARNING;
} else {