| 1 | -- flaw, a Lua OO management framework for Awesome WM widgets. |
| 2 | -- Copyright (C) 2009,2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net> |
| 3 | |
| 4 | -- This program is free software: you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation, either version 3 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | |
| 14 | -- You should have received a copy of the GNU General Public License |
| 15 | -- along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | |
| 18 | -- Grab environment. |
| 19 | local io = io |
| 20 | local math = math |
| 21 | local tonumber = tonumber |
| 22 | local os = os |
| 23 | local string = string |
| 24 | |
| 25 | local beautiful = require('beautiful') |
| 26 | local naughty = require('naughty') |
| 27 | |
| 28 | local flaw = { |
| 29 | helper = require('flaw.helper'), |
| 30 | gadget = require('flaw.gadget'), |
| 31 | provider = require('flaw.provider') |
| 32 | } |
| 33 | |
| 34 | |
| 35 | --- Network activity. |
| 36 | -- |
| 37 | -- <p>This module contains a provider for network status and activity |
| 38 | -- and three gadgets: a text gadget, an icon gadget and a graph |
| 39 | -- gadget.</p> |
| 40 | -- |
| 41 | -- <h2>Gadgets</h2> |
| 42 | -- |
| 43 | -- <p>The ID of the gadgets designates the network device to be |
| 44 | -- monitored, in the <code>/proc/net/dev</code> file, as indicated in |
| 45 | -- the provider's documentation below. Network gadgets provide no |
| 46 | -- custom parameters. See the <a href="flaw.gadget.html">gadget</a> |
| 47 | -- module documentation to learn about standard gadgets |
| 48 | -- parameters.</p> |
| 49 | -- |
| 50 | -- <h3>Icon Gadget</h3> |
| 51 | -- |
| 52 | -- <p>The network icon gadget can be instantiated by indexing the |
| 53 | -- gadget module with <code>icon.network</code>. Here is an exemple |
| 54 | -- which assumes the icon path is stored in a <b>beautiful</b> |
| 55 | -- property.</p> |
| 56 | -- |
| 57 | -- <div class='example'> |
| 58 | -- g = flaw.gadget.icon.network(<br/> |
| 59 | -- 'eth0', {}, { image = image(beautiful.net_icon) })<br/> |
| 60 | -- </div> |
| 61 | -- |
| 62 | -- <h3>Text Gadget</h3> |
| 63 | -- |
| 64 | -- <p>The network text gadget can be instantiated by indexing the |
| 65 | -- gadget module with <code>text.network</code>. By default, the |
| 66 | -- gadget pattern is <code>in:$net_in out:$net_out</code>. See the |
| 67 | -- provider's documentation below to learn about the available |
| 68 | -- variables.</p> |
| 69 | -- |
| 70 | -- <div class='example'> |
| 71 | -- g = flaw.gadget.text.network('wlan0') |
| 72 | -- </div> |
| 73 | -- |
| 74 | -- <h3>Graph Gadget</h3> |
| 75 | -- |
| 76 | -- <p>The network graph gadget can be instantiated by indexing the |
| 77 | -- gadget module with <code>graph.network</code>. By default, the |
| 78 | -- chart displayed by the gadget shows the evolution of |
| 79 | -- <code>percents_in</code>. See the provider's documentation below to |
| 80 | -- learn about the available variables.</p> |
| 81 | -- |
| 82 | -- <div class='example'> |
| 83 | -- g = flaw.gadget.graph.network(<br/> |
| 84 | -- 'eth1', {}, { width = 60, height = 18 }) |
| 85 | -- </div> |
| 86 | -- |
| 87 | -- <h2>Provider</h2> |
| 88 | -- |
| 89 | -- <p>The network provider loads its information from |
| 90 | -- <code>/proc/net/dev</code>. It stores the data for each line, which |
| 91 | -- represent the network devices of the machine. The file format is |
| 92 | -- explained in the <code>proc</code> manual page (for example at <a |
| 93 | -- href="http://linux.die.net/man/5/proc">http://linux.die.net/man/5/proc</a></p> |
| 94 | -- |
| 95 | -- <p>The network provider computed data is composed of the following |
| 96 | -- fields.</p> |
| 97 | -- |
| 98 | -- <ul> |
| 99 | -- |
| 100 | -- <li><code>net_in</code> |
| 101 | -- |
| 102 | -- <p>The number of packets received by the monitored interface |
| 103 | -- between two updates of the provider.</p></li> |
| 104 | -- |
| 105 | -- <li><code>net_out</code> |
| 106 | -- |
| 107 | -- <p>The number of packets emitted by the monitored interface |
| 108 | -- between two updates of the provider.</p></li> |
| 109 | -- |
| 110 | -- <li><code>all_net_in</code> |
| 111 | -- |
| 112 | -- <p>The total number of packets received by the monitored interface |
| 113 | -- since the machine start up.</p></li> |
| 114 | -- |
| 115 | -- <li><code>all_net_out</code> |
| 116 | -- |
| 117 | -- <p>The total number of packets emitted by the monitored interface |
| 118 | -- since the machine start up.</p></li> |
| 119 | -- |
| 120 | -- <li><code>percents_in</code> |
| 121 | -- |
| 122 | -- <p>The incoming data load in percents on the monitored |
| 123 | -- interface. This value is for now computed with a hard-coded maximum |
| 124 | -- value of 256000 (in <code>DL_BANDWIDTH</code>).</p></li> |
| 125 | -- |
| 126 | -- <li><code>percents_out</code> |
| 127 | -- |
| 128 | -- <p>The ougoing data load in percents on the monitored |
| 129 | -- interface. This value is for now computed with a hard-coded maximum |
| 130 | -- value of 64000 (in <code>UP_BANDWIDTH</code>).</p></li> |
| 131 | -- |
| 132 | -- </ul> |
| 133 | -- |
| 134 | -- |
| 135 | -- @author David Soulayrol <david.soulayrol AT gmail DOT com> |
| 136 | -- @copyright 2009,2010,2011 David Soulayrol |
| 137 | module('flaw.network') |
| 138 | |
| 139 | |
| 140 | -- Bandwidth in bytes. |
| 141 | -- TODO: should become a parameter for the provider. |
| 142 | DL_BANDWIDTH = 256000 |
| 143 | UP_BANDWIDTH = 64000 |
| 144 | |
| 145 | --- The network provider prototype. |
| 146 | -- |
| 147 | -- <p>The network provider type is set to |
| 148 | -- <code>network._NAME</code>.</p> |
| 149 | -- |
| 150 | -- @class table |
| 151 | -- @name NetworkProvider |
| 152 | NetworkProvider = flaw.provider.CyclicProvider:new{ type = _NAME } |
| 153 | |
| 154 | --- Callback for provider refresh. |
| 155 | function NetworkProvider:do_refresh() |
| 156 | local file = io.open('/proc/net/dev') |
| 157 | local sep |
| 158 | local adapter |
| 159 | local line = '' |
| 160 | local input, output |
| 161 | |
| 162 | while line ~= nil do |
| 163 | line = file:read() |
| 164 | |
| 165 | if line ~= nil then |
| 166 | -- Skip the adapter prefix. |
| 167 | sep = string.find (line, ':') |
| 168 | if sep ~= nil then |
| 169 | adapter = flaw.helper.strings.lstrip(string.sub(line, 0, sep - 1)) |
| 170 | if adapter ~= nil then |
| 171 | if self.data[adapter] == nil then |
| 172 | self.data[adapter] = { |
| 173 | all_net_in = 0, all_net_out = 0, net_in = 0, net_out = 0 } |
| 174 | end |
| 175 | -- First decimal number are total bytes |
| 176 | local split_line = flaw.helper.strings.split( |
| 177 | string.sub(line, sep + 1)) |
| 178 | local interval = os.time() - self.timestamp |
| 179 | |
| 180 | input = tonumber(split_line[1]) |
| 181 | output = tonumber(split_line[9]) |
| 182 | |
| 183 | self.data[adapter].net_in = |
| 184 | (input - self.data[adapter].all_net_in) / interval |
| 185 | self.data[adapter].net_out = |
| 186 | (output - self.data[adapter].all_net_out) / interval |
| 187 | |
| 188 | self.data[adapter].percents_in = |
| 189 | (self.data[adapter].net_in / DL_BANDWIDTH) * 100 |
| 190 | self.data[adapter].percents_out = |
| 191 | (self.data[adapter].net_out / UP_BANDWIDTH) * 100 |
| 192 | |
| 193 | self.data[adapter].all_net_in = input |
| 194 | self.data[adapter].all_net_out = output |
| 195 | end |
| 196 | end |
| 197 | end |
| 198 | end |
| 199 | io.close(file); |
| 200 | end |
| 201 | |
| 202 | --- A factory for network providers. |
| 203 | -- |
| 204 | -- <p>Only one provider is built for all network adapters. Its data is |
| 205 | -- a map indexed by the name of the interface.</p> |
| 206 | -- |
| 207 | -- @return a brand new network provider, or the existing one if found |
| 208 | -- in the providers cache. |
| 209 | function provider_factory() |
| 210 | local p = flaw.provider.get(_NAME, '') |
| 211 | -- Create the provider if necessary. |
| 212 | if p == nil then |
| 213 | p = NetworkProvider:new{ id = '', data = {} } |
| 214 | flaw.provider.add(p) |
| 215 | end |
| 216 | return p |
| 217 | end |
| 218 | |
| 219 | -- An icon gadget for network status display. |
| 220 | flaw.gadget.register.icon(_M, { delay = 1 }) |
| 221 | |
| 222 | -- A Text gadget for network status display. |
| 223 | flaw.gadget.register.text(_M, { delay = 1, pattern = 'in:$net_in out:$net_out' }) |
| 224 | |
| 225 | -- A graph gadget for network load display. |
| 226 | flaw.gadget.register.graph(_M, { delay = 1, values = { 'percents_in' } }) |