0

Is it possible to SET the interface statistics in Linux after it's been brought up? I'm dealing with rrdtool (mrtg) that gets upset by a daily ifdown and ifup which brings the interface counters back to zero. Ideally I would like to continue counting from where I left and setting the interface values to what they were before the interface went down seems to be the easiest path.

I checked writing to /sys/class/net/ax0/statistics/rx_packets but that gives a Permission Denied error.

netstat, ifup, ifconfig and friends don't seem to support changing these values either.

Anything else I can try?

captcha
  • 348
  • 4
  • 18
  • Those stats are collected by the kernel driver or kernel proper. It's unlikely you will be able to set those without hacking the kernel and/or drivers. – kaylum Jun 12 '20 at 10:24
  • Hmm.. not the answer I was hoping for. I might keep track of those stats in some external variables then. Thanks. – captcha Jun 12 '20 at 11:17
  • can't you write rx and tx from ifconfig to a file and subtract them from the current rx and tx reading? – Slawomir Dziuba Jun 12 '20 at 11:28
  • yeah, I think I'm gonna have to do something like that. Thanks. It just means I need to write some logic to detect when the interface got reset to zero. – captcha Jun 12 '20 at 11:36
  • see https://askubuntu.com/questions/348038/how-to-reset-ifconfig-counters – Slawomir Dziuba Jun 12 '20 at 11:37
  • @SlawomirDziuba Thanks, but I don't want to reset the counters, I want them set at a specific (non-zero) value. – captcha Jun 12 '20 at 13:28
  • Perhaps your question is really an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). If you need further help it may be worth posting a question about the underlying problem of "rrdtool (mrtg) that gets upset by a daily ifdown and ifup". – kaylum Jun 12 '20 at 22:52
  • @kaylum I'm sure it is, that's why I included references to rrdtool. I'm not too fussed from which field the solution comes. In the end everything is an XY problem. Sometimes however we just want a fix rather than academically deep-dive into what may be the most efficient solution. – captcha Jun 13 '20 at 23:31

1 Answers1

0

You can't set the kernel counters, no - but do you really need to?
MRTG will usually graph a rate, based on the difference between samples. So your MRTG/RRD will store packets-per-second values every cycle (usually 5min but maybe 1min). When your device resets the counters, then MRTG will see the value apparently go backwards - which will be discounted as out of range, so one failed sample. But, the next sample will work, and a new rate be given. If you're getting a big spike in the MRTG graph at the point of the reset, this will be due to an incorrect 'counter rollover' detection. You can prevent this by either setting the MRTG AbsMax setting (to prevent this high value from being valid) or (better) by using SNMPv2 counters (where a reset is more obvious). If you set your RRD file to have a large enough heartbeat and XFF, then this one missing sample will be interpolated, and so your graphs (which, remember, show the rate rather than the total) will continue to look fine. Should you need the total, it can be derived by sum(rate x interval) which is automatically done by the Routers2 frontend for MRTG/RRD.

Steve Shipway
  • 3,754
  • 3
  • 22
  • 39
  • Closer inspection of why I was seeing these spikes revealed that due to some unrelated error in my script, sometimes not all values were being fed to rrdtool. I wrote some code to detect this and fix one issue that way. For the interface resets I ended up using external variables to keep track of the counters and pick up from last time once the interface came up. I would still use the technique of setting interface counters if it were possible, but knowing that rrdtool can set AbsMax values is very useful to know, I might end up using that too. – captcha Jun 25 '20 at 04:52