Merge pull request #245 from keyvanakbary/master

Add pihole_ads_last_10min metric
This commit is contained in:
Vincent Composieux 2024-10-19 11:53:28 +02:00 committed by GitHub
commit 69abc2dac7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 13 deletions

View File

@ -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: forward_destinations
2019/05/09 20:19:52 New Prometheus metric registered: querytypes 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: 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:52 Starting HTTP server
2019/05/09 20:19:54 New tick of statistics: 648 ads blocked / 66796 total DNS querie 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_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 | | 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 ## Pihole-Exporter Helm Chart

View File

@ -185,6 +185,16 @@ var (
}, },
[]string{"hostname"}, []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. // Init initializes all Prometheus metrics made available by Pi-hole exporter.
@ -207,6 +217,7 @@ func Init() {
initMetric("querytypes", QueryTypes) initMetric("querytypes", QueryTypes)
initMetric("status", Status) initMetric("status", Status)
initMetric("queries_last_10min", QueriesLast10min) initMetric("queries_last_10min", QueriesLast10min)
initMetric("ads_last_10min", AdsLast10min)
} }
func initMetric(name string, metric *prometheus.GaugeVec) { func initMetric(name string, metric *prometheus.GaugeVec) {

View File

@ -102,6 +102,22 @@ func (c *Client) GetHostname() string {
return c.config.PIHoleHostname 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) { func (c *Client) setMetrics(stats *Stats) {
metrics.DomainsBlocked.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsBeingBlocked)) metrics.DomainsBlocked.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsBeingBlocked))
metrics.DNSQueriesToday.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DNSQueriesToday)) 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) 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. metrics.QueriesLast10min.WithLabelValues(c.config.PIHoleHostname).Set(latestEpochStats(stats.DomainsOverTime))
// The last epoch is the current in-progress time slot, with queries still being added. metrics.AdsLast10min.WithLabelValues(c.config.PIHoleHostname).Set(latestEpochStats(stats.AdsOverTime))
// 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

@ -39,6 +39,7 @@ type Stats struct {
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"` 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. // ToString method returns a string of the current statistics struct.