2018-12-05 11:03:41 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
|
|
|
)
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
// how far back to keep the stats
|
|
|
|
const statsHistoryElements = 60 + 1 // +1 for calculating delta
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
// entries for single time period (for example all per-second entries)
|
|
|
|
type statsEntries map[string][statsHistoryElements]float64
|
|
|
|
|
|
|
|
// each periodic stat is a map of arrays
|
|
|
|
type periodicStats struct {
|
2019-02-10 17:47:43 +00:00
|
|
|
entries statsEntries
|
2018-12-05 11:03:41 +00:00
|
|
|
period time.Duration // how long one entry lasts
|
2019-02-10 17:47:43 +00:00
|
|
|
lastRotate time.Time // last time this data was rotated
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
// stats is the DNS server historical statistics
|
2018-12-05 11:03:41 +00:00
|
|
|
type stats struct {
|
2019-02-10 17:47:43 +00:00
|
|
|
perSecond periodicStats
|
|
|
|
perMinute periodicStats
|
|
|
|
perHour periodicStats
|
|
|
|
perDay periodicStats
|
|
|
|
|
|
|
|
requests *counter // total number of requests
|
|
|
|
filtered *counter // total number of filtered requests
|
|
|
|
filteredLists *counter // total number of requests blocked by filter lists
|
|
|
|
filteredSafebrowsing *counter // total number of requests blocked by safebrowsing
|
|
|
|
filteredParental *counter // total number of requests blocked by the parental control
|
|
|
|
whitelisted *counter // total number of requests whitelisted by filter lists
|
|
|
|
safesearch *counter // total number of requests for which safe search rules were applied
|
|
|
|
errorsTotal *counter // total number of errors
|
|
|
|
elapsedTime *histogram // requests duration histogram
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
// initializes an empty stats structure
|
|
|
|
func newStats() *stats {
|
|
|
|
s := &stats{
|
|
|
|
requests: newDNSCounter("requests_total"),
|
|
|
|
filtered: newDNSCounter("filtered_total"),
|
|
|
|
filteredLists: newDNSCounter("filtered_lists_total"),
|
|
|
|
filteredSafebrowsing: newDNSCounter("filtered_safebrowsing_total"),
|
|
|
|
filteredParental: newDNSCounter("filtered_parental_total"),
|
|
|
|
whitelisted: newDNSCounter("whitelisted_total"),
|
|
|
|
safesearch: newDNSCounter("safesearch_total"),
|
|
|
|
errorsTotal: newDNSCounter("errors_total"),
|
|
|
|
elapsedTime: newDNSHistogram("request_duration"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes empty per-sec/minute/hour/day stats
|
|
|
|
s.purgeStats()
|
|
|
|
return s
|
|
|
|
}
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
func initPeriodicStats(periodic *periodicStats, period time.Duration) {
|
2019-02-25 15:56:51 +00:00
|
|
|
periodic.Lock()
|
2019-02-10 17:47:43 +00:00
|
|
|
periodic.entries = statsEntries{}
|
|
|
|
periodic.lastRotate = time.Now()
|
2018-12-05 11:03:41 +00:00
|
|
|
periodic.period = period
|
2019-02-25 15:56:51 +00:00
|
|
|
periodic.Unlock()
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
func (s *stats) purgeStats() {
|
|
|
|
initPeriodicStats(&s.perSecond, time.Second)
|
|
|
|
initPeriodicStats(&s.perMinute, time.Minute)
|
|
|
|
initPeriodicStats(&s.perHour, time.Hour)
|
|
|
|
initPeriodicStats(&s.perDay, time.Hour*24)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *periodicStats) Inc(name string, when time.Time) {
|
|
|
|
// calculate how many periods ago this happened
|
|
|
|
elapsed := int64(time.Since(when) / p.period)
|
2018-12-29 14:23:42 +00:00
|
|
|
// log.Tracef("%s: %v as %v -> [%v]", name, time.Since(when), p.period, elapsed)
|
2018-12-05 11:03:41 +00:00
|
|
|
if elapsed >= statsHistoryElements {
|
|
|
|
return // outside of our timeframe
|
|
|
|
}
|
|
|
|
p.Lock()
|
2019-02-10 17:47:43 +00:00
|
|
|
currentValues := p.entries[name]
|
2018-12-05 11:03:41 +00:00
|
|
|
currentValues[elapsed]++
|
2019-02-10 17:47:43 +00:00
|
|
|
p.entries[name] = currentValues
|
2018-12-05 11:03:41 +00:00
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *periodicStats) Observe(name string, when time.Time, value float64) {
|
|
|
|
// calculate how many periods ago this happened
|
|
|
|
elapsed := int64(time.Since(when) / p.period)
|
2018-12-29 14:23:42 +00:00
|
|
|
// log.Tracef("%s: %v as %v -> [%v]", name, time.Since(when), p.period, elapsed)
|
2018-12-05 11:03:41 +00:00
|
|
|
if elapsed >= statsHistoryElements {
|
|
|
|
return // outside of our timeframe
|
|
|
|
}
|
|
|
|
p.Lock()
|
|
|
|
{
|
|
|
|
countname := name + "_count"
|
2019-02-10 17:47:43 +00:00
|
|
|
currentValues := p.entries[countname]
|
2019-01-25 09:21:57 +00:00
|
|
|
v := currentValues[elapsed]
|
2019-02-10 17:47:43 +00:00
|
|
|
// log.Tracef("Will change p.entries[%s][%d] from %v to %v", countname, elapsed, value, value+1)
|
2019-01-25 09:21:57 +00:00
|
|
|
v++
|
|
|
|
currentValues[elapsed] = v
|
2019-02-10 17:47:43 +00:00
|
|
|
p.entries[countname] = currentValues
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
totalname := name + "_sum"
|
2019-02-10 17:47:43 +00:00
|
|
|
currentValues := p.entries[totalname]
|
2018-12-05 11:03:41 +00:00
|
|
|
currentValues[elapsed] += value
|
2019-02-10 17:47:43 +00:00
|
|
|
p.entries[totalname] = currentValues
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *periodicStats) statsRotate(now time.Time) {
|
|
|
|
p.Lock()
|
2019-02-10 17:47:43 +00:00
|
|
|
rotations := int64(now.Sub(p.lastRotate) / p.period)
|
2018-12-05 11:03:41 +00:00
|
|
|
if rotations > statsHistoryElements {
|
|
|
|
rotations = statsHistoryElements
|
|
|
|
}
|
|
|
|
// calculate how many times we should rotate
|
|
|
|
for r := int64(0); r < rotations; r++ {
|
2019-02-10 17:47:43 +00:00
|
|
|
for key, values := range p.entries {
|
2018-12-05 11:03:41 +00:00
|
|
|
newValues := [statsHistoryElements]float64{}
|
|
|
|
for i := 1; i < len(values); i++ {
|
|
|
|
newValues[i] = values[i-1]
|
|
|
|
}
|
2019-02-10 17:47:43 +00:00
|
|
|
p.entries[key] = newValues
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if rotations > 0 {
|
2019-02-10 17:47:43 +00:00
|
|
|
p.lastRotate = now
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
func (s *stats) statsRotator() {
|
2018-12-05 11:03:41 +00:00
|
|
|
for range time.Tick(time.Second) {
|
|
|
|
now := time.Now()
|
2019-02-10 17:47:43 +00:00
|
|
|
s.perSecond.statsRotate(now)
|
|
|
|
s.perMinute.statsRotate(now)
|
|
|
|
s.perHour.statsRotate(now)
|
|
|
|
s.perDay.statsRotate(now)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// counter that wraps around prometheus Counter but also adds to periodic stats
|
|
|
|
type counter struct {
|
|
|
|
name string // used as key in periodic stats
|
|
|
|
value int64
|
2018-12-29 16:13:00 +00:00
|
|
|
|
|
|
|
sync.Mutex
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDNSCounter(name string) *counter {
|
2018-12-29 14:23:42 +00:00
|
|
|
// log.Tracef("called")
|
2018-12-05 11:03:41 +00:00
|
|
|
return &counter{
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
func (s *stats) incWithTime(c *counter, when time.Time) {
|
|
|
|
s.perSecond.Inc(c.name, when)
|
|
|
|
s.perMinute.Inc(c.name, when)
|
|
|
|
s.perHour.Inc(c.name, when)
|
|
|
|
s.perDay.Inc(c.name, when)
|
2018-12-29 16:13:00 +00:00
|
|
|
c.Lock()
|
2018-12-05 11:03:41 +00:00
|
|
|
c.value++
|
2018-12-29 16:13:00 +00:00
|
|
|
c.Unlock()
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type histogram struct {
|
|
|
|
name string // used as key in periodic stats
|
|
|
|
count int64
|
|
|
|
total float64
|
2018-12-29 16:13:00 +00:00
|
|
|
|
|
|
|
sync.Mutex
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDNSHistogram(name string) *histogram {
|
|
|
|
return &histogram{
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
func (s *stats) observeWithTime(h *histogram, value float64, when time.Time) {
|
|
|
|
s.perSecond.Observe(h.name, when, value)
|
|
|
|
s.perMinute.Observe(h.name, when, value)
|
|
|
|
s.perHour.Observe(h.name, when, value)
|
|
|
|
s.perDay.Observe(h.name, when, value)
|
2018-12-29 16:13:00 +00:00
|
|
|
h.Lock()
|
2018-12-05 11:03:41 +00:00
|
|
|
h.count++
|
|
|
|
h.total += value
|
2018-12-29 16:13:00 +00:00
|
|
|
h.Unlock()
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
// stats
|
|
|
|
// -----
|
2019-02-10 17:47:43 +00:00
|
|
|
func (s *stats) incrementCounters(entry *logEntry) {
|
|
|
|
s.incWithTime(s.requests, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
if entry.Result.IsFiltered {
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.filtered, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
}
|
2018-12-06 13:55:05 +00:00
|
|
|
|
2018-12-06 14:27:38 +00:00
|
|
|
switch entry.Result.Reason {
|
|
|
|
case dnsfilter.NotFilteredWhiteList:
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.whitelisted, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
case dnsfilter.NotFilteredError:
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.errorsTotal, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
case dnsfilter.FilteredBlackList:
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.filteredLists, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
case dnsfilter.FilteredSafeBrowsing:
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.filteredSafebrowsing, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
case dnsfilter.FilteredParental:
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.filteredParental, entry.Time)
|
2018-12-06 14:27:38 +00:00
|
|
|
case dnsfilter.FilteredInvalid:
|
|
|
|
// do nothing
|
|
|
|
case dnsfilter.FilteredSafeSearch:
|
2019-02-10 17:47:43 +00:00
|
|
|
s.incWithTime(s.safesearch, entry.Time)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2019-02-10 17:47:43 +00:00
|
|
|
s.observeWithTime(s.elapsedTime, entry.Elapsed.Seconds(), entry.Time)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
// getAggregatedStats returns aggregated stats data for the 24 hours
|
|
|
|
func (s *stats) getAggregatedStats() map[string]interface{} {
|
2018-12-05 11:03:41 +00:00
|
|
|
const numHours = 24
|
2019-02-10 17:47:43 +00:00
|
|
|
historical := s.generateMapFromStats(&s.perHour, 0, numHours)
|
2018-12-05 11:03:41 +00:00
|
|
|
// sum them up
|
|
|
|
summed := map[string]interface{}{}
|
2019-02-10 17:47:43 +00:00
|
|
|
for key, values := range historical {
|
2018-12-05 11:03:41 +00:00
|
|
|
summedValue := 0.0
|
|
|
|
floats, ok := values.([]float64)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, v := range floats {
|
|
|
|
summedValue += v
|
|
|
|
}
|
|
|
|
summed[key] = summedValue
|
|
|
|
}
|
|
|
|
// don't forget to divide by number of elements in returned slice
|
|
|
|
if val, ok := summed["avg_processing_time"]; ok {
|
|
|
|
if flval, flok := val.(float64); flok {
|
|
|
|
flval /= numHours
|
|
|
|
summed["avg_processing_time"] = flval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
summed["stats_period"] = "24 hours"
|
2019-02-10 17:47:43 +00:00
|
|
|
return summed
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
func (s *stats) generateMapFromStats(stats *periodicStats, start int, end int) map[string]interface{} {
|
2019-02-25 15:56:51 +00:00
|
|
|
stats.RLock()
|
|
|
|
defer stats.RUnlock()
|
|
|
|
|
2018-12-05 11:03:41 +00:00
|
|
|
// clamp
|
|
|
|
start = clamp(start, 0, statsHistoryElements)
|
|
|
|
end = clamp(end, 0, statsHistoryElements)
|
|
|
|
|
|
|
|
avgProcessingTime := make([]float64, 0)
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
count := getReversedSlice(stats.entries[s.elapsedTime.name+"_count"], start, end)
|
|
|
|
sum := getReversedSlice(stats.entries[s.elapsedTime.name+"_sum"], start, end)
|
2018-12-05 11:03:41 +00:00
|
|
|
for i := 0; i < len(count); i++ {
|
|
|
|
var avg float64
|
|
|
|
if count[i] != 0 {
|
|
|
|
avg = sum[i] / count[i]
|
|
|
|
avg *= 1000
|
|
|
|
}
|
|
|
|
avgProcessingTime = append(avgProcessingTime, avg)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := map[string]interface{}{
|
2019-02-10 17:47:43 +00:00
|
|
|
"dns_queries": getReversedSlice(stats.entries[s.requests.name], start, end),
|
|
|
|
"blocked_filtering": getReversedSlice(stats.entries[s.filtered.name], start, end),
|
|
|
|
"replaced_safebrowsing": getReversedSlice(stats.entries[s.filteredSafebrowsing.name], start, end),
|
|
|
|
"replaced_safesearch": getReversedSlice(stats.entries[s.safesearch.name], start, end),
|
|
|
|
"replaced_parental": getReversedSlice(stats.entries[s.filteredParental.name], start, end),
|
2018-12-05 11:03:41 +00:00
|
|
|
"avg_processing_time": avgProcessingTime,
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
// getStatsHistory gets stats history aggregated by the specified time unit
|
|
|
|
// timeUnit is either time.Second, time.Minute, time.Hour, or 24*time.Hour
|
|
|
|
// start is start of the time range
|
|
|
|
// end is end of the time range
|
|
|
|
// returns nil if time unit is not supported
|
|
|
|
func (s *stats) getStatsHistory(timeUnit time.Duration, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
|
2018-12-05 11:03:41 +00:00
|
|
|
var stats *periodicStats
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
switch timeUnit {
|
|
|
|
case time.Second:
|
|
|
|
stats = &s.perSecond
|
|
|
|
case time.Minute:
|
|
|
|
stats = &s.perMinute
|
|
|
|
case time.Hour:
|
|
|
|
stats = &s.perHour
|
|
|
|
case 24 * time.Hour:
|
|
|
|
stats = &s.perDay
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2019-02-10 17:47:43 +00:00
|
|
|
|
|
|
|
if stats == nil {
|
|
|
|
return nil, fmt.Errorf("unsupported time unit: %v", timeUnit)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
2018-12-05 11:03:41 +00:00
|
|
|
// check if start and time times are within supported time range
|
|
|
|
timeRange := timeUnit * statsHistoryElements
|
|
|
|
if startTime.Add(timeRange).Before(now) {
|
2019-02-10 17:47:43 +00:00
|
|
|
return nil, fmt.Errorf("start_time parameter is outside of supported range: %s", startTime.String())
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
if endTime.Add(timeRange).Before(now) {
|
2019-02-10 17:47:43 +00:00
|
|
|
return nil, fmt.Errorf("end_time parameter is outside of supported range: %s", startTime.String())
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate start and end of our array
|
|
|
|
// basically it's how many hours/minutes/etc have passed since now
|
|
|
|
start := int(now.Sub(endTime) / timeUnit)
|
|
|
|
end := int(now.Sub(startTime) / timeUnit)
|
|
|
|
|
|
|
|
// swap them around if they're inverted
|
|
|
|
if start > end {
|
|
|
|
start, end = end, start
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:47:43 +00:00
|
|
|
return s.generateMapFromStats(stats, start, end), nil
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clamp(value, low, high int) int {
|
|
|
|
if value < low {
|
|
|
|
return low
|
|
|
|
}
|
|
|
|
if value > high {
|
|
|
|
return high
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// helper functions for stats
|
|
|
|
// --------------------------
|
|
|
|
func getReversedSlice(input [statsHistoryElements]float64, start int, end int) []float64 {
|
|
|
|
output := make([]float64, 0)
|
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
output = append([]float64{input[i]}, output...)
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|