Add queries_last_10min metric

This commit is contained in:
santigz 2024-04-21 18:37:31 +02:00
parent d826a22fb7
commit 860c4b6491
4 changed files with 27 additions and 1 deletions

View File

@ -183,7 +183,7 @@ func (c Config) hostnameURL() string {
// PIHoleStatsURL returns the stats url // PIHoleStatsURL returns the stats url
func (c Config) PIHoleStatsURL() string { 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&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&overTimeData10mins&jsonForceObject"
} }
// PIHoleLoginURL returns the login url // PIHoleLoginURL returns the login url

View File

@ -175,6 +175,16 @@ var (
}, },
[]string{"hostname"}, []string{"hostname"},
) )
// QueriesLast10min - Number of queries in the last full slot of 10 minutes
QueriesLast10min = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "queries_last_10min",
Namespace: "pihole",
Help: "Number of queries in the last full slot of 10 minutes",
},
[]string{"hostname"},
)
) )
// Init initializes all Prometheus metrics made available by Pi-hole exporter. // Init initializes all Prometheus metrics made available by Pi-hole exporter.
@ -196,6 +206,7 @@ func Init() {
initMetric("forward_destinations", ForwardDestinations) initMetric("forward_destinations", ForwardDestinations)
initMetric("querytypes", QueryTypes) initMetric("querytypes", QueryTypes)
initMetric("status", Status) initMetric("status", Status)
initMetric("queries_last_10min", QueriesLast10min)
} }
func initMetric(name string, metric *prometheus.GaugeVec) { 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 { for queryType, value := range stats.QueryTypes {
metrics.QueryTypes.WithLabelValues(c.config.PIHoleHostname, queryType).Set(value) 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]))
} }
func (c *Client) getPHPSessionID() (string, error) { func (c *Client) getPHPSessionID() (string, error) {

View File

@ -38,6 +38,7 @@ type Stats struct {
ForwardDestinations map[string]float64 `json:"forward_destinations"` ForwardDestinations map[string]float64 `json:"forward_destinations"`
QueryTypes map[string]float64 `json:"querytypes"` QueryTypes map[string]float64 `json:"querytypes"`
Status string `json:"status"` Status string `json:"status"`
DomainsOverTime map[int]int `json:"domains_over_time"`
} }
// ToString method returns a string of the current statistics struct. // ToString method returns a string of the current statistics struct.