lede-packages-rs

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

system.lua (6428B)


      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 local uci = require "uci"
     22 
     23 -- System config
     24 local context = uci.cursor("/etc/config", "/tmp/.uci")
     25 
     26 -- scalar index
     27 local sysDesc             = 1
     28 local sysObjectID         = 2
     29 local sysUpTime           = 3
     30 local sysContact          = 4
     31 local sysName             = 5
     32 local sysLocation         = 6
     33 local sysServices         = 7
     34 local sysORLastChange     = 8
     35 
     36 -- table index
     37 local sysORTable          = 9
     38 
     39 -- entry index
     40 local sysOREntry          = 1
     41 
     42 -- list index
     43 local sysORIndex          = 1
     44 local sysORID             = 2
     45 local sysORDesc           = 3
     46 local sysORUpTime         = 4
     47 
     48 local startup_time = 0
     49 local or_last_changed_time = 0
     50 
     51 local function mib_system_startup(time)
     52     startup_time = time
     53     or_last_changed_time = time
     54 end
     55 
     56 mib_system_startup(os.time())
     57 
     58 local sysGroup = {}
     59 local or_oid_cache = {}
     60 local or_index_cache = {}
     61 local or_table_cache = {}
     62 
     63 local or_table_reg = function (oid, desc)
     64     local row = {}
     65     row['oid'] = {}
     66     for i in string.gmatch(oid, "%d") do
     67         table.insert(row['oid'], tonumber(i))
     68     end
     69     row['desc'] = desc
     70     row['uptime'] = os.time()
     71     table.insert(or_table_cache, row)
     72     
     73     or_last_changed_time = os.time()
     74 
     75     or_oid_cache[oid] = #or_table_cache
     76 
     77     or_index_cache = {}
     78     for i in ipairs(or_table_cache) do
     79         table.insert(or_index_cache, i)
     80     end
     81 end
     82 
     83 local or_table_unreg = function (oid)
     84     local or_idx = or_oid_cache[oid]
     85 
     86     if or_table_cache[or_idx] ~= nil then
     87         table.remove(or_table_cache, or_idx)
     88         or_last_changed_time = os.time()
     89 
     90         or_index_cache = {}
     91         for i in ipairs(or_table_cache) do
     92             table.insert(or_index_cache, i)
     93         end
     94     end
     95 end
     96 
     97 local last_load_time = os.time()
     98 local function need_to_reload()
     99     if os.difftime(os.time(), last_load_time) < 3 then
    100         return false
    101     else
    102         last_load_time = os.time()
    103         return true
    104     end
    105 end
    106 
    107 local function load_config()
    108     if need_to_reload() == true then
    109         context:load("smartsnmpd")
    110     end
    111 end
    112 
    113 context:load("smartsnmpd")
    114 
    115 local sysMethods = {
    116     ["or_table_reg"] = or_table_reg, 
    117     ["or_table_unreg"] = or_table_unreg
    118 }
    119 mib.module_method_register(sysMethods)
    120 
    121 sysGroup = {
    122     rocommunity = 'public',
    123     [sysDesc]         = mib.ConstString(function () load_config() return mib.sh_call("uname -a") end),
    124     [sysObjectID]     = mib.ConstOid(function ()
    125                                          load_config()
    126                                          local oid
    127                                          local objectid
    128                                          context:foreach("smartsnmpd", "smartsnmpd", function (s)
    129                                              objectid = s.objectid
    130                                          end)
    131                                          if objectid ~= nil then
    132                                             oid = {}
    133                                             for i in string.gmatch(objectid, "%d+") do
    134                                                 table.insert(oid, tonumber(i))
    135                                             end
    136                                          end
    137                                          return oid
    138                                      end),
    139     [sysUpTime]       = mib.ConstTimeticks(function () load_config() return os.difftime(os.time(), startup_time) * 100 end),
    140     [sysContact]      = mib.ConstString(function () 
    141                                             load_config()
    142                                             local contact
    143                                             context:foreach("smartsnmpd", "smartsnmpd", function (s)
    144                                                 contact = s.contact
    145                                             end)
    146                                             return contact
    147                                         end),
    148     [sysName]         = mib.ConstString(function () load_config() return mib.sh_call("uname -n") end),
    149     [sysLocation]     = mib.ConstString(function ()
    150                                             load_config()
    151                                             local location
    152                                             context:foreach("smartsnmpd", "smartsnmpd", function (s)
    153                                                 location = s.location
    154                                             end)
    155                                             return location
    156                                         end),
    157     [sysServices]     = mib.ConstInt(function ()
    158                                          load_config()
    159                                          local services
    160                                          context:foreach("smartsnmpd", "smartsnmpd", function (s)
    161                                              services = tonumber(s.services)
    162                                          end)
    163                                          return services
    164                                      end),
    165     [sysORLastChange] = mib.ConstTimeticks(function () load_config() return os.difftime(os.time(), or_last_changed_time) * 100 end),
    166     [sysORTable]      = {
    167         [sysOREntry]  = {
    168             [sysORIndex]  = mib.UnaIndex(function () load_config() return or_index_cache end),
    169             [sysORID]     = mib.ConstOid(function (i) load_config() return or_table_cache[i].oid end),
    170             [sysORDesc]   = mib.ConstString(function (i) load_config() return or_table_cache[i].desc end),
    171             [sysORUpTime] = mib.ConstTimeticks(function (i) load_config() return os.difftime(os.time(), or_table_cache[i].uptime) * 100 end),
    172         }
    173     }
    174 }
    175 
    176 return sysGroup