Пример #1
0
void each_sec(int x)
{
	static int cnt = 0;
	static struct timeval t1, t2;
	static int seconds = 0;
	double drift;
	static unsigned long long int oldrec, oldsent, newrec, newsent;
	
	if (bandwidth) {
		if (cnt == 0) {
			gettimeofday( &t1, NULL );
			if (parse_netdev(&newrec, &newsent, config.interface)) {
				fprintf(stderr, "Error when parsing /proc/net/dev file.\n");
				exit(1);
			}
			cnt++;
			alarm(1);
			return;
		}

		oldrec = newrec;
		oldsent = newsent;
		if (parse_netdev(&newrec, &newsent, config.interface)) {
			fprintf(stderr, "Error when parsing /proc/net/dev file.\n");
			exit(1);
		}

		if (cnt % 2 == 0) {		// Even
			gettimeofday( &t1, NULL );
			drift=(t1.tv_sec - t2.tv_sec) + ((t1.tv_usec - t2.tv_usec)/MICRO_PER_SECOND);
		} else {				// Odd
			gettimeofday( &t2, NULL );
			drift=(t2.tv_sec - t1.tv_sec) + ((t2.tv_usec - t1.tv_usec)/MICRO_PER_SECOND);
		}
		if (config.debug > 3) printf("%.6lf seconds elapsed\n", drift);

		downspeed = (newrec - oldrec) / drift / 128.0;		// Kbits = / 128; KBytes = / 1024
		upspeed = (newsent - oldsent) / drift / 128.0;		// Kbits = / 128; KBytes = / 1024
		
		cnt++;
	}

	if (++seconds == 60)
		seconds = 0;
	if (seconds % 30 == 0) every30s = true;

	if (config.debug > 3) printf("seconds: %d\n", seconds);
	alarm(1);
}
Пример #2
0
/**
 * The "pxebs" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Return status code
 */
