diff --git a/config/configuration.go b/config/configuration.go index 4305561..1263a51 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -183,7 +183,7 @@ func (c Config) hostnameURL() string { // PIHoleStatsURL returns the stats url func (c Config) PIHoleStatsURL() string { - return c.hostnameURL() + "/admin/api.php?summaryRaw&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&jsonForceObject" + return c.hostnameURL() + "/admin/api.php?summaryRaw&overTimeData10mins&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&jsonForceObject" } // PIHoleLoginURL returns the login url diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 4a19dbc..af33bc8 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -175,6 +175,24 @@ var ( }, []string{"hostname"}, ) + + DNSQueriesLast10min = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "dns_queries_last_10min", + Namespace: "pihole", + Help: "Number of DNS queries in the last full slot of 10 minutes", + }, + []string{"hostname", "window"}, + ) + + AdsBlockedLast10min = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "ads_blocked_last_10min", + Namespace: "pihole", + Help: "Number of Ads blocked in the last full slot of 10 minutes", + }, + []string{"hostname"}, + ) ) // Init initializes all Prometheus metrics made available by Pi-hole exporter. @@ -196,6 +214,8 @@ func Init() { initMetric("forward_destinations", ForwardDestinations) initMetric("querytypes", QueryTypes) initMetric("status", Status) + initMetric("dns_queries_last_10min", DNSQueriesLast10min) + initMetric("ads_blocked_last_10min", AdsBlockedLast10min) } func initMetric(name string, metric *prometheus.GaugeVec) { diff --git a/internal/pihole/client.go b/internal/pihole/client.go index b94a7ff..5a57ec2 100644 --- a/internal/pihole/client.go +++ b/internal/pihole/client.go @@ -160,6 +160,20 @@ func (c *Client) setMetrics(stats *Stats) { for queryType, value := range stats.QueryTypes { metrics.QueryTypes.WithLabelValues(c.config.PIHoleHostname, queryType).Set(value) } + + var lastEpoch, secondLastEpoch int + for timestamp := range stats.DomainsOverTime { + secondLastEpoch = lastEpoch + lastEpoch = timestamp + metrics.DNSQueriesLast10min.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsOverTime[secondLastEpoch])) + } + + for timestamp := range stats.AdsOverTime { + secondLastEpoch = lastEpoch + lastEpoch = timestamp + metrics.AdsBlockedLast10min.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.AdsOverTime[secondLastEpoch])) + } + } func (c *Client) getPHPSessionID() (string, error) { diff --git a/internal/pihole/model.go b/internal/pihole/model.go index 53a2a01..cd4c910 100644 --- a/internal/pihole/model.go +++ b/internal/pihole/model.go @@ -38,6 +38,19 @@ type Stats struct { ForwardDestinations map[string]float64 `json:"forward_destinations"` 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` + GravityLastUpdated GravityLastUpdated `json:gravity_last_updated` +} + +type GravityLastUpdated struct { + FileExists bool `json:"file_exists"` + Absolute int `json:"absolute"` + Relative struct { + Days int `json:"days"` + Hours int `json:"hours"` + Minutes int `json:"minutes"` + } `json:"relative"` } // ToString method returns a string of the current statistics struct.