diff options
Diffstat (limited to 'src/libs/zbxsysinfo/netbsd/net.c')
-rw-r--r-- | src/libs/zbxsysinfo/netbsd/net.c | 243 |
1 files changed, 146 insertions, 97 deletions
diff --git a/src/libs/zbxsysinfo/netbsd/net.c b/src/libs/zbxsysinfo/netbsd/net.c index f92f1cdab3b..ee0f84ce2ca 100644 --- a/src/libs/zbxsysinfo/netbsd/net.c +++ b/src/libs/zbxsysinfo/netbsd/net.c @@ -20,6 +20,7 @@ #include "common.h" #include "sysinfo.h" #include "zbxjson.h" +#include "log.h" static struct nlist kernel_symbols[] = { @@ -34,7 +35,7 @@ static int get_ifdata(const char *if_name, zbx_uint64_t *ibytes, zbx_uint64_t *ipackets, zbx_uint64_t *ierrors, zbx_uint64_t *idropped, zbx_uint64_t *obytes, zbx_uint64_t *opackets, zbx_uint64_t *oerrors, zbx_uint64_t *tbytes, zbx_uint64_t *tpackets, zbx_uint64_t *terrors, - zbx_uint64_t *icollisions) + zbx_uint64_t *icollisions, char **error) { struct ifnet_head head; struct ifnet *ifp; @@ -45,89 +46,102 @@ static int get_ifdata(const char *if_name, int ret = SYSINFO_RET_FAIL; if (NULL == if_name || '\0' == *if_name) - return SYSINFO_RET_FAIL; + { + *error = zbx_strdup(NULL, "Network interface name cannot be empty."); + return FAIL; + } - if (NULL != (kp = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL))) /* requires root privileges */ + if (NULL == (kp = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL))) /* requires root privileges */ { - if (N_UNDF == kernel_symbols[IFNET_ID].n_type) - if (0 != kvm_nlist(kp, &kernel_symbols[0])) - kernel_symbols[IFNET_ID].n_type = N_UNDF; + *error = zbx_strdup(NULL, "Cannot obtain a descriptor to access kernel virtual memory."); + return FAIL; + } - if (N_UNDF != kernel_symbols[IFNET_ID].n_type) - { - len = sizeof(struct ifnet_head); + if (N_UNDF == kernel_symbols[IFNET_ID].n_type) + if (0 != kvm_nlist(kp, &kernel_symbols[0])) + kernel_symbols[IFNET_ID].n_type = N_UNDF; + + if (N_UNDF != kernel_symbols[IFNET_ID].n_type) + { + len = sizeof(struct ifnet_head); - if (kvm_read(kp, kernel_symbols[IFNET_ID].n_value, &head, len) >= len) + if (kvm_read(kp, kernel_symbols[IFNET_ID].n_value, &head, len) >= len) + { + len = sizeof(struct ifnet); + + /* if_ibytes; total number of octets received */ + /* if_ipackets; packets received on interface */ + /* if_ierrors; input errors on interface */ + /* if_iqdrops; dropped on input, this interface */ + /* if_obytes; total number of octets sent */ + /* if_opackets; packets sent on interface */ + /* if_oerrors; output errors on interface */ + /* if_collisions; collisions on csma interfaces */ + + if (ibytes) + *ibytes = 0; + if (ipackets) + *ipackets = 0; + if (ierrors) + *ierrors = 0; + if (idropped) + *idropped = 0; + if (obytes) + *obytes = 0; + if (opackets) + *opackets = 0; + if (oerrors) + *oerrors = 0; + if (tbytes) + *tbytes = 0; + if (tpackets) + *tpackets = 0; + if (terrors) + *terrors = 0; + if (icollisions) + *icollisions = 0; + + for (ifp = head.tqh_first; ifp; ifp = v.if_list.tqe_next) { - len = sizeof(struct ifnet); - - /* if_ibytes; total number of octets received */ - /* if_ipackets; packets received on interface */ - /* if_ierrors; input errors on interface */ - /* if_iqdrops; dropped on input, this interface */ - /* if_obytes; total number of octets sent */ - /* if_opackets; packets sent on interface */ - /* if_oerrors; output errors on interface */ - /* if_collisions; collisions on csma interfaces */ - - if (ibytes) - *ibytes = 0; - if (ipackets) - *ipackets = 0; - if (ierrors) - *ierrors = 0; - if (idropped) - *idropped = 0; - if (obytes) - *obytes = 0; - if (opackets) - *opackets = 0; - if (oerrors) - *oerrors = 0; - if (tbytes) - *tbytes = 0; - if (tpackets) - *tpackets = 0; - if (terrors) - *terrors = 0; - if (icollisions) - *icollisions = 0; - - for (ifp = head.tqh_first; ifp; ifp = v.if_list.tqe_next) + if (kvm_read(kp, (u_long)ifp, &v, len) < len) + break; + + if (0 == strcmp(if_name, v.if_xname)) { - if (kvm_read(kp, (u_long)ifp, &v, len) < len) - break; - - if (0 == strcmp(if_name, v.if_xname)) - { - if (ibytes) - *ibytes += v.if_ibytes; - if (ipackets) - *ipackets += v.if_ipackets; - if (ierrors) - *ierrors += v.if_ierrors; - if (idropped) - *idropped += v.if_iqdrops; - if (obytes) - *obytes += v.if_obytes; - if (opackets) - *opackets += v.if_opackets; - if (oerrors) - *oerrors += v.if_oerrors; - if (tbytes) - *tbytes += v.if_ibytes + v.if_obytes; - if (tpackets) - *tpackets += v.if_ipackets + v.if_opackets; - if (terrors) - *terrors += v.if_ierrors + v.if_oerrors; - if (icollisions) - *icollisions += v.if_collisions; - ret = SYSINFO_RET_OK; - } + if (ibytes) + *ibytes += v.if_ibytes; + if (ipackets) + *ipackets += v.if_ipackets; + if (ierrors) + *ierrors += v.if_ierrors; + if (idropped) + *idropped += v.if_iqdrops; + if (obytes) + *obytes += v.if_obytes; + if (opackets) + *opackets += v.if_opackets; + if (oerrors) + *oerrors += v.if_oerrors; + if (tbytes) + *tbytes += v.if_ibytes + v.if_obytes; + if (tpackets) + *tpackets += v.if_ipackets + v.if_opackets; + if (terrors) + *terrors += v.if_ierrors + v.if_oerrors; + if (icollisions) + *icollisions += v.if_collisions; + ret = SYSINFO_RET_OK; } } } - kvm_close(kp); + } + + kvm_close(kp); + + if (SYSINFO_RET_FAIL == ret) + { + *error = zbx_strdup(NULL, "Cannot find information for this network interface."); + return SYSINFO_RET_FAIL; } return ret; @@ -135,17 +149,24 @@ static int get_ifdata(const char *if_name, int NET_IF_IN(AGENT_REQUEST *request, AGENT_RESULT *result) { - char *if_name, *mode; + char *if_name, *mode, *error; zbx_uint64_t ibytes, ipackets, ierrors, idropped; if (2 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } if_name = get_rparam(request, 0); mode = get_rparam(request, 1); - if (SYSINFO_RET_OK != get_ifdata(if_name, &ibytes, &ipackets, &ierrors, &idropped, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + if (SYSINFO_RET_OK != get_ifdata(if_name, &ibytes, &ipackets, &ierrors, &idropped, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, &error)) + { + SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; + } if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bytes")) /* default parameter */ SET_UI64_RESULT(result, ibytes); @@ -156,24 +177,34 @@ int NET_IF_IN(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(mode, "dropped")) SET_UI64_RESULT(result, idropped); else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; + } return SYSINFO_RET_OK; } int NET_IF_OUT(AGENT_REQUEST *request, AGENT_RESULT *result) { - char *if_name, *mode; + char *if_name, *mode, *error; zbx_uint64_t obytes, opackets, oerrors; if (2 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } if_name = get_rparam(request, 0); mode = get_rparam(request, 1); - if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, &obytes, &opackets, &oerrors, NULL, NULL, NULL, NULL)) + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, &obytes, &opackets, &oerrors, NULL, NULL, + NULL, NULL, &error)) + { + SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; + } if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bytes")) /* default parameter */ SET_UI64_RESULT(result, obytes); @@ -182,24 +213,34 @@ int NET_IF_OUT(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(mode, "errors")) SET_UI64_RESULT(result, oerrors); else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; + } return SYSINFO_RET_OK; } int NET_IF_TOTAL(AGENT_REQUEST *request, AGENT_RESULT *result) { - char *if_name, *mode; + char *if_name, *mode, *error; zbx_uint64_t tbytes, tpackets, terrors; if (2 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } if_name = get_rparam(request, 0); mode = get_rparam(request, 1); - if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &tbytes, &tpackets, &terrors, NULL)) + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &tbytes, &tpackets, + &terrors, NULL, &error)) + { + SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; + } if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bytes")) /* default parameter */ SET_UI64_RESULT(result, tbytes); @@ -208,23 +249,33 @@ int NET_IF_TOTAL(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(mode, "errors")) SET_UI64_RESULT(result, terrors); else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; + } return SYSINFO_RET_OK; } int NET_IF_COLLISIONS(AGENT_REQUEST *request, AGENT_RESULT *result) { - char *if_name; + char *if_name, *error; zbx_uint64_t icollisions; if (1 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } if_name = get_rparam(request, 0); - if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &icollisions)) + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, &icollisions, &error)) + { + SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; + } SET_UI64_RESULT(result, icollisions); @@ -233,27 +284,25 @@ int NET_IF_COLLISIONS(AGENT_REQUEST *request, AGENT_RESULT *result) int NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { - int ret = SYSINFO_RET_FAIL, i; + int i; struct zbx_json j; struct if_nameindex *interfaces; + if (NULL == (interfaces = if_nameindex())) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); + return SYSINFO_RET_FAIL; + } + zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); - if (NULL != (interfaces = if_nameindex())) + for (i = 0; 0 != interfaces[i].if_index; i++) { - i = 0; - - while (0 != interfaces[i].if_index) - { - zbx_json_addobject(&j, NULL); - zbx_json_addstring(&j, "{#IFNAME}", interfaces[i].if_name, ZBX_JSON_TYPE_STRING); - zbx_json_close(&j); - i++; - } - - ret = SYSINFO_RET_OK; + zbx_json_addobject(&j, NULL); + zbx_json_addstring(&j, "{#IFNAME}", interfaces[i].if_name, ZBX_JSON_TYPE_STRING); + zbx_json_close(&j); } zbx_json_close(&j); @@ -262,5 +311,5 @@ int NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) zbx_json_free(&j); - return ret; + return SYSINFO_RET_OK; } |