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.

125 lines
4.7 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. require "ubus"
  21. require "uloop"
  22. uloop.init()
  23. local conn = ubus.connect()
  24. if not conn then
  25. error("Failed to connect to ubusd")
  26. end
  27. local if_cache = {}
  28. local if_status_cache = {}
  29. local if_index_cache = {}
  30. local last_load_time = os.time()
  31. local function need_to_reload()
  32. if os.time() - last_load_time >= 3 then
  33. last_load_time = os.time()
  34. return true
  35. else
  36. return false
  37. end
  38. end
  39. local function load_config()
  40. if need_to_reload() == true then
  41. if_cache = {}
  42. if_status_cache = {}
  43. if_index_cache = {}
  44. -- if description
  45. for k, v in pairs(conn:call("network.device", "status", {})) do
  46. if_status_cache[k] = {}
  47. end
  48. for name_ in pairs(if_status_cache) do
  49. for k, v in pairs(conn:call("network.device", "status", { name = name_ })) do
  50. if k == 'mtu' then
  51. if_status_cache[name_].mtu = v
  52. elseif k == 'macaddr' then
  53. if_status_cache[name_].macaddr = v
  54. elseif k == 'up' then
  55. if v == true then
  56. if_status_cache[name_].up = 1
  57. else
  58. if_status_cache[name_].up = 2
  59. end
  60. elseif k == 'statistics' then
  61. for item, stat in pairs(v) do
  62. if item == 'rx_bytes' then
  63. if_status_cache[name_].in_octet = stat
  64. elseif item == 'tx_bytes' then
  65. if_status_cache[name_].out_octet = stat
  66. elseif item == 'rx_errors' then
  67. if_status_cache[name_].in_errors = stat
  68. elseif item == 'tx_errors' then
  69. if_status_cache[name_].out_errors = stat
  70. elseif item == 'rx_dropped' then
  71. if_status_cache[name_].in_discards = stat
  72. elseif item == 'tx_dropped' then
  73. if_status_cache[name_].out_discards = stat
  74. end
  75. end
  76. end
  77. end
  78. end
  79. if_cache['desc'] = {}
  80. for name, status in pairs(if_status_cache) do
  81. table.insert(if_cache['desc'], name)
  82. for k, v in pairs(status) do
  83. if if_cache[k] == nil then if_cache[k] = {} end
  84. table.insert(if_cache[k], v)
  85. end
  86. end
  87. -- if index
  88. for i in ipairs(if_cache['desc']) do
  89. table.insert(if_index_cache, i)
  90. end
  91. end
  92. end
  93. mib.module_methods.or_table_reg("1.3.6.1.2.1.2", "The MIB module for managing Interfaces implementations")
  94. local ifGroup = {
  95. [1] = mib.ConstInt(function () load_config() return #if_index_cache end),
  96. [2] = {
  97. [1] = {
  98. [1] = mib.ConstIndex(function () load_config() return if_index_cache end),
  99. [2] = mib.ConstString(function (i) load_config() return if_cache['desc'][i] end),
  100. [4] = mib.ConstInt(function (i) load_config() return if_cache['mtu'][i] end),
  101. [6] = mib.ConstString(function (i) load_config() return if_cache['macaddr'][i] end),
  102. [8] = mib.ConstInt(function (i) load_config() return if_cache['up'][i] end),
  103. [10] = mib.ConstCount(function (i) load_config() return if_cache['in_octet'][i] end),
  104. [13] = mib.ConstCount(function (i) load_config() return if_cache['in_discards'][i] end),
  105. [14] = mib.ConstCount(function (i) load_config() return if_cache['in_errors'][i] end),
  106. [16] = mib.ConstCount(function (i) load_config() return if_cache['out_octet'][i] end),
  107. [19] = mib.ConstCount(function (i) load_config() return if_cache['out_discards'][i] end),
  108. [20] = mib.ConstCount(function (i) load_config() return if_cache['out_errors'][i] end),
  109. }
  110. }
  111. }
  112. return ifGroup