Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_iwinfo.lua
blobcdf0cceb6e61b0cfed13035461e4f481e535edb8
1 -- Authors: Relu Patrascu <ikoflexer@gmail.com>
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2004-11-17
4 --
5 -- statusd_iwinfo.lua
6 --
7 -- Copyright (c) Relu Patrascu 2004.
8 --
9 -- Ion is free software; you can redistribute it and/or modify it under
10 -- the terms of the GNU Lesser General Public License as published by
11 -- the Free Software Foundation; either version 2.1 of the License, or
12 -- (at your option) any later version.
14 -- Nov. 5, 2004
15 -- Disclaimer
16 -- Neither am I a lua expert nor do I have the time to invest in writing
17 -- better code here. I simply needed this utility and it works OK for me.
18 -- I give no guarantees of any kind for this code. Suggestions for
19 -- improvement are welcome.
20 -- ikoflexer at gmail dot com
22 -- Nov 17, 2004
23 -- ikoflexer at gmail dot com
25 -- Made it return a space " " instead of empty string ""
26 -- when /proc/net/wireless doesn't report any interface.
27 -- Otherwise old info lingers in the status bar.
29 if not statusd_iwinfo then
30 statusd_iwinfo = { interval = 10*1000 }
31 end
33 local iwinfo_timer
35 local function get_iwinfo_iwcfg()
36 local f = io.open('/proc/net/wireless', 'r')
37 if not f then
38 return
39 end
40 -- first 2 lines -- headers
41 f:read('*l')
42 f:read('*l')
43 -- the third line has wifi info
44 local s = f:read('*l')
45 f:close()
46 local st, en, iface = 0, 0, 0
47 if not s then
48 return
49 end
50 st, en, iface = string.find(s, '(%w+):')
51 local f1 = io.popen("/sbin/iwconfig " .. iface)
52 if not f1 then
53 return
54 else
55 local iwOut = f1:read('*a')
56 f1:close()
57 st,en,proto = string.find(iwOut, '(802.11[%-]*%a*)')
58 st,en,ssid = string.find(iwOut, 'ESSID[=:]"([%w+%s*]*)"', en)
59 st,en,bitrate = string.find(iwOut, 'Bit Rate[=:]([%s%w%.]*%/%a+)', en)
60 bitrate = string.gsub(bitrate, "%s", "")
61 st,en,linkq = string.find(iwOut, 'Link Quality[=:](%d+%/%d+)', en)
62 st,en,signal = string.find(iwOut, 'Signal level[=:](%-%d+)', en)
63 st,en,noise = string.find(iwOut, 'Noise level[=:](%-%d+)', en)
65 return proto, ssid, bitrate, linkq, signal, noise
66 end
67 end
69 local function update_iwinfo()
70 local proto, ssid, bitrate, linkq, signal, noise = get_iwinfo_iwcfg()
72 -- In case get_iwinfo_iwcfg doesn't return any values we don't want stupid lua
73 -- errors about concatenating nil values.
74 ssid = ssid or "N/A"
75 bitrate = bitrate or "N/A"
76 linkq = linkq or "N/A"
77 signal = signal or "N/A"
78 noise = noise or "N/A"
79 proto = proto or "N/A"
81 statusd.inform("iwinfo_cfg", ssid.." "..bitrate.." "..linkq.." "..signal.."/"..noise.."dBm "..proto)
82 statusd.inform("iwinfo_ssid", ssid)
83 statusd.inform("iwinfo_bitrate", bitrate)
84 statusd.inform("iwinfo_linkq", linkq)
85 statusd.inform("iwinfo_signal", signal)
86 statusd.inform("iwinfo_noise", noise)
87 statusd.inform("iwinfo_proto", proto)
88 iwinfo_timer:set(statusd_iwinfo.interval, update_iwinfo)
89 end
91 -- Init
92 iwinfo_timer = statusd.create_timer()
93 update_iwinfo()