-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (100 loc) · 2.61 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
116
117
118
119
120
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
zsend "github.com/blacked/go-zabbix"
docopt "github.com/docopt/docopt-go"
)
var version = "[manual build]"
func main() {
usage := `zabbix-agent-extension-certchecker
Usage:
zabbix-agent-extension-certchecker [options]
zabbix-agent-extension-certchecker [-h --help]
Discovery options:
--discovery Discovery certificate file in directory.
--path <path> Certificate path [default: /etc/nginx/certs].
--suffix-cert <crt> Certificate file suffix [default: crt].
--suffix-key <key> Private key file suffix [default: key].
Certificate check options:
-c --certificate <file> Certificate file
-k --private-key <file> Private key file
-z --zabbix <host> Hostname or IP address of zabbix server
[default: 127.0.0.1].
-p --port <port> Port of zabbix server [default: 10051].
--zabbix-prefix <prefix> Custom prefix for key [default: certificate].
Misc options:
--help Show this screen.
`
args, _ := docopt.Parse(usage, nil, true, version, false)
if args["--discovery"].(bool) {
path := args["--path"].(string)
suffixCert := args["--suffix-cert"].(string)
suffixKey := args["--suffix-key"].(string)
if !strings.HasSuffix(path, "/") {
path = strings.Join([]string{path, "/"}, "")
}
err := discovery(path, suffixCert, suffixKey)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
os.Exit(0)
}
certificate, ok := args["--certificate"].(string)
if !ok {
fmt.Println("need setup --certificate <file>")
os.Exit(1)
}
privateKey, ok := args["--private-key"].(string)
if !ok {
fmt.Println("need setup --private-key <file>")
os.Exit(1)
}
err := checkCertKeyFile(
certificate,
privateKey,
)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
certData, err := parseCertFile(certificate)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
loc, _ := time.LoadLocation("UTC")
now := time.Now().In(loc).Unix()
remaining := certData.NotAfter.Unix() - now
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
zabbix := args["--zabbix"].(string)
port, err := strconv.Atoi(args["--port"].(string))
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
zabbixPrefix := args["--zabbix-prefix"].(string)
var metrics []*zsend.Metric
metrics = createCertificateMetrics(
hostname,
certData,
metrics,
zabbixPrefix,
remaining,
)
packet := zsend.NewPacket(metrics)
sender := zsend.NewSender(
zabbix,
port,
)
sender.Send(packet)
fmt.Println("OK")
}