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.

170 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 = "latn",
  45. type = "latency"
  46. }
  47. }
  48. local errors = {
  49. {
  50. name = "uas",
  51. type = "gauge"
  52. },
  53. {
  54. name = "rx_corrupted",
  55. type = "gauge"
  56. },
  57. {
  58. name = "rx_retransmitted",
  59. type = "gauge"
  60. },
  61. {
  62. name = "tx_retransmitted",
  63. type = "gauge"
  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 = "profile",
  79. type = "gauge"
  80. },
  81. {
  82. name = "mode",
  83. type = "gauge"
  84. },
  85. {
  86. name = "state_num",
  87. type = "gauge"
  88. },
  89. {
  90. name = "power_state_num",
  91. type = "gauge"
  92. },
  93. {
  94. name = "uptime",
  95. type = "uptime"
  96. }
  97. }
  98. local function build_metric(name, direction)
  99. if direction ~= '' then
  100. return string.format("%s_%s", name, direction)
  101. else
  102. return name
  103. end
  104. end
  105. local function get_values(hostname, variables, metrics, direction)
  106. for _, information in pairs(variables) do
  107. local name = information["name"]
  108. if metrics and metrics[name] ~= nil then
  109. local value = metrics[name]
  110. local metric = build_metric(name, direction)
  111. local t = {
  112. host = host,
  113. plugin = 'dsl',
  114. type = information["type"],
  115. type_instance = metric,
  116. values = {value}
  117. }
  118. collectd.log_debug(string.format("%s: %s=%s", "collectd-mod-dsl(lua)", metric, tostring(value)))
  119. collectd.dispatch_values(t)
  120. else
  121. collectd.log_info(string.format("%s: Unable to get %s", "collectd-mod-dsl(lua)", name))
  122. end
  123. end
  124. end
  125. local function read()
  126. local lines = io.lines(hostname_file)
  127. local hostname = lines()
  128. local conn = ubus.connect()
  129. if not conn then
  130. collectd.log_error("collectd-mod-dsl(lua): Failed to connect to ubus")
  131. return 0
  132. end
  133. local metrics = conn:call("dsl", "metrics", {})
  134. if metrics then
  135. if metrics["up"] then
  136. local near_errors = metrics["errors"]["near"]
  137. local far_errors = metrics["errors"]["far"]
  138. local down_line = metrics["downstream"]
  139. local up_line = metrics["upstream"]
  140. local erb = metrics["erb"]
  141. get_values(hostname, errors, near_errors, "near")
  142. get_values(hostname, errors, far_errors, "far")
  143. get_values(hostname, line_vars, down_line, "down")
  144. get_values(hostname, line_vars, up_line, "up")
  145. get_values(hostname, erb_vars, erb, "")
  146. end
  147. get_values(hostname, general_vars, metrics, "")
  148. return 0
  149. end
  150. collectd.log_error("collectd-mod-dsl(lua): No ubus dsl object found")
  151. return 0
  152. end
  153. collectd.register_read(read)