Skip to content

Commit 3cfb8ff

Browse files
author
Lorenz Kästle
committed
General code refactoring
Just some quality of life code changes which should not influence the behaviour of the program. The intention is to improve readability.
1 parent 31dcca8 commit 3cfb8ff

File tree

5 files changed

+86
-115
lines changed

5 files changed

+86
-115
lines changed

check_interfaces.c

Lines changed: 65 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
* text strings to output in the perfdata
1616
*/
1717

18-
char *if_vars_default[] = {"inOctets", "outOctets", "inDiscards", "outDiscards", "inErrors", "outErrors",
18+
static char *if_vars_default[] = {"inOctets", "outOctets", "inDiscards", "outDiscards", "inErrors", "outErrors",
1919
"inUcast", "outUcast", "speed", "inBitps", "outBitps"};
2020

21-
char *if_vars_cisco[] = {"inOctets", "outOctets", "inDiscards", "outDiscards", "inCRCs", "outCollisions",
21+
static char *if_vars_cisco[] = {"inOctets", "outOctets", "inDiscards", "outDiscards", "inCRCs", "outCollisions",
2222
"inUcast", "outUcast", "speed", "inBitps", "outBitps"};
2323

2424
/*
2525
* OIDs, hardcoded to remove the dependency on MIBs
2626
*/
27-
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" */
27+
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" */
2828

29-
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" */
29+
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" */
3030

31-
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" */
31+
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" */
3232

33-
char *oid_extended[] = {".1.3.6.1.2.1.31.1.1.1.6", /* ifHCInOctets */
33+
static char *oid_extended[] = {".1.3.6.1.2.1.31.1.1.1.6", /* ifHCInOctets */
3434
".1.3.6.1.2.1.31.1.1.1.10", /* ifHCOutOctets */
3535
".1.3.6.1.2.1.2.2.1.11", /* ifInUcastPkts */
3636
".1.3.6.1.2.1.2.2.1.17", /* ifOutUcastPkts */
@@ -40,18 +40,16 @@ char *oid_extended[] = {".1.3.6.1.2.1.31.1.1.1.6", /* ifHCInOctets */
4040
".1.3.6.1.2.1.31.1.1.1.1", /* name */
4141
0};
4242

43-
char *oid_alias_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.18", 0}; /* "alias" */
44-
char *oid_alias_get[] = {".1.3.6.1.2.1.31.1.1.1.18.1", 0}; /* "alias" */
45-
char *oid_alias_bintec[] = {".1.3.6.1.2.1.31.1.1.1.18.0", 0}; /* "alias" */
46-
char *oid_names_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.1", 0}; /* "name" */
47-
48-
char *oid_names_get[] = {".1.3.6.1.2.1.31.1.1.1.1.1", 0}; /* "name" */
49-
char *oid_names_bintec[] = {".1.3.6.1.2.1.31.1.1.1.1.0", 0}; /* "name - NOT TESTED!" */
50-
51-
char *oid_extended_cisco[] = {".1.3.6.1.4.1.9.5.1.4.1.1.23", /* portAdditionalOperStatus */
43+
static char *oid_alias_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.18", 0}; /* "alias" */
44+
static char *oid_alias_get[] = {".1.3.6.1.2.1.31.1.1.1.18.1", 0}; /* "alias" */
45+
static char *oid_alias_bintec[] = {".1.3.6.1.2.1.31.1.1.1.18.0", 0}; /* "alias" */
46+
static char *oid_names_bulkget[] = {".1.3.6.1.2.1.31.1.1.1.1", 0}; /* "name" */
47+
static char *oid_names_get[] = {".1.3.6.1.2.1.31.1.1.1.1.1", 0}; /* "name" */
48+
static char *oid_names_bintec[] = {".1.3.6.1.2.1.31.1.1.1.1.0", 0}; /* "name - NOT TESTED!" */
49+
static char *oid_extended_cisco[] = {".1.3.6.1.4.1.9.5.1.4.1.1.23", /* portAdditionalOperStatus */
5250
0};
5351

54-
char *oid_vals_default[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
52+
static char *oid_vals_default[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
5553
".1.3.6.1.2.1.2.2.1.8", /* ifOperStatus */
5654
".1.3.6.1.2.1.2.2.1.10", /* ifInOctets */
5755
".1.3.6.1.2.1.2.2.1.13", /* ifInDiscards */
@@ -61,7 +59,7 @@ char *oid_vals_default[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
6159
".1.3.6.1.2.1.2.2.1.20", /* ifOutErrors */
6260
0};
6361

64-
char *oid_vals_cisco[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
62+
static char *oid_vals_cisco[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
6563
".1.3.6.1.2.1.2.2.1.8", /* ifOperStatus */
6664
".1.3.6.1.2.1.2.2.1.10", /* ifInOctets */
6765
".1.3.6.1.2.1.2.2.1.13", /* ifInDiscards */
@@ -76,7 +74,7 @@ char *oid_vals_cisco[] = {".1.3.6.1.2.1.2.2.1.7", /* ifAdminStatus */
7674
/*
7775
* operating modes
7876
*/
79-
const char *modes[] = {"default", "cisco", "nonbulk", "bintec", NULL};
77+
static const char *modes[] = {"default", "cisco", "nonbulk", "bintec", NULL};
8078

8179
#ifdef DEBUG
8280
static char *implode_result;
@@ -92,34 +90,17 @@ enum returncode {
9290
typedef enum returncode returncode_t;
9391

9492
// Forward declarations
95-
void parse_and_check_commandline(int argc, char **argv, struct configuration_struct *config);
96-
bool fetch_interface_aliases(struct configuration_struct * /*config*/, char ** /*oid_aliasp*/, netsnmp_session *snmp_session,
93+
static void parse_and_check_commandline(int argc, char **argv, struct configuration_struct *config);
94+
static bool fetch_interface_aliases(struct configuration_struct * /*config*/, char ** /*oid_aliasp*/, netsnmp_session *snmp_session,
9795
netsnmp_session *session, struct ifStruct *interfaces, int ifNumber);
98-
bool fetch_interface_names(struct configuration_struct * /*config*/, char **oid_namesp, netsnmp_session *snmp_session,
96+
static bool fetch_interface_names(struct configuration_struct * /*config*/, char **oid_namesp, netsnmp_session *snmp_session,
9997
netsnmp_session *session, struct ifStruct *interfaces, int ifNumber);
100-
returncode_t print_output(struct configuration_struct *config, struct ifStruct *oldperfdata, long double starttime,
98+
static returncode_t print_output(struct configuration_struct *config, struct ifStruct *oldperfdata, long double starttime,
10199
struct ifStruct *interfaces, String *out, char **if_vars, unsigned int number_of_matched_interfaces,
102100
struct timeval *time_value, int uptime, int ifNumber);
103-
void print_version(void);
101+
static void print_version(void);
104102

105103
int main(int argc, char *argv[]) {
106-
netsnmp_session session;
107-
netsnmp_session *snmp_session;
108-
netsnmp_pdu *pdu;
109-
netsnmp_pdu *response;
110-
111-
netsnmp_variable_list *vars;
112-
int status;
113-
int status2;
114-
int count = 0; /* used for: the number of interfaces we receive, the number
115-
of regex matches */
116-
size_t size;
117-
118-
/* uptime counter */
119-
unsigned int uptime = 0;
120-
121-
int ifNumber = 0;
122-
123104
config_t config = {
124105
.crit_on_down_flag = true,
125106
.get_aliases_flag = false,
@@ -151,38 +132,12 @@ int main(int argc, char *argv[]) {
151132
.mode = DEFAULT,
152133
};
153134

154-
struct ifStruct *interfaces = NULL; /* current interface data */
155-
struct ifStruct *oldperfdata = NULL; /* previous check interface data */
156-
struct OIDStruct *OIDp;
157-
158-
long double starttime;
159-
int ignore_count = 0;
160-
161-
char outstr[MAX_STRING];
162-
memset(outstr, 0, sizeof(outstr));
163-
String out;
164-
out.max = MAX_STRING;
165-
out.len = 0;
166-
out.text = outstr;
167-
168-
struct OIDStruct lastOid;
169-
170-
static char **oid_ifp;
171-
static char **oid_vals;
172-
static char **if_vars;
173-
static char **oid_aliasp;
174-
static char **oid_namesp;
175-
176-
oid_ifp = oid_if_bulkget;
177-
oid_vals = oid_vals_default;
178-
if_vars = if_vars_default;
179-
180135
parse_and_check_commandline(argc, argv, &config);
181136

182137
struct timeval time_value;
183138
struct timezone time_zone;
184139
gettimeofday(&time_value, &time_zone);
185-
starttime = (long double)time_value.tv_sec + (((long double)time_value.tv_usec) / 1000000);
140+
long double starttime = (long double)time_value.tv_sec + (((long double)time_value.tv_usec) / 1000000);
186141

187142
// +1 for the `:` between hostname and port
188143
size_t peername_max_len = strlen(config.hostname) + strlen(config.port) + 1;
@@ -199,6 +154,8 @@ int main(int argc, char *argv[]) {
199154
#ifdef DEBUG
200155
benchmark_start("Start SNMP session");
201156
#endif
157+
netsnmp_session session;
158+
netsnmp_session *snmp_session;
202159
if (config.user) {
203160
/* use snmpv3 */
204161
snmp_session = start_session_v3(&session, config.user, config.auth_proto, config.auth_pass, config.priv_proto, config.priv_pass,
@@ -210,6 +167,10 @@ int main(int argc, char *argv[]) {
210167
benchmark_end();
211168
#endif
212169

170+
size_t size = 0;
171+
char **oid_aliasp;
172+
char **oid_namesp;
173+
char **oid_ifp = oid_if_bulkget;
213174
if (config.mode == NONBULK) {
214175
oid_ifp = oid_if_get;
215176
size = (sizeof(oid_if_get) / sizeof(char *)) - 1;
@@ -228,8 +189,10 @@ int main(int argc, char *argv[]) {
228189
}
229190

230191
/* allocate the space for the interface OIDs */
231-
OIDp = (struct OIDStruct *)calloc(size, sizeof(struct OIDStruct));
192+
struct OIDStruct *OIDp = (struct OIDStruct *)calloc(size, sizeof(struct OIDStruct));
232193

194+
char **oid_vals = oid_vals_default;
195+
char **if_vars = if_vars_default;
233196
if (config.mode == CISCO) {
234197
if_vars = if_vars_cisco;
235198
oid_vals = oid_vals_cisco;
@@ -243,8 +206,25 @@ int main(int argc, char *argv[]) {
243206
* is necessary to work around buggy switches that lie about the ifNumber
244207
*/
245208

246-
for (bool lastifflag = false; lastifflag != true;) {
209+
netsnmp_pdu *response;
210+
netsnmp_pdu *pdu;
211+
int count = 0; /* used for: the number of interfaces we receive, the number
212+
of regex matches */
213+
/* uptime counter */
214+
unsigned int uptime = 0;
215+
int ifNumber = 0;
216+
struct OIDStruct lastOid;
217+
struct ifStruct *interfaces = NULL; /* current interface data */
218+
struct ifStruct *oldperfdata = NULL; /* previous check interface data */
219+
char outstr[MAX_STRING];
220+
memset(outstr, 0, sizeof(outstr));
221+
String out = {
222+
.max = MAX_STRING,
223+
.len = 0,
224+
.text = outstr,
225+
};
247226

227+
for (bool lastifflag = false; lastifflag != true;) {
248228
/* build our request depending on the mode */
249229
if (count == 0) {
250230
create_pdu(config.mode, oid_ifp, &pdu, &OIDp, 2, config.pdu_max_repetitions);
@@ -267,7 +247,7 @@ int main(int argc, char *argv[]) {
267247
benchmark_start("Send SNMP request for OIDs: %s", implode_result);
268248
#endif
269249
/* send the request */
270-
status = snmp_synch_response(snmp_session, pdu, &response);
250+
int status = snmp_synch_response(snmp_session, pdu, &response);
271251
#ifdef DEBUG
272252
benchmark_end();
273253
free(implode_result);
@@ -277,8 +257,7 @@ int main(int argc, char *argv[]) {
277257
}
278258

279259
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
280-
281-
vars = response->variables;
260+
netsnmp_variable_list *vars = response->variables;
282261

283262
if (count == 0) {
284263
/* assuming that the uptime and ifNumber come first */
@@ -460,14 +439,16 @@ int main(int argc, char *argv[]) {
460439
for (int i = 0; i < ifNumber; i++) {
461440
/* When --if-name is set ignore descr in favor of name, else use old
462441
* behaviour */
442+
int status = 0;
463443
if (config.get_names_flag) {
464444
status = !regexec(&config.re, interfaces[i].name, (size_t)0, NULL, 0) ||
465445
(config.match_aliases_flag && !(regexec(&config.re, interfaces[i].alias, (size_t)0, NULL, 0)));
466446
} else {
467447
status = !regexec(&config.re, interfaces[i].descr, (size_t)0, NULL, 0) ||
468448
(config.match_aliases_flag && !(regexec(&config.re, interfaces[i].alias, (size_t)0, NULL, 0)));
469449
}
470-
status2 = 0;
450+
451+
int status2 = 0;
471452
if (status && config.exclude_list) {
472453
if (config.get_names_flag) {
473454
status2 = !regexec(&config.exclude_re, interfaces[i].name, (size_t)0, NULL, 0) ||
@@ -506,15 +487,15 @@ int main(int argc, char *argv[]) {
506487
* N.B. if the interfaces are continuous we could try
507488
* a bulk get instead
508489
*/
490+
int ignore_count = 0;
509491
for (int j = 0, k = 0; j < ifNumber; j++) {
510492
/* add the interface to the oldperfdata list */
511493
strcpy_nospaces(oldperfdata[j].descr, interfaces[j].descr);
512494

513495
if (!interfaces[j].ignore) {
514-
515496
/* fetch the standard values first */
516497
if (create_request(snmp_session, &OIDp, oid_vals, interfaces[j].index, &response, config.sleep_usecs)) {
517-
for (vars = response->variables; vars; vars = vars->next_variable) {
498+
for (netsnmp_variable_list *vars = response->variables; vars; vars = vars->next_variable) {
518499
k = -1;
519500
/* compare the received value to the requested value */
520501
for (int i = 0; oid_vals[i]; i++) {
@@ -583,7 +564,7 @@ int main(int argc, char *argv[]) {
583564

584565
/* now fetch the extended oids (64 bit counters etc.) */
585566
if (create_request(snmp_session, &OIDp, oid_extended, interfaces[j].index, &response, config.sleep_usecs)) {
586-
for (vars = response->variables; vars; vars = vars->next_variable) {
567+
for (netsnmp_variable_list *vars = response->variables; vars; vars = vars->next_variable) {
587568
k = -1;
588569
/* compare the received value to the requested value */
589570
for (int i = 0; oid_extended[i]; i++) {
@@ -624,7 +605,7 @@ int main(int argc, char *argv[]) {
624605
case 5: /* ifHighSpeed */
625606
if (vars->type == ASN_GAUGE) {
626607
/* convert to bits / sec */
627-
interfaces[j].speed = ((u64) * (vars->val.integer)) * 1000000ULL;
608+
interfaces[j].speed = ((unsigned long long)*(vars->val.integer)) * 1000000ULL;
628609
}
629610
break;
630611
case 6: /* alias */
@@ -648,7 +629,7 @@ int main(int argc, char *argv[]) {
648629
/* now fetch the Cisco-specific extended oids */
649630
if (config.mode == CISCO &&
650631
create_request(snmp_session, &OIDp, oid_extended_cisco, interfaces[j].index, &response, config.sleep_usecs)) {
651-
for (vars = response->variables; vars; vars = vars->next_variable) {
632+
for (netsnmp_variable_list *vars = response->variables; vars; vars = vars->next_variable) {
652633
k = -1;
653634
/* compare the received value to the requested value */
654635
for (int i = 0; oid_extended_cisco[i]; i++) {
@@ -842,12 +823,12 @@ returncode_t print_output(struct configuration_struct *config, struct ifStruct *
842823

843824
if (config->lastcheck && (interfaces[i].speed || config->speed) && !interfaces[i].admin_down &&
844825
(oldperfdata[i].inOctets || oldperfdata[i].outOctets)) {
845-
interfaces[i].inbitps =
846-
(subtract64(interfaces[i].inOctets, oldperfdata[i].inOctets, config->lastcheck, uptime) / (u64)config->lastcheck) *
847-
8ULL;
848-
interfaces[i].outbitps =
849-
(subtract64(interfaces[i].outOctets, oldperfdata[i].outOctets, config->lastcheck, uptime) / (u64)config->lastcheck) *
850-
8ULL;
826+
interfaces[i].inbitps = (subtract64(interfaces[i].inOctets, oldperfdata[i].inOctets, config->lastcheck, uptime) /
827+
(unsigned long long)config->lastcheck) *
828+
8ULL;
829+
interfaces[i].outbitps = (subtract64(interfaces[i].outOctets, oldperfdata[i].outOctets, config->lastcheck, uptime) /
830+
(unsigned long long)config->lastcheck) *
831+
8ULL;
851832
if (config->speed) {
852833
inload = (long double)interfaces[i].inbitps / ((long double)config->speed / 100L);
853834
outload = (long double)interfaces[i].outbitps / ((long double)config->speed / 100L);

snmp_bulkget.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void print64(struct counter64 *count64, const unsigned long *count32) {
9090
if (!(isZeroU64(count64))) {
9191
char buffer[I64CHARSZ + 1];
9292
printU64(buffer, count64);
93+
9394
#ifdef DEBUG
9495
printf("64:%s", buffer);
9596
#else
@@ -104,23 +105,23 @@ void print64(struct counter64 *count64, const unsigned long *count32) {
104105
}
105106
}
106107

107-
u64 convertto64(struct counter64 *val64, const unsigned long *val32) {
108-
u64 temp64;
108+
unsigned long long convertto64(struct counter64 *val64, const unsigned long *val32) {
109+
unsigned long long temp64;
109110

110111
if ((isZeroU64(val64))) {
111112
if (val32) {
112-
temp64 = (u64)(*val32);
113+
temp64 = (unsigned long long)(*val32);
113114
} else {
114115
temp64 = 0;
115116
}
116117
} else {
117-
temp64 = ((u64)(val64->high) << 32) + val64->low;
118+
temp64 = ((unsigned long long)(val64->high) << 32) + val64->low;
118119
}
119120

120121
return (temp64);
121122
}
122123

123-
u64 subtract64(u64 big64, u64 small64, unsigned int lastcheck, int uptime) {
124+
unsigned long long subtract64(unsigned long long big64, unsigned long long small64, unsigned int lastcheck, int uptime) {
124125
if (big64 < small64) {
125126
/* either the device was reset or the counter overflowed
126127
*/
@@ -300,7 +301,7 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, char *prefi
300301
int plugins;
301302
int uptime_old;
302303
#endif
303-
u64 value = 0;
304+
unsigned long long value = 0;
304305
char *valstr;
305306

306307
/* first split at spaces */
@@ -370,10 +371,9 @@ int parse_perfdata(char *oldperfdatap, struct ifStruct *oldperfdata, char *prefi
370371
/*
371372
* fill the ifStruct with values
372373
*/
373-
void set_value(struct ifStruct *oldperfdata, char *interface, char *var, u64 value, int ifNumber, char *if_vars[]) {
374-
int i;
374+
void set_value(struct ifStruct *oldperfdata, char *interface, char *var, unsigned long long value, int ifNumber, char *if_vars[]) {
375375

376-
for (i = 0; i < ifNumber; i++) {
376+
for (int i = 0; i < ifNumber; i++) {
377377
if (strcmp(interface, oldperfdata[i].descr) == 0) {
378378
if (strcmp(var, if_vars[0]) == 0) {
379379
oldperfdata[i].inOctets = value;

0 commit comments

Comments
 (0)