diff --git a/README.md b/README.md index a66baa2..2284304 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,8 @@ $ ./pihole_exporter -pihole_hostname 192.168.1.10 -pihole_api_token $API_TOKEN 2019/05/09 20:19:52 New Prometheus metric registered: forward_destinations 2019/05/09 20:19:52 New Prometheus metric registered: querytypes 2019/05/09 20:19:52 New Prometheus metric registered: status +2019/05/09 20:19:52 New Prometheus metric registered: queries_last_10min +2019/05/09 20:19:52 New Prometheus metric registered: ads_last_10min 2019/05/09 20:19:52 Starting HTTP server 2019/05/09 20:19:54 New tick of statistics: 648 ads blocked / 66796 total DNS querie ... @@ -224,6 +226,7 @@ scrape_configs: | pihole_querytypes | This represent the number of queries made by Pi-hole by type | | pihole_status | This represent if Pi-hole is enabled | | queries_last_10min | This represent the number of queries in the last full slot of 10 minutes | +| ads_last_10min | This represent the number of ads in the last full slot of 10 minutes | ## Pihole-Exporter Helm Chart diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index d79fa8d..e4f6282 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -185,6 +185,16 @@ var ( }, []string{"hostname"}, ) + + // AdsLast10min - Number of ads in the last full slot of 10 minutes + AdsLast10min = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "ads_last_10min", + Namespace: "pihole", + Help: "Number of ads in the last full slot of 10 minutes", + }, + []string{"hostname"}, + ) ) // Init initializes all Prometheus metrics made available by Pi-hole exporter. @@ -207,6 +217,7 @@ func Init() { initMetric("querytypes", QueryTypes) initMetric("status", Status) initMetric("queries_last_10min", QueriesLast10min) + initMetric("ads_last_10min", AdsLast10min) } func initMetric(name string, metric *prometheus.GaugeVec) { diff --git a/internal/pihole/client.go b/internal/pihole/client.go index 1b96bec..b8e2709 100644 --- a/internal/pihole/client.go +++ b/internal/pihole/client.go @@ -102,6 +102,22 @@ func (c *Client) GetHostname() string { return c.config.PIHoleHostname } +// Pi-hole returns a map of unix epoch time with the number of stats in slots of 10 minutes. +// The last epoch is the current in-progress time slot, with stats still being added. +// We return the second latest epoch stats, which is definitive. +func latestEpochStats(statsOverTime map[int]int) float64 { + var lastEpoch, secondLastEpoch int + for timestamp := range statsOverTime { + if timestamp > lastEpoch { + secondLastEpoch = lastEpoch + lastEpoch = timestamp + } else if timestamp > secondLastEpoch && timestamp != lastEpoch { + secondLastEpoch = timestamp + } + } + return float64(statsOverTime[secondLastEpoch]) +} + func (c *Client) setMetrics(stats *Stats) { metrics.DomainsBlocked.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsBeingBlocked)) metrics.DNSQueriesToday.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DNSQueriesToday)) @@ -161,19 +177,8 @@ func (c *Client) setMetrics(stats *Stats) { metrics.QueryTypes.WithLabelValues(c.config.PIHoleHostname, queryType).Set(value) } - // Pi-hole returns a map of unix epoch time with the number of queries in slots of 10 minutes. - // The last epoch is the current in-progress time slot, with queries still being added. - // We return the second latest epoch, which is definitive. - var lastEpoch, secondLastEpoch int - for timestamp := range stats.DomainsOverTime { - if timestamp > lastEpoch { - secondLastEpoch = lastEpoch - lastEpoch = timestamp - } else if timestamp > secondLastEpoch && timestamp != lastEpoch { - secondLastEpoch = timestamp - } - } - metrics.QueriesLast10min.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsOverTime[secondLastEpoch])) + metrics.QueriesLast10min.WithLabelValues(c.config.PIHoleHostname).Set(latestEpochStats(stats.DomainsOverTime)) + metrics.AdsLast10min.WithLabelValues(c.config.PIHoleHostname).Set(latestEpochStats(stats.AdsOverTime)) } func (c *Client) getPHPSessionID() (string, error) { diff --git a/internal/pihole/model.go b/internal/pihole/model.go index 2c7678b..4f44d06 100644 --- a/internal/pihole/model.go +++ b/internal/pihole/model.go @@ -39,6 +39,7 @@ type Stats struct { QueryTypes map[string]float64 `json:"querytypes"` Status string `json:"status"` DomainsOverTime map[int]int `json:"domains_over_time"` + AdsOverTime map[int]int `json:"ads_over_time"` } // ToString method returns a string of the current statistics struct.