mirror of
https://github.com/ebrianne/adguard-exporter.git
synced 2025-01-18 11:21:20 +01:00
Added more metrics. HTTP protocol only supported
This commit is contained in:
parent
7cbc8b8893
commit
86c12b3921
@ -8,7 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
b64 "encoding/base64"
|
|
||||||
|
|
||||||
"github.com/ebrianne/adguard-exporter/internal/metrics"
|
"github.com/ebrianne/adguard-exporter/internal/metrics"
|
||||||
)
|
)
|
||||||
@ -24,15 +23,13 @@ type Client struct {
|
|||||||
protocol string
|
protocol string
|
||||||
hostname string
|
hostname string
|
||||||
port uint16
|
port uint16
|
||||||
username string
|
b64password string
|
||||||
password string
|
|
||||||
sessionID string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient method initializes a new AdGuard client.
|
// NewClient method initializes a new AdGuard client.
|
||||||
func NewClient(protocol, hostname string, port uint16, username, password string, interval time.Duration) *Client {
|
func NewClient(protocol, hostname string, port uint16, b64password string, interval time.Duration) *Client {
|
||||||
if protocol != "http" && protocol != "https" {
|
if protocol != "http" {
|
||||||
log.Printf("protocol %s is invalid. Must be http or https.", protocol)
|
log.Printf("protocol %s is invalid. Must be http.", protocol)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,14 +37,9 @@ func NewClient(protocol, hostname string, port uint16, username, password string
|
|||||||
protocol: protocol,
|
protocol: protocol,
|
||||||
hostname: hostname,
|
hostname: hostname,
|
||||||
port: port,
|
port: port,
|
||||||
username: username,
|
b64password: b64password,
|
||||||
password: password,
|
|
||||||
interval: interval,
|
interval: interval,
|
||||||
httpClient: http.Client{
|
httpClient: http.Client{},
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
||||||
return http.ErrUseLastResponse
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +57,29 @@ func (c *Client) Scrape() {
|
|||||||
|
|
||||||
func (c *Client) setMetrics(stats *Stats) {
|
func (c *Client) setMetrics(stats *Stats) {
|
||||||
metrics.AvgProcessingTime.WithLabelValues(c.hostname).Set(float64(stats.AvgProcessingTime))
|
metrics.AvgProcessingTime.WithLabelValues(c.hostname).Set(float64(stats.AvgProcessingTime))
|
||||||
|
metrics.DnsQueries.WithLabelValues(c.hostname).Set(float64(stats.DnsQueries))
|
||||||
|
metrics.BlockedFiltering.WithLabelValues(c.hostname).Set(float64(stats.BlockedFiltering))
|
||||||
|
metrics.ParentalFiltering.WithLabelValues(c.hostname).Set(float64(stats.ParentalFiltering))
|
||||||
|
metrics.SafeBrowsingFiltering.WithLabelValues(c.hostname).Set(float64(stats.SafeBrowsingFiltering))
|
||||||
|
metrics.SafeSearchFiltering.WithLabelValues(c.hostname).Set(float64(stats.SafeSearchFiltering))
|
||||||
|
|
||||||
|
for l := range stats.TopQueries {
|
||||||
|
for domain, value := range stats.TopQueries[l] {
|
||||||
|
metrics.TopQueries.WithLabelValues(c.hostname, domain).Set(float64(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for l := range stats.TopBlocked {
|
||||||
|
for domain, value := range stats.TopBlocked[l] {
|
||||||
|
metrics.TopBlocked.WithLabelValues(c.hostname, domain).Set(float64(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for l := range stats.TopClients {
|
||||||
|
for source, value := range stats.TopClients[l] {
|
||||||
|
metrics.TopClients.WithLabelValues(c.hostname, source).Set(float64(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getStatistics() *Stats {
|
func (c *Client) getStatistics() *Stats {
|
||||||
@ -74,7 +89,7 @@ func (c *Client) getStatistics() *Stats {
|
|||||||
|
|
||||||
req, err := http.NewRequest("GET", statsURL, nil)
|
req, err := http.NewRequest("GET", statsURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("An error has occured when creating HTTP statistics request", err)
|
log.Fatal("An error has occured when creating HTTP statistics request ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.isUsingPassword() {
|
if c.isUsingPassword() {
|
||||||
@ -99,15 +114,10 @@ func (c *Client) getStatistics() *Stats {
|
|||||||
return &stats
|
return &stats
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) basicAuth() string {
|
|
||||||
auth := c.username + ":" + c.password
|
|
||||||
return base64.StdEncoding.EncodeToString([]byte(auth))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) isUsingPassword() bool {
|
func (c *Client) isUsingPassword() bool {
|
||||||
return len(c.password) > 0
|
return len(c.b64password) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) authenticateRequest(req *http.Request) {
|
func (c *Client) authenticateRequest(req *http.Request) {
|
||||||
req.Header.Add("Authorization", "Basic " + c.basicAuth())
|
req.Header.Add("Authorization", "Basic " + c.b64password)
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,17 @@ const (
|
|||||||
|
|
||||||
type Stats struct {
|
type Stats struct {
|
||||||
AvgProcessingTime float64 `json:"avg_processing_time"`
|
AvgProcessingTime float64 `json:"avg_processing_time"`
|
||||||
|
DnsQueries int `json:"num_dns_queries"`
|
||||||
|
BlockedFiltering int `json:"num_blocked_filtering"`
|
||||||
|
ParentalFiltering int `json:"num_replaced_parental"`
|
||||||
|
SafeBrowsingFiltering int `json:"num_replaced_safebrowsing"`
|
||||||
|
SafeSearchFiltering int `json:"num_replaced_safesearch"`
|
||||||
|
TopQueries []map[string]int `json:"top_queried_domains"`
|
||||||
|
TopBlocked []map[string]int `json:"top_blocked_domains"`
|
||||||
|
TopClients []map[string]int `json:"top_clients"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToString method returns a string of the current statistics struct.
|
// ToString method returns a string of the current statistics struct.
|
||||||
func (s *Stats) ToString() string {
|
func (s *Stats) ToString() string {
|
||||||
return fmt.Sprintf("Average Processing Time %d s", s.AvgProcessingTime)
|
return fmt.Sprintf("%d ads blocked / %d total DNS queries", s.BlockedFiltering, s.DnsQueries)
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,91 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{"hostname"},
|
[]string{"hostname"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DnsQueries = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "num_dns_queries",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "Number of DNS queries",
|
||||||
|
},
|
||||||
|
[]string{"hostname"},
|
||||||
|
)
|
||||||
|
|
||||||
|
BlockedFiltering = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "num_blocked_filtering",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the number of domains blocked",
|
||||||
|
},
|
||||||
|
[]string{"hostname"},
|
||||||
|
)
|
||||||
|
|
||||||
|
ParentalFiltering = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "num_replaced_parental",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the number of domains blocked (parental)",
|
||||||
|
},
|
||||||
|
[]string{"hostname"},
|
||||||
|
)
|
||||||
|
|
||||||
|
SafeBrowsingFiltering = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "num_replaced_safebrowsing",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the number of domains blocked (safe browsing)",
|
||||||
|
},
|
||||||
|
[]string{"hostname"},
|
||||||
|
)
|
||||||
|
|
||||||
|
SafeSearchFiltering = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "num_replaced_safesearch",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the number of domains blocked (safe search)",
|
||||||
|
},
|
||||||
|
[]string{"hostname"},
|
||||||
|
)
|
||||||
|
|
||||||
|
TopQueries = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "top_queried_domains",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the top queried domains",
|
||||||
|
},
|
||||||
|
[]string{"hostname", "domain"},
|
||||||
|
)
|
||||||
|
|
||||||
|
TopBlocked = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "top_blocked_domains",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the top bloacked domains",
|
||||||
|
},
|
||||||
|
[]string{"hostname", "domain"},
|
||||||
|
)
|
||||||
|
|
||||||
|
TopClients = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "top_clients",
|
||||||
|
Namespace: "adguard",
|
||||||
|
Help: "This represent the top clients",
|
||||||
|
},
|
||||||
|
[]string{"hostname", "client"},
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init initializes all Prometheus metrics made available by AdGuard exporter.
|
// Init initializes all Prometheus metrics made available by AdGuard exporter.
|
||||||
func Init() {
|
func Init() {
|
||||||
initMetric("avg_processing_time", AvgProcessingTime)
|
initMetric("avg_processing_time", AvgProcessingTime)
|
||||||
|
initMetric("num_dns_queries", DnsQueries)
|
||||||
|
initMetric("num_blocked_filtering", BlockedFiltering)
|
||||||
|
initMetric("num_replaced_parental", ParentalFiltering)
|
||||||
|
initMetric("num_replaced_safebrowsing", SafeBrowsingFiltering)
|
||||||
|
initMetric("num_replaced_safesearch", SafeSearchFiltering)
|
||||||
|
initMetric("top_queried_domains", TopQueries)
|
||||||
|
initMetric("top_blocked_domains", TopBlocked)
|
||||||
|
initMetric("top_clients", TopClients)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initMetric(name string, metric *prometheus.GaugeVec) {
|
func initMetric(name string, metric *prometheus.GaugeVec) {
|
||||||
|
13
main.go
13
main.go
@ -6,6 +6,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
"github.com/ebrianne/adguard-exporter/config"
|
"github.com/ebrianne/adguard-exporter/config"
|
||||||
"github.com/ebrianne/adguard-exporter/internal/metrics"
|
"github.com/ebrianne/adguard-exporter/internal/metrics"
|
||||||
@ -32,8 +33,18 @@ func main() {
|
|||||||
handleExitSignal()
|
handleExitSignal()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func basicAuth(username, password string) string {
|
||||||
|
auth := username + ":" + password
|
||||||
|
return base64.StdEncoding.EncodeToString([]byte(auth))
|
||||||
|
}
|
||||||
|
|
||||||
func initAdguardClient(protocol, hostname string, port uint16, username, password string, interval time.Duration) {
|
func initAdguardClient(protocol, hostname string, port uint16, username, password string, interval time.Duration) {
|
||||||
client := adguard.NewClient(protocol, hostname, port, username, password, interval)
|
b64password := ""
|
||||||
|
if len(username) > 0 && len(password) > 0 {
|
||||||
|
b64password = basicAuth(username, password)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := adguard.NewClient(protocol, hostname, port, b64password, interval)
|
||||||
go client.Scrape()
|
go client.Scrape()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user