-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (120 loc) · 4.55 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
package main
import (
"errors"
"flag"
"fmt"
"github.com/inexio/go-monitoringplugin"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"os"
"strconv"
"sync"
"time"
)
// sender credentials
var sendingHomeServer = flag.String("sending-homeserver", "", "Sending Matrix homeserver")
var sendingUsername = flag.String("sending-username", "", "Sending Matrix username")
var sendingPassword = flag.String("sending-password", "", "Sending Matrix password")
// receiver credentials
var receivingHomeServer = flag.String("receiving-homeserver", "", "Receiving Matrix homeserver")
var receivingUsername = flag.String("receiving-username", "", "Receiving Matrix username")
var receivingPassword = flag.String("receiving-password", "", "Receiving Matrix password")
var timeoutSeconds = flag.Int("timeout", 10, "Timeout before throwing critical error if there is no matching message received")
var roomID = flag.String("room-id", "", "ID of the Matrix room")
type ErrorAndCode struct {
ExitCode int
Error error
}
func main() {
var mutex = &sync.Mutex{}
var errSlice []ErrorAndCode
flag.Parse()
if *sendingHomeServer == "" || *sendingPassword == "" || *sendingUsername == "" || *receivingHomeServer == "" || *receivingUsername == "" || *receivingPassword == "" || *roomID == "" {
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
//fmt.Println("Logging into", *sendingHomeServer, "as", *sendingUsername)
client, err := mautrix.NewClient(*sendingHomeServer, "", "")
if err != nil {
errSlice = append(errSlice, ErrorAndCode{3, errors.New("Homeserver not known " + *sendingHomeServer)})
OutputMonitoring(errSlice, "checked")
}
_, err = client.Login(&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *sendingUsername},
Password: *sendingPassword,
StoreCredentials: true,
})
if err != nil {
errSlice = append(errSlice, ErrorAndCode{3, errors.New("Could not login as " + *sendingUsername + " to " + *sendingHomeServer)})
OutputMonitoring(errSlice, "checked")
}
sendingText := "chainTestText" + strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
_, err = client.SendText(id.RoomID(*roomID), sendingText)
if err != nil {
errSlice = append(errSlice, ErrorAndCode{3, errors.New("Could not send message in" + *roomID)})
OutputMonitoring(errSlice, "checked")
}
client2, err := mautrix.NewClient(*receivingHomeServer, "", "")
if err != nil {
errSlice = append(errSlice, ErrorAndCode{3, errors.New("Homeserver not known" + *receivingHomeServer)})
OutputMonitoring(errSlice, "checked")
}
_, err = client2.Login(&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *receivingUsername},
Password: *receivingPassword,
StoreCredentials: true,
})
if err != nil {
errSlice = append(errSlice, ErrorAndCode{3, errors.New("Could not login to " + *sendingHomeServer)})
OutputMonitoring(errSlice, "checked")
}
signal := make(chan bool, 1)
errChan := make(chan bool, 1)
syncer := client2.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
//fmt.Printf("%[5]s <%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body, evt.Timestamp)
err := client2.MarkRead(id.RoomID(*roomID), evt.ID)
if err != nil {
mutex.Lock()
errSlice = append(errSlice, ErrorAndCode{3, errors.New("Could not mark message as read")})
mutex.Unlock()
}
if evt.Content.AsMessage().Body == sendingText {
mutex.Lock()
errSlice = append(errSlice, ErrorAndCode{0, errors.New("The chain check was successfull")})
mutex.Unlock()
signal <- true
}
})
go func() {
err = client2.Sync()
if err != nil {
mutex.Lock()
errSlice = append(errSlice, ErrorAndCode{3, errors.New("sync stopped with error")})
mutex.Unlock()
}
}()
go func() {
time.Sleep(time.Duration(*timeoutSeconds) * time.Second)
errChan <- true
}()
select {
case <-signal:
client2.StopSync()
case <-errChan:
client2.StopSync()
errSlice = append(errSlice, ErrorAndCode{2, errors.New("Message was not received")})
}
OutputMonitoring(errSlice, "checked")
}
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()
}