feat: add domains and ads blocked in the last 10 min metric

This commit is contained in:
Mohsiur Rahman 2024-04-26 00:44:56 +01:00
parent d826a22fb7
commit bf651965be
4 changed files with 48 additions and 1 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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) {

View File

@ -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.