Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

fix(pihole): convert to perc manually instead of percentage-of-incremental-row #922

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/pihole/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ var (
Ctx: "pihole.dns_queries_percentage",
Type: module.Stacked,
Dims: Dims{
{ID: "queries_cached", Name: "cached", Algo: module.PercentOfAbsolute},
{ID: "ads_blocked_today", Name: "blocked", Algo: module.PercentOfAbsolute},
{ID: "queries_forwarded", Name: "forwarded", Algo: module.PercentOfAbsolute},
{ID: "queries_cached_perc", Name: "cached", Div: precision},
{ID: "ads_blocked_today_perc", Name: "blocked", Div: precision},
{ID: "queries_forwarded_perc", Name: "forwarded", Div: precision},
},
},
// clients
Expand Down
16 changes: 16 additions & 0 deletions modules/pihole/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"time"
)

const (
precision = 1000
)

func (p *Pihole) collect() (map[string]int64, error) {
pmx := new(piholeMetrics)
p.scrapePihole(pmx, true)
Expand Down Expand Up @@ -43,6 +47,11 @@ func collectSummary(mx map[string]int64, pmx *piholeMetrics) {
mx["unique_clients"] = pmx.summary.UniqueClients
mx["blocking_status_enabled"] = boolToInt(pmx.summary.Status == "enabled")
mx["blocking_status_disabled"] = boolToInt(pmx.summary.Status != "enabled")

tot := pmx.summary.QueriesCached + pmx.summary.AdsBlockedToday + pmx.summary.QueriesForwarded
mx["queries_cached_perc"] = calcPercentage(pmx.summary.QueriesCached, tot)
mx["ads_blocked_today_perc"] = calcPercentage(pmx.summary.AdsBlockedToday, tot)
mx["queries_forwarded_perc"] = calcPercentage(pmx.summary.QueriesForwarded, tot)
}

func collectQueryTypes(mx map[string]int64, pmx *piholeMetrics) {
Expand Down Expand Up @@ -179,3 +188,10 @@ func boolToInt(b bool) int64 {
}
return 1
}

func calcPercentage(value, total int64) (v int64) {
if total == 0 {
return 0
}
return int64(float64(value) * 100 / float64(total) * precision)
}
6 changes: 6 additions & 0 deletions modules/pihole/pihole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func TestPihole_Collect(t *testing.T) {
"SRV": 0,
"TXT": 0,
"ads_blocked_today": 0,
"ads_blocked_today_perc": 0,
"ads_percentage_today": 0,
"blocking_status_disabled": 1,
"blocking_status_enabled": 0,
Expand All @@ -124,7 +125,9 @@ func TestPihole_Collect(t *testing.T) {
"dns_queries_today": 0,
"domains_being_blocked": 0,
"queries_cached": 0,
"queries_cached_perc": 0,
"queries_forwarded": 0,
"queries_forwarded_perc": 0,
"top_blocked_domain_a1": 33,
"top_blocked_domain_a2": 66,
"top_client_c1": 33,
Expand Down Expand Up @@ -156,13 +159,16 @@ func TestPihole_Collect_OnlySummary(t *testing.T) {

expected := map[string]int64{
"ads_blocked_today": 0,
"ads_blocked_today_perc": 0,
"ads_percentage_today": 0,
"blocking_status_disabled": 1,
"blocking_status_enabled": 0,
"dns_queries_today": 0,
"domains_being_blocked": 0,
"queries_cached": 0,
"queries_cached_perc": 0,
"queries_forwarded": 0,
"queries_forwarded_perc": 0,
"unique_clients": 0,
}

Expand Down