{"id":228,"date":"2006-10-12T15:16:48","date_gmt":"2006-10-12T12:16:48","guid":{"rendered":"http:\/\/www.void.gr\/kargig\/blog\/?p=228"},"modified":"2006-10-12T18:57:17","modified_gmt":"2006-10-12T15:57:17","slug":"netroute2-offline-stats-hack","status":"publish","type":"post","link":"https:\/\/www.void.gr\/kargig\/blog\/2006\/10\/12\/netroute2-offline-stats-hack\/","title":{"rendered":"Netroute2 stats with rrdtool"},"content":{"rendered":"<p>On my previous port about netroute2 I said that it was really easy to grab some stats with mrtg about line activity. This has a small drawback, you must have your collector\/server that will run mrtg always open so that it can constantly get the snmp statistics from the modem\/router. So my quest was to figure out a way to keep the statistics on the modem and be able to get them when I want and create the graphs that I like. This is not something mrtg can do, this is an rrdtool job \ud83d\ude42<\/p>\n<p>Most things said here are based on my previous post about netroute2: <a href=\"http:\/\/www.void.gr\/kargig\/blog\/2006\/10\/08\/intracom-netroute2-hacks\/\">Intracom netroute2 hacks<\/a>. You will definitely need to have read it before continuing&#8230;<\/p>\n<p>Tweaking the Netroute2:<br \/>\nThe first thing was to create a script to give me information about incoming and outgoing traffic on ppp0 interface. I also to export these stats on a file &#8220;parsable&#8221; by rrdtool. The following  script does exactly that:<\/p>\n<p><code><br \/>\n#!\/bin\/sh<br \/>\nifconfig=\"\/sbin\/ifconfig\"<br \/>\ngrep=\"\/bin\/grep\"<br \/>\ndate=\"\/bin\/date\"<br \/>\ncut=\"\/usr\/bin\/cut\"<br \/>\ntail=\"\/usr\/bin\/tail\"<br \/>\ndelim=\":\"<br \/>\nOUTPUT=\"\/tmp\/var\/for_rrd.log\"<br \/>\nRX=`$ifconfig ppp0 | $grep -A 6 ppp0 | $tail -n 1 | $cut -d\":\" -f2 | $cut -d\" \" -f1`<br \/>\nTX=`$ifconfig ppp0 | $grep -A 6 ppp0 | $tail -n 1 | $cut -d\":\" -f3 | $cut -d\" \" -f1`<br \/>\necho `$date +%s`\"$delim$RX$delim$TX\" >> $OUTPUT<br \/>\n<\/code><\/p>\n<p>What this script does is parse the output of ifconfig ppp0, find the number of bytes for incoming and outgoing and store them to \/tmp\/var\/for_rrd.log. Remember that netroute2 has \/tmp writable, so we can use\/abuse it \ud83d\ude42<br \/>\nThe output of the script is something like this:<\/p>\n<blockquote><p>\nTIMESTAMP:OUTGOING:INCOMING<\/p>\n<p>1160651881:7938452:1310405\n<\/p><\/blockquote>\n<p>So now we can have the stats stored someplace. It&#8217;s time to tell netroute2&#8217;s cron to do it every X minutes.<br \/>\n<code><br \/>\n# cat \/etc\/cron.d\/cron_rrdscript<br \/>\n*\/1 * * * * root \/etc\/rrdscript.sh &gt; \/dev\/null 2&gt;&1<br \/>\n<\/code><\/p>\n<p>Now, every 1 minute our log is going to be filled with our traffic.  You did remember to run \/etc\/init.d\/checkpoint &#8230; didn&#8217;t you ? If you don&#8217;t know why you should do it&#8230;DON&#8217;T continue reading on!!! Go back and read this:   <a href=\"http:\/\/www.void.gr\/kargig\/blog\/2006\/10\/08\/intracom-netroute2-hacks\/\">Intracom netroute2 hacks<\/a><\/p>\n<p>RRDTool time now&#8230;<br \/>\nI suppose you have already installed rrdtool on your favorite distro somehow (emerge,apt-get,rpm,blahblahblah).  First thing we need to do is create an rrd database to keep our stats. Save the following in create_netroute2_rate.txt<br \/>\n<code><br \/>\nrrdtool create netroute_rate.rrd --start 1159455804 \\<br \/>\nDS:IN:COUNTER:600:0:12500000 \\<br \/>\nDS:OUT:COUNTER:600:0:12500000 \\<br \/>\n--step 60 \\<br \/>\nRRA:AVERAGE:0.5:1:2000 \\<br \/>\nRRA:AVERAGE:0.5:6:2000 \\<br \/>\nRRA:AVERAGE:0.5:24:2000 \\<br \/>\nRRA:AVERAGE:0.5:288:2000 \\<br \/>\nRRA:MAX:0.5:1:2000 \\<br \/>\nRRA:MAX:0.5:6:2000 \\<br \/>\nRRA:MAX:0.5:24:2000 \\<br \/>\nRRA:MAX:0.5:288:2000<br \/>\n<\/code><\/p>\n<p><strong>BIG FAT WARNING!! <\/strong>You <strong>HAVE<\/strong> to change the timestamp &#8211;start 1159455804 with the current timestamp if you want to get accurate stats. 1159455804 was when I started getting my stats. You MUST change this with your current one. The command &#8220;date +%s&#8221; is your friend in this&#8230;<\/p>\n<p>Now execute sth like this: &#8220;\/bin\/sh create_netroute2_rate.txt&#8221;. You should have a beautiful netroute_rate.rrd file inside your current dir. Time to fill up this database with information from the file &#8220;for_rrd.log&#8221;.<br \/>\nIf you don&#8217;t have yet public key authentication with your netroute2, now is the crucial time to do it. Paragraph F) of my previous post (<a href=\"http:\/\/www.void.gr\/kargig\/blog\/2006\/10\/08\/intracom-netroute2-hacks\/\">Intracom netroute2 hacks<\/a>) is exactly that.<\/p>\n<p>(timelapse)<\/p>\n<p>I am supposing you already did it&#8230;To check if it&#8217;s working issue something like this:<br \/>\n<code>ssh -l root -p 22 PUT.IP.OF.NETROUTE2.HERE uname -a<\/code><br \/>\nHopefully you&#8217;ll get prompted with something like: &#8220;Linux netroute- 2.4.30-netta2 #2 Tue Mar 21 10:51:17 EET 2006 ppc unknown&#8221;. If not check again what went wrong&#8230;<\/p>\n<p>Now a command like the following will grab &#8220;for_rrd.log&#8221; from netroute2 and put it in \/var\/rrds\/ (that&#8217;s the dir I am using to store my rrd databases, logs, scripts, etc)<br \/>\n<code>scp root@PUT.IP.OF.NETROUTE2.HERE:\/tmp\/var\/for_rrd.log \/var\/rrds\/for_rrd.log<\/code><\/p>\n<p>Now..parsing time! This is my netroute-rrd.sh:<br \/>\n<code><br \/>\n#!\/bin\/sh<br \/>\ncd \/var\/rrds\/<br \/>\nNEWFILE=\"for_rrd.log\"<br \/>\nOLDFILE=\"for_rrd.log.old\"<br \/>\nif [ ! -f $NEWFILE ]; then<br \/>\n        echo \"No new file found\"<br \/>\n        exit 1<br \/>\nfi<br \/>\ndiff $OLDFILE $NEWFILE | grep -v \",\" | sed 's\/^>[ \\t]\\+\/\/' > to_parse<br \/>\ncat to_parse |while read line;<br \/>\ndo<br \/>\n  echo \"${line}\";<br \/>\n  rrdtool update netroute_rate.rrd $line<br \/>\n  echo \"$?\"<br \/>\ndone<br \/>\nrm -f to_parse<br \/>\nmv for_rrd.log for_rrd.log.old<br \/>\n<\/code><\/p>\n<p>If you execute this script it will compare this script with the older one you had downloaded, diff them and parse the output with rrdtool. I do that to ensure that rrdtool only parses what was not parsed before. It&#8217;s really stupid to parse the log file everytime from the beggining&#8230;<\/p>\n<p>Now you have your database filled up with values&#8230;so what ? how can you see the results ? With a script that takes advantage of rrdgraph \ud83d\ude42 This script is quite big, more than 150lines so I will not copy\/paste it here but I will provide you with a tarball of it and all the previously mentioned scripts ment to be stored on your box.<br \/>\n<a href='http:\/\/void.gr\/kargig\/blog\/wp-content\/netroute2scripts.tar.gz' title=''>Netroute2 RRDTool Scripts<\/a><\/p>\n<p>Combine all these together with a cron job from your pc like the following:<br \/>\n*\/5 * * * * scp root@PUT.IP.OF.NETROUTE2.HERE:\/tmp\/var\/for_rrd.log \/var\/rrds\/for_rrd.log ; \/etc\/scripts\/netroute-rrd.sh; \/etc\/scripts\/netroute-rrd-graph.sh<\/p>\n<p>and you will have a \/var\/rrds\/index.html with 5 graphs: Hourly, Daily, Weekly, Monthly and Yearly.<\/p>\n<p>Now you can monitor your Internet traffic even when your stats collector is offline.<br \/>\nThere is a chance that you will get some spikes when netroute2 reboots. To eliminate them read this: <a href=\"http:\/\/www.void.gr\/kargig\/blog\/2006\/01\/15\/howto-remove-spikes-from-rrd-graphs\/\">HOWTO remove spikes from RRD graphs<\/a><\/p>\n<p>If you are greek and want to know some more stuff about rrdtool there&#8217;s a tutorial here: <a href=\"http:\/\/ilug.void.gr\/forum\/viewtopic.php?id=23\">RRD [HOWTO]<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>On my previous port about netroute2 I said that it was really easy to grab some stats with mrtg about line activity. This has a small drawback, you must have your collector\/server that will run mrtg always open so that it can constantly get the snmp statistics from the modem\/router. So my quest was to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"footnotes":""},"categories":[3,8],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-linux","category-networking"],"aioseo_notices":[],"views":6178,"_links":{"self":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":0,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}