From: Michael Stapelberg Date: Fri, 24 Oct 2008 19:05:12 +0000 (+0200) Subject: Implement getting current speed from network interfaces X-Git-Tag: 1.0~45 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=62f3fc6838bf0e59bb490b0da229649bf86d847c;p=i3%2Fi3status Implement getting current speed from network interfaces The network interface must be supported by ethtool, as its syscall is used for this. This is at least the case for e1000 cards. Unfortunately, getting the speed needs root privileges. Therefore, you have to enable it in configuration separately. --- diff --git a/config.c b/config.c index e8c95dc..1b6fbec 100644 --- a/config.c +++ b/config.c @@ -19,6 +19,7 @@ const char *wmii_path; const char *time_format; const char *battery_path; bool use_colors; +bool get_ethspeed; const char *wmii_normcolors = "#222222 #333333"; char order[MAX_ORDER][2]; const char **run_watches; @@ -161,6 +162,10 @@ int load_configuration(const char *configfile) { { use_colors = true; } + OPT("get_ethspeed") + { + get_ethspeed = true; + } OPT("normcolors") { wmii_normcolors = strdup(dest_value); diff --git a/config.h b/config.h index 9437ef2..fdd95a2 100644 --- a/config.h +++ b/config.h @@ -18,6 +18,7 @@ extern const char order[MAX_ORDER][2]; extern unsigned int interval; extern bool use_colors; +extern bool get_ethspeed; #endif char *glob_path(const char *path); diff --git a/wmiistatus.1 b/wmiistatus.1 index fe7e725..4a77a5f 100644 --- a/wmiistatus.1 +++ b/wmiistatus.1 @@ -55,6 +55,10 @@ Followed by a comma separated list of the modules (run, wlan, eth, battery, load .B color If specified, color mode is on. This affects WLAN which will be displayed red if WLAN is down and the run_watch system which will display the status in red/green. +.TP +.B get_ethspeed +Get current speed of the ethernet interface using the same mechanism as ethtool. You need to start wmiistatus with root privileges to use this. + .TP .B normcolors Specifies the colors for background/border in the same format (html colorcodes) as wmii's configuration, that is #222222 #333333 for example. diff --git a/wmiistatus.c b/wmiistatus.c index a7f0b7a..e99625a 100644 --- a/wmiistatus.c +++ b/wmiistatus.c @@ -50,6 +50,9 @@ #include #include #include +#include +#include + #define _IS_WMIISTATUS_C #include "wmiistatus.h" @@ -262,7 +265,7 @@ static char *get_wireless_info() { else (void)snprintf(part, sizeof(part), "W: down"); } else { - char *ip_address; + const char *ip_address; (void)snprintf(part, sizeof(part), "W: (%02d%%) ", quality); ip_address = get_ip_address(wlan_interface); strcpy(part+strlen(part), ip_address); @@ -311,10 +314,34 @@ static const char *get_ip_address(const char *interface) { static char *get_eth_info() { static char part[512]; const char *ip_address = get_ip_address(eth_interface); + int ethspeed = 0; + + if (get_ethspeed) { + struct ifreq ifr; + struct ethtool_cmd ecmd; + int fd, err; + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) + write_error_to_statusbar("Could not open socket"); + + ecmd.cmd = ETHTOOL_GSET; + (void)memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_data = (caddr_t)&ecmd; + (void)strcpy(ifr.ifr_name, eth_interface); + if ((err = ioctl(fd, SIOCETHTOOL, &ifr)) == 0) + ethspeed = ecmd.speed; + else write_error_to_statusbar("Could not get interface speed. Insufficient privileges?"); + + (void)close(fd); + } if (ip_address == NULL) (void)snprintf(part, sizeof(part), "E: down"); - else (void)snprintf(part, sizeof(part), "E: %s", ip_address); + else { + if (get_ethspeed) + (void)snprintf(part, sizeof(part), "E: %s (%d Mbit/s)", ip_address, ethspeed); + else (void)snprintf(part, sizeof(part), "E: %s", ip_address); + } return part; }