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.

172 lines
5.1 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. --- a/Makefile.am
  18. +++ b/Makefile.am
  19. @@ -963,6 +963,12 @@ df_la_LDFLAGS = $(PLUGIN_LDFLAGS)
  20. df_la_LIBADD = libignorelist.la libmount.la
  21. endif
  22. +if BUILD_PLUGIN_DHCPLEASES
  23. +pkglib_LTLIBRARIES += dhcpleases.la
  24. +dhcpleases_la_SOURCES = src/dhcpleases.c
  25. +dhcpleases_la_LDFLAGS = $(PLUGIN_LDFLAGS)
  26. +endif
  27. +
  28. if BUILD_PLUGIN_DISK
  29. pkglib_LTLIBRARIES += disk.la
  30. disk_la_SOURCES = src/disk.c
  31. --- a/README
  32. +++ b/README
  33. @@ -106,6 +106,9 @@ Features
  34. Disk utilization: Sectors read/written, number of read/write actions,
  35. average time an IO-operation took to complete.
  36. + - dhcpleases
  37. + Collect number of current dhcp leases.
  38. +
  39. - dns
  40. DNS traffic: Query types, response codes, opcodes and traffic/octets
  41. transferred.
  42. --- a/configure.ac
  43. +++ b/configure.ac
  44. @@ -7063,6 +7063,7 @@ AC_PLUGIN([curl_xml], [$plugi
  45. AC_PLUGIN([dbi], [$with_libdbi], [General database statistics])
  46. AC_PLUGIN([dcpmm], [$with_libpmwapi], [Intel(R) Optane(TM) DC Persistent Memory performance and health statistics])
  47. AC_PLUGIN([df], [$plugin_df], [Filesystem usage statistics])
  48. +AC_PLUGIN([dhcpleases], [yes], [DHCP Leases])
  49. AC_PLUGIN([disk], [$plugin_disk], [Disk usage statistics])
  50. AC_PLUGIN([dns], [$with_libpcap], [DNS traffic analysis])
  51. AC_PLUGIN([dpdkevents], [$plugin_dpdkevents], [Events from DPDK])
  52. @@ -7512,6 +7513,7 @@ AC_MSG_RESULT([ curl_xml . . . . . .
  53. AC_MSG_RESULT([ dbi . . . . . . . . . $enable_dbi])
  54. AC_MSG_RESULT([ dcpmm . . . . . . . $enable_dcpmm])
  55. AC_MSG_RESULT([ df . . . . . . . . . $enable_df])
  56. +AC_MSG_RESULT([ dhcpleases. . . . . . $enable_dhcpleases])
  57. AC_MSG_RESULT([ disk . . . . . . . . $enable_disk])
  58. AC_MSG_RESULT([ dns . . . . . . . . . $enable_dns])
  59. AC_MSG_RESULT([ dpdkevents. . . . . . $enable_dpdkevents])
  60. --- a/src/collectd.conf.in
  61. +++ b/src/collectd.conf.in
  62. @@ -119,6 +119,7 @@
  63. #@BUILD_PLUGIN_DBI_TRUE@LoadPlugin dbi
  64. #@BUILD_PLUGIN_DCPMM_TRUE@LoadPlugin dcpmm
  65. #@BUILD_PLUGIN_DF_TRUE@LoadPlugin df
  66. +#@BUILD_PLUGIN_DHCPLEASES_TRUE@LoadPlugin dhcpleases
  67. #@BUILD_PLUGIN_DISK_TRUE@LoadPlugin disk
  68. #@BUILD_PLUGIN_DNS_TRUE@LoadPlugin dns
  69. #@BUILD_PLUGIN_DPDKEVENTS_TRUE@LoadPlugin dpdkevents
  70. @@ -690,6 +691,10 @@
  71. # SelectNumericQueryTypes true
  72. #</Plugin>
  73. +#<Plugin dhcpleases>
  74. +# Path "/tmp/dhcp.leases"
  75. +#</Plugin>
  76. +
  77. #<Plugin "dpdkevents">
  78. # <EAL>
  79. # Coremask "0x1"
  80. --- /dev/null
  81. +++ b/src/dhcpleases.c
  82. @@ -0,0 +1,83 @@
  83. +#include <errno.h>
  84. +#include <stdio.h>
  85. +
  86. +#include "utils/common/common.h"
  87. +
  88. +#include "configfile.h"
  89. +#include "plugin.h"
  90. +
  91. +static char *dhcp_lease_file;
  92. +
  93. +static const char *config_keys[] = {
  94. + "Path",
  95. +};
  96. +static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
  97. +
  98. +/* copied from ping.c plugin */
  99. +static int config_set_string(const char *name, /* {{{ */
  100. + char **var, const char *value) {
  101. + char *tmp;
  102. +
  103. + tmp = strdup(value);
  104. + if (tmp == NULL) {
  105. + ERROR("dhcpleases plugin: Setting `%s' to `%s' failed: strdup failed: %s", name,
  106. + value, STRERRNO);
  107. + return 1;
  108. + }
  109. +
  110. + if (*var != NULL)
  111. + free(*var);
  112. + *var = tmp;
  113. + return 0;
  114. +} /* }}} int config_set_string */
  115. +
  116. +static int dhcpleases_config(const char *key, const char *value) {
  117. + if (strcasecmp(key, "Path") == 0) {
  118. + int status = config_set_string(key, &dhcp_lease_file, value);
  119. + if (status != 0)
  120. + return status;
  121. + }
  122. + return 0;
  123. +}
  124. +
  125. +static void dhcpleases_submit(gauge_t counter) {
  126. + value_list_t vl = VALUE_LIST_INIT;
  127. + value_t values[] = {
  128. + {.gauge = counter},
  129. + };
  130. +
  131. + vl.values = values;
  132. + vl.values_len = STATIC_ARRAY_SIZE(values);
  133. +
  134. + sstrncpy(vl.plugin, "dhcpleases", sizeof(vl.plugin));
  135. + sstrncpy(vl.type, "count", sizeof(vl.type));
  136. +
  137. + plugin_dispatch_values(&vl);
  138. +}
  139. +
  140. +static int dhcp_leases_read(void) {
  141. +
  142. + FILE *fh;
  143. + char buffer[1024];
  144. + gauge_t count = 0;
  145. +
  146. + if ((fh = fopen(dhcp_lease_file, "r")) == NULL) {
  147. + WARNING("interface plugin: fopen: %s", STRERRNO);
  148. + return -1;
  149. + }
  150. +
  151. + while (fgets(buffer, 1024, fh) != NULL) {
  152. + count++;
  153. + }
  154. + fclose(fh);
  155. +
  156. + dhcpleases_submit(count);
  157. +
  158. + return 0;
  159. +}
  160. +
  161. +void module_register(void) {
  162. + plugin_register_config("dhcpleases", dhcpleases_config, config_keys,
  163. + config_keys_num);
  164. + plugin_register_read("dhcpleases", dhcp_leases_read);
  165. +}