-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_puppetserver.go
185 lines (152 loc) · 5.17 KB
/
check_puppetserver.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
184
185
package main
import (
"crypto/tls"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/xorpaul/go-nagios"
)
var debug bool
type QueryResponse struct {
ServerResponse []byte
Time float64
}
// Debugf is a helper function for debug logging if mainCfgSection["debug"] is set
func Debugf(s string) {
if debug {
log.Print("DEBUG " + s)
}
}
func sendQuery(url string, client *http.Client) QueryResponse {
var resp *http.Response
var out []byte
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
os.Exit(3)
}
req.Header.Add("Accept", "*/*")
before := time.Now()
resp, err = client.Do(req)
duration := time.Since(before).Seconds()
Debugf("Sending query " + url + " took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
if err != nil {
Debugf("Error while sending request to " + url + "err: " + err.Error())
nr := nagios.NagiosResult{ExitCode: 3, Text: "Error while sending request: " + err.Error(), Perfdata: "time=" + strconv.FormatFloat(duration, 'f', 5, 64) + "s"}
nagios.NagiosExit(nr)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
nr := nagios.NagiosResult{ExitCode: 2, Text: "Received non 200 HTTP response code from " + url, Perfdata: "time=" + strconv.FormatFloat(duration, 'f', 5, 64) + "s"}
nagios.NagiosExit(nr)
}
out, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
os.Exit(3)
}
Debugf("Response is: " + string(out))
return QueryResponse{ServerResponse: out, Time: duration}
}
func main() {
log.SetFlags(0)
log.SetOutput(os.Stdout)
hostnameOut, err := exec.Command("hostname", "-f").CombinedOutput()
if _, ok := err.(*exec.ExitError); ok { // there is error code
Debugf("WARNING: hostname -f failed with output:" + string(hostnameOut))
}
fqdn := strings.TrimSpace(string(hostnameOut))
var (
hostFlag = flag.String("H", "localhost", "Hostname to query")
uriFlag = flag.String("u", "/status/v1/services", "URI to query, see https://puppet.com/docs/puppet/7/server/status-api/v1/services.html")
portFlag = flag.Int("p", 8140, "Port to send the query to")
warningFlag = flag.Float64("w", 5, "WARNING threshold in seconds")
criticalFlag = flag.Float64("c", 15, "CRITICAL threshold in seconds")
debugFlag = flag.Bool("debug", false, "log debug output")
// tls flags
certFile = flag.String("cert", "/etc/puppetlabs/puppet/ssl/certs/"+fqdn+".pem", "A PEM eoncoded client certificate file")
keyFile = flag.String("key", "/etc/puppetlabs/puppet/ssl/private_keys/"+fqdn+".pem", "A PEM encoded private key file for the client certificate")
)
flag.Parse()
if len(os.Getenv("VIMRUNTIME")) > 0 {
*hostFlag = "localhost"
*certFile = "ssl/cert.pem"
*keyFile = "ssl/key.pem"
*debugFlag = true
*criticalFlag = 0.02
}
if *hostFlag == "" {
log.Println("Hostname parameter -H is mandatory!")
os.Exit(1)
}
if *certFile == "" {
log.Println("Client certificate parameter -cert is mandatory!")
os.Exit(1)
}
if *keyFile == "" {
log.Println("Client certificate key file parameter -key is mandatory!")
os.Exit(1)
}
debug = *debugFlag
// TLS stuff
tlsConfig := &tls.Config{}
tlsConfig.InsecureSkipVerify = true
// initialize http client with defaults
client := &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}
var certFilenames = map[string]string{
"cert": *certFile,
"key": *keyFile,
}
for _, filename := range certFilenames {
if filename != "" {
if _, err := os.Stat(filename); os.IsNotExist(err) {
// generate certs
log.Println("Certificate file: " + filename + " not found! Exiting...\n")
os.Exit(1)
} else {
Debugf("Certificate file: " + filename + " found.\n")
}
}
}
Debugf("Trying to load cert file: " + *certFile + " and key file: " + *keyFile)
mycert, err := tls.LoadX509KeyPair(*certFile, *keyFile)
if err != nil {
panic(err)
}
tlsConfig.Certificates = make([]tls.Certificate, 1)
tlsConfig.Certificates[0] = mycert
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
transport := &http.Transport{TLSClientConfig: tlsConfig}
client = &http.Client{Transport: transport}
url := "https://" + *hostFlag + ":" + strconv.Itoa(*portFlag) + *uriFlag
out := sendQuery(url, client)
time := strconv.FormatFloat(out.Time, 'f', 5, 64)
nr := nagios.NagiosResult{ExitCode: 3, Text: "unexpected result", Perfdata: "time=" + time + "s"}
if len(out.ServerResponse) > 0 {
nr.ExitCode = 0
nr.Text = "Puppet Server looks good, received 200 from " + url + " in " + time + "s"
} else {
nr.ExitCode = 1
nr.Text = "Received empty response for request against " + url
}
if out.Time >= *criticalFlag {
nr.ExitCode = 2
nr.Text = "Response time " + time + "s >= " + strconv.FormatFloat(*criticalFlag, 'f', 2, 64) + "s - " + nr.Text
} else if out.Time >= *warningFlag {
nr.ExitCode = 1
nr.Text = "Response time " + time + "s >= " + strconv.FormatFloat(*warningFlag, 'f', 2, 64) + "s - " + nr.Text
}
// getMetrics(*hostFlag, *portFlag, client)
nagios.NagiosExit(nr)
}
// func getMetrics(host string, port int, client *http.Client) {
// url := "https://" + host + ":" + strconv.Itoa(port) + "/metrics/v2/list"
// out := sendQuery(url, client)
// fmt.Println(out)
// }