int NET_IF_IN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { net_stat_t ns; char if_name[MAX_STRING_LEN], mode[16]; if (num_param(param) > 2) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, if_name, sizeof(if_name))) return SYSINFO_RET_FAIL; if (0 != get_param(param, 2, mode, sizeof(mode))) *mode = '\0'; if (SYSINFO_RET_OK != get_net_stat(if_name, &ns)) return SYSINFO_RET_FAIL; if ('\0' == *mode || 0 == strcmp(mode, "bytes")) /* default parameter */ SET_UI64_RESULT(result, ns.ibytes); else if (0 == strcmp(mode, "packets")) SET_UI64_RESULT(result, ns.ipackets); else if (0 == strcmp(mode, "errors")) SET_UI64_RESULT(result, ns.ierr); else if (0 == strcmp(mode, "dropped")) SET_UI64_RESULT(result, ns.idrop); else return SYSINFO_RET_FAIL; return SYSINFO_RET_OK; }
int NET_IF_IN(AGENT_REQUEST *request, AGENT_RESULT *result) { char *if_name, *mode, *error; net_stat_t ns; 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_FAIL == get_net_stat(if_name, &ns, &error)) { SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; } if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "bytes")) SET_UI64_RESULT(result, ns.ibytes); else if (0 == strcmp(mode, "packets")) SET_UI64_RESULT(result, ns.ipackets); else if (0 == strcmp(mode, "errors")) SET_UI64_RESULT(result, ns.ierr); else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } return SYSINFO_RET_OK; }
int NET_IF_COLLISIONS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char if_name[MAX_STRING_LEN]; net_stat_t ns; int ret = SYSINFO_RET_OK; assert(result); init_result(result); if (num_param(param) > 1) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, if_name, sizeof(if_name))) return SYSINFO_RET_FAIL; if (SYSINFO_RET_OK == get_net_stat(if_name, &ns)) { SET_UI64_RESULT(result, ns.colls); } else ret = SYSINFO_RET_FAIL; return ret; }
/* deprecated, will rewrite this soon in update_net_stats() -hifi */ void update_wifi_stats() { struct net_stat *ns; struct ifaddrs *ifap, *ifa; struct ifmediareq ifmr; struct ieee80211_nodereq nr; struct ieee80211_bssid bssid; int s, ibssid; /* Get iface table */ if (getifaddrs(&ifap) < 0) { return; } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { ns = get_net_stat((const char *) ifa->ifa_name); s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); /* Get media type */ bzero(&ifmr, sizeof(ifmr)); strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ); if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) { close(s); return; } /* We can monitor only wireless interfaces * which are not in hostap mode */ if ((ifmr.ifm_active & IFM_IEEE80211) && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) { /* Get wi status */ memset(&bssid, 0, sizeof(bssid)); strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name)); ibssid = ioctl(s, SIOCG80211BSSID, &bssid); bzero(&nr, sizeof(nr)); bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr)); strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname)); if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) { ns->linkstatus = nr.nr_rssi; } } cleanup: close(s); } }
int NET_IF_IN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char if_name[MAX_STRING_LEN], mode[MAX_STRING_LEN]; net_stat_t ns; int ret = SYSINFO_RET_OK; assert(result); init_result(result); if (num_param(param) > 2) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, if_name, sizeof(if_name)) || *if_name == '\0') return SYSINFO_RET_FAIL; if (0 != get_param(param, 2, mode, sizeof(mode))) *mode = '\0'; /* default parameter */ if ('\0' == *mode) zbx_snprintf(mode, sizeof(mode), "bytes"); if (SYSINFO_RET_OK == get_net_stat(if_name, &ns)) { if (0 == strcmp(mode, "bytes")) { SET_UI64_RESULT(result, ns.ibytes); } else if (0 == strcmp(mode, "packets")) { SET_UI64_RESULT(result, ns.ipackets); } else if (0 == strcmp(mode, "errors")) { SET_UI64_RESULT(result, ns.ierr); } else ret = SYSINFO_RET_FAIL; } else ret = SYSINFO_RET_FAIL; return ret; }
int NET_IF_COLLISIONS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { net_stat_t ns; char if_name[MAX_STRING_LEN]; if (num_param(param) > 1) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, if_name, sizeof(if_name))) return SYSINFO_RET_FAIL; if (SYSINFO_RET_OK != get_net_stat(if_name, &ns)) return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, ns.colls); return SYSINFO_RET_OK; }
int NET_IF_COLLISIONS(AGENT_REQUEST *request, AGENT_RESULT *result) { char *if_name, *error; net_stat_t ns; 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_FAIL == get_net_stat(if_name, &ns, &error)) { SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, ns.colls); return SYSINFO_RET_OK; }
void update_net_stats() { int i; double delta; struct ifnet ifnet; struct ifnet_head ifhead; /* interfaces are in a tail queue */ u_long ifnetaddr; static struct nlist namelist[] = { { "_ifnet" }, { NULL } }; static kvm_t *nkd; if (!nkd_init) { nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); if (nkd == NULL) { warnx("cannot kvm_openfiles: %s", errbuf); warnx("maybe you need to setgid kmem this program?"); return; } else if (kvm_nlist(nkd, namelist) != 0) { warn("cannot kvm_nlist"); return; } else { nkd_init = 1; } } if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead, sizeof(ifhead)) < 0) { warn("cannot kvm_read"); return; } /* get delta */ delta = current_update_time - last_update_time; if (delta <= 0.0001) { return; } for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first; ifnet.if_list.tqe_next && i < 16; ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) { struct net_stat *ns; long long last_recv, last_trans; kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet)); ns = get_net_stat(ifnet.if_xname, NULL, NULL); ns->up = 1; last_recv = ns->recv; last_trans = ns->trans; if (ifnet.if_ibytes < ns->last_read_recv) { ns->recv += ((long long) 4294967295U - ns->last_read_recv) + ifnet.if_ibytes; } else { ns->recv += (ifnet.if_ibytes - ns->last_read_recv); } ns->last_read_recv = ifnet.if_ibytes; if (ifnet.if_obytes < ns->last_read_trans) { ns->trans += ((long long) 4294967295U - ns->last_read_trans) + ifnet.if_obytes; } else { ns->trans += (ifnet.if_obytes - ns->last_read_trans); } ns->last_read_trans = ifnet.if_obytes; ns->recv += (ifnet.if_ibytes - ns->last_read_recv); ns->last_read_recv = ifnet.if_ibytes; ns->trans += (ifnet.if_obytes - ns->last_read_trans); ns->last_read_trans = ifnet.if_obytes; ns->recv_speed = (ns->recv - last_recv) / delta; ns->trans_speed = (ns->trans - last_trans) / delta; } }
void update_net_stats() { struct net_stat *ns; double delta; long long r, t, last_recv, last_trans; struct ifaddrs *ifap, *ifa; struct if_data *ifd; /* get delta */ delta = current_update_time - last_update_time; if (delta <= 0.0001) { return; } if (getifaddrs(&ifap) < 0) { return; } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL); if (ifa->ifa_flags & IFF_UP) { struct ifaddrs *iftmp; ns->up = 1; last_recv = ns->recv; last_trans = ns->trans; if (ifa->ifa_addr->sa_family != AF_LINK) { continue; } for (iftmp = ifa->ifa_next; iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0; iftmp = iftmp->ifa_next) { if (iftmp->ifa_addr->sa_family == AF_INET) { memcpy(&(ns->addr), iftmp->ifa_addr, iftmp->ifa_addr->sa_len); } } ifd = (struct if_data *) ifa->ifa_data; r = ifd->ifi_ibytes; t = ifd->ifi_obytes; if (r < ns->last_read_recv) { ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r; } else { ns->recv += (r - ns->last_read_recv); } ns->last_read_recv = r; if (t < ns->last_read_trans) { ns->trans += (long long) 4294967295U - ns->last_read_trans + t; } else { ns->trans += (t - ns->last_read_trans); } ns->last_read_trans = t; /* calculate speeds */ ns->recv_speed = (ns->recv - last_recv) / delta; ns->trans_speed = (ns->trans - last_trans) / delta; } else { ns->up = 0; } } freeifaddrs(ifap); }