You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.3 KiB

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