int main(int argc, char **argv) { int i; struct glibtop_netlist netlist; char **ifnames = glibtop_get_netlist(&netlist); for (i = 0; i < netlist.number; i++) { struct glibtop_netload netload; glibtop_get_netload(&netload, ifnames[i]); printf("%s\n", ifnames[i]); printf(" %lu, %lu\n", netload.bytes_in, netload.bytes_out); } struct glibtop_cpu cpu; glibtop_get_cpu(&cpu); printf("%lu, %lu, %lu\n", cpu.user, cpu.sys, cpu.nice); struct glibtop_mem mem; glibtop_get_mem(&mem); printf("%lu, %lu\n", mem.total, mem.used); struct glibtop_swap swap; glibtop_get_swap(&swap); printf("%lu, %lu\n", swap.total, swap.used); struct glibtop_conntrack conntrack; glibtop_get_conntrack(&conntrack); printf("%lu, %lu\n", conntrack.tcp_conns, conntrack.udp_conns); struct glibtop_wireless wireless; glibtop_get_wireless(&wireless, "wlan0"); printf("%ld, %ld\n", wireless.signal_dbm, wireless.noise_dbm); return 0; }
void device_info_fill(DeviceInfo *device_info) { glibtop_netload netload; glibtop_get_netload(&netload, device_info->name); device_info->tx = netload.bytes_out; device_info->rx = netload.bytes_in; device_info->up = (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_UP) ? TRUE : FALSE); device_info->running = (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_RUNNING) ? TRUE : FALSE); if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_LOOPBACK)) { device_info->type = DEVICE_TYPE_LOOPBACK; } else if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_WIRELESS)) { device_info->type = DEVICE_TYPE_WIRELESS; } else if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_POINTOPOINT)) { if (g_str_has_prefix(device_info->name, "plip")) { device_info->type = DEVICE_TYPE_PLIP; } else if (g_str_has_prefix(device_info->name, "sl")) { device_info->type = DEVICE_TYPE_SLIP; } else { device_info->type = DEVICE_TYPE_PPP; } } else { device_info->type = DEVICE_TYPE_ETHERNET; } }
static void pprint_get_netload(const char *iface) { glibtop_netload buf; glibtop_get_netload(&buf, iface); HEADER_PPRINT(glibtop_get_netload); printf("glibtop_get_netload (iface = \"%s\")\n", iface); PPRINT(flags, "%#llx"); FOOTER_PPRINT(); }
static PyObject* gtop_netload(PyObject *self, PyObject *args) { glibtop_netload buf; union { char ip4[INET_ADDRSTRLEN]; char ip6[INET6_ADDRSTRLEN]; char hw [32]; /* 8 * 2 + 7 * 1 ... 32 is enough :) */ } addr; const char *iface; PyObject *d; if(!PyArg_ParseTuple(args, "s", &iface)) return NULL; glibtop_get_netload(&buf, iface); d = PyDict_New(); my_dict_add_and_decref(d, "if_flags", PyL_ULL(buf.if_flags)); my_dict_add_and_decref(d, "mtu", PyL_UL(buf.mtu)); my_dict_add_and_decref(d, "subnet", PyS_S(inet_ntop(AF_INET, &buf.subnet, addr.ip4, sizeof addr.ip4))); my_dict_add_and_decref(d, "address", PyS_S(inet_ntop(AF_INET, &buf.address, addr.ip4, sizeof addr.ip4))); my_dict_add_and_decref(d, "packets_in", PyL_ULL(buf.packets_in)); my_dict_add_and_decref(d, "packets_out", PyL_ULL(buf.packets_out)); my_dict_add_and_decref(d, "packets_total", PyL_ULL(buf.packets_total)); my_dict_add_and_decref(d, "bytes_in", PyL_ULL(buf.bytes_in)); my_dict_add_and_decref(d, "bytes_out", PyL_ULL(buf.bytes_out)); my_dict_add_and_decref(d, "bytes_total", PyL_ULL(buf.bytes_total)); my_dict_add_and_decref(d, "errors_in", PyL_ULL(buf.errors_in)); my_dict_add_and_decref(d, "errors_out", PyL_ULL(buf.errors_out)); my_dict_add_and_decref(d, "errors_total", PyL_ULL(buf.errors_total)); my_dict_add_and_decref(d, "collisions", PyL_ULL(buf.collisions)); my_dict_add_and_decref(d, "prefix6", PyS_S(inet_ntop(AF_INET6, buf.prefix6, addr.ip6, sizeof addr.ip6))); my_dict_add_and_decref(d, "address6", PyS_S(inet_ntop(AF_INET6, buf.address6, addr.ip6, sizeof addr.ip6))); my_dict_add_and_decref(d, "scope6", PyI_L(buf.scope6)); my_dict_add_and_decref(d, "hwaddress", PyS_S(hwaddress_format_for_display(&buf, addr.hw, sizeof addr.hw))); return _struct_new(d); }
static gboolean info_nic_update_stats (gpointer data) { Netinfo *info = data; GtkTreeModel *model; gchar rx_pkt[10], rx_error[10]; gchar tx_pkt[10], tx_error[10]; gchar collisions[10]; const gchar *nic; gchar *text_tx_bytes, *text_rx_bytes; glibtop_netload netload; g_return_val_if_fail (info != NULL, FALSE); model = gtk_combo_box_get_model (GTK_COMBO_BOX (info->combo)); nic = info_get_nic (info); if (!nic) return FALSE; glibtop_get_netload (&netload, nic); text_rx_bytes = util_legible_bytes (netload.bytes_in); text_tx_bytes = util_legible_bytes (netload.bytes_out); g_sprintf (rx_pkt, "%lld", netload.packets_in); g_sprintf (tx_pkt, "%lld", netload.packets_out); g_sprintf (rx_error, "%lld", netload.errors_in); g_sprintf (tx_error, "%lld", netload.errors_out); g_sprintf (collisions, "%lld", netload.collisions); gtk_label_set_text (GTK_LABEL (info->tx_bytes), text_tx_bytes); gtk_label_set_text (GTK_LABEL (info->tx), tx_pkt); gtk_label_set_text (GTK_LABEL (info->tx_errors), tx_error); gtk_label_set_text (GTK_LABEL (info->rx_bytes), text_rx_bytes); gtk_label_set_text (GTK_LABEL (info->rx), rx_pkt); gtk_label_set_text (GTK_LABEL (info->rx_errors), rx_error); gtk_label_set_text (GTK_LABEL (info->collisions), collisions); g_free (text_tx_bytes); g_free (text_rx_bytes); return TRUE; }
static void collect_data(NetData* data) { //swap buffers around double* current = data->last; double* last = data->last = data->current; data->current = current; //zero the current data memset(current, 0, sizeof(double) * DATA_FIELDS); size_t i = 0; while (i != data->num_interfaces) { glibtop_netload buf; glibtop_get_netload(&buf, data->interfaces[i]); if (buf.if_flags & (1L << GLIBTOP_IF_FLAGS_LOOPBACK)) { //add all data to local data collection add_data(¤t[NET_LOCAL], buf.bytes_total); } else { add_data(¤t[NET_IN], buf.bytes_in); add_data(¤t[NET_OUT], buf.bytes_out); } ++i; } //compute the rates i = 0; while (i != DATA_FIELDS) { data->rate[i] = current[i] - last[i]; ++i; } }
gboolean is_dummy_device(const char* device) { glibtop_netload netload; glibtop_get_netload(&netload, device); if (netload.if_flags & (1 << GLIBTOP_IF_FLAGS_LOOPBACK)) return TRUE; /* Skip interfaces without any IPv4/IPv6 address (or those with only a LINK ipv6 addr) However we need to be able to exclude these while still keeping the value so when they get online (with NetworkManager for example) we don't get a suddent peak. Once we're able to get this, ignoring down interfaces will be possible too. */ if (!(netload.flags & (1 << GLIBTOP_NETLOAD_ADDRESS6) && netload.scope6 != GLIBTOP_IF_IN6_SCOPE_LINK) && !(netload.flags & (1 << GLIBTOP_NETLOAD_ADDRESS))) return TRUE; return FALSE; }
void get_device_info(const char *device, DevInfo *devinfo) { glibtop_netload netload; guint8 *hw; g_assert(device); memset(devinfo, 0, sizeof *devinfo); devinfo->name = g_strdup(device); devinfo->type = DEV_UNKNOWN; glibtop_get_netload(&netload, device); devinfo->tx = netload.bytes_out; devinfo->rx = netload.bytes_in; devinfo->up = (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_UP) ? TRUE : FALSE); devinfo->running = (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_RUNNING) ? TRUE : FALSE); devinfo->ip = format_ipv4(netload.address); devinfo->netmask = format_ipv4(netload.subnet); devinfo->ipv6 = format_ipv6(netload.address6); devinfo->qual = 0; devinfo->essid = NULL; hw = netload.hwaddress; if (hw[6] || hw[7]) { devinfo->hwaddr = g_strdup_printf( "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", hw[0], hw[1], hw[2], hw[3], hw[4], hw[5], hw[6], hw[7]); } else { devinfo->hwaddr = g_strdup_printf( "%02X:%02X:%02X:%02X:%02X:%02X", hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]); } /* stolen from gnome-applets/multiload/linux-proc.c */ if(netload.if_flags & (1L << GLIBTOP_IF_FLAGS_LOOPBACK)) { devinfo->type = DEV_LO; } #ifdef HAVE_IW else if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_WIRELESS)) { devinfo->type = DEV_WIRELESS; get_wireless_info (devinfo); } #endif /* HAVE_IW */ else if(netload.if_flags & (1L << GLIBTOP_IF_FLAGS_POINTOPOINT)) { if (g_str_has_prefix(device, "plip")) { devinfo->type = DEV_PLIP; } else if (g_str_has_prefix(device, "sl")) { devinfo->type = DEV_SLIP; } else { devinfo->type = DEV_PPP; } get_ptp_info(devinfo); } else { devinfo->type = DEV_ETHERNET; } }
void info_get_nic_information (const gchar *nic, Netinfo *info) { GtkTreeModel *model; GtkTreeIter iter; gchar dst[INFO_ADDRSTRLEN]; InfoIpAddr *ip; gint prefix; struct in_addr addr, subnet; gchar *address_string, *subnet_string; gchar address6_string[INET6_ADDRSTRLEN]; glibtop_netload netload; #ifdef __linux__ mii_data_result data; #endif gtk_label_set_text (GTK_LABEL (info->hw_address), NOT_AVAILABLE); gtk_label_set_text (GTK_LABEL (info->mtu), NOT_AVAILABLE); gtk_label_set_text (GTK_LABEL (info->state), NOT_AVAILABLE); gtk_label_set_text (GTK_LABEL (info->multicast), NOT_AVAILABLE); gtk_label_set_text (GTK_LABEL (info->link_speed), NOT_AVAILABLE); glibtop_get_netload (&netload, nic); /* IPv6 */ /* FIXME: It shows only one IPv6 address. Bug #563768 */ inet_ntop (AF_INET6, netload.address6, address6_string, INET6_ADDRSTRLEN); prefix = info_ip6_masklen (netload.prefix6); ip = g_new0 (InfoIpAddr, 1); ip->ip_addr = g_strdup (address6_string); ip->ip_prefix = g_strdup_printf ("%d", prefix); ip->ip_bcast = g_strdup (""); switch (netload.scope6) { case GLIBTOP_IF_IN6_SCOPE_LINK: ip->ip_scope = g_strdup ("Link"); break; case GLIBTOP_IF_IN6_SCOPE_SITE: ip->ip_scope = g_strdup ("Site"); break; case GLIBTOP_IF_IN6_SCOPE_GLOBAL: ip->ip_scope = g_strdup ("Global"); break; case GLIBTOP_IF_IN6_SCOPE_HOST: ip->ip_scope = g_strdup ("Host"); break; case GLIBTOP_IF_IN6_SCOPE_UNKNOWN: ip->ip_scope = g_strdup (_("Unknown")); break; default: ip->ip_scope = g_strdup (_("Unknown")); break; } model = gtk_tree_view_get_model (GTK_TREE_VIEW (info->list_ip_addr)); gtk_list_store_append (GTK_LIST_STORE (model), &iter); gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, "IPv6", 1, ip->ip_addr, 2, ip->ip_prefix, 3, ip->ip_bcast, 4, ip->ip_scope, -1); info_ip_addr_free (ip); /* IPv4 */ addr.s_addr = netload.address; subnet.s_addr = netload.subnet; address_string = g_strdup (inet_ntoa (addr)); subnet_string = g_strdup (inet_ntoa (subnet)); ip = g_new0 (InfoIpAddr, 1); ip->ip_addr = g_strdup (address_string); ip->ip_prefix = g_strdup (subnet_string); /* FIXME: Get the broadcast address: Bug #563765 */ ip->ip_bcast = g_strdup (""); model = gtk_tree_view_get_model (GTK_TREE_VIEW (info->list_ip_addr)); gtk_list_store_append (GTK_LIST_STORE (model), &iter); gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, "IPv4", 1, ip->ip_addr, 2, ip->ip_prefix, 3, ip->ip_bcast, 4, "", -1); g_free (address_string); g_free (subnet_string); info_ip_addr_free (ip); /* Get general information about the interface */ /* Get the Hardware Address */ if (netload.flags & (1L << GLIBTOP_NETLOAD_HWADDRESS)) { g_sprintf (dst, "%02x:%02x:%02x:%02x:%02x:%02x", (int) ((guchar *) &netload.hwaddress)[0], (int) ((guchar *) &netload.hwaddress)[1], (int) ((guchar *) &netload.hwaddress)[2], (int) ((guchar *) &netload.hwaddress)[3], (int) ((guchar *) &netload.hwaddress)[4], (int) ((guchar *) &netload.hwaddress)[5]); } else { g_sprintf (dst, NOT_AVAILABLE); } gtk_label_set_text (GTK_LABEL (info->hw_address), dst); /* Get the interface's Maximum Transfer Unit */ g_sprintf (dst, "%d", netload.mtu); gtk_label_set_text (GTK_LABEL (info->mtu), dst); bzero (dst, strlen(dst)); /* Get Flags to determine other properties */ /* Is the interface up? */ if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_UP)) { gtk_label_set_text (GTK_LABEL (info->state), _("Active")); } else { gtk_label_set_text (GTK_LABEL (info->state), _("Inactive")); } /* Is this a loopback device? */ if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_LOOPBACK)) { gtk_label_set_text (GTK_LABEL (info->hw_address), _("Loopback")); ip->ip_bcast = g_strdup (""); info_setup_configure_button (info, FALSE); } else { info_setup_configure_button (info, TRUE); } /* Does this interface supports multicast? */ if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_MULTICAST)) { gtk_label_set_text (GTK_LABEL (info->multicast), _("Enabled")); } else { gtk_label_set_text (GTK_LABEL (info->multicast), _("Disabled")); } /* Get the Point-To-Point address if any */ /* FIXME: Bug #563767 */ /* Get the link negotiation speed. Only available on Linux * systems, and lately only for with super user privileges * See Bug #387198 */ #ifdef __linux__ data = mii_get_basic (nic); if (data.has_data) { gtk_label_set_text (GTK_LABEL (info->link_speed), data.media); } #else gtk_label_set_text (GTK_LABEL (info->link_speed), NOT_AVAILABLE); #endif }
static void append_sysinfo() { struct glibtop_cpu cpu; struct glibtop_mem mem; struct glibtop_swap swap; struct glibtop_conntrack conntrack; #ifndef NETLOAD_IFACE struct glibtop_netlist netlist; int i; char **ifnames; #endif const time_t timestamp = time(NULL); if (timestamp == data_history[last_index].timestamp) return; glibtop_get_cpu(&cpu); glibtop_get_mem(&mem); glibtop_get_swap(&swap); glibtop_get_conntrack(&conntrack); uint64_t in = 0; uint64_t out = 0; #ifndef NETLOAD_IFACE ifnames = glibtop_get_netlist(&netlist); for (i = 0; i < netlist.number; ++i) #endif { struct glibtop_netload netload; #ifndef NETLOAD_IFACE glibtop_get_netload (&netload, ifnames[i]); if (netload.is_loopback) continue; /* Skip interfaces without any IPv4/IPv6 address (or those with only a LINK ipv6 addr) However we need to be able to exclude these while still keeping the value so when they get online (with NetworkManager for example) we don't get a suddent peak. Once we're able to get this, ignoring down interfaces will be possible too. */ if (!netload.has_addr || (netload.has_addr && netload.is_linklocal)) continue; #else glibtop_get_netload (&netload, NETLOAD_IFACE); #endif /* Don't skip interfaces that are down (GLIBTOP_IF_FLAGS_UP) to avoid spikes when they are brought up */ in += netload.bytes_in; out += netload.bytes_out; #ifndef NETLOAD_IFACE free(ifnames[i]); #endif } #ifndef NETLOAD_IFACE free(ifnames); #endif #ifdef IW_IFACE struct glibtop_wireless wireless; glibtop_get_wireless(&wireless, IW_IFACE); #endif pthread_mutex_lock(&info_mutex); last_index = (last_index+1) % MAX_HISTORY; data_history[last_index].cpu_total = (float)cpu.total; data_history[last_index].cpu_used = (float)(cpu.user + cpu.nice + cpu.sys); data_history[last_index].memory_usage = ((float)mem.used) / ((float)mem.total); data_history[last_index].swap_usage = swap.total == 0 ? 0.0f : ((float)swap.used) / ((float)swap.total); data_history[last_index].timestamp = timestamp; data_history[last_index].network_in = (float) in; data_history[last_index].network_out = (float) out; data_history[last_index].tcp_conns = (float) conntrack.tcp_conns; data_history[last_index].udp_conns = (float) conntrack.udp_conns; #ifdef IW_IFACE data_history[last_index].iw_signal = (float) wireless.signal_dbm; data_history[last_index].iw_noise = (float) wireless.noise_dbm; #else data_history[last_index].iw_signal = 0.0f; data_history[last_index].iw_noise = 0.0f; #endif pthread_mutex_unlock(&info_mutex); }
void GetNet (int Maximum, int data [4], LoadGraph *g) { enum Types { IN_COUNT = 0, OUT_COUNT = 1, LOCAL_COUNT = 2, COUNT_TYPES = 3 }; static int ticks = 0; static gulong past[COUNT_TYPES] = {0}; static AutoScaler scaler; gulong present[COUNT_TYPES] = {0}; guint i; gchar **devices; glibtop_netlist netlist; if(ticks == 0) { autoscaler_init(&scaler, 60, 501); } devices = glibtop_get_netlist(&netlist); for(i = 0; i < netlist.number; ++i) { int index; glibtop_netload netload; glibtop_get_netload(&netload, devices[i]); g_return_if_fail((netload.flags & needed_netload_flags) == needed_netload_flags); if (!(netload.if_flags & (1L << GLIBTOP_IF_FLAGS_UP))) continue; if (netload.if_flags & (1L << GLIBTOP_IF_FLAGS_LOOPBACK)) { /* for loopback in and out are identical, so only count in */ present[LOCAL_COUNT] += netload.bytes_in; continue; } /* * Do not include virtual devices (VPN, PPPOE...) to avoid * counting the same throughput several times. */ if (is_net_device_virtual(devices[i])) continue; present[IN_COUNT] += netload.bytes_in; present[OUT_COUNT] += netload.bytes_out; } g_strfreev(devices); netspeed_add(g->netspeed_in, present[IN_COUNT]); netspeed_add(g->netspeed_out, present[OUT_COUNT]); if(ticks < 2) /* avoid initial spike */ { ticks++; memset(data, 0, COUNT_TYPES * sizeof data[0]); } else { int delta[COUNT_TYPES]; int max; int total = 0; for (i = 0; i < COUNT_TYPES; i++) { /* protect against weirdness */ if (present[i] >= past[i]) delta[i] = (present[i] - past[i]); else delta[i] = 0; total += delta[i]; } max = autoscaler_get_max(&scaler, total); for (i = 0; i < COUNT_TYPES; i++) data[i] = rint (Maximum * (float)delta[i] / max); } //data[4] = Maximum - data[3] - data[2] - data[1] - data[0]; data[COUNT_TYPES] = Maximum; for (i = 0; i < COUNT_TYPES; i++) data[COUNT_TYPES] -= data[i]; memcpy(past, present, sizeof past); }
static void get_net (LoadGraph *g) { glibtop_netlist netlist; char **ifnames; guint32 i; guint64 in = 0, out = 0; GTimeVal time; unsigned din, dout; gchar *text1; ifnames = glibtop_get_netlist(&netlist); for (i = 0; i < netlist.number; ++i) { glibtop_netload netload; glibtop_get_netload (&netload, ifnames[i]); if (netload.if_flags & (1 << GLIBTOP_IF_FLAGS_LOOPBACK)) continue; /* Skip interfaces without any IPv4/IPv6 address (or those with only a LINK ipv6 addr) However we need to be able to exclude these while still keeping the value so when they get online (with NetworkManager for example) we don't get a suddent peak. Once we're able to get this, ignoring down interfaces will be possible too. */ if (not (netload.flags & (1 << GLIBTOP_NETLOAD_ADDRESS6) and netload.scope6 != GLIBTOP_IF_IN6_SCOPE_LINK) and not (netload.flags & (1 << GLIBTOP_NETLOAD_ADDRESS))) continue; /* Don't skip interfaces that are down (GLIBTOP_IF_FLAGS_UP) to avoid spikes when they are brought up */ in += netload.bytes_in; out += netload.bytes_out; } g_strfreev(ifnames); g_get_current_time (&time); if (in >= g->net.last_in && out >= g->net.last_out && g->net.time.tv_sec != 0) { float dtime; dtime = time.tv_sec - g->net.time.tv_sec + (float) (time.tv_usec - g->net.time.tv_usec) / G_USEC_PER_SEC; din = static_cast<unsigned>((in - g->net.last_in) / dtime); dout = static_cast<unsigned>((out - g->net.last_out) / dtime); } else { /* Don't calc anything if new data is less than old (interface removed, counters reset, ...) or if it is the first time */ din = 0; dout = 0; } g->net.last_in = in; g->net.last_out = out; g->net.time = time; net_scale(g, din, dout); gtk_label_set_text (GTK_LABEL (g->labels.net_in), procman::format_rate(din).c_str()); text1 = SI_gnome_vfs_format_file_size_for_display (in); gtk_label_set_text (GTK_LABEL (g->labels.net_in_total), text1); g_free (text1); gtk_label_set_text (GTK_LABEL (g->labels.net_out), procman::format_rate(dout).c_str()); text1 = SI_gnome_vfs_format_file_size_for_display (out); gtk_label_set_text (GTK_LABEL (g->labels.net_out_total), text1); g_free (text1); }