1

I want to collect stats from my DSL modem every 10 minutes. According to lots of websites it's best to use rrd for this. My modem(TD-W8968) struggles with SNMP so I've made an expect script to pull stats via telnet. The crontab for collection is already set up. My problem is that my stats output looks like this after cropping unnecessary lines with head etc.:

Status: Showtime
Last Retrain Reason:    0
Last initialization procedure status:   0
Max:    Upstream rate = 1080 Kbps, Downstream rate = 11128 Kbps
Channel:        FAST, Upstream rate = 512 Kbps, Downstream rate = 2048 Kbps

Link Power State:       L0
Mode:                   G.DMT 
TPS-TC:                 ATM Mode
Trellis:                ON
Line Status:            No Defect
Training Status:        Showtime
                Down            Up
SNR (dB):        21.8            12.0
Attn(dB):        26.0            15.0
Pwr(dBm):        5.1             4.0
2

You can use awk to handle situations like this. For example, to extract from your rawfile just the 2 SNR and 2 Attn numbers and give them to rrd in the order both downs, then both ups:

awk '/^SNR/  { snrdown = $3; snrup = $4; }
     /^Attn/ { attndown = $2; attnup = $3; }
     END     { data = sprintf("N:%s:%s:%s:%s", snrdown, attndown, snrup, attnup);
               system("rrdtool update " data " myrrdfile");
             }' <rawfile

The first awk line matches only the line beginning "SNR". By default awk splits lines into fields separated by whitespace. Fields are numbered from 1. So $3 is the field with 21.8 in this example. The value is saved in a variable we name arbitrarily.

Similarly, the 2nd awk line matches only the "Attn" line, but since there is no space between that and "(dB)" our first number is in field $2.

The END part is done after all lines have been read from the file rawfile. It creates a string of data for rrd (which needs something like N:21.8:26.0:...). Each %s in the format string is replaced by one of the variables following the format.

awk is well worth getting to know. It is very versatile.

  • Thanks :) Now I'm off to good old google to learn about rrdtool – Wilhelm Erasmus Sep 5 '15 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.