Merge pull request #227 from santigz/master

Add queries_last_10min metric
This commit is contained in:
Vincent Composieux 2024-04-27 00:23:39 +02:00 committed by GitHub
commit 1b91d9e71b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 1 deletions

View File

@ -223,6 +223,7 @@ scrape_configs:
| pihole_forward_destinations | This represent the number of forward destinations requests made by Pi-hole by destination | | pihole_forward_destinations | This represent the number of forward destinations requests made by Pi-hole by destination |
| pihole_querytypes | This represent the number of queries made by Pi-hole by type | | pihole_querytypes | This represent the number of queries made by Pi-hole by type |
| pihole_status | This represent if Pi-hole is enabled | | 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 |
## Pihole-Exporter Helm Chart ## Pihole-Exporter Helm Chart

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.