static int pxebs_exec ( int argc, char **argv ) {
	struct pxebs_options opts;
	struct net_device *netdev;
	unsigned int pxe_type;
	int rc;

	/* Parse options */
	if ( ( rc = parse_options ( argc, argv, &pxebs_cmd, &opts ) ) != 0 )
		return rc;

	/* Parse net device name */
	if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
		return rc;

	/* Parse boot server type */
	if ( ( rc = parse_integer ( argv[ optind + 1 ], &pxe_type ) ) != 0 )
		return rc;

	/* Perform Boot Server Discovery */
	if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
		printf ( "Could not discover boot server on %s: %s\n",
			 netdev->name, strerror ( rc ) );
		return rc;
	}

	return 0;
}
Пример #3
0
char *
get_netusage(unsigned long long int *rec, unsigned long long int *sent)
{
	unsigned long long int newrec, newsent;
	newrec = newsent = 0;
	char downspeedstr[15], upspeedstr[15];
	static char retstr[42];
	int retval;

	retval = parse_netdev(&newrec, &newsent);
	if (retval) {
	    fprintf(stdout, "Error when parsing /proc/net/dev file.\n");
	    exit(1);
	}

	calculate_speed(downspeedstr, newrec, *rec);
	calculate_speed(upspeedstr, newsent, *sent);

	sprintf(retstr, "\x01\uf063 %s  \x02\uf062 %s", downspeedstr, upspeedstr);
	/*sprintf(retstr, "\uf175 %s \uf176 %s", downspeedstr, upspeedstr);*/

	*rec = newrec;
	*sent = newsent;
	return retstr;
}
Пример #4
0
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, "D: %s U: %s", downspeedstr, upspeedstr);

	free(downspeedstr);
	free(upspeedstr);
	return retstr;
}
Пример #5
0
int
main(void) {
	char *status;
	char *tmchicago;
	char *alsavolume;
	char *mpdinfo;
	char *mailinfo;
	char *networkinfo;
	char *layoutinfo;
	static unsigned long long int rec, sent;
	time_t count5min = time(NULL);

	/*
	if (!(dpy = XOpenDisplay(NULL))) {
		fprintf(stderr, "dwmstatus: cannot open display.\n");
		return 1;
	}
	*/

	xkblayoutSetup();

	parse_netdev(&rec,&sent);
	mailinfo = getMailCount();
	for (;;sleep(1)) {
		/* Put together the pieces of our status */
		tmchicago = mktimes("%a, %b %d %I:%M", tzchicago);
		alsavolume = get_volume();
		mpdinfo = getmpdstat();
		networkinfo = get_netusage(&rec,&sent);
		layoutinfo = getKeyboardLayout();

		if(runevery(&count5min, 300)) {
			free(mailinfo);
			mailinfo = getMailCount();
		}

		/* Build the status string from our pieces */
		status = smprintf("%s %s %s %s %s[%s%s%%%s] %s[%s%s%s]", mpdinfo, networkinfo, mailinfo, layoutinfo, colcyan, colgreen, alsavolume, colcyan, colcyan, colyellow, tmchicago, colcyan);

		/* Send it to the wm for display */
		setstatus(status);

		/* Clean up our pieces */
		free(layoutinfo);
		free(tmchicago);
		free(alsavolume);
		free(mpdinfo);
		free(networkinfo);
		free(status);
	}

	free(mailinfo);

	xkblayoutCleanup();

	XCloseDisplay(dpy);

	return 0;
}
Пример #6
0
char *
netusage(unsigned long long int *oldrec, unsigned long long int *oldsent)
{
    unsigned long long int newrec, newsent;
    newrec = newsent = 0;

    if (!parse_netdev(&newrec, &newsent)) {
        fprintf(stdout, "error parsing /proc/net/dev\n");
        exit(1);
    }

    return smprintf("↓ %s ↑ %s", cal_bytes(newrec-*oldrec), cal_bytes(newsent-*oldsent));
}
Пример #7
0
int
main(void)
{
    unsigned short i;
    char *tmtz, *net, *avgs, *root, *vol, *bat;
    tmtz = net = avgs = root = vol = bat = NULL;
    char *line;
    static unsigned long long rec = 0, sent = 0;

    if (!(dpy = XOpenDisplay(NULL))) {
        fprintf(stderr, "dwmstatus: cannot open display.\n");
        return 1;
    }

    parse_netdev(&rec, &sent);

    for (i = 0;; sleep(1), i++) {
        if (i % 10 == 0) {
            free(bat);
            bat = batstat();
        }
        if (i % 5 == 0) {
            free(avgs);
            free(root);
            free(vol);
            avgs = loadavg();
            root = getfree("/");
            vol = runcmd("amixer get PCM | grep -om1 '[0-9]*%'");
        }
        net = netusage(&rec, &sent);
        tmtz = mktimes("%a %b %d %T");

        line = smprintf("♪ %s ⚡ %s │ %s │ / %s │ %s │ %s",
                        vol, bat, net, root, avgs, tmtz);

        XStoreName(dpy, DefaultRootWindow(dpy), line);
        XSync(dpy, False);

        free(net);
        free(tmtz);
        free(line);

        if (i == 60)
            i = 0;
    }

    XCloseDisplay(dpy);

    return 0;
}
Пример #8
0
int
main(void)
{
	char *status = NULL;
	char *tmprs = NULL;
	char *avgs = NULL;
    time_t count60 = 0;
    char *uptm = NULL;
    char *vol = NULL;
	char *netstats = NULL;
    char *ram = NULL;
	static unsigned long long int rec, sent;

	if (!(dpy = XOpenDisplay(NULL))) {
		fprintf(stderr, "dwmstatus: cannot open display.\n");
		return 1;
	}

	parse_netdev(&rec, &sent);
	for (;;sleep(1)) {
	    /* checks every minutes */
	    if ( runevery(&count60, 60) )
        {
            free(tmprs);
            free(uptm);
            tmprs = mktimes("\x06\uf073 %b-%d   %I:%M  %p", tz);
            uptm = up();

            free(ram);
            ram = get_ram();
        }
        /* checks every second */
		avgs = loadavg();
        vol = get_volume();
		netstats = get_netusage(&rec, &sent);

		status = smprintf("%s%s %s %s%s%s",
				 netstats, avgs, ram, uptm, vol, tmprs);
		setstatus(status);
        free(vol);
		free(avgs);
		free(status);
	}

	XCloseDisplay(dpy);

	return 0;
}
Пример #9
0
char *
get_netusage(unsigned long long int *rec, unsigned long long int *sent) {
	unsigned long long int newrec, newsent;
	newrec = newsent = 0;
	char downspeedstr[15], upspeedstr[15];

	if (parse_netdev(&newrec, &newsent)) {
	    fprintf(stdout, "Error when parsing /proc/net/dev file.\n");
	    exit(1);
	}

	calculate_speed(downspeedstr, newrec, *rec);
	calculate_speed(upspeedstr, newsent, *sent);

	*rec = newrec;
	*sent = newsent;
	
	return smprintf("%s[ %s↓ %s%s %s| %s↑ %s%s%s ]", 
			colcyan, colyellow, colbyellow, downspeedstr, 
			colcyan,
			colyellow, colbyellow, upspeedstr, colcyan);
}
Пример #10
0
/**
 * Execute if<xxx> command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @v cmd		Command descriptor
 * @v payload		Command to execute
 * @v verb		Verb describing the action of the command
 * @ret rc		Return status code
 */
int ifcommon_exec ( int argc, char **argv,
                    struct command_descriptor *cmd,
                    int ( * payload ) ( struct net_device * ),
                    int stop_on_first_success ) {
    struct ifcommon_options opts;
    struct net_device *netdev;
    int rc;

    /* Parse options */
    if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
        return rc;

    if ( optind != argc ) {
        /* Treat arguments as a list of interfaces to try */
        while ( optind != argc ) {
            if ( ( rc = parse_netdev ( argv[optind++],
                                       &netdev ) ) != 0 ) {
                continue;
            }
            if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
                    stop_on_first_success ) {
                return 0;
            }
        }
    } else {
        /* Try all interfaces */
        rc = -ENODEV;
        for_each_netdev ( netdev ) {
            if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
                    stop_on_first_success ) {
                return 0;
            }
        }
    }

    return rc;
}