Skip to content

Commit

Permalink
Merge pull request #227 from santigz/master
Browse files Browse the repository at this point in the history
Add queries_last_10min metric
  • Loading branch information
eko committed Apr 26, 2024
2 parents d826a22 + 031d2a2 commit 1b91d9e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -223,6 +223,7 @@ scrape_configs:
| 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_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
Expand Down
2 changes: 1 addition & 1 deletion config/configuration.go
Expand Up @@ -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&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&overTimeData10mins&jsonForceObject"
}

// PIHoleLoginURL returns the login url
Expand Down
11 changes: 11 additions & 0 deletions internal/metrics/metrics.go
Expand Up @@ -175,6 +175,16 @@ var (
},
[]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.
Expand All @@ -196,6 +206,7 @@ func Init() {
initMetric("forward_destinations", ForwardDestinations)
initMetric("querytypes", QueryTypes)
initMetric("status", Status)
initMetric("queries_last_10min", QueriesLast10min)
}

func initMetric(name string, metric *prometheus.GaugeVec) {
Expand Down
14 changes: 14 additions & 0 deletions internal/pihole/client.go
Expand Up @@ -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)
}

// 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) {
Expand Down
1 change: 1 addition & 0 deletions internal/pihole/model.go
Expand Up @@ -38,6 +38,7 @@ 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"`
}

// ToString method returns a string of the current statistics struct.
Expand Down

0 comments on commit 1b91d9e

Please sign in to comment.