-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (148 loc) · 3.76 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/spf13/cobra"
)
type Auth struct {
Access_token string
Expires_at int
Refresh_token string
}
type Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
type Silenced struct {
Metadata Metadata
Expire int `json:"expire"`
Expire_on_resolve bool `json:"expire_on_resolve"`
Creator string `json:"creator"`
Check string `json:"check"`
Subscription string `json:"subscription"`
Begin int `json:"begin"`
}
var (
username, password, host, port string
threshold, timeout int
)
func configureRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sensu-stale-silence-check",
Short: "A Sensu Go check plugin for finding stale silence entries",
RunE: run,
}
cmd.Flags().StringVarP(&username,
"username",
"u",
os.Getenv("SENSU_API_USER"),
"A Sensu Go user with API access.")
cmd.MarkFlagRequired("username")
cmd.Flags().StringVarP(&password,
"password",
"p",
os.Getenv("SENSU_API_PASSWORD"),
"A Sensu Go user's password.")
cmd.MarkFlagRequired("password")
cmd.Flags().StringVarP(&host,
"host",
"H",
os.Getenv("SENSU_API_HOST"),
"The Sensu API host.")
cmd.MarkFlagRequired("host")
cmd.Flags().StringVarP(&port,
"port",
"P",
"8080",
"The port the Sensu API is listening on.")
cmd.Flags().IntVarP(&threshold,
"threshold",
"t",
604800,
"Threshold in seconds to consider a silenced entry stale")
cmd.Flags().IntVarP(&timeout,
"timeout",
"T",
10,
"Time in seconds to consider the API unresponsive")
return cmd
}
func run(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
_ = cmd.Help()
return fmt.Errorf("invalid argument(s) received")
}
return nil
}
func getAuthToken() string {
myauth := Auth{}
client := &http.Client{Timeout: time.Duration(timeout) * time.Second}
req, err := http.NewRequest("GET", "http://"+host+":"+port+"/auth", nil)
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
err2 := json.NewDecoder(resp.Body).Decode(&myauth)
if err2 != nil {
log.Fatal(err2)
}
return myauth.Access_token
}
func querySilenced(token string, silenced2 *[]Silenced) {
bearer := "Bearer " + token
client := &http.Client{Timeout: time.Duration(timeout) * time.Second}
req, err := http.NewRequest("GET", "http://"+host+":"+port+"/api/core/v2/namespaces/default/silenced", nil)
req.Header.Add("Authorization", bearer)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err2 := json.Unmarshal([]byte(body), silenced2)
if err2 != nil {
log.Fatal(err)
}
}
func checkIfSilencedOld(silenced3 []Silenced) {
if len(silenced3) > 0 {
active_entry := false
for i := 0; i < len(silenced3); i++ {
n := time.Unix(int64(silenced3[i].Begin), 0)
duration := time.Since(n)
// Catch silence entries that will never resolve.
if int(duration.Seconds()) > threshold && silenced3[i].Expire == int(-1) && !silenced3[i].Expire_on_resolve {
fmt.Println("A silenced entry " + silenced3[i].Metadata.Name + " has been active since " + n.String())
active_entry = true
}
}
if !active_entry {
fmt.Println("Good news nobody, no stale entries found!")
os.Exit(0)
} else {
os.Exit(1)
}
} else {
fmt.Println("Good news nobody, the silenced endpoint is empty!")
os.Exit(0)
}
}
func main() {
rootCmd := configureRootCommand()
if err := rootCmd.Execute(); err != nil {
log.Fatal(err.Error())
}
token := getAuthToken()
silenced := []Silenced{}
querySilenced(token, &silenced)
checkIfSilencedOld(silenced)
}