sites

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit ed32ab8516670b9692c11ce96dba52e8c8981620
parent 8e65e2c75438130ec785dc6646eae41112205285
Author: Silvan Jegen <s.jegen@gmail.com>
Date:   Thu,  6 Dec 2012 21:01:51 +0100

Add the netusage helper function for dwmstatus

Diffstat:
Adwm.suckless.org/dwmstatus/dwmstatus-netusage.c | 211+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdwm.suckless.org/dwmstatus/index.md | 1+
2 files changed, 212 insertions(+), 0 deletions(-)

diff --git a/dwm.suckless.org/dwmstatus/dwmstatus-netusage.c b/dwm.suckless.org/dwmstatus/dwmstatus-netusage.c @@ -0,0 +1,211 @@ +#define _BSD_SOURCE +#include <unistd.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <strings.h> +#include <sys/time.h> +#include <time.h> +#include <sys/types.h> +#include <sys/wait.h> + +#include <X11/Xlib.h> + +char *tzargentina = "America/Buenos_Aires"; +char *tzutc = "UTC"; +char *tzberlin = "Europe/Berlin"; + +static Display *dpy; + +char * +smprintf(char *fmt, ...) +{ + va_list fmtargs; + char *ret; + int len; + + va_start(fmtargs, fmt); + len = vsnprintf(NULL, 0, fmt, fmtargs); + va_end(fmtargs); + + ret = malloc(++len); + if (ret == NULL) { + perror("malloc"); + exit(1); + } + + va_start(fmtargs, fmt); + vsnprintf(ret, len, fmt, fmtargs); + va_end(fmtargs); + + return ret; +} + +void +settz(char *tzname) +{ + setenv("TZ", tzname, 1); +} + +int +parse_netdev(unsigned long long int *receivedabs, unsigned long long int *sentabs) +{ + char *buf; + char *eth0start; + static int bufsize; + FILE *devfd; + + buf = (char *) calloc(255, 1); + bufsize = 255; + devfd = fopen("/proc/net/dev", "r"); + + // ignore the first two lines of the file + fgets(buf, bufsize, devfd); + fgets(buf, bufsize, devfd); + + while (fgets(buf, bufsize, devfd)) { + if ((eth0start = strstr(buf, "eth0:")) != NULL) { + + // With thanks to the conky project at http://conky.sourceforge.net/ + sscanf(eth0start + 6, "%llu %*d %*d %*d %*d %*d %*d %*d %llu",\ + receivedabs, sentabs); + free(buf); + return 0; + } + } + free(buf); + return 1; +} + +char * +get_netusage() +{ + unsigned long long int oldrec, oldsent, newrec, newsent; + double downspeed, upspeed; + char *downspeedstr, *upspeedstr; + char *retstr; + int retval; + + downspeedstr = (char *) malloc(15); + upspeedstr = (char *) malloc(15); + retstr = (char *) malloc(42); + + retval = parse_netdev(&oldrec, &oldsent); + if (retval) { + fprintf(stdout, "Error when parsing /proc/net/dev file.\n"); + exit(1); + } + + sleep(1); + retval = parse_netdev(&newrec, &newsent); + if (retval) { + fprintf(stdout, "Error when parsing /proc/net/dev file.\n"); + exit(1); + } + + downspeed = (newrec - oldrec) / 1024.0; + if (downspeed > 1024.0) { + downspeed /= 1024.0; + sprintf(downspeedstr, "%.3f MB/s", downspeed); + } else { + sprintf(downspeedstr, "%.2f KB/s", downspeed); + } + + upspeed = (newsent - oldsent) / 1024.0; + if (upspeed > 1024.0) { + upspeed /= 1024.0; + sprintf(upspeedstr, "%.3f MB/s", upspeed); + } else { + sprintf(upspeedstr, "%.2f KB/s", upspeed); + } + sprintf(retstr, "down: %s up: %s", downspeedstr, upspeedstr); + + free(downspeedstr); + free(upspeedstr); + return retstr; +} + +char * +mktimes(char *fmt, char *tzname) +{ + char buf[129]; + time_t tim; + struct tm *timtm; + + bzero(buf, sizeof(buf)); + settz(tzname); + tim = time(NULL); + timtm = localtime(&tim); + if (timtm == NULL) { + perror("localtime"); + exit(1); + } + + if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) { + fprintf(stderr, "strftime == 0\n"); + exit(1); + } + + return smprintf("%s", buf); +} + +void +setstatus(char *str) +{ + XStoreName(dpy, DefaultRootWindow(dpy), str); + XSync(dpy, False); +} + +char * +loadavg(void) +{ + double avgs[3]; + + if (getloadavg(avgs, 3) < 0) { + perror("getloadavg"); + exit(1); + } + + return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); +} + +int +main(void) +{ + char *status; + char *avgs; + char *tmar; + char *tmutc; + char *tmbln; + char *netstats; + + if (!(dpy = XOpenDisplay(NULL))) { + fprintf(stderr, "dwmstatus: cannot open display.\n"); + return 1; + } + + for (;;sleep(90)) { + avgs = loadavg(); + tmar = mktimes("%H:%M", tzargentina); + tmutc = mktimes("%H:%M", tzutc); + tmbln = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzberlin); + netstats = get_netusage(); + + status = smprintf("[L: %s|N: %s|A: %s|U: %s|%s]", + avgs, netstats, tmar, tmutc, tmbln); + setstatus(status); + free(avgs); + free(netstats); + free(tmar); + free(tmutc); + free(tmbln); + free(status); + } + + XCloseDisplay(dpy); + + return 0; +} + diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md @@ -30,6 +30,7 @@ add them here as file or as code example. * [Support for ACPI battery status Linux](new-acpi-battery.c) * [Reading out a temperature from /sys](dwmstatus-temperature.c) +* [Reading eth0 up-, and downspeed from /proc/net](dwmstatus-netusage.c) Questions ---------