reset /proc/net/dev?

Discussion in 'Linux Networking' started by Bob Tennent, Nov 19, 2014.

  1. Bob Tennent

    Bob Tennent Guest

    Is there any way to set the packet and byte counts to zero
    other than re-booting? ifdowning the interface doesn't do
    it.

    Bob T.
     
    Bob Tennent, Nov 19, 2014
    #1
    1. Advertisements

  2. Bob Tennent

    JT Guest

    If your kernel uses a module (mostly 'e100') you can try 'modprobe -r
    e100' to unload it - and 'modprobe e100' to load it again. You need to
    restart the interface manually after that (ifup).

    If network support is built-in in your kernel: sorry - no go (most of
    the times)


    JT
     
    JT, Nov 20, 2014
    #2
    1. Advertisements

  3. Bob Tennent

    Rick Jones Guest

    A colleague of mine wrote this some time ago. Depending on your
    reason for wanting to zero stats, it may suffice. Someone wrote a
    different one not too long ago but I can never remember who it was or
    where it is.

    rick jones

    /*
    * beforeafter
    *
    * SYNOPSIS
    *
    * beforeafter before_file after_file
    *
    * Description
    *
    * Subtract the numbers in before_file from the numbers in
    * after_file.
    *
    * Example
    *
    * # netstat -s > netstat.before
    * # run some test here
    * # netstat -s > netstat.after
    * # beforeafter netstat.before netstat.after
    *
    * Note
    *
    * The "long double" is usually implemented as the IEEE double
    * extended precision: its mantissa is _at_least_ 64 bits.
    * Therefore, "long double" should be able to handle 64-bit
    * integer numbers.
    */

    #include <stdio.h>
    #include <ctype.h>
    #include <unistd.h>
    #include <stdlib.h>

    char *USAGE = "before_file after_file";

    main(argc, argv)
    int argc;
    char *argv[];

    {
    FILE *fp1; /* "before" file */
    FILE *fp2; /* "after" file */
    char *fname1;
    char *fname2;
    int i;
    int c;
    int c2;
    int separator = 1;
    int separator2 = 1;
    unsigned int n1 = 0;
    unsigned int n2 = 0;
    long double d1 = 0.0;
    long double d2 = 0.0;
    long double delta = 0.0;
    long double p31;
    long double p32;
    long double p63;
    long double p64;

    /*
    * Checke # of arguments.
    */
    if (argc != 3) {
    long double x;

    fprintf(stderr, "Usage: %s %s\n", argv[0], USAGE);

    printf ("\n");
    printf (" Testing how many decimal digits can be handled ...\n");
    printf (" #digits #bits 1=pass, 0=fail\n");
    x = 999999999.0L -
    999999998.0L;
    printf ("%10s %6s %1.0Lf\n", "9", "~30", x);
    x = 999999999999999999.0L -
    999999999999999998.0L;
    printf ("%10s %6s %1.0Lf\n", "18", "~60", x);
    x = 999999999999999999999999999.0L -
    999999999999999999999999998.0L;
    printf ("%10s %6s %1.0Lf\n", "27", "~90", x);
    x = 999999999999999999999999999999.0L -
    999999999999999999999999999998.0L;
    printf ("%10s %6s %1.0Lf\n", "30", "~100", x);
    x = 999999999999999999999999999999999.0L -
    999999999999999999999999999999998.0L;
    printf ("%10s %6s %1.0Lf\n", "33", "~110", x);
    x = 999999999999999999999999999999999999.0L -
    999999999999999999999999999999999998.0L;
    printf ("%10s %6s %1.0Lf\n", "36", "~120", x);

    exit (1);
    }
    /*
    * Open files.
    */
    fname1 = argv[1];
    fname2 = argv[2];
    fp1 = fopen(fname1, "r");
    fp2 = fopen(fname2, "r");
    if (!fp1 || !fp2) {
    fprintf(stderr, "fp1 = %p fp2 = %p", fp1, fp2);
    perror ("Could not open files");
    exit (2);
    }
    /*
    * Prepare for 32-bit and 64-bit overflow check.
    */
    for (p31=1.0, i=0; i<31; i++) {
    p31 *= 2.0; /* 2^31 */
    }
    p32 = p31 * 2.0; /* 2^32 */
    p63 = p31 * p32; /* 2^63 */
    p64 = p63 * 2.0; /* 2^64 */

    /*
    * Parse.
    */
    while ((c = getc(fp1)) != EOF) {
    if (c==' ' || c=='\t' || c==':' || c=='(' || c=='\n') {
    printf("%c", c);
    separator = 1;
    } else if (!isdigit(c)) {
    printf("%c", c);
    separator = 0;
    } else {
    if (separator == 0) {
    printf("%c", c); /* this digit is a part of a word */
    continue;
    }
    n1 = c - '0';
    d1 = n1;
    while ((c = getc(fp1)) != EOF) {
    if (isdigit(c)) {
    n1 = c - '0';
    d1 = 10.0 * d1 + n1;
    } else {
    break;
    }
    }
    /*
    * Find the counterpart in the "after" file.
    */
    while ((c2 = getc(fp2)) != EOF) {
    if (c2==' ' || c2=='\t' || c2==':' || c2=='(' || c2=='\n') {
    separator2 = 1;
    } else if (!isdigit(c2)) {
    separator2 = 0;
    } else {
    if (separator2 == 0) {
    continue;
    }
    n2 = c2 - '0';
    d2 = n2;
    while ((c2 = getc(fp2)) != EOF) {
    if (isdigit(c2)) {
    n2 = c2 - '0';
    d2 = 10.0 * d2 + n2;
    } else {
    break;
    }
    }
    break;
    }
    }
    delta = d2 - d1;
    /*
    * Check a counter overflow. These checks are not perfect,
    * but it should work reasonably well when counters are
    * monotonically increasing.
    *
    * Although this program also handles the case where a
    * counter is decreasing, we don't check underflow of a
    * decreasing counter because we can not distinguish bitween
    * a 32-bit counter underflow and a normal 64-bit counter
    * increase.
    */
    if (d2 < p32 && d1 < p32) {
    if (delta < -p31) {
    /* most likely a 32-bit counter wrapped around */
    delta += p32;
    }
    } else if (d2 < p64 && d1 < p64) {
    if (delta < -p63) {
    /* most likely a 64-bit counter wrapped around */
    delta += p64;
    }
    }
    printf ("%1.0Lf", delta);
    if (c != EOF) {
    printf("%c", c);
    }
    }
    }
    }
     
    Rick Jones, Nov 20, 2014
    #3
  4. Bob Tennent

    Bob Tennent Guest

    Thanks. The reason for wanting to zero the counters is
    because I use xnetload to monitor interface use and it
    stops working when the numbers are too big. It accesses
    /proc/net/dev directly rather than using netstat. I may have
    to look at the source code for xnetload.

    Bob T.
     
    Bob Tennent, Nov 20, 2014
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.