Adding configuration of adguard port (#5)

* Added possibility to configure url adguard port
This commit is contained in:
Eldwan Brianne 2021-01-11 18:34:09 +01:00 committed by GitHub
parent 7571d78c9c
commit a4ffbd5f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 23 deletions

View File

@ -1,14 +1,13 @@
FROM --platform=$BUILDPLATFORM golang:1.15-alpine3.12 AS build FROM golang:1.15-alpine3.12 as build
ARG TARGETOS ARG TARGETOS
ARG TARGETARCH ARG TARGETARCH
WORKDIR /tmp/adguard_exporter WORKDIR /tmp/adguard_exporter
RUN apk --no-cache add git alpine-sdk upx RUN apk --no-cache add git alpine-sdk
COPY . . COPY . .
RUN GO111MODULE=on go mod vendor RUN GO111MODULE=on go mod vendor
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags '-s -w' -o adguard_exporter ./ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-s -w' -o adguard_exporter ./
RUN upx -f --brute adguard_exporter
FROM scratch FROM scratch
LABEL name="adguard-exporter" LABEL name="adguard-exporter"

View File

@ -70,6 +70,7 @@ docker run \
-e 'adguard_hostname=192.168.10.252' \ -e 'adguard_hostname=192.168.10.252' \
-e 'adguard_username=admin' \ -e 'adguard_username=admin' \
-e 'adguard_password=mypassword' \ -e 'adguard_password=mypassword' \
-e 'adguard_port=' \ #optional if adguard is not using port 80 (http)/443 (https)
-e 'interval=10s' \ -e 'interval=10s' \
-e 'log_limit=10000' \ -e 'log_limit=10000' \
-e 'server_port=9617' \ -e 'server_port=9617' \
@ -120,7 +121,8 @@ services:
- adguard_protocol=http - adguard_protocol=http
- adguard_hostname=192.168.10.252 - adguard_hostname=192.168.10.252
- adguard_username=admin - adguard_username=admin
- adguard_password=/run/secrets/my-adguard-pass - adguard_password=/run/secrets/my-adguard-pass
- adguard_port= #optional
- interval=10s - interval=10s
- log_limit=10000 - log_limit=10000
``` ```
@ -150,7 +152,8 @@ services:
- adguard_protocol=http - adguard_protocol=http
- adguard_hostname=192.168.10.252 - adguard_hostname=192.168.10.252
- adguard_username=admin - adguard_username=admin
- adguard_password=/run/secrets/my-adguard-pass - adguard_password=/run/secrets/my-adguard-pass
- adguard_port= #optional
- interval=10s - interval=10s
- log_limit=10000 - log_limit=10000
``` ```
@ -219,6 +222,9 @@ scrape_configs:
# Password defined on the Adguard interface # Password defined on the Adguard interface
-adguard_password string (optional) -adguard_password string (optional)
# Port to use to communicate with Adguard API
-adguard_port string (optional)
# Limit for the return log data # Limit for the return log data
-log_limit string (optional) (default "1000") -log_limit string (optional) (default "1000")

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"os"
"reflect" "reflect"
"time" "time"
@ -19,9 +20,10 @@ type Config struct {
AdguardHostname string `config:"adguard_hostname"` AdguardHostname string `config:"adguard_hostname"`
AdguardUsername string `config:"adguard_username"` AdguardUsername string `config:"adguard_username"`
AdguardPassword string `config:"adguard_password"` AdguardPassword string `config:"adguard_password"`
AdguardPort string `config:"adguard_port"`
ServerPort string `config:"server_port"` ServerPort string `config:"server_port"`
Interval time.Duration `config:"interval"` Interval time.Duration `config:"interval"`
LogLimit string `config:"log_limit"` LogLimit string `config:"log_limit"`
} }
func getDefaultConfig() *Config { func getDefaultConfig() *Config {
@ -30,9 +32,10 @@ func getDefaultConfig() *Config {
AdguardHostname: "127.0.0.1", AdguardHostname: "127.0.0.1",
AdguardUsername: "", AdguardUsername: "",
AdguardPassword: "", AdguardPassword: "",
AdguardPort: "80",
ServerPort: "9617", ServerPort: "9617",
Interval: 10 * time.Second, Interval: 10 * time.Second,
LogLimit: "1000", LogLimit: "1000",
} }
} }
@ -48,7 +51,20 @@ func Load() *Config {
cfg := getDefaultConfig() cfg := getDefaultConfig()
err := loader.Load(context.Background(), cfg) err := loader.Load(context.Background(), cfg)
if err != nil { if err != nil {
panic(err) log.Printf("Could not load the configuration...")
os.Exit(1)
}
//Set the adguard port based on the input configuration
if cfg.AdguardPort == "" {
if cfg.AdguardProtocol == "http" {
cfg.AdguardPort = "80"
} else if cfg.AdguardProtocol == "https" {
cfg.AdguardPort = "443"
} else {
log.Printf("protocol %s is invalid. Must be http or https.", cfg.AdguardProtocol)
os.Exit(1)
}
} }
cfg.show() cfg.show()

View File

@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"time" "time"
@ -36,16 +35,13 @@ type Client struct {
} }
// NewClient method initializes a new AdGuard client. // NewClient method initializes a new AdGuard client.
func NewClient(protocol, hostname string, username, password string, interval time.Duration, logLimit string) *Client { func NewClient(protocol, hostname, username, password, adport string, interval time.Duration, logLimit string) *Client {
if protocol != "http" && protocol != "https" {
log.Printf("protocol %s is invalid. Must be http or https.", protocol) temp, err := strconv.Atoi(adport)
os.Exit(1) if err != nil {
} log.Fatal(err)
port = 80
if protocol == "https" {
port = 443
} }
port = uint16(temp)
return &Client{ return &Client{
protocol: protocol, protocol: protocol,
@ -194,7 +190,7 @@ func (c *Client) MakeRequest(url string) []byte {
log.Fatal("An error has occurred when creating HTTP statistics request", err) log.Fatal("An error has occurred when creating HTTP statistics request", err)
} }
req.Host = "adguard.home-lab.io" req.Host = c.hostname
req.Header.Add("User-Agent", "Mozilla/5.0") req.Header.Add("User-Agent", "Mozilla/5.0")
if c.isUsingPassword() { if c.isUsingPassword() {

View File

@ -26,14 +26,14 @@ func main() {
metrics.Init() metrics.Init()
initAdguardClient(conf.AdguardProtocol, conf.AdguardHostname, conf.AdguardUsername, conf.AdguardPassword, conf.Interval, conf.LogLimit) initAdguardClient(conf.AdguardProtocol, conf.AdguardHostname, conf.AdguardUsername, conf.AdguardPassword, conf.AdguardPort, conf.Interval, conf.LogLimit)
initHttpServer(conf.ServerPort) initHttpServer(conf.ServerPort)
handleExitSignal() handleExitSignal()
} }
func initAdguardClient(protocol, hostname string, username, password string, interval time.Duration, logLimit string) { func initAdguardClient(protocol, hostname, username, password, port string, interval time.Duration, logLimit string) {
client := adguard.NewClient(protocol, hostname, username, password, interval, logLimit) client := adguard.NewClient(protocol, hostname, username, password, port, interval, logLimit)
go client.Scrape() go client.Scrape()
} }