Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nf_conntrack session and wireless client metric #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# openwrt_exporter
A openwrt exporter written in lua.

Example usage
-------------
1. install luasocket
2. paste the lua file in your openwrt router and add "lua exporter.lua -p 9100" in /etc/rc.local

Grafana ScreenShoot
-------------------
![alt text](https://raw.githubusercontent.com/frankiexyz/openwrt_exporter/master/openwrt.png)
37 changes: 34 additions & 3 deletions metrics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local unpack = unpack or table.unpack
-- This table defines the scrapers to run.
-- Each corresponds directly to a scraper_<name> function.
scrapers = { "cpu", "load_averages", "memory", "file_handles", "network",
"network_devices", "time", "uname"}
"network_devices", "time", "uname", "wifi_clients", "nat_sessions"}

-- Parsing

Expand Down Expand Up @@ -45,6 +45,17 @@ function get_contents(filename)
return contents
end

function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end

-- Metric printing

function print_metric(metric, labels, value)
Expand Down Expand Up @@ -152,6 +163,26 @@ function scraper_network()
end
end

function scraper_wifi_clients()
local netdevstat = line_split(get_contents("/proc/net/dev"))
local client = {}
local labels = {}
wifi_metric = metric("wifi_client", "gauge")
for i, line in ipairs(netdevstat) do
if string.match(netdevstat[i], "wlan") then
local cmd = ("iwinfo " .. space_split(netdevstat[i])[1]:gsub(":", "") .. " assoclist |grep ':[0-Z][0-Z]:'|wc -l")
client = os.capture(cmd, false)
labels['iface'] = space_split(netdevstat[i])[1]:gsub(":", "")
wifi_metric(labels, client)
end
end
end

function scraper_nat_sessions()
local cmd = ("cat /proc/net/nf_conntrack|wc -l")
local result = os.capture(cmd, false)
metric("nat_sessions", "gauge", nil, result)
end
function scraper_network_devices()
local netdevstat = line_split(get_contents("/proc/net/dev"))
local netdevsubstat = {"receive_bytes", "receive_packets", "receive_errs",
Expand All @@ -172,7 +203,7 @@ function scraper_network_devices()
end
end
for i, ndss in ipairs(netdevsubstat) do
netdev_metric = metric("node_network_" .. ndss, "gauge")
netdev_metric = metric("node_network_" .. ndss, "counter")
for ii, d in ipairs(devs) do
netdev_metric({device=d}, nds_table[d][i])
end
Expand Down Expand Up @@ -220,7 +251,7 @@ function run_all_scrapers()
local name = "node_exporter_scrape_duration_seconds"
local duration_metric = metric(name, "summary")
for i,scraper in ipairs(scrapers) do
local labels = {collector=scraper, result="success"}
local labels = {collector=scraper, result="success"}
duration_metric(labels, times[scraper])
print_metric(name.."_sum", labels, scrape_time_sums[scraper])
print_metric(name.."_count", labels, scrape_counts[scraper])
Expand Down
5 changes: 5 additions & 0 deletions metrics.out
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,8 @@ node_procs_running 3
node_time 1.44606e+09
# TYPE node_uname_info gauge
node_uname_info{domainname="(none)",machine="mips",nodename="owsla",release="3.3.8",sysname="Linux",version="#2 Sat Oct 20 12:31:22 UTC 2012"} 1
# TYPE wifi_client gauge
# wifi_client{iface="wlan1"} 3
# wifi_client{iface="wlan0"} 1
# # TYPE nat_sessions gauge
# nat_sessions 994
Binary file added openwrt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.