-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (122 loc) · 3.95 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"errors"
"fmt"
"os"
customSensu "las/accs/entities-status/sensu"
"github.com/apex/log"
"github.com/sensu/sensu-go/types"
"github.com/sensu/sensu-plugin-sdk/sensu"
)
// Config represents the check plugin config.
type Config struct {
sensu.PluginConfig
Namespace string
Timeout string
RuntimeAssets string
SensuAPIUrl string
SensuAccessToken string
SensuFormat string
Debug bool
}
var (
config = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-entities-status",
Short: "Sensu Entities Status. Get entities status.",
Keyspace: "las/accs/entities-status/config",
},
}
options = []sensu.ConfigOption{
&sensu.PluginConfigOption[string]{
Path: "namespace",
Env: "SENSU_NAMESPACE", // provided by the sensuctl command plugin execution environment
Argument: "namespace",
Shorthand: "n",
Default: "",
Usage: "Sensu Namespace to perform the runbook automation (defaults to $SENSU_NAMESPACE)",
Value: &config.Namespace,
},
&sensu.PluginConfigOption[string]{
Path: "sensu-api-url",
Env: "SENSU_API_URL", // provided by the sensuctl command plugin execution environment
Argument: "sensu-api-url",
Shorthand: "",
Default: "",
Usage: "Sensu API URL (defaults to $SENSU_API_URL)",
Value: &config.SensuAPIUrl,
},
&sensu.PluginConfigOption[string]{
Path: "sensu-access-token",
Env: "SENSU_ACCESS_TOKEN", // provided by the sensuctl command plugin execution environment
Argument: "sensu-access-token",
Shorthand: "",
Default: "",
Usage: "Sensu API Access Token (defaults to $SENSU_ACCESS_TOKEN)",
Value: &config.SensuAccessToken,
},
&sensu.PluginConfigOption[string]{
Path: "sensu-format",
Env: "SENSU_FORMAT", // provided by the sensuctl command plugin execution environment
Argument: "sensu-format",
Shorthand: "",
Default: "tabular",
Usage: "Sensu Format (defaults to $SENSU_FORMAT). Authorized values: tabular, yaml wrapped-json",
Value: &config.SensuFormat,
},
&sensu.PluginConfigOption[bool]{
Path: "sensu-debug",
Env: "SENSU_DEBUG", // provided by the sensuctl command plugin execution environment
Argument: "sensu-debug",
Shorthand: "",
Default: false,
Usage: "Activate debug logs",
Value: &config.Debug,
},
}
)
func main() {
plugin := sensu.NewGoCheck(&config.PluginConfig, options, checkArgs, executeCheck, false)
plugin.Execute()
}
func checkArgs(event *types.Event) (int, error) {
if len(config.SensuAPIUrl) == 0 {
return sensu.CheckStateCritical, errors.New("--sensu-api-url flag or $SENSU_API_URL environment variable must be set")
} else if len(config.Namespace) == 0 {
return sensu.CheckStateCritical, errors.New("--namespace flag or $SENSU_NAMESPACE environment variable must be set")
}
return sensu.CheckStateOK, nil
}
func printResult(statusMap map[string]customSensu.EntityStatus) {
// Depending on format different output is possible
if config.SensuFormat == "tabular" {
customSensu.PrintTabularResult(statusMap)
} else if config.SensuFormat == "yaml" {
customSensu.PrintYAMLResult(statusMap)
} else if config.SensuFormat == "wrapped-json" {
customSensu.PrintJSONResult(statusMap)
} else {
fmt.Fprintln(os.Stderr, "Invalid format output")
}
}
func executeCheck(event *types.Event) (int, error) {
if config.Debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.FatalLevel)
}
endpointURL := fmt.Sprintf("%s/api/core/v2/namespaces/%s/events",
config.SensuAPIUrl,
config.Namespace,
)
header := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", config.SensuAccessToken),
}
evts, err := customSensu.EventExtractJSONWithHeader(endpointURL, header, nil)
if err != nil {
return sensu.CheckStateCritical, err
}
entitiesStatus := customSensu.GetEntitiesStatus(evts)
printResult(entitiesStatus)
return sensu.CheckStateOK, nil
}