@@ -4,30 +4,58 @@ import (
44 "encoding/json"
55 "fmt"
66 "io/ioutil"
7- "log"
87 "net/http"
98 "net/url"
109 "os"
1110 "strconv"
1211 "strings"
1312 "time"
1413
14+ log "github.com/sirupsen/logrus"
15+
1516 "github.com/eko/pihole-exporter/config"
1617 "github.com/eko/pihole-exporter/internal/metrics"
1718)
1819
20+ type ClientStatus byte
21+
22+ const (
23+ MetricsCollectionInProgress ClientStatus = iota
24+ MetricsCollectionSuccess
25+ MetricsCollectionError
26+ MetricsCollectionTimeout
27+ )
28+
29+ func (status ClientStatus ) String () string {
30+ return []string {"MetricsCollectionInProgress" , "MetricsCollectionSuccess" , "MetricsCollectionError" , "MetricsCollectionTimeout" }[status ]
31+ }
32+
33+ type ClientChannel struct {
34+ Status ClientStatus
35+ Err error
36+ }
37+
38+ func (c * ClientChannel ) String () string {
39+ if c .Err != nil {
40+ return fmt .Sprintf ("ClientChannel<Status: %s, Err: '%s'>" , c .Status , c .Err .Error ())
41+ } else {
42+ return fmt .Sprintf ("ClientChannel<Status: %s, Err: <nil>>" , c .Status )
43+ }
44+ }
45+
1946// Client struct is a PI-Hole client to request an instance of a PI-Hole ad blocker.
2047type Client struct {
2148 httpClient http.Client
2249 interval time.Duration
2350 config * config.Config
51+ Status chan * ClientChannel
2452}
2553
2654// NewClient method initializes a new PI-Hole client.
27- func NewClient (config * config.Config ) * Client {
55+ func NewClient (config * config.Config , envConfig * config. EnvConfig ) * Client {
2856 err := config .Validate ()
2957 if err != nil {
30- log .Print (err )
58+ log .Error (err )
3159 os .Exit (1 )
3260 }
3361
@@ -39,40 +67,34 @@ func NewClient(config *config.Config) *Client {
3967 CheckRedirect : func (req * http.Request , via []* http.Request ) error {
4068 return http .ErrUseLastResponse
4169 },
70+ Timeout : envConfig .Timeout ,
4271 },
72+ Status : make (chan * ClientChannel , 1 ),
4373 }
4474}
4575
4676func (c * Client ) String () string {
4777 return c .config .PIHoleHostname
4878}
4979
50- /*
51- // Metrics scrapes pihole and sets them
52- func (c *Client) Metrics() http.HandlerFunc {
53- return func(writer http.ResponseWriter, request *http.Request) {
54- stats, err := c.getStatistics()
55- if err != nil {
56- writer.WriteHeader(http.StatusBadRequest)
57- _, _ = writer.Write([]byte(err.Error()))
58- return
59- }
80+ func (c * Client ) CollectMetricsAsync (writer http.ResponseWriter , request * http.Request ) {
81+ log .Printf ("Collecting from %s" , c .config .PIHoleHostname )
82+ if stats , err := c .getStatistics (); err == nil {
6083 c .setMetrics (stats )
61-
62- log.Printf("New tick of statistics: %s", stats.ToString())
63- promhttp.Handler().ServeHTTP(writer, request)
84+ c .Status <- & ClientChannel {Status : MetricsCollectionSuccess , Err : nil }
85+ log .Printf ("New tick of statistics from %s: %s" , c .config .PIHoleHostname , stats )
86+ } else {
87+ c .Status <- & ClientChannel {Status : MetricsCollectionError , Err : err }
6488 }
65- }*/
89+ }
6690
6791func (c * Client ) CollectMetrics (writer http.ResponseWriter , request * http.Request ) error {
68-
6992 stats , err := c .getStatistics ()
7093 if err != nil {
7194 return err
7295 }
7396 c .setMetrics (stats )
74-
75- log .Printf ("New tick of statistics from %s: %s" , c , stats )
97+ log .Printf ("New tick of statistics from %s: %s" , c .config .PIHoleHostname , stats )
7698 return nil
7799}
78100
@@ -137,7 +159,7 @@ func (c *Client) getPHPSessionID() (sessionID string) {
137159
138160 resp , err := c .httpClient .Do (req )
139161 if err != nil {
140- log .Printf ("An error has occured during login to PI-Hole: %v" , err )
162+ log .Errorf ("An error has occured during login to PI-Hole: %v" , err )
141163 }
142164
143165 for _ , cookie := range resp .Cookies () {
0 commit comments