lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

interfaces.lua (4833B)


      1 -- 
      2 -- This file is part of SmartSNMP
      3 -- Copyright (C) 2014, Credo Semiconductor Inc.
      4 -- 
      5 -- This program is free software; you can redistribute it and/or modify
      6 -- it under the terms of the GNU General Public License as published by
      7 -- the Free Software Foundation; either version 2 of the License, or
      8 -- (at your option) any later version.
      9 -- 
     10 -- This program is distributed in the hope that it will be useful,
     11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 -- GNU General Public License for more details.
     14 -- 
     15 -- You should have received a copy of the GNU General Public License along
     16 -- with this program; if not, write to the Free Software Foundation, Inc.,
     17 -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18 -- 
     19 
     20 local mib = require "smartsnmp"
     21 require "ubus"
     22 require "uloop"
     23 
     24 uloop.init()
     25 
     26 local conn = ubus.connect()
     27 if not conn then
     28     error("Failed to connect to ubusd")
     29 end
     30 
     31 local if_cache = {}
     32 local if_status_cache = {}
     33 local if_index_cache = {}
     34 
     35 local last_load_time = os.time()
     36 local function need_to_reload()
     37     if os.time() - last_load_time >= 3 then
     38         last_load_time = os.time()
     39         return true
     40     else
     41         return false
     42     end
     43 end
     44 
     45 local function load_config()
     46     if need_to_reload() == true then
     47         if_cache = {}
     48         if_status_cache = {}
     49         if_index_cache = {}
     50 
     51         -- if description
     52         for k, v in pairs(conn:call("network.device", "status", {})) do
     53             if_status_cache[k] = {}
     54         end
     55 
     56         for name_ in pairs(if_status_cache) do
     57             for k, v in pairs(conn:call("network.device", "status", { name = name_ })) do
     58                 if k == 'mtu' then
     59                     if_status_cache[name_].mtu = v
     60                 elseif k == 'macaddr' then
     61                     if_status_cache[name_].macaddr = v
     62                 elseif k == 'up' then
     63                     if v == true then            
     64                         if_status_cache[name_].up = 1
     65                     else
     66                         if_status_cache[name_].up = 2
     67                     end
     68                 elseif k == 'statistics' then
     69                     for item, stat in pairs(v) do
     70                         if item == 'rx_bytes' then
     71                             if_status_cache[name_].in_octet = stat
     72                         elseif item == 'tx_bytes' then
     73                             if_status_cache[name_].out_octet = stat
     74                         elseif item == 'rx_errors' then
     75                             if_status_cache[name_].in_errors = stat
     76                         elseif item == 'tx_errors' then
     77                             if_status_cache[name_].out_errors = stat
     78                         elseif item == 'rx_dropped' then
     79                             if_status_cache[name_].in_discards = stat
     80                         elseif item == 'tx_dropped' then
     81                             if_status_cache[name_].out_discards = stat
     82                         end
     83                     end
     84                 end
     85             end
     86         end
     87 
     88         if_cache['desc'] = {}
     89         for name, status in pairs(if_status_cache) do
     90             table.insert(if_cache['desc'], name)
     91             for k, v in pairs(status) do
     92                 if if_cache[k] == nil then if_cache[k] = {} end
     93                 table.insert(if_cache[k], v)
     94             end
     95         end
     96 
     97         -- if index
     98         for i in ipairs(if_cache['desc']) do
     99             table.insert(if_index_cache, i)
    100         end
    101     end
    102 end
    103 
    104 mib.module_methods.or_table_reg("1.3.6.1.2.1.2", "The MIB module for managing Interfaces implementations")
    105 
    106 local ifGroup = {
    107     [1]  = mib.ConstInt(function () load_config() return #if_index_cache end),
    108     [2] = {
    109         [1] = {
    110             [1] = mib.ConstIndex(function () load_config() return if_index_cache end),
    111             [2] = mib.ConstString(function (i) load_config() return if_cache['desc'][i] end),
    112             [4] = mib.ConstInt(function (i) load_config() return if_cache['mtu'][i] end),
    113             [6] = mib.ConstString(function (i) load_config() return if_cache['macaddr'][i] end),
    114             [8] = mib.ConstInt(function (i) load_config() return if_cache['up'][i] end),
    115             [10] = mib.ConstCount(function (i) load_config() return if_cache['in_octet'][i] end),
    116             [13] = mib.ConstCount(function (i) load_config() return if_cache['in_discards'][i] end),
    117             [14] = mib.ConstCount(function (i) load_config() return if_cache['in_errors'][i] end),
    118             [16] = mib.ConstCount(function (i) load_config() return if_cache['out_octet'][i] end),
    119             [19] = mib.ConstCount(function (i) load_config() return if_cache['out_discards'][i] end),
    120             [20] = mib.ConstCount(function (i) load_config() return if_cache['out_errors'][i] end),
    121         }
    122     }
    123 }
    124 
    125 return ifGroup