/
main.go
111 lines (92 loc) · 2.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
package main
import (
"fmt"
"log"
"os"
"strings"
"sync"
"time"
"github.com/byuoitav/shure-monitoring-service"
"github.com/byuoitav/shure-monitoring-service/avevent"
"github.com/byuoitav/shure-monitoring-service/couch"
"github.com/byuoitav/shure-monitoring-service/eventmonitor"
"github.com/spf13/pflag"
)
// _onlineEventInterval specifies how often online events are sent for the receiver
var _onlineEventInterval = 3 * time.Minute
// _onlineThreshold specifies how long a reciever must be online to be considered online
var _onlineThreshold = 30 * time.Second
func main() {
var (
dbAddr string
dbUser string
dbPass string
eventHubAddr string
)
pflag.StringVar(&dbAddr, "db-address", "", "The address to the couch database")
pflag.StringVar(&dbUser, "db-username", "", "The username for the couch database")
pflag.StringVar(&dbPass, "db-password", "", "The password for the couch database")
pflag.StringVar(&eventHubAddr, "eventhub-address", "", "The address for the event hub")
pflag.Parse()
// Get System Information
systemID := os.Getenv("SYSTEM_ID")
parts := strings.Split(systemID, "-")
roomID := fmt.Sprintf("%s-%s", parts[0], parts[1])
device := parts[2]
// If this device is not CP1 then don't monitor
if !strings.EqualFold(device, "CP1") {
log.Printf("Monitoring disabled for this device as it is not CP1...")
// Hang
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}
c, err := couch.New(dbAddr, dbUser, dbPass)
if err != nil {
log.Panicf("Failed to initialize couch: %s", err)
}
recvs, err := c.GetRoomReceivers(roomID)
if err != nil {
log.Panicf("Failed to get receivers from database: %s", err)
}
e, err := avevent.NewLogEmitter(eventHubAddr, systemID)
if err != nil {
log.Panicf("Failed to start log emitter")
}
m := eventmonitor.Service{
EventEmitter: e,
}
log.Printf("Beginning monitoring...")
// Initialize monitoring
for i := range recvs {
go func(r *shure.Receiver) {
_ = m.Monitor(r)
}(&recvs[i])
}
log.Printf("Monitoring initialized on %d receivers", len(recvs))
// Wait 1 minute before reporting the first time
// This allows time to register the receivers as online
time.Sleep(1 * time.Minute)
// report online status for each receiver every 3 minutes
for {
for i := range recvs {
recvs[i].OnlineMu.RLock()
// If the receiver has been online for more than 30 seconds then report it as online
if recvs[i].Online && time.Since(recvs[i].LastUpdated) > _onlineThreshold {
e.Send(shure.Event{
Key: "responsive",
Value: "Ok",
Device: recvs[i].Name,
})
} else {
e.Send(shure.Event{
Key: "responsive",
Value: "No Response",
Device: recvs[i].Name,
})
}
recvs[i].OnlineMu.RUnlock()
}
time.Sleep(_onlineEventInterval)
}
}