-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (62 loc) · 2.03 KB
/
main.go
File metadata and controls
70 lines (62 loc) · 2.03 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/inexio/go-monitoringplugin"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"os"
)
var opts struct{
URL string `short:"u" long:"url" description:"The url for requesting the federation check" required:"true"`
ServerName string `short:"n" long:"server-name" description:"The Server name that you specified in the synapse configuration" required:"true"`
}
type CheckFederation struct {
FederationOK bool `json:"FederationOK"`
}
type ErrorAndCode struct {
ExitCode int
Error error
}
func CheckFederationSuccess(url string, serverName string) []ErrorAndCode {
var errSlice []ErrorAndCode
if url == "" || serverName == ""{
errSlice = append(errSlice, ErrorAndCode{2, errors.New("URL and server name is required to send GET request")})
return errSlice
}
url = url + serverName
client := resty.New()
request := client.SetDebugBodyLimit(1000).R()
response, err := request.Get(url)
if err != nil {
errSlice = append(errSlice, ErrorAndCode{3, errors.Wrap(err, "error during http request")})
return errSlice
}
var resp CheckFederation
err = json.Unmarshal(response.Body(), &resp)
if resp.FederationOK != true {
errSlice = append(errSlice, ErrorAndCode{2, errors.New("The federation check is not successful, please check what´s wrong")})
return errSlice
}
errSlice = append(errSlice, ErrorAndCode{0, errors.New("Federation Check succeeded")})
return errSlice
}
func OutputMonitoring(errSlice []ErrorAndCode, defaultMessage string) {
response := monitoringplugin.NewResponse(defaultMessage)
for i := 0; i < len(errSlice); i++ {
response.UpdateStatus(errSlice[i].ExitCode, errSlice[i].Error.Error())
}
response.OutputAndExit()
}
func main() {
var errSlice []ErrorAndCode
var err error
_, err = flags.ParseArgs(&opts, os.Args)
if err != nil {
fmt.Println("error parsing flags")
os.Exit(3)
}
errSlice = CheckFederationSuccess(opts.URL, opts.ServerName)
OutputMonitoring(errSlice, "successfully checked: " + opts.ServerName)
}