Linux Usage Statistics Using Ruby Gem usagewatch

July 17, 2013

I recently had a Ruby project which needed detailed Linux Usage Statistics, such as CPU, DISK, TCP/UDP, Load, Bandwidth, Disk I/O, and Memory. So I authored and open sourced under (MIT License) a Ruby module usagewatch which provides you an easy way to get these stats and incorporate them into your own application.

You can clone the project from Github:

git clone https://github.com/nethacker/usagewatch.git

Or download releases:

https://github.com/nethacker/usagewatch/releases

Or you can get it from rubygems.org:

gem install usagewatch

After you install the usagewatch gem you can use it as such:

require 'usagewatch'
 
usw = Usagewatch
 
usw.uw_diskused       #Total Disk Used Across All Partitions in Gigabytes
usw.uw_diskused_perc  #Total Percentage Disk Used Across All Partitions
usw.uw_cpuused        #Total CPU Used
usw.uw_tcpused        #Total TCP Connections Used
usw.uw_udpused        #Total UDP Connections Used
usw.uw_memused        #Total Percentage Active Memory Used
usw.uw_load           #Average System Load Of The Past Minute
usw.uw_bandrx         #Mbit/s Current Bandwidth Received
usw.uw_bandtx         #Mbit/s Current Bandwidth Transmitted
usw.uw_diskioreads    #Current Disk Reads Completed Per Second
usw.uw_diskiowrites   #Current Disk Writes Completed Per Second
usw.uw_cputop         #Top Ten Processes By CPU Consumption (in beta version 0.0.6.beta1)
usw.uw_memtop         #Top Ten Processes By Memory Consumption (in beta version 0.0.6.beta1)

You can also run the example.rb provided in the Gem:

ruby example.rb
 
Example Output:
 
11.56 Gigabytes Disk Used
7.0% Disk Used
0.25% CPU Used
30 TCP Connections Used
0 UDP Connections Used
43% Active Memory Used
0.01 Average System Load Of The Past Minute
0.008 Mbit/s Current Bandwidth Received
0.2 Mbit/s Current Bandwidth Transmitted
0/s Current Disk Reads Completed
2/s Current Disk Writes Completed
Top Ten Processes By CPU Consumption:
[["/usr/lib64/erlang/erts-5.8.5/bin/beam.smp", "5.2"], ["ruby", "4.1"], ["ps", "2.0"], ["abrt-dump-oops", "0.8"], ["aoe_ktio", "0.7"], ["aoe_tx", "0.4"], ["ata_sff", "0.2"], ["auditd", "0.1"], ["awk", "0.1"], ["-bash", "0.1"]] Note only in 0.0.6.beta1
Top Ten Processes By Memory Consumption:
[["unicorn", "4.8"], ["unicorn", "4.7"], ["unicorn", "4.6"], ["unicorn", "4.6"], ["unicorn", "4.5"], ["unicorn", "4.5"], ["unicorn", "4.3"], ["unicorn", "4.3"], ["unicorn", "4.2"], ["/usr/lib64/erlang/erts-5.8.5/bin/beam.smp", "4.0"]] Note only in 0.0.6.beta1

I hope this is helpful for anyone who needs Linux Usage statistics in a more accessible programming friendly manner.

This has been tested with Ruby 1.9.3p429 and OS Versions: CentOS 5x 6x, Fedora 18 and Ubuntu 12.04

Comments are closed.