Skip to content

Instantly share code, notes, and snippets.

@rubiojr
Created January 13, 2013 18:44
Show Gist options
  • Save rubiojr/4525640 to your computer and use it in GitHub Desktop.
Save rubiojr/4525640 to your computer and use it in GitHub Desktop.
Getting OpenWRT router stats with ruby
require 'excon'
require 'pp'
require 'yajl'
# Auth parameters
username = 'root'
password = 'secret'
host = '192.168.1.1'
# Will parse returned JSON content
parser = Yajl::Parser.new
#
# Authenticate
#
# Also returns router's dashboard status information, i.e.
# the info you see in the router 'Status -> Overview' dashboard
#
# Returned keys
# ["conncount",
# "leases",
# "wan",
# "membuffers",
# "connmax",
# "memfree",
# "uptime",
# "wifinets",
# "memtotal",
# "localtime",
# "memcached",
# "loadavg"
# ]
r = Excon.post "http://#{host}/cgi-bin/luci?status=1",
:body => "username=#{username}&password=#{password}",
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }
# Do something with the data
# pp parser.parse(r.body)
# Get sysauth cookie and session path, will use it to send
# other requests
sysauth, path = r.headers['Set-Cookie'].split
path = path.split('=')[1..-1].join('=')
## Available statistics
### Network traffic
# Data used by the graphs available in **'Status -> Realtime Graphs -> Traffic'**
# Network traffic
#
# Returned data format: time, rx bytes, rx packets, tx bytes, tx packets
#
# Data sample:
# [
# [1358095466, 1482617413, 101161697, 1420821587, 62541131],
# [1358095467, 1482626173, 101161703, 1420830431, 62541137],
# ...
# ]
r = Excon.get "http://192.168.66.1/#{path}/admin/status/realtime/bandwidth_status/eth0.1",
:headers => { 'Cookie' => sysauth }
# Do something with the data
pp parser.parse(r.body)
### System load
# Data used by the graphs available in **'Status -> Realtime Graphs -> Load'**
#
# System load
#
# Returned data format: time, 1min load, 5min load, 15min load
#
# Data sample:
# [
# [1358098335, 36, 40, 56],
# [1358098336, 36, 40, 56],
# ...
# ]
r = Excon.get "http://#{host}/#{path}/admin/status/realtime/load_status",
:headers => { 'Cookie' => sysauth }
# Do something with the data
pp parser.parse(r.body)
### Connections status
# Data used by the graphs available in **'Status -> Realtime Graphs -> Connections'**
#
# Connections Status
#
# Returned data format:
#
# statistics: TIME, # udp connections, # tcp connections, # other connections
# connections: returns a Hash, self descriptive.
#
# TCP and UDP number of connections is always 0, looks like a minor bug in this
# version of OpenWRT (1.0)
# Data sample:
#
# {
# "connections" => [
# {"bytes"=>"746295",
# "src"=>"192.168.1.4",
# "sport"=>"36865",
# "layer4"=>"tcp",
# "dst"=>"1.2.3.4",
# "dport"=>"333",
# "layer3"=>"ipv4",
# "packets"=>"10674"},
# ...
# ],
# "statistics" => [
# [1358097511, 0, 0, 48],
# ...
# ]
# }
#
#
r = Excon.get "http://#{host}/#{path}/admin/status/realtime/connections_status",
:headers => { 'Cookie' => sysauth }
# connections_status does not return valid JSON apparently, fix it
r.body.gsub! /(connections|statistics)/, '"\\1"'
# Do something with the data
pp parser.parse r.body
### Wireless interface status
# Data used by the graphs available in **'Status -> Realtime Graphs -> Wireless'**
#
# Wireless interface wl0 status
#
# Returned data format: time, rate, rssi (signal strength), noise
#
# Data sample:
# [
# [1358099217, 61650, 213, 167],
# [1358099218, 61650, 213, 166],
# ...
# ]
#
r = Excon.get "http://#{host}/#{path}/admin/status/realtime/wireless_status/wl0",
:headers => { 'Cookie' => sysauth }
# Do something with the data
pp parser.parse r.body
@gopesht
Copy link

gopesht commented Mar 30, 2014

what if i don't have openwrt router and want to test these data.What should I do and how shall i write code for production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment