-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (122 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu/sensu-go/types"
)
// Config represents the check plugin config.
type Config struct {
sensu.PluginConfig
Url string
CheckString string
HttpTimeout int
TLStimeout int
UserAgent string
}
var (
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-check-http-go",
Short: "A simple replacement for the ruby based http check for sensu",
Keyspace: "sensu.io/plugins/sensu-check-http-go/config",
},
}
options = []*sensu.PluginConfigOption{
&sensu.PluginConfigOption{
Path: "url",
Env: "CHECK_URL",
Argument: "url",
Shorthand: "u",
Default: "",
Usage: "URL to check",
Value: &plugin.Url,
},
&sensu.PluginConfigOption{
Path: "checkstring",
Env: "CHECKSTRING",
Argument: "checkstring",
Shorthand: "c",
Default: "",
Usage: "String to Match",
Value: &plugin.CheckString,
},
&sensu.PluginConfigOption{
Path: "httptimeout",
Env: "HTTPTIMEOUT",
Argument: "timeout",
Shorthand: "t",
Default: 10,
Usage: "Timeout value in seconds",
Value: &plugin.HttpTimeout,
},
&sensu.PluginConfigOption{
Path: "TLSHandshakeTimeout",
Env: "TLSHANDSHAKETIMEOUT",
Argument: "tlstimeout",
Shorthand: "z",
Default: 1000,
Usage: "TLS handshake timeout in milliseconds",
Value: &plugin.TLStimeout,
},
&sensu.PluginConfigOption[string]{
Path: "user-agent",
Env: "CHECK_USER_AGENT",
Argument: "user-agent",
Shorthand: "a",
Default: "Mozilla/5.0 (Commodore 64; AIX 11; HP/UX 12) AppleWebKit/42.20 (KHTML, like Gecko) EvilGoogle/96.0.4664.45 SafariRocks/537.36",
Usage: "Custom user agent for the HTTP request",
Value: &plugin.UserAgent,
},
}
)
func main() {
check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, false)
check.Execute()
}
func checkArgs(event *types.Event) (int, error) {
if len(plugin.Url) == 0 {
return sensu.CheckStateWarning, fmt.Errorf("Please specify an URL ( -u https://example.com ) and a check string ( -c \"farts\" ) for this check to run")
}
return sensu.CheckStateOK, nil
}
func executeCheck(event *types.Event) (int, error) {
c := http.Client{
Timeout: time.Duration(plugin.HttpTimeout) * time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: time.Duration(plugin.TLStimeout) * time.Millisecond,
ResponseHeaderTimeout: time.Duration(plugin.HttpTimeout) * time.Second,
},
}
req, err := http.NewRequest("GET", plugin.Url, nil)
if err != nil {
fmt.Printf("Error creating request: %s", err)
return sensu.CheckStateCritical, nil
}
// Set the user agent
req.Header.Set("User-Agent", plugin.UserAgent)
// Send the request
resp, err := c.Do(req)
if err != nil {
fmt.Printf("URL: %s, Error %s", plugin.Url, err)
return sensu.CheckStateCritical, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// we are we checking for a string? or just the status code?
if resp.StatusCode != 200 {
fmt.Printf("ERROR: %s, status: %v", plugin.Url, resp.StatusCode)
return sensu.CheckStateCritical, nil
} else if plugin.CheckString == "" && resp.StatusCode == 200 {
fmt.Printf("OK: %s, status: %v", plugin.Url, resp.StatusCode)
return sensu.CheckStateOK, nil
} else if resp.StatusCode == 200 && !strings.Contains(string(body), plugin.CheckString) {
fmt.Printf("ERROR: %s, status: %v, String not found: %s", plugin.Url, resp.StatusCode, plugin.CheckString)
return sensu.CheckStateCritical, nil
}
fmt.Printf("OK: %s, status: %v, String found: %s", plugin.Url, resp.StatusCode, plugin.CheckString)
return sensu.CheckStateOK, nil
}