summaryrefslogtreecommitdiffstats
path: root/net/smartsnmpd/files/mibs/system.lua
blob: 5b0a4af950d5cababfe455914170ed4101c9dc72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
-- 
-- This file is part of SmartSNMP
-- Copyright (C) 2014, Credo Semiconductor Inc.
-- 
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-- 

local mib = require "smartsnmp"
local uci = require "uci"

-- System config
local context = uci.cursor("/etc/config", "/tmp/.uci")

-- scalar index
local sysDesc             = 1
local sysObjectID         = 2
local sysUpTime           = 3
local sysContact          = 4
local sysName             = 5
local sysLocation         = 6
local sysServices         = 7
local sysORLastChange     = 8

-- table index
local sysORTable          = 9

-- entry index
local sysOREntry          = 1

-- list index
local sysORIndex          = 1
local sysORID             = 2
local sysORDesc           = 3
local sysORUpTime         = 4

local startup_time = 0
local or_last_changed_time = 0

local function mib_system_startup(time)
    startup_time = time
    or_last_changed_time = time
end

mib_system_startup(os.time())

local sysGroup = {}
local or_oid_cache = {}
local or_index_cache = {}
local or_table_cache = {}

local or_table_reg = function (oid, desc)
    local row = {}
    row['oid'] = {}
    for i in string.gmatch(oid, "%d") do
        table.insert(row['oid'], tonumber(i))
    end
    row['desc'] = desc
    row['uptime'] = os.time()
    table.insert(or_table_cache, row)
    
    or_last_changed_time = os.time()

    or_oid_cache[oid] = #or_table_cache

    or_index_cache = {}
    for i in ipairs(or_table_cache) do
        table.insert(or_index_cache, i)
    end
end

local or_table_unreg = function (oid)
    local or_idx = or_oid_cache[oid]

    if or_table_cache[or_idx] ~= nil then
        table.remove(or_table_cache, or_idx)
        or_last_changed_time = os.time()

        or_index_cache = {}
        for i in ipairs(or_table_cache) do
            table.insert(or_index_cache, i)
        end
    end
end

local last_load_time = os.time()
local function need_to_reload()
    if os.difftime(os.time(), last_load_time) < 3 then
        return false
    else
        last_load_time = os.time()
        return true
    end
end

local function load_config()
    if need_to_reload() == true then
        context:load("smartsnmpd")
    end
end

context:load("smartsnmpd")

local sysMethods = {
    ["or_table_reg"] = or_table_reg, 
    ["or_table_unreg"] = or_table_unreg
}
mib.module_method_register(sysMethods)

sysGroup = {
    rocommunity = 'public',
    [sysDesc]         = mib.ConstString(function () load_config() return mib.sh_call("uname -a") end),
    [sysObjectID]     = mib.ConstOid(function ()
                                         load_config()
                                         local oid
                                         local objectid
                                         context:foreach("smartsnmpd", "smartsnmpd", function (s)
                                             objectid = s.objectid
                                         end)
                                         if objectid ~= nil then
                                            oid = {}
                                            for i in string.gmatch(objectid, "%d+") do
                                                table.insert(oid, tonumber(i))
                                            end
                                         end
                                         return oid
                                     end),
    [sysUpTime]       = mib.ConstTimeticks(function () load_config() return os.difftime(os.time(), startup_time) * 100 end),
    [sysContact]      = mib.ConstString(function () 
                                            load_config()
                                            local contact
                                            context:foreach("smartsnmpd", "smartsnmpd", function (s)
                                                contact = s.contact
                                            end)
                                            return contact
                                        end),
    [sysName]         = mib.ConstString(function () load_config() return mib.sh_call("uname -n") end),
    [sysLocation]     = mib.ConstString(function ()
                                            load_config()
                                            local location
                                            context:foreach("smartsnmpd", "smartsnmpd", function (s)
                                                location = s.location
                                            end)
                                            return location
                                        end),
    [sysServices]     = mib.ConstInt(function ()
                                         load_config()
                                         local services
                                         context:foreach("smartsnmpd", "smartsnmpd", function (s)
                                             services = tonumber(s.services)
                                         end)
                                         return services
                                     end),
    [sysORLastChange] = mib.ConstTimeticks(function () load_config() return os.difftime(os.time(), or_last_changed_time) * 100 end),
    [sysORTable]      = {
        [sysOREntry]  = {
            [sysORIndex]  = mib.UnaIndex(function () load_config() return or_index_cache end),
            [sysORID]     = mib.ConstOid(function (i) load_config() return or_table_cache[i].oid end),
            [sysORDesc]   = mib.ConstString(function (i) load_config() return or_table_cache[i].desc end),
            [sysORUpTime] = mib.ConstTimeticks(function (i) load_config() return os.difftime(os.time(), or_table_cache[i].uptime) * 100 end),
        }
    }
}

return sysGroup