-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
87 lines (78 loc) · 2.13 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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu/cert-checks/internal/cert"
"github.com/sensu/sensu-go/types"
)
// Config represents the check plugin config.
type Config struct {
sensu.PluginConfig
Cert string
ServerName string
}
var (
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "cert-checks",
Short: "Inspects certificate data",
Keyspace: "sensu.io/plugins/cert-checks/config",
},
}
options = []*sensu.PluginConfigOption{
{
Path: "cert",
Env: "CHECK_CERT",
Argument: "cert",
Shorthand: "c",
Usage: "URL to certificate. Supports https, tcp, and file schemes",
Value: &plugin.Cert,
},
{
Path: "servername",
Env: "CHECK_SERVER_NAME",
Argument: "servername",
Shorthand: "s",
Usage: "optional TLS servername extension argument",
Value: &plugin.ServerName,
},
}
)
func main() {
useStdin := false
fi, err := os.Stdin.Stat()
if err != nil {
fmt.Printf("Error check stdin: %v\n", err)
panic(err)
}
//Check the Mode bitmask for Named Pipe to indicate stdin is connected
if fi.Mode()&os.ModeNamedPipe != 0 {
useStdin = true
}
check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin)
check.Execute()
}
func checkArgs(event *types.Event) (int, error) {
if plugin.Cert == "" {
return sensu.CheckStateWarning, fmt.Errorf("--cert is required. must be URL to certificate. ex: file:///var/run/app/site.crt, https://dev1.sensu.io:8443, tcp://127.0.0.1:443")
}
return sensu.CheckStateOK, nil
}
func executeCheck(event *types.Event) (int, error) {
ctx := context.Background()
if plugin.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(plugin.Timeout))
defer cancel()
}
metrics, err := cert.CollectMetrics(ctx, plugin.Cert, cert.Config{ServerName: plugin.ServerName})
if err != nil {
fmt.Printf("cert-checks failed with error: %s\n", err.Error())
return sensu.CheckStateCritical, nil
}
fmt.Println(metrics.Output())
return sensu.CheckStateOK, nil
}