1

I'm trying to create a simple monitoring system that will show a graph of sales that is linked entirely to data of bandwidth, CPU and Memory stats as they relate to sales. So if I sale 1 widget per 5 seconds, that will be contingent upon my bandwidth etc being above a certain threshold. So I've formulated a way to do the graph but I'm just having a time getting the stats. I can use any of a number of monitoring systems but I just need 1 line for bandwidth, CPU Usage, and Memory Usage once per second. I made a python script that gets all the IP information from all interfaces. But for if I had a script I could run from terminal that simply output a single line I could just pipe that to a file and then access the file to run my real time graph.

So for network I can use:

$ ifconfig

I pipe it into a json file for easy reference.

I can use:

$ free -m

for memory which I suppose I can pipe into a file as well

Then I could use:

$ cat /proc/loadavg

for CPU and again pipe that.

Is there an easier way to do this? So that I receive a single line for each command and can easily (more importantly quickly) send that information via Javascript or HTML5 FileReader to the graphing solution?

2 Answers 2

2

I don't think there's an easy way to aggregate the data that you want, but you could stick a bunch of commands together then pipe it out to whatever. I spent a little bit of time to come up with an example.

{
  printf '%s' '{"load":["'$(cut -d' ' --output-delimiter='","' -f-3 /proc/loadavg)
  printf '%s' '"],"net":{'
  tail -n+3 /proc/net/dev|awk -F' ' '{
    gsub(/:/,"");
    printf "\"%s\":{\"rxbytes\":\"%s\",\"rxpackets\":\"%s\",\"rxerrs\":\"%s\",\"rxdrop\":\"%s\",\"txbytes\":\"%s\",\"txpackets\":\"%s\",\"txerrs\":\"%s\",\"txdrop\":\"%s\"},",$1,$2,$3,$4,$5,$10,$11,$12,$13
  }'|sed 's/,$//'
  printf '%s' '},"mem":{'
  grep -E '^(MemTotal|MemFree|SwapTotal|SwapFree):' /proc/meminfo|tr 'A-Z' 'a-z'|awk -F' ' '{gsub(/:/,""); printf "\"%s\":\"%s\",",$1,$2}'|sed 's/,$//'
  printf '%s' '}}'
}

You could squish it down into this:

{ printf '%s' '{"load":["'$(cut -d' ' --output-delimiter='","' -f-3 /proc/loadavg); printf '%s' '"],"net":{'; tail -n+3 /proc/net/dev|awk -F' ' '{ gsub(/:/,""); printf "\"%s\":{\"rxbytes\":\"%s\",\"rxpackets\":\"%s\",\"rxerrs\":\"%s\",\"rxdrop\":\"%s\",\"txbytes\":\"%s\",\"txpackets\":\"%s\",\"txerrs\":\"%s\",\"txdrop\":\"%s\"},",$1,$2,$3,$4,$5,$10,$11,$12,$13 }'|sed 's/,$//'; printf '%s' '},"mem":{'; grep -E '^(MemTotal|MemFree|SwapTotal|SwapFree):' /proc/meminfo|tr 'A-Z' 'a-z'|awk -F' ' '{gsub(/:/,""); printf "\"%s\":\"%s\",",$1,$2}'|sed 's/,$//'; printf '%s' '}}'; }

That should output some (valid) JSON that looks like this:

{"load":["0.00","0.01","0.05"],"net":{"lo":{"rxbytes":"2524231953","rxpackets":"1381864","rxerrs":"0","rxdrop":"0","txbytes":"2524231953","txpackets":"1381864","txerrs":"0","txdrop":"0"},"eth0":{"rxbytes":"2093914043","rxpackets":"2859952","rxerrs":"0","rxdrop":"0","txbytes":"1774385937","txpackets":"1485252","txerrs":"0","txdrop":"0"}},"mem":{"memtotal":"1692584","memfree":"20248","swaptotal":"917500","swapfree":"916288"}}

You could then add | whatever after the close curly to pipe it to whatever.

7
  • Wow...that's pretty awesome. How did you come up with that? I'm new to bash scripts...I know just some simple command type stuff. Is there a super special book I can buy that will make me a bash super hero? Feb 2, 2013 at 6:03
  • @Verber Most books on bash are awful and full of errors. I have never found any bash books that are anything above OK. If you want a solid reference, I highly recommend the BashGuide, and Bash Hackers.
    – Chris Down
    Feb 2, 2013 at 7:13
  • Ok thanks for the suggestions...I almost just went credit card crazy on Amazon buying books Feb 2, 2013 at 7:52
  • Man! This forum is so much nicer than Stack Overflow...No offense to anyone here but on SO it seems like I always have guys who have severe deficiencies that they like to take out on others. Feb 2, 2013 at 8:03
  • Ya, Chris is right. Most BASH books kinda suck. In addition to the sites he gave, I find myself referring to tldp.org/LDP/abs/html a ton... and lots of Googleing. That script I gave you is more string processing than scripting - if you find yourself wanting to manipulate data a lot you should look into learning a little bit of sed/awk/Perl/whatever the hot language of the day is. That thing up there is a freaking eye sore because of the JSON formatting. :) Feb 2, 2013 at 8:05
0

You might want to look into i3status. It's not really designed for this purpose, but it sounds like it would fulfil your requirements quite nicely.

5
  • Hmm. Are you looking for a command that will concatenate all that information into a single line (like comma delimited), or are you looking for a way to get all that data from a single command in a normalized way? Feb 1, 2013 at 21:29
  • I'm looking for a single command in a normalized way: 1 for bandwidth, 1 for CPU usage, 1 for Memory usuage. I will be making calls to each of these individually so it's best to keep them separate. Feb 2, 2013 at 5:39
  • I did take a look at i3status...looks pretty good. Is there any way to only call it once? Or will it just clip along by itself until you stop it? Feb 2, 2013 at 6:02
  • @Verber i3status | head -1
    – Chris Down
    Feb 2, 2013 at 7:11
  • ahhh! that works. The command below actually is what I will need. Easy to grab it with JSON, using the new HTML File API...then into flotr2 should work awesome. I'll throw up a little post when I'm done. Feb 2, 2013 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .