diff --git a/man/dhcpd-pools.1.in b/man/dhcpd-pools.1.in index e6b1c86..753d6df 100644 --- a/man/dhcpd-pools.1.in +++ b/man/dhcpd-pools.1.in @@ -107,10 +107,11 @@ The default format is \fB\-\-mustach\fR=\fITEMPLATE\fR Output using mustach .I template -file. Mustache tags in the template are same as json output without IP -address information. When the native output formats controlled with +file. This is useful when the native output formats controlled with .B \-\-format -option do not provide what you need you should use mustach instead. +option do not provide what you need. See below example mustach template +that is using all available {{tags}} to demonstrate what can be displayed +and how. .IP @bindir@/dhcpd-pools --config @docdir@/dhcpd.conf --leases @docdir@/dhcpd.leases --mustach @docdir@/mustach.template @@ -286,6 +287,9 @@ ISC dhcpd configuration file. .TP @DHCPDLEASE_FILE@ ISC dhcpd lease file. +.TP +@docdir@/prometheus.template +Prometheus text file collector mustach template. .SH AUTHORS Original design by Sami Kerola. .br diff --git a/samples/Makemodule.am b/samples/Makemodule.am index 2d1bddd..054f001 100644 --- a/samples/Makemodule.am +++ b/samples/Makemodule.am @@ -1,4 +1,5 @@ dist_doc_DATA = \ samples/dhcpd.conf \ samples/dhcpd.leases \ - samples/mustach.template + samples/mustach.template \ + samples/prometheus.template diff --git a/samples/mustach.template b/samples/mustach.template index f8582c3..a569a15 100644 --- a/samples/mustach.template +++ b/samples/mustach.template @@ -13,6 +13,8 @@ Subnets:{{#subnets}} backup_count: {{backup_count}} backup_percent: {{backup_percent}} status: {{status}} + gettimeofday: {{gettimeofday}} + lease_file_mtime: {{lease_file_mtime}} {{/subnets}} Shared-networks:{{#shared-networks}} @@ -27,6 +29,8 @@ Shared-networks:{{#shared-networks}} backup_count: {{backup_count}} backup_percent: {{backup_percent}} status: {{status}} + gettimeofday: {{gettimeofday}} + lease_file_mtime: {{lease_file_mtime}} {{/shared-networks}} Summary:{{#summary}} @@ -41,4 +45,6 @@ Summary:{{#summary}} backup_count: {{backup_count}} backup_percent: {{backup_percent}} status: {{status}} + gettimeofday: {{gettimeofday}} + lease_file_mtime: {{lease_file_mtime}} {{/summary}} diff --git a/samples/prometheus.template b/samples/prometheus.template new file mode 100644 index 0000000..f8a5726 --- /dev/null +++ b/samples/prometheus.template @@ -0,0 +1,30 @@ +# This mustach template can be used as Prometheus text file. +# https://prometheus.io/ + +# HELP dhcpd_pools_ranges The range statistics. +# TYPE dhcpd_pools_ranges gauge +{{#subnets}}dhcpd_pools_ranges{range="{{first_ip}}",used="1"} {{used}} {{gettimeofday}}000 +dhcpd_pools_ranges{range="{{first_ip}}",touched="1"} {{touched}} {{gettimeofday}}000 +dhcpd_pools_ranges{range="{{first_ip}}",defined="1"} {{defined}} {{gettimeofday}}000 +dhcpd_pools_ranges{range="{{first_ip}}",free="1"} {{free}} {{gettimeofday}}000 +dhcpd_pools_ranges{range="{{first_ip}}",touch_count="1"} {{touch_count}} {{gettimeofday}}000 +dhcpd_pools_ranges{range="{{first_ip}}",status="1"} {{status}} {{gettimeofday}}000 +{{/subnets}} +# HELP dhcpd_pools_shared_nets The shared networks statistics. +# TYPE dhcpd_pools_shared_nets gauge +{{#shared-networks}}dhcpd_pools_shared_nets{location="{{location}}",defined="1"} {{defined}} {{gettimeofday}}000 +dhcpd_pools_shared_nets{location="{{location}}",used="1"} {{used}} {{gettimeofday}}000 +dhcpd_pools_shared_nets{location="{{location}}",touched="1"} {{touched}} {{gettimeofday}}000 +dhcpd_pools_shared_nets{location="{{location}}",free="1"} {{free}} {{gettimeofday}}000 +dhcpd_pools_shared_nets{location="{{location}}",touch_count="1"} {{touch_count}} {{gettimeofday}}000 +dhcpd_pools_shared_nets{location="{{location}}",status="1"} {{status}} {{gettimeofday}}000 +{{/shared-networks}} +# HELP dhcpd_pools_summary Statistics of the all networks. +# TYPE dhcpd_pools_summary gauge +{{#summary}}dhcpd_pools_summary{location="{{location}}",defined="1"} {{defined}} {{gettimeofday}}000 +dhcpd_pools_summary{location="{{location}}",used="1"} {{used}} {{gettimeofday}}000 +dhcpd_pools_summary{location="{{location}}",touched="1"} {{touched}} {{gettimeofday}}000 +dhcpd_pools_summary{location="{{location}}",free="1"} {{free}} {{gettimeofday}}000 +dhcpd_pools_summary{location="{{location}}",touch_count="1"} {{touch_count}} {{gettimeofday}}000 +dhcpd_pools_summary{location="{{location}}",status="1"} {{status}} {{gettimeofday}}000 +{{/summary}} diff --git a/src/mustach-dhcpd-pools.c b/src/mustach-dhcpd-pools.c index 8fa1bec..652db0a 100644 --- a/src/mustach-dhcpd-pools.c +++ b/src/mustach-dhcpd-pools.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include "close-stream.h" @@ -144,6 +145,20 @@ static int must_put_range(void *closure, const char *name, int escape fprintf(file, "%d", e->oh.status); return 0; } + if (!strcmp(name, "gettimeofday")) { + struct timeval tp; + + gettimeofday(&tp, NULL); + fprintf(file, "%ld", tp.tv_sec); + return 0; + } + if (!strcmp(name, "lease_file_mtime")) { + struct stat st; + + stat(e->state->dhcpdlease_file, &st); + fprintf(file, "%ld", st.st_mtime); + return 0; + } return 0; } @@ -198,6 +213,20 @@ static int must_put_shnet(void *closure, const char *name, int escape fprintf(file, "%d", e->oh.status); return 0; } + if (!strcmp(name, "gettimeofday")) { + struct timeval tp; + + gettimeofday(&tp, NULL); + fprintf(file, "%ld", tp.tv_sec); + return 0; + } + if (!strcmp(name, "lease_file_mtime")) { + struct stat st; + + stat(e->state->dhcpdlease_file, &st); + fprintf(file, "%ld", st.st_mtime); + return 0; + } return 0; } diff --git a/tests/mustach b/tests/mustach index 4a282c6..af53900 100755 --- a/tests/mustach +++ b/tests/mustach @@ -9,11 +9,13 @@ if [ ! -d tests/outputs ]; then fi dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \ - --mustach $top_srcdir/samples/mustach.template -o tests/outputs/$IAM + --mustach $top_srcdir/samples/mustach.template | + sed '/gettimeofday:/d; /lease_file_mtime:/d' >| tests/outputs/$IAM echo "--- skip ok ---" >> tests/outputs/$IAM dhcpd-pools -c $top_srcdir/samples/dhcpd.conf -l $top_srcdir/samples/dhcpd.leases \ - --mustach $top_srcdir/samples/mustach.template --skip-ok --warning 44 >> tests/outputs/$IAM + --mustach $top_srcdir/samples/mustach.template --skip-ok --warning 44 | + sed '/gettimeofday:/d; /lease_file_mtime:/d' >> tests/outputs/$IAM diff -u $top_srcdir/tests/expected/$IAM tests/outputs/$IAM exit $?