-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (85 loc) · 2.17 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
// Copyright (c) 2020 mgIT GmbH. All rights reserved.
// Distributed under the Apache License. See LICENSE for details.
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Config struct {
Listen string `json:"listen"`
ClamD struct {
Enable bool `json:"enable"`
ClamDOptions
} `json:"clamd"`
Icap struct {
Enable bool `json:"enable"`
IcapOptions
} `json:"icap"`
}
func run() error {
var (
flagConfig = flag.String("config", "config.json", "configuration file")
)
flag.Parse()
if flag.NArg() != 0 {
flag.Usage()
return fmt.Errorf("invalid number of arguments")
}
cfgFile, err := os.Open(*flagConfig)
if err != nil {
return fmt.Errorf("failed to open config %q: %v", *flagConfig, err)
}
defer cfgFile.Close()
var cfg Config
if err := json.NewDecoder(cfgFile).Decode(&cfg); err != nil {
return fmt.Errorf("failed to decode config %q: %v", *flagConfig, err)
}
registry := prometheus.NewPedanticRegistry()
if cfg.ClamD.Enable {
log.Println("enabling clamd checker")
c := NewClamDChecker(cfg.ClamD.ClamDOptions)
if err := registry.Register(c); err != nil {
return fmt.Errorf("failed to register clamd checker: %v", err)
}
}
if cfg.Icap.Enable {
log.Println("enabling icap checker")
c := NewIcapChecker(cfg.Icap.IcapOptions)
if err := registry.Register(c); err != nil {
return fmt.Errorf("failed to register icap checker: %v", err)
}
}
if cfg.Listen == "" {
cfg.Listen = ":9328"
}
listen, err := net.Listen("tcp", cfg.Listen)
if err != nil {
return fmt.Errorf("failed to listen at %q: %v", cfg.Listen, err)
}
defer listen.Close()
log.Println("listening on", listen.Addr())
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 5 * time.Minute,
}
if err := srv.Serve(listen); err != nil {
return fmt.Errorf("failed to serve: %v", err)
}
return nil
}
func main() {
if err := run(); err != nil {
log.Printf("Error: %v", err)
os.Exit(1)
}
}