-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
115 lines (95 loc) · 2.55 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
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"log"
"net"
"net/http"
"regexp"
"time"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/danielb42/handlekeeper"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
prmListenAddr string
prmInputFile string
prmScrapeIntvl int
prmRadicaleAddr string
prmDebug bool
mtrUp = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "radicale",
Name: "up",
Help: "shows if radicale could be reached via tcp",
},
)
mtrRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "radicale",
Name: "requests",
Help: "number of requests to ressources",
},
[]string{
"type",
"ressource",
},
)
)
func inspectLine(line string) {
pattern := `INFO: (.+) request at (.+/.+)/(.+)? received`
result := regexp.MustCompile(pattern).FindStringSubmatch(line)
if prmDebug {
println("parsed: ", line)
}
if result != nil {
mtrRequests.WithLabelValues(result[1], result[2]).Inc()
}
}
func checkTCP() bool {
conn, err := net.Dial("tcp", prmRadicaleAddr)
if err != nil {
mtrUp.Set(0)
return false
}
conn.Close()
mtrUp.Set(1)
return true
}
func serveMetrics() {
prometheus.MustRegister(mtrUp)
prometheus.MustRegister(mtrRequests)
http.Handle("/metrics", promhttp.Handler())
go func() {
log.Fatalf("Metrics server crashed: %s", http.ListenAndServe(prmListenAddr, nil))
}()
}
func parseFlags() {
kingpin.Flag("listen", "address:port to serve /metrics on").Short('l').Default(":9191").StringVar(&prmListenAddr)
kingpin.Flag("inputfile", "exporter input file (truncated!)").Short('i').Default("/var/log/radicale/radicale_exporter_input.log").StringVar(&prmInputFile)
kingpin.Flag("scrapeinterval", "Prometheus scrape interval").Short('s').Default("15").IntVar(&prmScrapeIntvl)
kingpin.Flag("radicale", "address:port to contact Radicale on").Short('r').Default(":5232").StringVar(&prmRadicaleAddr)
kingpin.Flag("debug", "more output and skip TCP socket check").Short('d').Default("false").BoolVar(&prmDebug)
kingpin.CommandLine.HelpFlag.Hidden()
kingpin.Parse()
}
func main() {
parseFlags()
serveMetrics()
hk, err := handlekeeper.NewHandlekeeper(prmInputFile)
if err != nil {
log.Fatalln("could not open input file")
}
defer hk.Close()
for {
if prmDebug || checkTCP() {
scanner := bufio.NewScanner(hk.Handle)
for scanner.Scan() {
inspectLine(scanner.Text())
}
hk.Handle.Truncate(0)
hk.Handle.Seek(0, 0)
}
time.Sleep(time.Duration(prmScrapeIntvl) * time.Second)
}
}