2019-05-08 23:45:04 +02:00
|
|
|
package pihole
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-06-06 00:03:29 +02:00
|
|
|
"os"
|
2019-05-08 23:45:04 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-01-05 20:38:33 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
"github.com/eko/pihole-exporter/config"
|
2019-05-08 23:45:04 +02:00
|
|
|
"github.com/eko/pihole-exporter/internal/metrics"
|
|
|
|
)
|
|
|
|
|
2021-12-26 23:36:30 +01:00
|
|
|
type ClientStatus byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
MetricsCollectionInProgress ClientStatus = iota
|
|
|
|
MetricsCollectionSuccess
|
|
|
|
MetricsCollectionError
|
|
|
|
MetricsCollectionTimeout
|
|
|
|
)
|
|
|
|
|
|
|
|
func (status ClientStatus) String() string {
|
|
|
|
return []string{"MetricsCollectionInProgress", "MetricsCollectionSuccess", "MetricsCollectionError", "MetricsCollectionTimeout"}[status]
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientChannel struct {
|
|
|
|
Status ClientStatus
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientChannel) String() string {
|
|
|
|
if c.Err != nil {
|
|
|
|
return fmt.Sprintf("ClientChannel<Status: %s, Err: '%s'>", c.Status, c.Err.Error())
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("ClientChannel<Status: %s, Err: <nil>>", c.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:11:31 +02:00
|
|
|
// Client struct is a PI-Hole client to request an instance of a PI-Hole ad blocker.
|
2019-05-08 23:45:04 +02:00
|
|
|
type Client struct {
|
2021-12-26 23:36:30 +01:00
|
|
|
httpClient http.Client
|
|
|
|
interval time.Duration
|
|
|
|
config *config.Config
|
|
|
|
Status chan *ClientChannel
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 21:11:31 +02:00
|
|
|
// NewClient method initializes a new PI-Hole client.
|
2022-01-05 22:05:32 +01:00
|
|
|
func NewClient(config *config.Config, envConfig *config.EnvConfig) *Client {
|
2021-08-16 23:55:05 +02:00
|
|
|
err := config.Validate()
|
|
|
|
if err != nil {
|
2022-01-05 20:38:33 +01:00
|
|
|
log.Error(err)
|
2020-06-06 00:03:29 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:25:14 +01:00
|
|
|
log.Printf("Creating client with config %s\n", config)
|
2021-12-10 04:48:28 +01:00
|
|
|
|
2021-12-10 15:39:22 +01:00
|
|
|
return &Client{
|
2021-08-16 23:55:05 +02:00
|
|
|
config: config,
|
2019-05-08 23:45:04 +02:00
|
|
|
httpClient: http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
2022-01-05 22:05:32 +01:00
|
|
|
Timeout: envConfig.Timeout,
|
2019-05-08 23:45:04 +02:00
|
|
|
},
|
2021-12-26 23:36:30 +01:00
|
|
|
Status: make(chan *ClientChannel, 1),
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
2021-12-10 04:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) String() string {
|
|
|
|
return c.config.PIHoleHostname
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:36:30 +01:00
|
|
|
func (c *Client) CollectMetricsAsync(writer http.ResponseWriter, request *http.Request) {
|
2021-12-26 23:36:30 +01:00
|
|
|
log.Printf("Collecting from %s", c.config.PIHoleHostname)
|
2021-12-26 23:36:30 +01:00
|
|
|
if stats, err := c.getStatistics(); err == nil {
|
2019-05-09 21:11:31 +02:00
|
|
|
c.setMetrics(stats)
|
2021-12-26 23:36:30 +01:00
|
|
|
c.Status <- &ClientChannel{Status: MetricsCollectionSuccess, Err: nil}
|
2021-12-26 23:36:30 +01:00
|
|
|
log.Printf("New tick of statistics from %s: %s", c.config.PIHoleHostname, stats)
|
2021-12-26 23:36:30 +01:00
|
|
|
} else {
|
|
|
|
c.Status <- &ClientChannel{Status: MetricsCollectionError, Err: err}
|
2019-05-09 21:11:31 +02:00
|
|
|
}
|
2021-12-26 23:36:30 +01:00
|
|
|
}
|
2021-12-10 15:39:22 +01:00
|
|
|
|
|
|
|
func (c *Client) CollectMetrics(writer http.ResponseWriter, request *http.Request) error {
|
2021-12-10 04:48:28 +01:00
|
|
|
stats, err := c.getStatistics()
|
|
|
|
if err != nil {
|
2021-12-10 15:39:22 +01:00
|
|
|
return err
|
2021-12-10 04:48:28 +01:00
|
|
|
}
|
|
|
|
c.setMetrics(stats)
|
2021-12-26 23:36:30 +01:00
|
|
|
log.Printf("New tick of statistics from %s: %s", c.config.PIHoleHostname, stats)
|
2021-12-10 15:39:22 +01:00
|
|
|
return nil
|
2021-12-10 04:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetHostname() string {
|
|
|
|
return c.config.PIHoleHostname
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:11:31 +02:00
|
|
|
func (c *Client) setMetrics(stats *Stats) {
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.DomainsBlocked.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DomainsBeingBlocked))
|
|
|
|
metrics.DNSQueriesToday.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DNSQueriesToday))
|
|
|
|
metrics.AdsBlockedToday.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.AdsBlockedToday))
|
|
|
|
metrics.AdsPercentageToday.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.AdsPercentageToday))
|
|
|
|
metrics.UniqueDomains.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.UniqueDomains))
|
|
|
|
metrics.QueriesForwarded.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.QueriesForwarded))
|
|
|
|
metrics.QueriesCached.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.QueriesCached))
|
|
|
|
metrics.ClientsEverSeen.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.ClientsEverSeen))
|
|
|
|
metrics.UniqueClients.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.UniqueClients))
|
|
|
|
metrics.DNSQueriesAllTypes.WithLabelValues(c.config.PIHoleHostname).Set(float64(stats.DNSQueriesAllTypes))
|
|
|
|
|
|
|
|
metrics.Reply.WithLabelValues(c.config.PIHoleHostname, "no_data").Set(float64(stats.ReplyNoData))
|
|
|
|
metrics.Reply.WithLabelValues(c.config.PIHoleHostname, "nx_domain").Set(float64(stats.ReplyNxDomain))
|
|
|
|
metrics.Reply.WithLabelValues(c.config.PIHoleHostname, "cname").Set(float64(stats.ReplyCname))
|
|
|
|
metrics.Reply.WithLabelValues(c.config.PIHoleHostname, "ip").Set(float64(stats.ReplyIP))
|
2019-05-09 21:11:31 +02:00
|
|
|
|
|
|
|
var isEnabled int = 0
|
|
|
|
if stats.Status == enabledStatus {
|
|
|
|
isEnabled = 1
|
|
|
|
}
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.Status.WithLabelValues(c.config.PIHoleHostname).Set(float64(isEnabled))
|
2019-05-09 21:11:31 +02:00
|
|
|
|
|
|
|
for domain, value := range stats.TopQueries {
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.TopQueries.WithLabelValues(c.config.PIHoleHostname, domain).Set(float64(value))
|
2019-05-09 21:11:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for domain, value := range stats.TopAds {
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.TopAds.WithLabelValues(c.config.PIHoleHostname, domain).Set(float64(value))
|
2019-05-09 21:11:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for source, value := range stats.TopSources {
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.TopSources.WithLabelValues(c.config.PIHoleHostname, source).Set(float64(value))
|
2019-05-09 21:11:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for destination, value := range stats.ForwardDestinations {
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.ForwardDestinations.WithLabelValues(c.config.PIHoleHostname, destination).Set(value)
|
2019-05-09 21:11:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for queryType, value := range stats.QueryTypes {
|
2021-08-16 23:55:05 +02:00
|
|
|
metrics.QueryTypes.WithLabelValues(c.config.PIHoleHostname, queryType).Set(value)
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 21:03:33 +02:00
|
|
|
func (c *Client) getPHPSessionID() (sessionID string) {
|
2021-08-16 23:55:05 +02:00
|
|
|
values := url.Values{"pw": []string{c.config.PIHolePassword}}
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
req, err := http.NewRequest("POST", c.config.PIHoleLoginURL(), strings.NewReader(values.Encode()))
|
2019-05-08 23:45:04 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("An error has occured when creating HTTP statistics request", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Add("Content-Length", strconv.Itoa(len(values.Encode())))
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2022-02-10 11:33:09 +01:00
|
|
|
log.Errorf("An error has occured during login to PI-Hole: %v", err)
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cookie := range resp.Cookies() {
|
|
|
|
if cookie.Name == "PHPSESSID" {
|
|
|
|
sessionID = cookie.Value
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 21:03:33 +02:00
|
|
|
return
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
func (c *Client) getStatistics() (*Stats, error) {
|
|
|
|
stats := new(Stats)
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
statsURL := c.config.PIHoleStatsURL()
|
2019-05-08 23:45:04 +02:00
|
|
|
|
2020-04-07 00:31:30 +02:00
|
|
|
if c.isUsingApiToken() {
|
2021-08-16 23:55:05 +02:00
|
|
|
statsURL = fmt.Sprintf("%s&auth=%s", statsURL, c.config.PIHoleApiToken)
|
2020-04-07 00:31:30 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 23:45:04 +02:00
|
|
|
req, err := http.NewRequest("GET", statsURL, nil)
|
|
|
|
if err != nil {
|
2021-08-16 23:55:05 +02:00
|
|
|
return nil, fmt.Errorf("an error has occured when creating HTTP statistics request: %w", err)
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:31:30 +02:00
|
|
|
if c.isUsingPassword() {
|
2019-05-10 21:03:33 +02:00
|
|
|
c.authenticateRequest(req)
|
|
|
|
}
|
2019-05-08 23:45:04 +02:00
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2021-08-16 23:55:05 +02:00
|
|
|
return nil, fmt.Errorf("an error has occured during retrieving PI-Hole statistics: %w", err)
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2021-08-16 23:55:05 +02:00
|
|
|
return nil, fmt.Errorf("unable to read PI-Hole statistics HTTP response: %w", err)
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
err = json.Unmarshal(body, stats)
|
2019-05-08 23:45:04 +02:00
|
|
|
if err != nil {
|
2021-08-16 23:55:05 +02:00
|
|
|
return nil, fmt.Errorf("unable to unmarshal PI-Hole statistics to statistics struct model: %w", err)
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 23:55:05 +02:00
|
|
|
return stats, nil
|
2019-05-08 23:45:04 +02:00
|
|
|
}
|
2019-05-10 21:03:33 +02:00
|
|
|
|
2020-04-07 00:31:30 +02:00
|
|
|
func (c *Client) isUsingPassword() bool {
|
2021-08-16 23:55:05 +02:00
|
|
|
return len(c.config.PIHolePassword) > 0
|
2019-05-10 21:03:33 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:31:30 +02:00
|
|
|
func (c *Client) isUsingApiToken() bool {
|
2021-08-16 23:55:05 +02:00
|
|
|
return len(c.config.PIHoleApiToken) > 0
|
2020-04-07 00:31:30 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 21:03:33 +02:00
|
|
|
func (c *Client) authenticateRequest(req *http.Request) {
|
2020-04-07 00:31:30 +02:00
|
|
|
cookie := http.Cookie{Name: "PHPSESSID", Value: c.getPHPSessionID()}
|
2019-05-10 21:03:33 +02:00
|
|
|
req.AddCookie(&cookie)
|
|
|
|
}
|