diff --git a/.clang-format b/.clang-format index 96a4695..0ff6811 100644 --- a/.clang-format +++ b/.clang-format @@ -1,12 +1,12 @@ -BasedOnStyle: LLVM UseTab: ForContinuationAndIndentation IndentWidth: 4 TabWidth: 4 AllowShortIfStatementsOnASingleLine: false BreakBeforeBraces: Attach AlignConsecutiveMacros: true -ColumnLimit: 140 +ColumnLimit: 100 IndentPPDirectives: AfterHash SortIncludes: Never -InsertBraces: true AllowShortEnumsOnASingleLine: false +BinPackArguments: true +InsertBraces: true diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 21c7e08..901f794 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,14 +13,6 @@ jobs: steps: - uses: actions/checkout@v5 - - uses: actions/setup-python@v6 - with: - python-version: '3.x' - cache: pip - - - name: Install pip packages - run: pip install -r .github/requirements.txt - - name: Install dependencies run: sudo apt-get update && sudo apt-get install -y libsnmp-dev @@ -32,18 +24,6 @@ jobs: - name: Make run: make - - name: Start simmulation - run: | - setup-snmpsim-data /tmp/data - snmpsim-command-responder --daemonize --pid-file /tmp/snmp --data-dir=/tmp/data/network/switch --agent-udpv4-endpoint=127.0.0.1:1611 - - - name: Sleep - run: sleep 30s - shell: bash - - - name: check_interfaces with some cisco stuff - run: ./check_interfaces -h 127.0.0.1 --port=1611 -c cisco-c3560 -d - clang-build: runs-on: ubuntu-latest env: @@ -53,14 +33,6 @@ jobs: steps: - uses: actions/checkout@v5 - - uses: actions/setup-python@v6 - with: - python-version: '3.x' - cache: pip - - - name: Install pip packages - run: pip install -r .github/requirements.txt - - name: Install dependencies run: sudo apt-get update && sudo apt-get install -y libsnmp-dev @@ -71,15 +43,3 @@ jobs: - name: Make run: make - - - name: Start simmulation - run: | - setup-snmpsim-data /tmp/data - snmpsim-command-responder --daemonize --pid-file /tmp/snmp --data-dir=/tmp/data/network/switch --agent-udpv4-endpoint=127.0.0.1:1611 - - - name: Sleep - run: sleep 30s - shell: bash - - - name: check_interfaces with some cisco stuff - run: ./check_interfaces -h 127.0.0.1 --port=1611 -c cisco-c3560 -d diff --git a/check_interfaces.c b/check_interfaces.c index d93fdd4..5496c5f 100644 --- a/check_interfaces.c +++ b/check_interfaces.c @@ -15,68 +15,71 @@ * text strings to output in the perfdata */ -char *if_vars_default[] = {"inOctets", "outOctets", "inDiscards", "outDiscards", "inErrors", "outErrors", - "inUcast", "outUcast", "speed", "inBitps", "outBitps"}; +static 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"}; +static 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}; +static 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" */ + +static 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" */ + +static 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" */ + +static 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}; + +static char *oid_alias_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.18", 0}; /* "alias" */ +static char *oid_alias_get[] = {".1.3.6.1.2.1.31.1.1.1.18.1", 0}; /* "alias" */ +static char *oid_alias_bintec[] = {".1.3.6.1.2.1.31.1.1.1.18.0", 0}; /* "alias" */ +static char *oid_names_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.1", 0}; /* "name" */ +static char *oid_names_get[] = {".1.3.6.1.2.1.31.1.1.1.1.1", 0}; /* "name" */ +static char *oid_names_bintec[] = {".1.3.6.1.2.1.31.1.1.1.1.0", 0}; /* "name - NOT TESTED!" */ +static char *oid_extended_cisco[] = {".1.3.6.1.4.1.9.5.1.4.1.1.23", /* portAdditionalOperStatus */ + 0}; + +static 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}; + +static 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}; +static const char *modes[] = {"default", "cisco", "nonbulk", "bintec", NULL}; #ifdef DEBUG static char *implode_result; @@ -92,34 +95,21 @@ 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 * /*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); +static void parse_and_check_commandline(int argc, char **argv, struct configuration_struct *config); +static bool fetch_interface_aliases(struct configuration_struct * /*config*/, + char ** /*oid_aliasp*/, netsnmp_session *snmp_session, + netsnmp_session *session, struct ifStruct *interfaces, + int ifNumber); +static bool fetch_interface_names(struct configuration_struct * /*config*/, char **oid_namesp, + netsnmp_session *snmp_session, netsnmp_session *session, + struct ifStruct *interfaces, int ifNumber); +static 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); +static void print_version(void); int main(int argc, char *argv[]) { - netsnmp_session session; - netsnmp_session *snmp_session; - netsnmp_pdu *pdu; - netsnmp_pdu *response; - - netsnmp_variable_list *vars; - int status; - int 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, @@ -151,38 +141,13 @@ int main(int argc, char *argv[]) { .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 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); + long double 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; @@ -199,17 +164,25 @@ int main(int argc, char *argv[]) { #ifdef DEBUG benchmark_start("Start SNMP session"); #endif + netsnmp_session session; + netsnmp_session *snmp_session; if (config.user) { /* use snmpv3 */ - 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); + 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); + snmp_session = start_session(&session, config.community, peername, config.mode, + config.global_timeout, config.session_retries); } #ifdef DEBUG benchmark_end(); #endif + size_t size = 0; + char **oid_aliasp; + char **oid_namesp; + char **oid_ifp = oid_if_bulkget; if (config.mode == NONBULK) { oid_ifp = oid_if_get; size = (sizeof(oid_if_get) / sizeof(char *)) - 1; @@ -228,8 +201,10 @@ int main(int argc, char *argv[]) { } /* allocate the space for the interface OIDs */ - OIDp = (struct OIDStruct *)calloc(size, sizeof(struct OIDStruct)); + struct OIDStruct *OIDp = (struct OIDStruct *)calloc(size, sizeof(struct OIDStruct)); + char **oid_vals = oid_vals_default; + char **if_vars = if_vars_default; if (config.mode == CISCO) { if_vars = if_vars_cisco; oid_vals = oid_vals_cisco; @@ -243,8 +218,25 @@ int main(int argc, char *argv[]) { * is necessary to work around buggy switches that lie about the ifNumber */ - for (bool lastifflag = false; lastifflag != true;) { + netsnmp_pdu *response; + netsnmp_pdu *pdu; + int count = 0; /* used for: the number of interfaces we receive, the number + of regex matches */ + /* uptime counter */ + unsigned int uptime = 0; + int ifNumber = 0; + struct OIDStruct lastOid; + struct ifStruct *interfaces = NULL; /* current interface data */ + struct ifStruct *oldperfdata = NULL; /* previous check interface data */ + char outstr[MAX_STRING]; + memset(outstr, 0, sizeof(outstr)); + String out = { + .max = MAX_STRING, + .len = 0, + .text = outstr, + }; + 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); @@ -267,7 +259,7 @@ int main(int argc, char *argv[]) { benchmark_start("Send SNMP request for OIDs: %s", implode_result); #endif /* send the request */ - status = snmp_synch_response(snmp_session, pdu, &response); + int status = snmp_synch_response(snmp_session, pdu, &response); #ifdef DEBUG benchmark_end(); free(implode_result); @@ -277,8 +269,7 @@ int main(int argc, char *argv[]) { } if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { - - vars = response->variables; + netsnmp_variable_list *vars = response->variables; if (count == 0) { /* assuming that the uptime and ifNumber come first */ @@ -309,7 +300,8 @@ int main(int argc, char *argv[]) { // get the real list length we need int real_count = 0; - for (netsnmp_variable_list *runner = response->variables; runner; runner = runner->next_variable) { + for (netsnmp_variable_list *runner = response->variables; runner; + runner = runner->next_variable) { if (vars->type == ASN_OCTET_STR) { real_count++; } @@ -341,7 +333,8 @@ int main(int argc, char *argv[]) { 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 @@ -356,7 +349,8 @@ int main(int argc, char *argv[]) { if (config.trimdescr && config.trimdescr < vars->val_len) { 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); } else { @@ -372,7 +366,8 @@ 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 { @@ -384,7 +379,8 @@ 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; } @@ -460,27 +456,36 @@ 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 */ + int status = 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))); + (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))); + (config.match_aliases_flag && + !(regexec(&config.re, interfaces[i].alias, (size_t)0, NULL, 0))); } - status2 = 0; + + int 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))); + 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))); + 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 { interfaces[i].ignore = 1; @@ -506,15 +511,17 @@ int main(int argc, char *argv[]) { * N.B. if the interfaces are continuous we could try * a bulk get instead */ + int ignore_count = 0; 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(snmp_session, &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 (netsnmp_variable_list *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++) { @@ -540,7 +547,10 @@ int main(int argc, char *argv[]) { * dormant(assume OK) */ interfaces[j].status = - (*(vars->val.integer) == 1 || *(vars->val.integer) == 5 || *(vars->val.integer) == 3) ? 1 : 0; + (*(vars->val.integer) == 1 || *(vars->val.integer) == 5 || + *(vars->val.integer) == 3) + ? 1 + : 0; } break; case 2: /* ifInOctets */ @@ -582,8 +592,10 @@ int main(int argc, char *argv[]) { } /* now fetch the extended oids (64 bit counters etc.) */ - if (create_request(snmp_session, &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 (netsnmp_variable_list *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++) { @@ -624,7 +636,8 @@ int main(int argc, char *argv[]) { case 5: /* ifHighSpeed */ if (vars->type == ASN_GAUGE) { /* convert to bits / sec */ - interfaces[j].speed = ((u64) * (vars->val.integer)) * 1000000ULL; + interfaces[j].speed = + ((unsigned long long)*(vars->val.integer)) * 1000000ULL; } break; case 6: /* alias */ @@ -647,8 +660,10 @@ int main(int argc, char *argv[]) { /* now fetch the Cisco-specific extended oids */ if (config.mode == CISCO && - create_request(snmp_session, &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 (netsnmp_variable_list *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++) { @@ -662,7 +677,8 @@ int main(int argc, char *argv[]) { { case 0: /* portAdditionalOperStatus */ if (vars->type == ASN_OCTET_STR) { - interfaces[j].err_disable = !!(vars->val.string[1] & (unsigned char)32U); + interfaces[j].err_disable = + !!(vars->val.string[1] & (unsigned char)32U); } break; } @@ -679,7 +695,8 @@ int main(int argc, char *argv[]) { * ignored) */ if (ignore_count) { - addstr(&out, " - %d %s administratively down", ignore_count, ignore_count != 1 ? "are" : "is"); + addstr(&out, " - %d %s administratively down", ignore_count, + ignore_count != 1 ? "are" : "is"); } if (OIDp) { @@ -691,7 +708,8 @@ int main(int argc, char *argv[]) { gettimeofday(&time_value, &time_zone); - returncode_t exit_code = print_output(&config, oldperfdata, starttime, interfaces, &out, if_vars, count, &time_value, uptime, ifNumber); + 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"); @@ -706,14 +724,16 @@ 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, +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); + parse_perfdata(config->oldperfdatap, oldperfdata, config->prefix, &parsed_lastcheck, + ifNumber, if_vars); } if (config->lastcheck) { @@ -747,9 +767,12 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * 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; @@ -788,7 +811,10 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * } } } 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); + 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 { @@ -799,8 +825,10 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * /* 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)) { addstr(&perf, "[WARNING] %s", interfaces[i].name); @@ -819,19 +847,25 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * 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].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 (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)); + (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 @@ -840,24 +874,33 @@ 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) * - 8ULL; + interfaces[i].inbitps = (subtract64(interfaces[i].inOctets, oldperfdata[i].inOctets, + config->lastcheck, uptime) / + (unsigned long long)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) / + (unsigned long long)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; } @@ -879,7 +922,8 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * } if (config->lastcheck && (interfaces[i].speed || config->speed) && - (interfaces[i].inbitps > 0ULL || interfaces[i].outbitps > 0ULL) && !interfaces[i].admin_down) { + (interfaces[i].inbitps > 0ULL || interfaces[i].outbitps > 0ULL) && + !interfaces[i].admin_down) { char *ins; char *outs; gauge_to_si(interfaces[i].inbitps, &ins); @@ -910,25 +954,34 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * /* 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)time_value->tv_sec + ((long double)time_value->tv_usec / 1000000)) - starttime), time_value->tv_sec); + 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); + 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(" %s=%llub %s=%llub", if_vars[9], interfaces[i].inbitps, if_vars[10], + interfaces[i].outbitps); } } printf("\n%*s", (int)perf.len, perf.text); @@ -942,8 +995,9 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct * return OK; } -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_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; @@ -1003,7 +1057,8 @@ bool fetch_interface_aliases(struct configuration_struct *config, char **oid_ali 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 @@ -1066,7 +1121,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 *snmp_session, netsnmp_session *session, +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; @@ -1127,7 +1183,8 @@ bool fetch_interface_names(struct configuration_struct *config, char **oid_names 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 @@ -1150,7 +1207,8 @@ 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"); } @@ -1234,7 +1292,8 @@ void parse_and_check_commandline(int argc, char **argv, struct configuration_str {"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) { + 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; @@ -1381,7 +1440,8 @@ void parse_and_check_commandline(int argc, char **argv, struct configuration_str } 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); diff --git a/snmp_bulkget.c b/snmp_bulkget.c index 377ccf8..de68473 100644 --- a/snmp_bulkget.c +++ b/snmp_bulkget.c @@ -90,6 +90,7 @@ void print64(struct counter64 *count64, const unsigned long *count32) { if (!(isZeroU64(count64))) { char buffer[I64CHARSZ + 1]; printU64(buffer, count64); + #ifdef DEBUG printf("64:%s", buffer); #else @@ -104,23 +105,24 @@ void print64(struct counter64 *count64, const unsigned long *count32) { } } -u64 convertto64(struct counter64 *val64, const unsigned long *val32) { - u64 temp64; +unsigned long long convertto64(struct counter64 *val64, const unsigned long *val32) { + unsigned long long temp64; if ((isZeroU64(val64))) { if (val32) { - temp64 = (u64)(*val32); + temp64 = (unsigned long long)(*val32); } else { temp64 = 0; } } else { - temp64 = ((u64)(val64->high) << 32) + val64->low; + temp64 = ((unsigned long long)(val64->high) << 32) + val64->low; } return (temp64); } -u64 subtract64(u64 big64, u64 small64, unsigned int lastcheck, int uptime) { +unsigned long long subtract64(unsigned long long big64, unsigned long long small64, + unsigned int lastcheck, int uptime) { if (big64 < small64) { /* either the device was reset or the counter overflowed */ @@ -141,7 +143,8 @@ u64 subtract64(u64 big64, u64 small64, unsigned int lastcheck, int uptime) { 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 *snmp_session; @@ -181,8 +184,10 @@ netsnmp_session *start_session(netsnmp_session *session, char *community, char * 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 *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"); @@ -198,11 +203,13 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, char *au 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 { @@ -218,22 +225,28 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, char *au 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); @@ -246,14 +259,17 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, char *au 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"); } } @@ -288,8 +304,8 @@ netsnmp_session *start_session_v3(netsnmp_session *session, char *user, char *au * 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[]) { +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; @@ -300,7 +316,7 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, char *prefi int plugins; int uptime_old; #endif - u64 value = 0; + unsigned long long value = 0; char *valstr; /* first split at spaces */ @@ -370,10 +386,10 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, char *prefi /* * fill the ifStruct with values */ -void set_value(struct ifStruct *oldperfdata, char *interface, char *var, u64 value, int ifNumber, char *if_vars[]) { - int i; +void set_value(struct ifStruct *oldperfdata, char *interface, char *var, unsigned long long value, + int ifNumber, char *if_vars[]) { - for (i = 0; i < ifNumber; i++) { + for (int i = 0; i < ifNumber; i++) { if (strcmp(interface, oldperfdata[i].descr) == 0) { if (strcmp(var, if_vars[0]) == 0) { oldperfdata[i].inOctets = value; @@ -408,8 +424,8 @@ void set_value(struct ifStruct *oldperfdata, char *interface, char *var, u64 val * pass this function a list of OIDs to retrieve * and it will fetch them with a single get */ -int create_request(netsnmp_session *snmp_session, struct OIDStruct **OIDpp, char **oid_list, int index, netsnmp_pdu **response, - unsigned int sleep_usecs) { +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; struct OIDStruct *OIDp; @@ -495,7 +511,8 @@ 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) { +void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, struct OIDStruct **oids, + int nonrepeaters, long max) { if (mode == NONBULK) { *pdu = snmp_pdu_create(SNMP_MSG_GET); diff --git a/snmp_bulkget.h b/snmp_bulkget.h index 30dd1ef..99c1049 100644 --- a/snmp_bulkget.h +++ b/snmp_bulkget.h @@ -32,11 +32,6 @@ #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' -#ifndef U64 -# define U64 -typedef unsigned long long u64; -#endif - /* * structs */ @@ -51,17 +46,17 @@ struct ifStruct { char descr[MAX_DESCR_LEN]; char alias[MAX_DESCR_LEN]; char name[MAX_DESCR_LEN]; - u64 inOctets; - u64 outOctets; + unsigned long long inOctets; + unsigned long long outOctets; unsigned long inDiscards; unsigned long outDiscards; unsigned long inErrors; unsigned long outErrors; unsigned long inUcast; unsigned long outUcast; - u64 speed; - u64 inbitps; - u64 outbitps; + unsigned long long speed; + unsigned long long inbitps; + unsigned long long outbitps; }; struct OIDStruct { @@ -105,7 +100,7 @@ typedef struct configuration_struct { char *iface_regex; unsigned long global_timeout; char *exclude_list; - u64 speed; + unsigned long long speed; unsigned int lastcheck; unsigned int sleep_usecs; int session_retries; @@ -122,14 +117,18 @@ size_t sizeof_oid_if_get(void); size_t sizeof_oid_if_bulkget(void); 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, - int session_retries); +unsigned long long convertto64(struct counter64 *, const unsigned long *); +unsigned long long subtract64(unsigned long long, unsigned long long, 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, int session_retries); int usage(char *); 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[]); +void set_value(struct ifStruct *, char *, char *, unsigned long long, 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 ac64588..115e872 100644 --- a/utils.c +++ b/utils.c @@ -6,6 +6,10 @@ #include #include +#ifdef HAVE_POW +# include +#endif + /* * Add a string */ @@ -60,12 +64,13 @@ 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++)]; @@ -80,9 +85,9 @@ void strcpy_nospaces(char *dest, char *src) { * AC_SEARCH_LIBS(pow, [c m], AC_DEFINE([HAVE_POW])) */ -int gauge_to_si(u64 bignum, char **str) { +int gauge_to_si(unsigned long long bignum, char **str) { long unsigned int i = 0; - u64 tmpll; + unsigned long long tmpll; static char units[] = "kMGTPE"; tmpll = bignum; @@ -130,8 +135,10 @@ 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); diff --git a/utils.h b/utils.h index 989f35c..6e2e4c1 100644 --- a/utils.h +++ b/utils.h @@ -7,15 +7,6 @@ #include #include -#ifdef HAVE_POW -# include -#endif - -#ifndef U64 -# define U64 -typedef unsigned long long u64; -#endif - struct stringStruct { size_t max; size_t len; @@ -27,7 +18,7 @@ typedef struct stringStruct String; int addstrold(char **, size_t *, const char *, ...); int addstr(String *, const char *, ...); void strcpy_nospaces(char *, char *); -int gauge_to_si(u64, char **); +int gauge_to_si(unsigned long long, char **); void benchmark_start(char const *, ...); void benchmark_end(void); char *implode(char const *, char **);