@@ -2,19 +2,27 @@ package main
22
33import (
44 "context"
5+ "crypto/tls"
6+ "errors"
57 "fmt"
8+ "os"
69 "time"
710
811 corev2 "github.com/sensu/core/v2"
912 "github.com/sensu/sensu-plugin-sdk/sensu"
13+ "go.etcd.io/etcd/client/pkg/v3/transport"
1014 clientv3 "go.etcd.io/etcd/client/v3"
1115)
1216
1317// Config represents the check plugin config.
1418type Config struct {
1519 sensu.PluginConfig
16- Url []string
17- Size int64
20+ Url []string
21+ Size int64
22+ CertFile string
23+ KeyFile string
24+ TrustedCAFile string
25+ Timeout int64
1826}
1927
2028var (
@@ -37,10 +45,35 @@ var (
3745 & sensu.PluginConfigOption [int64 ]{
3846 Path : "size" ,
3947 Argument : "size" ,
40- Default : 3_000_000_000 , // Alarm at 3G , default DB is set to 4G
48+ Default : 1_500_000_000 , // Alarm at 1.5G , default DB is set to 2G
4149 Usage : "Maximum aatabase Size" ,
4250 Value : & plugin .Size ,
4351 },
52+ & sensu.PluginConfigOption [string ]{
53+ Path : "cert-file" ,
54+ Argument : "cert-file" ,
55+ Usage : "Path to the cert" ,
56+ Value : & plugin .CertFile ,
57+ },
58+ & sensu.PluginConfigOption [string ]{
59+ Path : "key-file" ,
60+ Argument : "key-file" ,
61+ Usage : "Path to the key" ,
62+ Value : & plugin .KeyFile ,
63+ },
64+ & sensu.PluginConfigOption [string ]{
65+ Path : "trusted-ca-file" ,
66+ Argument : "trusted-ca-file" ,
67+ Usage : "Path to the CA file" ,
68+ Value : & plugin .TrustedCAFile ,
69+ },
70+ & sensu.PluginConfigOption [int64 ]{
71+ Path : "timeout" ,
72+ Argument : "timeout" ,
73+ Usage : "Request timeout" ,
74+ Default : 5 ,
75+ Value : & plugin .Timeout ,
76+ },
4477 }
4578)
4679
@@ -50,15 +83,42 @@ func main() {
5083}
5184
5285func checkArgs (event * corev2.Event ) (int , error ) {
86+
87+ if _ , err := os .Stat (plugin .CertFile ); errors .Is (err , os .ErrNotExist ) {
88+ fmt .Printf ("could not load certificate(%s): %v" , plugin .CertFile , err )
89+ return sensu .CheckStateCritical , nil
90+ }
91+
92+ if _ , err := os .Stat (plugin .KeyFile ); errors .Is (err , os .ErrNotExist ) {
93+ fmt .Printf ("could not load certificate key(%s): %v" , plugin .KeyFile , err )
94+ return sensu .CheckStateCritical , nil
95+ }
96+
97+ if _ , err := os .Stat (plugin .TrustedCAFile ); errors .Is (err , os .ErrNotExist ) {
98+ fmt .Printf ("could not load CA(%s): %v" , plugin .TrustedCAFile , err )
99+ return sensu .CheckStateCritical , nil
100+ }
101+
53102 return sensu .CheckStateOK , nil
54103}
55104
56105func executeCheck (event * corev2.Event ) (int , error ) {
106+ tlsConfig := & tls.Config {}
107+ if len (plugin .CertFile ) > 0 && len (plugin .KeyFile ) > 0 && len (plugin .TrustedCAFile ) > 0 {
108+ tlsInfo := transport.TLSInfo {
109+ CertFile : plugin .CertFile ,
110+ KeyFile : plugin .KeyFile ,
111+ TrustedCAFile : plugin .TrustedCAFile ,
112+ }
113+ tlsConfig , _ = tlsInfo .ClientConfig ()
114+ }
57115
58116 cli , err := clientv3 .New (clientv3.Config {
59117 Endpoints : plugin .Url ,
60- DialTimeout : 5 * time .Second ,
118+ DialTimeout : time .Duration (plugin .Timeout ) * time .Second ,
119+ TLS : tlsConfig ,
61120 })
121+
62122 if err != nil {
63123 fmt .Printf ("could not connect: %s" , err )
64124 return sensu .CheckStateCritical , nil
0 commit comments