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.

169 lines
3.0 KiB

  1. -- Copyright 2019 Florian Eckert <fe@dev.tdt.de>
  2. -- Copyright 2021 Jeroen Peelaerts <jeroen@steganos.dev>
  3. -- Licensed to the public under the GNU General Public License v2.
  4. local ubus = require("ubus")
  5. local hostname_file = "/proc/sys/kernel/hostname"
  6. local line_vars = {
  7. {
  8. name = "vector",
  9. type = "bool"
  10. },
  11. {
  12. name = "trellis",
  13. type = "bool"
  14. },
  15. {
  16. name = "bitswap",
  17. type = "bool"
  18. },
  19. {
  20. name = "retx",
  21. type = "bool"
  22. },
  23. {
  24. name = "satn",
  25. type = "snr"
  26. },
  27. {
  28. name = "latn",
  29. type = "snr"
  30. },
  31. {
  32. name = "attndr",
  33. type = "bitrate"
  34. },
  35. {
  36. name = "snr",
  37. type = "snr"
  38. },
  39. {
  40. name = "data_rate",
  41. type = "bitrate"
  42. },
  43. {
  44. name = "interleave_delay",
  45. type = "latency"
  46. }
  47. }
  48. local errors = {
  49. {
  50. name = "uas",
  51. type = "count"
  52. },
  53. {
  54. name = "rx_corrupted",
  55. type = "errors"
  56. },
  57. {
  58. name = "rx_retransmitted",
  59. type = "errors"
  60. },
  61. {
  62. name = "tx_retransmitted",
  63. type = "errors"
  64. }
  65. }
  66. local erb_vars = {
  67. {
  68. name = "sent",
  69. type = "gauge"
  70. },
  71. {
  72. name = "discarded",
  73. type = "gauge"
  74. }
  75. }
  76. local general_vars = {
  77. {
  78. name = "state_num",
  79. type = "gauge"
  80. },
  81. {
  82. name = "power_state_num",
  83. type = "gauge"
  84. },
  85. {
  86. name = "uptime",
  87. type = "uptime"
  88. }
  89. }
  90. local function build_metric(name, direction)
  91. if direction ~= '' then
  92. return string.format("%s_%s", name, direction)
  93. else
  94. return name
  95. end
  96. end
  97. local function get_values(hostname, variables, metrics, direction)
  98. for _, information in pairs(variables) do
  99. local name = information["name"]
  100. if metrics and metrics[name] ~= nil then
  101. local value = metrics[name]
  102. local metric = build_metric(name, direction)
  103. if information["type"] == "bool" then
  104. if metrics[name] == true then
  105. value = 1
  106. else
  107. value = 0
  108. end
  109. end
  110. local t = {
  111. host = host,
  112. plugin = 'dsl',
  113. type = information["type"],
  114. type_instance = metric,
  115. values = {value}
  116. }
  117. collectd.log_debug(string.format("%s: %s=%s", "collectd-mod-dsl(lua)", metric, tostring(value)))
  118. collectd.dispatch_values(t)
  119. else
  120. collectd.log_info(string.format("%s: Unable to get %s", "collectd-mod-dsl(lua)", name))
  121. end
  122. end
  123. end
  124. local function read()
  125. local lines = io.lines(hostname_file)
  126. local hostname = lines()
  127. local conn = ubus.connect()
  128. if not conn then
  129. collectd.log_error("collectd-mod-dsl(lua): Failed to connect to ubus")
  130. return 0
  131. end
  132. local metrics = conn:call("dsl", "metrics", {})
  133. if metrics then
  134. if metrics["up"] then
  135. local near_errors = metrics["errors"]["near"]
  136. local far_errors = metrics["errors"]["far"]
  137. local down_line = metrics["downstream"]
  138. local up_line = metrics["upstream"]
  139. local erb = metrics["erb"]
  140. get_values(hostname, errors, near_errors, "near")
  141. get_values(hostname, errors, far_errors, "far")
  142. get_values(hostname, line_vars, down_line, "down")
  143. get_values(hostname, line_vars, up_line, "up")
  144. get_values(hostname, erb_vars, erb, "")
  145. end
  146. get_values(hostname, general_vars, metrics, "")
  147. return 0
  148. end
  149. collectd.log_error("collectd-mod-dsl(lua): No ubus dsl object found")
  150. return 0
  151. end
  152. collectd.register_read(read)