-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.go
102 lines (90 loc) · 2.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/boynux/squid-exporter/collector"
"github.com/boynux/squid-exporter/config"
kitlog "github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
)
func init() {
prometheus.MustRegister(versioncollector.NewCollector("squid_exporter"))
}
func main() {
cfg := config.NewConfig()
if *config.VersionFlag {
log.Println(version.Print("squid_exporter"))
os.Exit(0)
}
collector.ExtractServiceTimes = cfg.ExtractServiceTimes
headers := []string{}
if cfg.UseProxyHeader {
headers = append(headers, createProxyHeader(cfg))
}
log.Println("Scraping metrics from", fmt.Sprintf("%s:%d", cfg.SquidHostname, cfg.SquidPort))
e := collector.New(&collector.CollectorConfig{
Hostname: cfg.SquidHostname,
Port: cfg.SquidPort,
Login: cfg.Login,
Password: cfg.Password,
Labels: cfg.Labels,
Headers: headers,
})
prometheus.MustRegister(e)
if cfg.Pidfile != "" {
procExporter := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{
PidFn: func() (int, error) {
content, err := os.ReadFile(cfg.Pidfile)
if err != nil {
return 0, fmt.Errorf("can't read pid file %q: %s", cfg.Pidfile, err)
}
value, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
return 0, fmt.Errorf("can't parse pid file %q: %s", cfg.Pidfile, err)
}
return value, nil
},
Namespace: "squid",
})
prometheus.MustRegister(procExporter)
}
// Serve metrics
http.Handle(cfg.MetricPath, promhttp.Handler())
if cfg.MetricPath != "/" {
landingConfig := web.LandingConfig{
Name: "Squid Exporter",
Description: "Prometheus exporter for Squid caching proxy servers",
HeaderColor: "#15a5be",
Version: version.Info(),
Links: []web.LandingLinks{
{
Address: cfg.MetricPath,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
log.Fatal(err)
}
http.Handle("/", landingPage)
}
systemdSocket := false
toolkitFlags := &web.FlagConfig{
WebListenAddresses: &[]string{cfg.ListenAddress},
WebSystemdSocket: &systemdSocket,
WebConfigFile: &cfg.WebConfigPath,
}
logger := kitlog.NewLogfmtLogger(kitlog.StdlibWriter{})
server := &http.Server{}
log.Fatal(web.ListenAndServe(server, toolkitFlags, logger))
}