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.

186 lines
5.5 KiB

  1. From: Nick Hainke <vincent@systemli.org>
  2. Date: Mon, 7 Dec 2020 23:07:30 +0100
  3. Subject: [PATCH] dhcpleases: add dhcpleases plugin
  4. Changelog: dhcpleases: add plugin for counting current dhcp leases
  5. The plugin is useful for the Freifunk Community. Currently, we use
  6. the exec-plugin. With that dhcpleases plugin we have native collectd
  7. support to measure this important statistic.
  8. Signed-off-by: Nick Hainke <vincent@systemli.org>
  9. ---
  10. Makefile.am | 6 ++++
  11. README | 3 ++
  12. configure.ac | 2 ++
  13. src/collectd.conf.in | 5 +++
  14. src/dhcpleases.c | 83 ++++++++++++++++++++++++++++++++++++++++++++
  15. 5 files changed, 99 insertions(+)
  16. create mode 100644 src/dhcpleases.c
  17. diff --git a/Makefile.am b/Makefile.am
  18. index 00947da0..5ee76a00 100644
  19. --- a/Makefile.am
  20. +++ b/Makefile.am
  21. @@ -963,6 +963,12 @@ df_la_LDFLAGS = $(PLUGIN_LDFLAGS)
  22. df_la_LIBADD = libignorelist.la libmount.la
  23. endif
  24. +if BUILD_PLUGIN_DHCPLEASES
  25. +pkglib_LTLIBRARIES += dhcpleases.la
  26. +dhcpleases_la_SOURCES = src/dhcpleases.c
  27. +dhcpleases_la_LDFLAGS = $(PLUGIN_LDFLAGS)
  28. +endif
  29. +
  30. if BUILD_PLUGIN_DISK
  31. pkglib_LTLIBRARIES += disk.la
  32. disk_la_SOURCES = src/disk.c
  33. diff --git a/README b/README
  34. index e42e9c24..dd104408 100644
  35. --- a/README
  36. +++ b/README
  37. @@ -106,6 +106,9 @@ Features
  38. Disk utilization: Sectors read/written, number of read/write actions,
  39. average time an IO-operation took to complete.
  40. + - dhcpleases
  41. + Collect number of current dhcp leases.
  42. +
  43. - dns
  44. DNS traffic: Query types, response codes, opcodes and traffic/octets
  45. transferred.
  46. diff --git a/configure.ac b/configure.ac
  47. index bcfb8cf5..6c2b6574 100644
  48. --- a/configure.ac
  49. +++ b/configure.ac
  50. @@ -7061,6 +7061,7 @@ AC_PLUGIN([curl_xml], [$plugin_curl_xml], [CURL generic xml
  51. AC_PLUGIN([dbi], [$with_libdbi], [General database statistics])
  52. AC_PLUGIN([dcpmm], [$with_libpmwapi], [Intel(R) Optane(TM) DC Persistent Memory performance and health statistics])
  53. AC_PLUGIN([df], [$plugin_df], [Filesystem usage statistics])
  54. +AC_PLUGIN([dhcpleases], [yes], [DHCP Leases])
  55. AC_PLUGIN([disk], [$plugin_disk], [Disk usage statistics])
  56. AC_PLUGIN([dns], [$with_libpcap], [DNS traffic analysis])
  57. AC_PLUGIN([dpdkevents], [$plugin_dpdkevents], [Events from DPDK])
  58. @@ -7508,6 +7509,7 @@ AC_MSG_RESULT([ curl_xml . . . . . . $enable_curl_xml])
  59. AC_MSG_RESULT([ dbi . . . . . . . . . $enable_dbi])
  60. AC_MSG_RESULT([ dcpmm . . . . . . . $enable_dcpmm])
  61. AC_MSG_RESULT([ df . . . . . . . . . $enable_df])
  62. +AC_MSG_RESULT([ dhcpleases. . . . . . $enable_dhcpleases])
  63. AC_MSG_RESULT([ disk . . . . . . . . $enable_disk])
  64. AC_MSG_RESULT([ dns . . . . . . . . . $enable_dns])
  65. AC_MSG_RESULT([ dpdkevents. . . . . . $enable_dpdkevents])
  66. diff --git a/src/collectd.conf.in b/src/collectd.conf.in
  67. index 562a55d9..94659e81 100644
  68. --- a/src/collectd.conf.in
  69. +++ b/src/collectd.conf.in
  70. @@ -119,6 +119,7 @@
  71. #@BUILD_PLUGIN_DBI_TRUE@LoadPlugin dbi
  72. #@BUILD_PLUGIN_DCPMM_TRUE@LoadPlugin dcpmm
  73. #@BUILD_PLUGIN_DF_TRUE@LoadPlugin df
  74. +#@BUILD_PLUGIN_DHCPLEASES_TRUE@LoadPlugin dhcpleases
  75. #@BUILD_PLUGIN_DISK_TRUE@LoadPlugin disk
  76. #@BUILD_PLUGIN_DNS_TRUE@LoadPlugin dns
  77. #@BUILD_PLUGIN_DPDKEVENTS_TRUE@LoadPlugin dpdkevents
  78. @@ -689,6 +690,10 @@
  79. # SelectNumericQueryTypes true
  80. #</Plugin>
  81. +#<Plugin dhcpleases>
  82. +# Path "/tmp/dhcp.leases"
  83. +#</Plugin>
  84. +
  85. #<Plugin "dpdkevents">
  86. # <EAL>
  87. # Coremask "0x1"
  88. diff --git a/src/dhcpleases.c b/src/dhcpleases.c
  89. new file mode 100644
  90. index 00000000..f43d62bf
  91. --- /dev/null
  92. +++ b/src/dhcpleases.c
  93. @@ -0,0 +1,83 @@
  94. +#include <errno.h>
  95. +#include <stdio.h>
  96. +
  97. +#include "utils/common/common.h"
  98. +
  99. +#include "configfile.h"
  100. +#include "plugin.h"
  101. +
  102. +static char *dhcp_lease_file;
  103. +
  104. +static const char *config_keys[] = {
  105. + "Path",
  106. +};
  107. +static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
  108. +
  109. +/* copied from ping.c plugin */
  110. +static int config_set_string(const char *name, /* {{{ */
  111. + char **var, const char *value) {
  112. + char *tmp;
  113. +
  114. + tmp = strdup(value);
  115. + if (tmp == NULL) {
  116. + ERROR("dhcpleases plugin: Setting `%s' to `%s' failed: strdup failed: %s", name,
  117. + value, STRERRNO);
  118. + return 1;
  119. + }
  120. +
  121. + if (*var != NULL)
  122. + free(*var);
  123. + *var = tmp;
  124. + return 0;
  125. +} /* }}} int config_set_string */
  126. +
  127. +static int dhcpleases_config(const char *key, const char *value) {
  128. + if (strcasecmp(key, "Path") == 0) {
  129. + int status = config_set_string(key, &dhcp_lease_file, value);
  130. + if (status != 0)
  131. + return status;
  132. + }
  133. + return 0;
  134. +}
  135. +
  136. +static void dhcpleases_submit(gauge_t counter) {
  137. + value_list_t vl = VALUE_LIST_INIT;
  138. + value_t values[] = {
  139. + {.gauge = counter},
  140. + };
  141. +
  142. + vl.values = values;
  143. + vl.values_len = STATIC_ARRAY_SIZE(values);
  144. +
  145. + sstrncpy(vl.plugin, "dhcpleases", sizeof(vl.plugin));
  146. + sstrncpy(vl.type, "count", sizeof(vl.type));
  147. +
  148. + plugin_dispatch_values(&vl);
  149. +}
  150. +
  151. +static int dhcp_leases_read(void) {
  152. +
  153. + FILE *fh;
  154. + char buffer[1024];
  155. + gauge_t count = 0;
  156. +
  157. + if ((fh = fopen(dhcp_lease_file, "r")) == NULL) {
  158. + WARNING("interface plugin: fopen: %s", STRERRNO);
  159. + return -1;
  160. + }
  161. +
  162. + while (fgets(buffer, 1024, fh) != NULL) {
  163. + count++;
  164. + }
  165. + fclose(fh);
  166. +
  167. + dhcpleases_submit(count);
  168. +
  169. + return 0;
  170. +}
  171. +
  172. +void module_register(void) {
  173. + plugin_register_config("dhcpleases", dhcpleases_config, config_keys,
  174. + config_keys_num);
  175. + plugin_register_read("dhcpleases", dhcp_leases_read);
  176. +}
  177. --
  178. 2.29.2