diff --git a/.clang-format b/.clang-format index 8b33e54..96a4695 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,12 @@ -UseTab: true -TabWidth: 4 +BasedOnStyle: LLVM +UseTab: ForContinuationAndIndentation IndentWidth: 4 +TabWidth: 4 +AllowShortIfStatementsOnASingleLine: false +BreakBeforeBraces: Attach +AlignConsecutiveMacros: true +ColumnLimit: 140 +IndentPPDirectives: AfterHash +SortIncludes: Never +InsertBraces: true +AllowShortEnumsOnASingleLine: false diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..2fb69ed --- /dev/null +++ b/.clangd @@ -0,0 +1,10 @@ +CompileFlags: + # Treat code as C++, use C++17 standard, enable more warnings. + Add: [-xc, -std=c17, -Wall, -Wextra, -Wno-missing-prototypes, -Wpedantic] +Diagnostics: + # Tweak Clang-Tidy checks. + ClangTidy: + Add: [performance*, modernize*, readability*] + CheckOptions: + readability-function-cognitive-complexity.DescribeBasicIncrements: false + readability-function-cognitive-complexity.Threshold: 50 diff --git a/check_interfaces.c b/check_interfaces.c index 43613de..eafa113 100644 --- a/check_interfaces.c +++ b/check_interfaces.c @@ -1,9 +1,6 @@ - - // This one is needed on FreeBSD and it has to be before the others or at least some of them #include - #include "snmp_bulkget.h" #include "utils.h" #include @@ -18,55 +15,44 @@ * 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_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"}; +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_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_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_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 */ +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.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 */ + ".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_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_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_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_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 */ +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 */ @@ -75,13 +61,13 @@ char *oid_vals_default[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */ ".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 */ +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.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}; @@ -106,23 +92,25 @@ enum returncode { 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); +void parse_and_check_commandline(int argc, char **argv, struct configuration_struct *config); +bool fetch_interface_aliases(struct configuration_struct * /*config*/, char ** /*oid_aliasp*/, netsnmp_session *snmp_session, + netsnmp_session *session, struct ifStruct *interfaces, int ifNumber); +bool fetch_interface_names(struct configuration_struct * /*config*/, char **oid_namesp, netsnmp_session *snmp_session, + 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 *time_value, int uptime, int ifNumber); void print_version(void); int main(int argc, char *argv[]) { - netsnmp_session session, *ss; + netsnmp_session session; + netsnmp_session *snmp_session; netsnmp_pdu *pdu; netsnmp_pdu *response; netsnmp_variable_list *vars; - int status, status2; + int status; + int status2; int count = 0; /* used for: the number of interfaces we receive, the number of regex matches */ size_t size; @@ -132,7 +120,7 @@ int main(int argc, char *argv[]) { int ifNumber = 0; - config_t config = { + config_t config = { .crit_on_down_flag = true, .get_aliases_flag = false, .match_aliases_flag = false, @@ -163,7 +151,7 @@ int main(int argc, char *argv[]) { .mode = DEFAULT, }; - struct ifStruct *interfaces = NULL; /* current interface data */ + struct ifStruct *interfaces = NULL; /* current interface data */ struct ifStruct *oldperfdata = NULL; /* previous check interface data */ struct OIDStruct *OIDp; @@ -191,36 +179,33 @@ int main(int argc, char *argv[]) { 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); + struct timeval time_value; + struct timezone time_zone; + gettimeofday(&time_value, &time_zone); + starttime = (long double)time_value.tv_sec + (((long double)time_value.tv_usec) / 1000000); // +1 for the `:` between hostname and port size_t peername_max_len = strlen(config.hostname) + strlen(config.port) + 1; - char *peername = calloc(1, peername_max_len+1); + char *peername = calloc(1, peername_max_len + 1); if (peername == NULL) { printf("Failed to allocate memory at %d in %s\n", __LINE__, __FUNCTION__); exit(3); } - strlcpy(peername, config.hostname, peername_max_len+1); - strlcat(peername, ":", peername_max_len+1); - strlcat(peername, config.port, peername_max_len+1); + strlcpy(peername, config.hostname, peername_max_len + 1); + strlcat(peername, ":", peername_max_len + 1); + strlcat(peername, config.port, peername_max_len + 1); #ifdef DEBUG benchmark_start("Start SNMP session"); #endif - if (config.user) + if (config.user) { /* use snmpv3 */ - ss = start_session_v3(&session, config.user, config.auth_proto, - config.auth_pass, config.priv_proto, - config.priv_pass, peername, - config.global_timeout, config.session_retries); - else - ss = start_session(&session, config.community, peername, - config.mode, config.global_timeout, - config.session_retries); + snmp_session = start_session_v3(&session, config.user, config.auth_proto, config.auth_pass, config.priv_proto, config.priv_pass, + peername, config.global_timeout, config.session_retries); + } else { + snmp_session = start_session(&session, config.community, peername, config.mode, config.global_timeout, config.session_retries); + } #ifdef DEBUG benchmark_end(); #endif @@ -261,16 +246,15 @@ int main(int argc, char *argv[]) { 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 { + 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) + if (config.mode == BINTEC || config.mode == NONBULK) { pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); - else { + } else { pdu = snmp_pdu_create(SNMP_MSG_GETBULK); pdu->non_repeaters = 0; pdu->max_repetitions = config.pdu_max_repetitions; @@ -283,13 +267,14 @@ int main(int argc, char *argv[]) { benchmark_start("Send SNMP request for OIDs: %s", implode_result); #endif /* send the request */ - status = snmp_synch_response(ss, pdu, &response); + status = snmp_synch_response(snmp_session, pdu, &response); #ifdef DEBUG benchmark_end(); free(implode_result); #endif - if (config.sleep_usecs) + if (config.sleep_usecs) { usleep(config.sleep_usecs); + } if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { @@ -300,14 +285,13 @@ int main(int argc, char *argv[]) { /* on some devices the ifNumber is not available... */ while (!ifNumber) { - if (!(memcmp(OIDp[0].name, vars->name, - OIDp[0].name_len * sizeof(oid)))) { + if (!(memcmp(OIDp[0].name, vars->name, OIDp[0].name_len * sizeof(oid)))) { /* uptime */ - if (vars->type == ASN_TIMETICKS) + 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))) { + } + } 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) { @@ -316,9 +300,7 @@ int main(int argc, char *argv[]) { exit(0); } } else { - addstr( - &out, - "(no IfNumber parameter, assuming 32 interfaces) "); + addstr(&out, "(no IfNumber parameter, assuming 32 interfaces) "); ifNumber = 32; } @@ -329,7 +311,7 @@ int main(int argc, char *argv[]) { int real_count = 0; for (netsnmp_variable_list *runner = response->variables; runner; runner = runner->next_variable) { if (vars->type == ASN_OCTET_STR) { - real_count ++; + real_count++; } } @@ -337,10 +319,8 @@ int main(int argc, char *argv[]) { ifNumber = real_count; } - interfaces = (struct ifStruct *)calloc((size_t)ifNumber, - sizeof(struct ifStruct)); - oldperfdata = (struct ifStruct *)calloc( - (size_t)ifNumber, sizeof(struct ifStruct)); + 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); @@ -358,13 +338,10 @@ int main(int argc, char *argv[]) { */ /* save the OID in case we need additional packets */ - memcpy(lastOid.name, vars->name, - (vars->name_length * sizeof(oid))); + 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)))) { + 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 @@ -377,21 +354,15 @@ int main(int argc, char *argv[]) { */ if (vars->type == ASN_OCTET_STR) { if (config.trimdescr && config.trimdescr < vars->val_len) { - interfaces[count].index = - vars->name[(vars->name_length - 1)]; + interfaces[count].index = vars->name[(vars->name_length - 1)]; - MEMCPY(interfaces[count].descr, - (vars->val.string) + config.trimdescr, - vars->val_len - config.trimdescr); + MEMCPY(interfaces[count].descr, (vars->val.string) + config.trimdescr, vars->val_len - config.trimdescr); - TERMSTR(interfaces[count].descr, - vars->val_len - config.trimdescr); + TERMSTR(interfaces[count].descr, vars->val_len - config.trimdescr); } else { - interfaces[count].index = - vars->name[(vars->name_length - 1)]; + interfaces[count].index = vars->name[(vars->name_length - 1)]; - MEMCPY(interfaces[count].descr, vars->val.string, - vars->val_len); + MEMCPY(interfaces[count].descr, vars->val.string, vars->val_len); TERMSTR(interfaces[count].descr, vars->val_len); } count++; @@ -401,10 +372,7 @@ int main(int argc, char *argv[]) { if (count < ifNumber) { if (lastifflag) { #ifdef DEBUG - fprintf( - stderr, - "Device says it has %d but really has %d interfaces\n", - ifNumber, count); + fprintf(stderr, "Device says it has %d but really has %d interfaces\n", ifNumber, count); #endif ifNumber = count; } else { @@ -416,10 +384,7 @@ int main(int argc, char *argv[]) { lastifflag = true; if (count > ifNumber) { #ifdef DEBUG - fprintf( - stderr, - "Device says it has %d but really has %d interfaces\n", - ifNumber, count); + fprintf(stderr, "Device says it has %d but really has %d interfaces\n", ifNumber, count); #endif ifNumber = count; } @@ -433,19 +398,17 @@ int main(int argc, char *argv[]) { * 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); + 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) { + } else if (status == STAT_ERROR && snmp_session->s_snmp_errno == SNMPERR_TIMEOUT) { printf("Timeout\n"); exit(EXITCODE_TIMEOUT); - } else - snmp_sess_perror("snmp_bulkget", ss); + } else { + snmp_sess_perror("snmp_bulkget", snmp_session); + } exit(2); } /* @@ -471,7 +434,7 @@ int main(int argc, char *argv[]) { */ if (config.match_aliases_flag && config.iface_regex) { - fetch_interface_aliases(&config, oid_aliasp, ss, &session, interfaces, ifNumber); + fetch_interface_aliases(&config, oid_aliasp, snmp_session, &session, interfaces, ifNumber); } /* If the get_names_flag is set, we also have to get the interface names so @@ -482,7 +445,7 @@ int main(int argc, char *argv[]) { * way * :-) */ if (config.get_names_flag && config.iface_regex) { - fetch_interface_names(&config, oid_namesp, ss, &session, interfaces, ifNumber); + fetch_interface_names(&config, oid_namesp, snmp_session, &session, interfaces, ifNumber); } if (config.iface_regex) { @@ -497,52 +460,41 @@ int main(int argc, char *argv[]) { 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))); + 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 (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); + fprintf(stderr, "Interface %d (%s) matched\n", interfaces[i].index, interfaces[i].descr); #endif - } else + } else { interfaces[i].ignore = 1; + } } regfree(&config.re); - if (config.exclude_list) + if (config.exclude_list) { regfree(&config.exclude_re); + } if (count) { #ifdef DEBUG - fprintf(stderr, "- %d interface%s found\n", count, - (count == 1) ? "" : "s"); + fprintf(stderr, "- %d interface%s found\n", count, (count == 1) ? "" : "s"); #endif } else { printf("- no interfaces matched regex"); @@ -561,15 +513,12 @@ int main(int argc, char *argv[]) { 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) { + if (create_request(snmp_session, &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))) { + if (!memcmp(OIDp[i].name, vars->name, OIDp[i].name_len * sizeof(oid))) { k = i; break; } @@ -578,8 +527,7 @@ int main(int argc, char *argv[]) { switch (k) /* the offset into oid_vals */ { case 0: /* ifAdminStatus */ - if (vars->type == ASN_INTEGER && - *(vars->val.integer) == 2) { + if (vars->type == ASN_INTEGER && *(vars->val.integer) == 2) { /* ignore interfaces that are administratively down */ interfaces[j].admin_down = 1; @@ -587,41 +535,43 @@ int main(int argc, char *argv[]) { } break; case 1: /*ifOperStatus */ - if (vars->type == ASN_INTEGER) + 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; + 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) + if (vars->type == ASN_COUNTER) { interfaces[j].inOctets = *(vars->val.integer); + } break; case 3: /* ifInDiscards */ - if (vars->type == ASN_COUNTER) + 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) + if (vars->type == ASN_COUNTER || vars->type == ASN_INTEGER) { interfaces[j].inErrors = *(vars->val.integer); + } break; case 5: /* ifOutOctets */ - if (vars->type == ASN_COUNTER) + if (vars->type == ASN_COUNTER) { interfaces[j].outOctets = *(vars->val.integer); + } break; case 6: /* ifOutDiscards */ - if (vars->type == ASN_COUNTER) + 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) + if (vars->type == ASN_COUNTER || vars->type == ASN_INTEGER) { interfaces[j].outErrors = *(vars->val.integer); + } break; } } @@ -632,15 +582,12 @@ int main(int argc, char *argv[]) { } /* 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) { + if (create_request(snmp_session, &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))) { + if (!memcmp(OIDp[i].name, vars->name, OIDp[i].name_len * sizeof(oid))) { k = i; break; } @@ -649,43 +596,46 @@ int main(int argc, char *argv[]) { switch (k) /* the offset into oid_extended */ { case 0: /* ifHCInOctets */ - if (vars->type == ASN_COUNTER64) - interfaces[j].inOctets = - convertto64((vars->val.counter64), 0); + 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); + if (vars->type == ASN_COUNTER64) { + interfaces[j].outOctets = convertto64((vars->val.counter64), 0); + } break; case 2: /* ifInUcastPkts */ - if (vars->type == ASN_COUNTER) + if (vars->type == ASN_COUNTER) { interfaces[j].inUcast = *(vars->val.integer); + } break; case 3: /* ifOutUcastPkts */ - if (vars->type == ASN_COUNTER) + 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)) + if (vars->type == ASN_GAUGE && !(interfaces[j].speed)) { interfaces[j].speed = *(vars->val.integer); + } break; case 5: /* ifHighSpeed */ - if (vars->type == ASN_GAUGE) + if (vars->type == ASN_GAUGE) { /* convert to bits / sec */ - interfaces[j].speed = - ((u64) * (vars->val.integer)) * 1000000ULL; + 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); + 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); + if (vars->type == ASN_OCTET_STR) { + MEMCPY(interfaces[j].name, vars->val.string, vars->val_len); + } break; } } @@ -697,16 +647,12 @@ int main(int argc, char *argv[]) { /* 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) { + create_request(snmp_session, &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))) { + if (!memcmp(OIDp[i].name, vars->name, OIDp[i].name_len * sizeof(oid))) { k = i; break; } @@ -715,9 +661,9 @@ int main(int argc, char *argv[]) { 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); + if (vars->type == ASN_OCTET_STR) { + interfaces[j].err_disable = !!(vars->val.string[1] & (unsigned char)32U); + } break; } } @@ -732,9 +678,9 @@ int main(int argc, char *argv[]) { /* 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 (ignore_count) { + addstr(&out, " - %d %s administratively down", ignore_count, ignore_count != 1 ? "are" : "is"); + } if (OIDp) { free(OIDp); @@ -743,15 +689,14 @@ int main(int argc, char *argv[]) { /* 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); + gettimeofday(&time_value, &time_zone); + returncode_t exit_code = print_output(&config, oldperfdata, starttime, interfaces, &out, if_vars, count, &time_value, uptime, ifNumber); #ifdef DEBUG benchmark_start("Close SNMP session"); #endif - snmp_close(ss); + snmp_close(snmp_session); snmp_close(&session); #ifdef DEBUG benchmark_end(); @@ -761,29 +706,30 @@ int main(int argc, char *argv[]) { 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) { +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 *time_value, int uptime, int ifNumber) { unsigned int parsed_lastcheck = 0; - if (config->oldperfdatap && config->oldperfdatap[0]) - parse_perfdata(config->oldperfdatap, oldperfdata, config->prefix, - &parsed_lastcheck, ifNumber, if_vars); + if (config->oldperfdatap && config->oldperfdatap[0]) { + parse_perfdata(config->oldperfdatap, oldperfdata, config->prefix, &parsed_lastcheck, ifNumber, if_vars); + } - if (config->lastcheck) + if (config->lastcheck) { config->lastcheck = (starttime - config->lastcheck); - else if (parsed_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) + if ((config->lastcheck + UPTIME_TOLERANCE_IN_SECS) > uptime) { config->lastcheck = 0; + } char perfstr[MAX_STRING]; memset(perfstr, 0, sizeof(perfstr)); @@ -796,17 +742,14 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * bool warnflag = false; for (int i = 0; i < ifNumber; i++) { - double inload = 0, outload = 0; + double inload = 0; + double 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; + 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 ((!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; @@ -819,8 +762,7 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * addstr(&perf, "%s", interfaces[i].descr); } if (!interfaces[i].admin_down) { - if (config->get_aliases_flag && - strlen(interfaces[i].alias)) { + if (config->get_aliases_flag && strlen(interfaces[i].alias)) { addstr(out, " (%s) down", interfaces[i].alias); addstr(&perf, " (%s) down", interfaces[i].alias); } else { @@ -834,77 +776,63 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * } } else { addstr(&perf, "[OK] "); - if (config->get_names_flag && strlen(interfaces[i].name)) + if (config->get_names_flag && strlen(interfaces[i].name)) { addstr(&perf, "%s", interfaces[i].name); - else + } else { addstr(&perf, "%s", interfaces[i].descr); - if (config->get_aliases_flag && strlen(interfaces[i].alias)) + } + if (config->get_aliases_flag && strlen(interfaces[i].alias)) { addstr(&perf, " (%s) down", interfaces[i].alias); - else + } 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, "[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))) { + (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)) + if (config->get_names_flag && strlen(interfaces[i].name)) { addstr(&perf, "[WARNING] %s", interfaces[i].name); - else + } else { addstr(&perf, "[WARNING] %s", interfaces[i].descr); + } - if (config->get_aliases_flag && strlen(interfaces[i].alias)) + if (config->get_aliases_flag && strlen(interfaces[i].alias)) { addstr(&perf, " (%s) has", interfaces[i].alias); - else + } else { addstr(&perf, " has"); + } /* if we are not in cisco mode simply use "errors" */ - if (config->mode != CISCO) + 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); + } 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)) + 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 + (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)); + (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] @@ -912,128 +840,110 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * } } - if (config->lastcheck && (interfaces[i].speed || config->speed) && - !interfaces[i].admin_down && + 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) * + (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) * + (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); + 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); + 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)) { + if ((config->bandwith > 0) && ((int)inload > config->bandwith || (int)outload > config->bandwith)) { warn++; warnflag = true; } } if (interfaces[i].status && !interfaces[i].ignore) { - if (!(warn)) + if (!(warn)) { addstr(&perf, "[OK]"); - else + } else { addstr(&perf, "[WARNING]"); + } addstr(&perf, " %s", nameOrDescr); - if (config->get_aliases_flag && strlen(interfaces[i].alias)) + 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; + (interfaces[i].inbitps > 0ULL || interfaces[i].outbitps > 0ULL) && !interfaces[i].admin_down) { + char *ins; + char *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); + 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') { + if (perf.len > 0U && perf.text[(perf.len - 1U)] != '\n') { addstr(&perf, "\n"); } } } - if (errorflag) + if (errorflag) { printf("CRITICAL:"); - else if (warnflag) + } else if (warnflag) { printf("WARNING:"); - else + } else { printf("OK:"); + } printf(" %d interface%s found", ifNumber, (ifNumber == 1) ? "" : "s"); - if (config->iface_regex) + 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); + printf("%*s | interfaces::check_multi::plugins=%d time=%.2Lf checktime=%ld", (int)out->len, out->text, number_of_matched_interfaces, + (((long double)time_value->tv_sec + ((long double)time_value->tv_usec / 1000000)) - starttime), time_value->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) + 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 + } 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(" %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) { + } + if (warnflag) { return WARNING; - } else { - return OK; } + return OK; } -bool fetch_interface_aliases(struct configuration_struct* config, char **oid_aliasp, - netsnmp_session *ss, netsnmp_session *session, struct ifStruct *interfaces, - int ifNumber) { +bool fetch_interface_aliases(struct configuration_struct *config, char **oid_aliasp, netsnmp_session *snmp_session, + netsnmp_session *session, struct ifStruct *interfaces, int ifNumber) { bool lastifflag = false; int count = 0; netsnmp_pdu *pdu; @@ -1045,15 +955,15 @@ bool fetch_interface_aliases(struct configuration_struct* config, char **oid_ali while (lastifflag == false) { /* build our request depending on the mode */ - if (count == 0) + if (count == 0) { create_pdu(config->mode, oid_aliasp, &pdu, &OIDp, 0, ifNumber); - else { + } else { /* we have not received all aliases in the preceding packet, so * fetch the next lot */ - if (config->mode == BINTEC || config->mode == NONBULK) + if (config->mode == BINTEC || config->mode == NONBULK) { pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); - else { + } else { pdu = snmp_pdu_create(SNMP_MSG_GETBULK); pdu->non_repeaters = 0; pdu->max_repetitions = ifNumber - count; @@ -1067,16 +977,16 @@ bool fetch_interface_aliases(struct configuration_struct* config, char **oid_ali #endif /* send the request */ int status; - status = snmp_synch_response(ss, pdu, &response); + status = snmp_synch_response(snmp_session, pdu, &response); #ifdef DEBUG benchmark_end(); free(implode_result); #endif - if (config->sleep_usecs) + if (config->sleep_usecs) { usleep(config->sleep_usecs); + } - if (status == STAT_SUCCESS && - response->errstat == SNMP_ERR_NOERROR) { + if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { netsnmp_variable_list *vars; vars = response->variables; @@ -1090,13 +1000,10 @@ bool fetch_interface_aliases(struct configuration_struct* config, char **oid_ali */ /* save the OID in case we need additional packets */ - memcpy(lastOid.name, vars->name, - (vars->name_length * sizeof(oid))); + memcpy(lastOid.name, vars->name, (vars->name_length * sizeof(oid))); lastOid.name_len = vars->name_length; - if ((vars->name_length < OIDp[0].name_len) || - (memcmp(OIDp[0].name, vars->name, - (vars->name_length - 1) * sizeof(oid)))) { + if ((vars->name_length < OIDp[0].name_len) || (memcmp(OIDp[0].name, vars->name, (vars->name_length - 1) * sizeof(oid)))) { #ifdef DEBUG fprintf(stderr, "reached end of aliases\n"); #endif @@ -1105,12 +1012,11 @@ bool fetch_interface_aliases(struct configuration_struct* config, char **oid_ali } /* now we fill our interfaces array with the alias - */ + */ if (vars->type == ASN_OCTET_STR) { int i = (int)vars->name[(vars->name_length - 1)]; if (i) { - MEMCPY(interfaces[count].alias, vars->val.string, - vars->val_len); + MEMCPY(interfaces[count].alias, vars->val.string, vars->val_len); TERMSTR(interfaces[count].alias, vars->val_len); } } @@ -1130,22 +1036,22 @@ bool fetch_interface_aliases(struct configuration_struct* config, char **oid_ali fprintf(stderr, "Sending another packet for aliases\n"); #endif } - } else + } else { lastifflag = true; + } } 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 aliases from %s\n", - (*session).peername); + 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 aliases from %s\n", (*session).peername); exit(EXITCODE_TIMEOUT); - } else - snmp_sess_perror("snmp_bulkget", ss); + } else { + snmp_sess_perror("snmp_bulkget", snmp_session); + } exit(2); } /* @@ -1160,7 +1066,8 @@ bool fetch_interface_aliases(struct configuration_struct* config, char **oid_ali return true; } -bool fetch_interface_names(struct configuration_struct* config, char **oid_namesp, netsnmp_session *ss, netsnmp_session *session, struct ifStruct *interfaces, int ifNumber) { +bool fetch_interface_names(struct configuration_struct *config, char **oid_namesp, netsnmp_session *snmp_session, netsnmp_session *session, + struct ifStruct *interfaces, int ifNumber) { bool lastifflag = false; netsnmp_pdu *pdu; struct OIDStruct lastOid; @@ -1174,15 +1081,15 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names while (lastifflag == false) { /* build our request depending on the mode */ - if (count == 0) + if (count == 0) { create_pdu(config->mode, oid_namesp, &pdu, &OIDp, 0, ifNumber); - else { + } else { /* we have not received all names in the preceding packet, so * fetch the next lot */ - if (config->mode == BINTEC || config->mode == NONBULK) + if (config->mode == BINTEC || config->mode == NONBULK) { pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); - else { + } else { pdu = snmp_pdu_create(SNMP_MSG_GETBULK); pdu->non_repeaters = 0; pdu->max_repetitions = ifNumber - count; @@ -1195,16 +1102,16 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names benchmark_start("Send SNMP request for OIDs: %s", implode_result); #endif /* send the request */ - int status = snmp_synch_response(ss, pdu, &response); + int status = snmp_synch_response(snmp_session, pdu, &response); #ifdef DEBUG benchmark_end(); free(implode_result); #endif - if (config->sleep_usecs) + if (config->sleep_usecs) { usleep(config->sleep_usecs); + } - if (status == STAT_SUCCESS && - response->errstat == SNMP_ERR_NOERROR) { + if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { netsnmp_variable_list *vars = response->variables; @@ -1217,13 +1124,10 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names */ /* save the OID in case we need additional packets */ - memcpy(lastOid.name, vars->name, - (vars->name_length * sizeof(oid))); + memcpy(lastOid.name, vars->name, (vars->name_length * sizeof(oid))); lastOid.name_len = vars->name_length; - if ((vars->name_length < OIDp[0].name_len) || - (memcmp(OIDp[0].name, vars->name, - (vars->name_length - 1) * sizeof(oid)))) { + if ((vars->name_length < OIDp[0].name_len) || (memcmp(OIDp[0].name, vars->name, (vars->name_length - 1) * sizeof(oid)))) { #ifdef DEBUG fprintf(stderr, "reached end of names\n"); #endif @@ -1232,12 +1136,11 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names } /* now we fill our interfaces array with the names - */ + */ if (vars->type == ASN_OCTET_STR) { int i = (int)vars->name[(vars->name_length - 1)]; if (i) { - MEMCPY(interfaces[count].name, vars->val.string, - vars->val_len); + MEMCPY(interfaces[count].name, vars->val.string, vars->val_len); TERMSTR(interfaces[count].name, vars->val_len); } } @@ -1247,30 +1150,27 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names if (count < ifNumber) { #ifdef DEBUG if (lastifflag) { - fprintf( - stderr, - "Device has %d interfaces but only has %d names\n", - ifNumber, count); + fprintf(stderr, "Device has %d interfaces but only has %d names\n", ifNumber, count); } else { fprintf(stderr, "Sending another packet for names\n"); } #endif - } else + } else { lastifflag = true; + } } 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 names from %s\n", - (*session).peername); + 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 names from %s\n", (*session).peername); exit(EXITCODE_TIMEOUT); - } else - snmp_sess_perror("snmp_bulkget", ss); + } else { + snmp_sess_perror("snmp_bulkget", snmp_session); + } exit(2); } /* @@ -1286,62 +1186,61 @@ bool fetch_interface_names(struct configuration_struct* config, char **oid_names return true; } -enum { PORT_OPTION = CHAR_MAX + 1, VERSION_OPTION = CHAR_MAX + 2 }; +enum { + PORT_OPTION = CHAR_MAX + 1, + VERSION_OPTION = CHAR_MAX + 2 +}; -void parse_and_check_commandline(int argc, char **argv, - struct configuration_struct *config) { +void parse_and_check_commandline(int argc, char **argv, struct configuration_struct *config) { int opt; char *progname = strrchr(argv[0], '/'); - if (*progname && *(progname + 1)) + if (*progname && *(progname + 1)) { progname++; - else + } else { progname = "check_interfaces"; + } /* parse options */ - static struct option longopts[] = { - {"aliases", no_argument, NULL, 'a'}, - {"match-aliases", no_argument, NULL, 'A'}, - {"bandwidth", required_argument, NULL, 'b'}, - {"community", required_argument, NULL, 'c'}, - {"down-is-ok", no_argument, NULL, 'd'}, - {"errors", required_argument, NULL, 'e'}, - {"out-errors", required_argument, NULL, 'f'}, - {"hostname", required_argument, NULL, 'h'}, - {"port", required_argument, NULL, PORT_OPTION}, - {"auth-proto", required_argument, NULL, 'j'}, - {"auth-phrase", required_argument, NULL, 'J'}, - {"priv-proto", required_argument, NULL, 'k'}, - {"priv-phrase", required_argument, NULL, 'K'}, - {"mode", required_argument, NULL, 'm'}, - {"perfdata", required_argument, NULL, 'p'}, - {"prefix", required_argument, NULL, 'P'}, - {"regex", required_argument, NULL, 'r'}, - {"exclude-regex", required_argument, NULL, 'R'}, - {"if-names", no_argument, NULL, 'N'}, - {"debug-print", no_argument, NULL, 'D'}, - {"speed", required_argument, NULL, 's'}, - {"lastcheck", required_argument, NULL, 't'}, - {"user", required_argument, NULL, 'u'}, - {"trim", required_argument, NULL, 'x'}, - {"help", no_argument, NULL, '?'}, - {"timeout", required_argument, NULL, 2}, - {"sleep", required_argument, NULL, 3}, - {"retries", required_argument, NULL, 4}, - {"max-repetitions", required_argument, NULL, 5}, - {"version", no_argument, NULL, VERSION_OPTION}, - {NULL, 0, NULL, 0}}; - - while ((opt = getopt_long(argc, argv, - "aAb:c:dDe:f:h:j:J:k:K:m:Np:P:r:R:s:t:u:x:?", - longopts, NULL)) != -1) { + static struct option longopts[] = {{"aliases", no_argument, NULL, 'a'}, + {"match-aliases", no_argument, NULL, 'A'}, + {"bandwidth", required_argument, NULL, 'b'}, + {"community", required_argument, NULL, 'c'}, + {"down-is-ok", no_argument, NULL, 'd'}, + {"errors", required_argument, NULL, 'e'}, + {"out-errors", required_argument, NULL, 'f'}, + {"hostname", required_argument, NULL, 'h'}, + {"port", required_argument, NULL, PORT_OPTION}, + {"auth-proto", required_argument, NULL, 'j'}, + {"auth-phrase", required_argument, NULL, 'J'}, + {"priv-proto", required_argument, NULL, 'k'}, + {"priv-phrase", required_argument, NULL, 'K'}, + {"mode", required_argument, NULL, 'm'}, + {"perfdata", required_argument, NULL, 'p'}, + {"prefix", required_argument, NULL, 'P'}, + {"regex", required_argument, NULL, 'r'}, + {"exclude-regex", required_argument, NULL, 'R'}, + {"if-names", no_argument, NULL, 'N'}, + {"debug-print", no_argument, NULL, 'D'}, + {"speed", required_argument, NULL, 's'}, + {"lastcheck", required_argument, NULL, 't'}, + {"user", required_argument, NULL, 'u'}, + {"trim", required_argument, NULL, 'x'}, + {"help", no_argument, NULL, '?'}, + {"timeout", required_argument, NULL, 2}, + {"sleep", required_argument, NULL, 3}, + {"retries", required_argument, NULL, 4}, + {"max-repetitions", required_argument, NULL, 5}, + {"version", no_argument, NULL, VERSION_OPTION}, + {NULL, 0, NULL, 0}}; + + while ((opt = getopt_long(argc, argv, "aAb:c:dDe:f:h:j:J:k:K:m:Np:P:r:R:s:t:u:x:?", longopts, NULL)) != -1) { switch (opt) { case 'a': config->get_aliases_flag = true; break; case 'A': - config->get_aliases_flag = - true; /* we need to see what we have matched... */ + config->get_aliases_flag = true; /* we need to see what we have matched... */ config->match_aliases_flag = true; break; case 'b': @@ -1461,28 +1360,28 @@ void parse_and_check_commandline(int argc, char **argv, freeaddrinfo(addr_list); #endif /* HAVE_GETADDRINFO */ - if (!config->community) + if (!config->community) { config->community = DEFAULT_COMMUNITY; + } - if (config->exclude_list && !config->iface_regex) + if (config->exclude_list && !config->iface_regex) { /* use .* as the default regex */ config->iface_regex = ".*"; + } /* get the start time */ /* parse the interfaces regex */ int status; if (config->iface_regex) { - status = regcomp(&config->re, config->iface_regex, - REG_ICASE | REG_EXTENDED | REG_NOSUB); + status = regcomp(&config->re, config->iface_regex, REG_ICASE | REG_EXTENDED | REG_NOSUB); if (status != 0) { printf("Error creating regex\n"); exit(3); } if (config->exclude_list) { - status = regcomp(&config->exclude_re, config->exclude_list, - REG_ICASE | REG_EXTENDED | REG_NOSUB); + status = regcomp(&config->exclude_re, config->exclude_list, REG_ICASE | REG_EXTENDED | REG_NOSUB); if (status != 0) { printf("Error creating exclusion regex\n"); exit(3); @@ -1491,8 +1390,9 @@ void parse_and_check_commandline(int argc, char **argv, } /* set the MIB variable if it is unset to avoid net-snmp warnings */ - if (getenv("MIBS") == NULL) + if (getenv("MIBS") == NULL) { setenv("MIBS", "", 1); + } } void print_version(void) { @@ -1503,7 +1403,6 @@ void print_version(void) { } int usage(char *progname) { - int i; printf( #ifdef PACKAGE_STRING PACKAGE_STRING "\n\n" @@ -1527,16 +1426,15 @@ int usage(char *progname) { printf(" -x|--trim\t\tcut this number of characters from the start of " "interface descriptions\n"); printf(" -m|--mode\t\tspecial operating mode ("); - for (i = 0; modes[i]; i++) { + for (int i = 0; modes[i]; i++) { printf("%s%s", i ? "," : "", modes[i]); } printf(")\n"); printf(" -j|--auth-proto\tSNMPv3 Auth Protocol (SHA|SHA-224|SHA-256|SHA-384|SHA-512|MD5)\n"); printf(" -J|--auth-phrase\tSNMPv3 Auth Phrase\n"); #ifdef HAVE_USM_DES_PRIV_PROTOCOL - printf( - " -k|--priv-proto\tSNMPv3 Privacy Protocol (AES|DES), unset means not " - "privacy protocol!\n"); + printf(" -k|--priv-proto\tSNMPv3 Privacy Protocol (AES|DES), unset means not " + "privacy protocol!\n"); #else printf(" -k|--priv-proto\tSNMPv3 Privacy Protocol (AES), unset means not " "privacy protocol!\n"); @@ -1547,8 +1445,7 @@ int usage(char *progname) { printf(" -a|--aliases\t\tretrieves the interface description\n"); printf(" -A|--match-aliases\talso match against aliases (Option -a " "automatically enabled)\n"); - printf( - " -D|--debug-print\tlist administrative down interfaces in perfdata\n"); + printf(" -D|--debug-print\tlist administrative down interfaces in perfdata\n"); printf(" -N|--if-names\t\tuse ifName instead of ifDescr\n"); printf(" --timeout\t\tsets the SNMP timeout (in ms)\n"); printf(" --sleep\t\tsleep between every SNMP query (in ms)\n"); diff --git a/snmp_bulkget.c b/snmp_bulkget.c index f8b52ac..377ccf8 100644 --- a/snmp_bulkget.c +++ b/snmp_bulkget.c @@ -52,8 +52,8 @@ /* asprintf and getopt_long */ #ifndef _GNU_SOURCE -#define _GNU_SOURCE -#include +# define _GNU_SOURCE +# include #endif #include @@ -82,14 +82,11 @@ * make non-posix code optional e.g. asprintf */ - - #ifdef DEBUG static char *implode_result; #endif -void print64(struct counter64 *count64, unsigned long *count32) { - +void print64(struct counter64 *count64, const unsigned long *count32) { if (!(isZeroU64(count64))) { char buffer[I64CHARSZ + 1]; printU64(buffer, count64); @@ -107,16 +104,18 @@ void print64(struct counter64 *count64, unsigned long *count32) { } } -u64 convertto64(struct counter64 *val64, unsigned long *val32) { +u64 convertto64(struct counter64 *val64, const unsigned long *val32) { u64 temp64; if ((isZeroU64(val64))) { - if (val32) + if (val32) { temp64 = (u64)(*val32); - else + } else { temp64 = 0; - } else + } + } else { temp64 = ((u64)(val64->high) << 32) + val64->low; + } return (temp64); } @@ -125,29 +124,26 @@ u64 subtract64(u64 big64, u64 small64, unsigned int lastcheck, int uptime) { if (big64 < small64) { /* either the device was reset or the counter overflowed */ - if ((lastcheck + UPTIME_TOLERANCE_IN_SECS) > uptime) + if ((lastcheck + UPTIME_TOLERANCE_IN_SECS) > uptime) { /* the device was reset, or the uptime counter rolled over * so play safe and return 0 */ return 0; - else { - /* we assume there was exactly 1 counter rollover - * - of course there may have been more than 1 if it - * is a 32 bit counter ... - */ - if (small64 > OFLO32) - return (OFLO64 - small64 + big64); - else - return (OFLO32 - small64 + big64); } - } else - return (big64 - small64); + /* we assume there was exactly 1 counter rollover + * - of course there may have been more than 1 if it + * is a 32 bit counter ... + */ + if (small64 > OFLO32) { + return (OFLO64 - small64 + big64); + } + return (OFLO32 - small64 + big64); + } + return (big64 - small64); } -netsnmp_session *start_session(netsnmp_session *session, char *community, - char *hostname, enum mode_enum mode, - unsigned long global_timeout, +netsnmp_session *start_session(netsnmp_session *session, char *community, char *hostname, enum mode_enum mode, unsigned long global_timeout, int session_retries) { - netsnmp_session *ss; + netsnmp_session *snmp_session; /* * Initialize the SNMP library @@ -159,10 +155,11 @@ netsnmp_session *start_session(netsnmp_session *session, char *community, session->peername = hostname; /* bulk gets require V2c or later */ - if (mode == NONBULK) + if (mode == NONBULK) { session->version = SNMP_VERSION_1; - else + } else { session->version = SNMP_VERSION_2c; + } session->community = (u_char *)community; session->community_len = strlen(community); @@ -173,23 +170,20 @@ netsnmp_session *start_session(netsnmp_session *session, char *community, * Open the session */ SOCK_STARTUP; - ss = snmp_open(session); /* establish the session */ + snmp_session = snmp_open(session); /* establish the session */ - if (!ss) { + if (!snmp_session) { snmp_sess_perror("snmp_bulkget", session); SOCK_CLEANUP; exit(1); } - return (ss); + return (snmp_session); } -netsnmp_session *start_session_v3(netsnmp_session *session, char *user, - char *auth_proto, char *auth_pass, - char *priv_proto, char *priv_pass, - char *hostname, unsigned long global_timeout, - int session_retries) { - netsnmp_session *ss; +netsnmp_session *start_session_v3(netsnmp_session *session, char *user, char *auth_proto, char *auth_pass, char *priv_proto, + char *priv_pass, char *hostname, unsigned long global_timeout, int session_retries) { + netsnmp_session *snmp_session; init_snmp("snmp_bulkget"); @@ -204,13 +198,11 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, if (priv_proto && priv_pass) { if (!strcmp(priv_proto, "AES")) { - session->securityPrivProto = snmp_duplicate_objid( - usmAESPrivProtocol, USM_PRIV_PROTO_AES_LEN); + session->securityPrivProto = snmp_duplicate_objid(usmAESPrivProtocol, USM_PRIV_PROTO_AES_LEN); session->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN; #ifdef HAVE_USM_DES_PRIV_PROTOCOL } else if (!strcmp(priv_proto, "DES")) { - session->securityPrivProto = snmp_duplicate_objid( - usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN); + session->securityPrivProto = snmp_duplicate_objid(usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN); session->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN; #endif } else { @@ -226,28 +218,22 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, if (auth_proto && auth_pass) { if (!strcmp(auth_proto, "SHA")) { - session->securityAuthProto = snmp_duplicate_objid( - usmHMACSHA1AuthProtocol, USM_AUTH_PROTO_SHA_LEN); + session->securityAuthProto = snmp_duplicate_objid(usmHMACSHA1AuthProtocol, USM_AUTH_PROTO_SHA_LEN); session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; } else if (!strcmp(auth_proto, "SHA-224")) { - session->securityAuthProto = snmp_duplicate_objid( - usmHMAC128SHA224AuthProtocol, USM_AUTH_PROTO_SHA_LEN); + session->securityAuthProto = snmp_duplicate_objid(usmHMAC128SHA224AuthProtocol, USM_AUTH_PROTO_SHA_LEN); session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; } else if (!strcmp(auth_proto, "SHA-256")) { - session->securityAuthProto = snmp_duplicate_objid( - usmHMAC192SHA256AuthProtocol, USM_AUTH_PROTO_SHA_LEN); + session->securityAuthProto = snmp_duplicate_objid(usmHMAC192SHA256AuthProtocol, USM_AUTH_PROTO_SHA_LEN); session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; } else if (!strcmp(auth_proto, "SHA-384")) { - session->securityAuthProto = snmp_duplicate_objid( - usmHMAC256SHA384AuthProtocol, USM_AUTH_PROTO_SHA_LEN); + session->securityAuthProto = snmp_duplicate_objid(usmHMAC256SHA384AuthProtocol, USM_AUTH_PROTO_SHA_LEN); session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; } else if (!strcmp(auth_proto, "SHA-512")) { - session->securityAuthProto = snmp_duplicate_objid( - usmHMAC384SHA512AuthProtocol, USM_AUTH_PROTO_SHA_LEN); + session->securityAuthProto = snmp_duplicate_objid(usmHMAC384SHA512AuthProtocol, USM_AUTH_PROTO_SHA_LEN); session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; } else if (!strcmp(auth_proto, "MD5")) { - session->securityAuthProto = snmp_duplicate_objid( - usmHMACMD5AuthProtocol, USM_AUTH_PROTO_MD5_LEN); + session->securityAuthProto = snmp_duplicate_objid(usmHMACMD5AuthProtocol, USM_AUTH_PROTO_MD5_LEN); session->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN; } else { printf("Unknown auth protocol %s\n", auth_proto); @@ -260,21 +246,16 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, session->securityPrivKeyLen = 0; } - if ((session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) || - (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV)) { - if (generate_Ku(session->securityAuthProto, - session->securityAuthProtoLen, - (unsigned char *)auth_pass, strlen(auth_pass), - session->securityAuthKey, - &session->securityAuthKeyLen) != SNMPERR_SUCCESS) + if ((session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) || (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV)) { + if (generate_Ku(session->securityAuthProto, session->securityAuthProtoLen, (unsigned char *)auth_pass, strlen(auth_pass), + session->securityAuthKey, &session->securityAuthKeyLen) != SNMPERR_SUCCESS) { printf("Error generating AUTH sess\n"); + } if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) { - if (generate_Ku(session->securityAuthProto, - session->securityAuthProtoLen, - (unsigned char *)priv_pass, strlen(priv_pass), - session->securityPrivKey, - &session->securityPrivKeyLen) != SNMPERR_SUCCESS) + if (generate_Ku(session->securityAuthProto, session->securityAuthProtoLen, (unsigned char *)priv_pass, strlen(priv_pass), + session->securityPrivKey, &session->securityPrivKeyLen) != SNMPERR_SUCCESS) { printf("Error generating PRIV sess\n"); + } } } @@ -285,15 +266,15 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, * Open the session */ SOCK_STARTUP; - ss = snmp_open(session); /* establish the session */ + snmp_session = snmp_open(session); /* establish the session */ - if (!ss) { + if (!snmp_session) { snmp_sess_perror("snmp_bulkget", session); SOCK_CLEANUP; exit(1); } - return (ss); + return (snmp_session); } /* @@ -307,11 +288,13 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, * outOctets=15023959911c inDiscards=0c outDiscards=0c inErrors=0c * outErrors=5431c inUcast=34020897c outUcast=35875426c speed=1000000000 */ -int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, - char *prefix, unsigned int *parsed_lastcheck, - int ifNumber, - char *perfdata_labels[]) { - char *last = 0, *last2 = 0, *word, *interface = 0, *var; +int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, char *prefix, unsigned int *parsed_lastcheck, int ifNumber, + char *perfdata_labels[]) { + char *last = 0; + char *last2 = 0; + char *word; + char *interface = 0; + char *var; char *ptr; #ifdef DEBUG int plugins; @@ -321,8 +304,7 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, char *valstr; /* first split at spaces */ - for (word = strtok_r(oldperfdatap, " ", &last); word; - word = strtok_r(NULL, " ", &last)) { + for (word = strtok_r(oldperfdatap, " ", &last); word; word = strtok_r(NULL, " ", &last)) { if ((ptr = strstr(word, "::check_multi::plugins="))) { #ifdef DEBUG /* check multi perfdata found */ @@ -357,25 +339,29 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, /* remove any prefix */ if (prefix) { - if (strlen(interface) > strlen(prefix)) + if (strlen(interface) > strlen(prefix)) { interface = interface + strlen(prefix); + } } #ifdef DEBUG - if (interface) + if (interface) { fprintf(stderr, "interface %s found\n", interface); + } #endif word = (ptr + strlen("::check_snmp::")); } /* finally split the name=value pair */ valstr = strchr(word, '='); - if (valstr) + if (valstr) { value = strtoull(valstr + 1, NULL, 10); + } var = strtok_r(word, "=", &last2); - if (interface && var && valstr) + if (interface && var && valstr) { set_value(oldperfdata, interface, var, value, ifNumber, perfdata_labels); + } } return (0); @@ -384,34 +370,34 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, /* * fill the ifStruct with values */ -void set_value(struct ifStruct *oldperfdata, char *interface, char *var, - u64 value, int ifNumber, char *if_vars[]) { +void set_value(struct ifStruct *oldperfdata, char *interface, char *var, u64 value, int ifNumber, char *if_vars[]) { int i; for (i = 0; i < ifNumber; i++) { if (strcmp(interface, oldperfdata[i].descr) == 0) { - if (strcmp(var, if_vars[0]) == 0) + if (strcmp(var, if_vars[0]) == 0) { oldperfdata[i].inOctets = value; - else if (strcmp(var, if_vars[1]) == 0) + } else if (strcmp(var, if_vars[1]) == 0) { oldperfdata[i].outOctets = value; - else if (strcmp(var, if_vars[2]) == 0) + } else if (strcmp(var, if_vars[2]) == 0) { oldperfdata[i].inDiscards = value; - else if (strcmp(var, if_vars[3]) == 0) + } else if (strcmp(var, if_vars[3]) == 0) { oldperfdata[i].outDiscards = value; - else if (strcmp(var, if_vars[4]) == 0) + } else if (strcmp(var, if_vars[4]) == 0) { oldperfdata[i].inErrors = value; - else if (strcmp(var, if_vars[5]) == 0) + } else if (strcmp(var, if_vars[5]) == 0) { oldperfdata[i].outErrors = value; - else if (strcmp(var, if_vars[6]) == 0) + } else if (strcmp(var, if_vars[6]) == 0) { oldperfdata[i].inUcast = value; - else if (strcmp(var, if_vars[7]) == 0) + } else if (strcmp(var, if_vars[7]) == 0) { oldperfdata[i].outUcast = value; - else if (strcmp(var, if_vars[8]) == 0) + } else if (strcmp(var, if_vars[8]) == 0) { oldperfdata[i].speed = value; - else if (strcmp(var, if_vars[9]) == 0) + } else if (strcmp(var, if_vars[9]) == 0) { oldperfdata[i].inbitps = value; - else if (strcmp(var, if_vars[10]) == 0) + } else if (strcmp(var, if_vars[10]) == 0) { oldperfdata[i].outbitps = value; + } continue; } @@ -422,22 +408,23 @@ void set_value(struct ifStruct *oldperfdata, char *interface, char *var, * pass this function a list of OIDs to retrieve * and it will fetch them with a single get */ -int create_request(netsnmp_session *ss, struct OIDStruct **OIDpp, - char **oid_list, int index, netsnmp_pdu **response, +int create_request(netsnmp_session *snmp_session, struct OIDStruct **OIDpp, char **oid_list, int index, netsnmp_pdu **response, unsigned int sleep_usecs) { netsnmp_pdu *pdu; - int status, i; + int status; struct OIDStruct *OIDp; /* store all the parsed OIDs in a structure for easy comparison */ - for (i = 0; oid_list[i]; i++) - ; - OIDp = (struct OIDStruct *)calloc(i, sizeof(*OIDp)); + size_t number_of_oids = 0; + for (int i = 0; oid_list[i]; i++) { + number_of_oids = i + 1; + } + OIDp = (struct OIDStruct *)calloc(number_of_oids, sizeof(*OIDp)); /* here we are retrieving single values, not walking the table */ pdu = snmp_pdu_create(SNMP_MSG_GET); - for (i = 0; oid_list[i]; i++) { + for (int i = 0; oid_list[i]; i++) { #ifdef DEBUG2 fprintf(stderr, "%d: adding %s\n", i, oid_list[i]); #endif @@ -446,7 +433,7 @@ int create_request(netsnmp_session *ss, struct OIDStruct **OIDpp, OIDp[i].name[OIDp[i].name_len++] = index; snmp_add_null_var(pdu, OIDp[i].name, OIDp[i].name_len); } - pdu->non_repeaters = i; + pdu->non_repeaters = number_of_oids; pdu->max_repetitions = 0; *OIDpp = OIDp; @@ -455,41 +442,40 @@ int create_request(netsnmp_session *ss, struct OIDStruct **OIDpp, implode_result = implode(", ", oid_list); benchmark_start("Send SNMP request for OIDs: %s", implode_result); #endif - status = snmp_synch_response(ss, pdu, response); + status = snmp_synch_response(snmp_session, pdu, response); #ifdef DEBUG benchmark_end(); free(implode_result); #endif - if (sleep_usecs) + if (sleep_usecs) { usleep(sleep_usecs); + } if (status == STAT_SUCCESS && (*response)->errstat == SNMP_ERR_NOERROR) { return (1); - } else if (status == STAT_SUCCESS && - (*response)->errstat == SNMP_ERR_NOSUCHNAME) { + } + if (status == STAT_SUCCESS && (*response)->errstat == SNMP_ERR_NOSUCHNAME) { /* if e.g. 64 bit counters are not supported, we will get this error */ return (1); - } else { - /* - * FAILURE: print what went wrong! - */ + } + /* + * 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 fetching interface stats from %s ", ss->peername); - for (i = 0; oid_list[i]; i++) { - printf("%c%s", i ? ',' : '(', oid_list[i]); - } - printf(")\n"); - exit(EXITCODE_TIMEOUT); - } else { - printf("other error\n"); - snmp_sess_perror("snmp_bulkget", ss); + if (status == STAT_SUCCESS) { + printf("Error in packet\nReason: %s\n", snmp_errstring((*response)->errstat)); + } else if (status == STAT_TIMEOUT) { + printf("Timeout fetching interface stats from %s ", snmp_session->peername); + for (int i = 0; oid_list[i]; i++) { + printf("%c%s", i ? ',' : '(', oid_list[i]); } - exit(2); + printf(")\n"); + exit(EXITCODE_TIMEOUT); + } else { + printf("other error\n"); + snmp_sess_perror("snmp_bulkget", snmp_session); } + exit(2); return (0); } @@ -509,12 +495,11 @@ int parseoids(int i, char *oid_list, struct OIDStruct *query) { return (0); } -void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, - struct OIDStruct **oids, int nonrepeaters, long max) { - int i; +void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, struct OIDStruct **oids, int nonrepeaters, long max) { - if (mode == NONBULK) + if (mode == NONBULK) { *pdu = snmp_pdu_create(SNMP_MSG_GET); + } else if (mode == BINTEC) { /* we cannot use a bulk get for bintec @@ -528,7 +513,7 @@ void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, (*pdu)->max_repetitions = max; } - for (i = 0; oidlist[i]; i++) { + for (int i = 0; oidlist[i]; i++) { parseoids(i, oidlist[i], *oids); snmp_add_null_var(*pdu, (*oids)[i].name, (*oids)[i].name_len); } diff --git a/snmp_bulkget.h b/snmp_bulkget.h index 083a928..30dd1ef 100644 --- a/snmp_bulkget.h +++ b/snmp_bulkget.h @@ -1,12 +1,11 @@ - #include #include #ifdef HAVE_GETADDRINFO -#include -#include -#include +# include +# include +# include #endif /* HAVE_GETADDRINFO */ #include @@ -18,11 +17,11 @@ * defines * MAX_STRING = allocate memory for this length of output string */ -#define MAX_STRING 65536 -#define MAX_DESCR_LEN 255 +#define MAX_STRING 65536 +#define MAX_DESCR_LEN 255 #define UPTIME_TOLERANCE_IN_SECS 30 -#define OFLO32 4294967295ULL -#define OFLO64 18446744073709551615ULL +#define OFLO32 4294967295ULL +#define OFLO64 18446744073709551615ULL /* default timeout is 30s */ #define DFLT_TIMEOUT 30000000UL @@ -31,10 +30,10 @@ #define EXITCODE_TIMEOUT 3 #define MEMCPY(a, b, c) memcpy(a, b, (sizeof(a) > c) ? c : sizeof(a)) -#define TERMSTR(a, b) a[(((sizeof(a) - 1) < b) ? (sizeof(a) - 1) : b)] = '\0' +#define TERMSTR(a, b) a[(((sizeof(a) - 1) < b) ? (sizeof(a) - 1) : b)] = '\0' #ifndef U64 -#define U64 +# define U64 typedef unsigned long long u64; #endif @@ -70,11 +69,15 @@ struct OIDStruct { size_t name_len; }; - /* * operating modes */ -enum mode_enum { DEFAULT, CISCO, NONBULK, BINTEC }; +enum mode_enum { + DEFAULT, + CISCO, + NONBULK, + BINTEC +}; // Config typedef struct configuration_struct { @@ -90,7 +93,7 @@ typedef struct configuration_struct { int err_tolerance; int coll_tolerance; char *hostname; - char* port; + char *port; char *user; char *auth_proto; char *auth_pass; @@ -118,22 +121,15 @@ size_t sizeof_oid_if_bintec(void); size_t sizeof_oid_if_get(void); size_t sizeof_oid_if_bulkget(void); -void print64(struct counter64 *, unsigned long *); -u64 convertto64(struct counter64 *, unsigned long *); +void print64(struct counter64 *, const unsigned long *); +u64 convertto64(struct counter64 *, const unsigned long *); u64 subtract64(u64, u64, unsigned int lastcheck, int uptime); -netsnmp_session *start_session(netsnmp_session *, char *, char *, - enum mode_enum, unsigned long global_timeout, - int session_retries); -netsnmp_session *start_session_v3(netsnmp_session *, char *, char *, char *, - char *, char *, char *, - unsigned long global_timeout, +netsnmp_session *start_session(netsnmp_session *, char *, char *, enum mode_enum, unsigned long global_timeout, int session_retries); +netsnmp_session *start_session_v3(netsnmp_session *, char *, char *, char *, char *, char *, char *, unsigned long global_timeout, int session_retries); int usage(char *); -int parse_perfdata(char *, struct ifStruct *, char *, unsigned int *, - int ifNumber, char *perfdata_lables[]); -void set_value(struct ifStruct *, char *, char *, u64, int ifNumber, - char *if_vars[]); +int parse_perfdata(char *, struct ifStruct *, char *, unsigned int *, int ifNumber, char *[]); +void set_value(struct ifStruct *, char *, char *, u64, int ifNumber, char *if_vars[]); int parseoids(int, char *, struct OIDStruct *); -int create_request(netsnmp_session *, struct OIDStruct **, char **, int, - netsnmp_pdu **, unsigned int sleep_usecs); +int create_request(netsnmp_session *, struct OIDStruct **, char **, int, netsnmp_pdu **, unsigned int sleep_usecs); void create_pdu(int, char **, netsnmp_pdu **, struct OIDStruct **, int, long); diff --git a/utils.c b/utils.c index 0cc3a6e..ac64588 100644 --- a/utils.c +++ b/utils.c @@ -60,13 +60,12 @@ int addstr(String *str, const char *format, ...) { * - only use for strings we already know the size of */ void strcpy_nospaces(char *dest, char *src) { - static unsigned char allowed[256] = - "_________________________________!_#_%__()*+,-.-0123456789_____?@" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ[_]^__abcdefghijklmnopqrstuvwxyz{_}_________" - "__" - "______________________________________________________________________" - "__" - "_______________________________________________"; + static unsigned char allowed[256] = "_________________________________!_#_%__()*+,-.-0123456789_____?@" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ[_]^__abcdefghijklmnopqrstuvwxyz{_}_________" + "__" + "______________________________________________________________________" + "__" + "_______________________________________________"; while (*src) { *(dest++) = allowed[(unsigned char)*(src++)]; @@ -94,11 +93,10 @@ int gauge_to_si(u64 bignum, char **str) { #ifdef HAVE_POW if (i) { - return asprintf(str, "%0.2f%c", ((double)bignum / pow(1000, i)), - units[i - 1]); - } else { - return asprintf(str, "%Ld", bignum); + return asprintf(str, "%0.2f%c", ((double)bignum / pow(1000, i)), units[i - 1]); } + return asprintf(str, "%Ld", bignum); + #else return asprintf(str, "%Ld", bignum); #endif @@ -112,7 +110,7 @@ void benchmark_start(char const *format, ...) { { va_list args; va_start(args, format); - int benchmark_task_length = vsnprintf(NULL, 0u, format, args); + int benchmark_task_length = vsnprintf(NULL, 0U, format, args); va_end(args); benchmark_task = (char *)malloc(benchmark_task_length + 1); benchmark_task[benchmark_task_length] = 0; @@ -132,17 +130,15 @@ void benchmark_end(void) { struct timespec benchmark_end_time; clock_gettime(CLOCK_MONOTONIC, &benchmark_end_time); fprintf(stderr, "[Finished benchmark after %f ms] %s\n", - ((double)benchmark_end_time.tv_sec * 1000.0 + - (double)benchmark_end_time.tv_nsec / 1000000.0) - - ((double)benchmark_start_time.tv_sec * 1000.0 + - (double)benchmark_start_time.tv_nsec / 1000000.0), + ((double)benchmark_end_time.tv_sec * 1000.0 + (double)benchmark_end_time.tv_nsec / 1000000.0) - + ((double)benchmark_start_time.tv_sec * 1000.0 + (double)benchmark_start_time.tv_nsec / 1000000.0), benchmark_task); } free(benchmark_task); } char *implode(char const *glue, char **pieces) { - size_t total_len = 0u; + size_t total_len = 0U; char **walk_pieces = pieces; while (*walk_pieces != NULL) { total_len += strlen(*walk_pieces++); @@ -153,7 +149,7 @@ char *implode(char const *glue, char **pieces) { total_len += strlen(glue) * (size_t)(walk_pieces_diff - 1); } - char *result = (char *)malloc(total_len + 1u); + char *result = (char *)malloc(total_len + 1U); if (walk_pieces_diff > 0) { strcpy(result, *pieces); diff --git a/utils.h b/utils.h index 879c499..989f35c 100644 --- a/utils.h +++ b/utils.h @@ -1,18 +1,18 @@ /* asprintf */ #ifndef _GNU_SOURCE -#define _GNU_SOURCE +# define _GNU_SOURCE #endif #include #include #ifdef HAVE_POW -#include +# include #endif #ifndef U64 -#define U64 +# define U64 typedef unsigned long long u64; #endif