-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitoring.go
84 lines (71 loc) · 1.7 KB
/
monitoring.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
package main
import (
"fmt"
"math/rand"
"os"
"sync"
)
type MonitorSystem interface {
handleAlarm(string, string, float64)
generateEventID(string, string) int
}
// MoMonitorSystem receive the alarms of the servers and generate messages
type PrinterMonitorSystem struct {
sync.Mutex
eventid map[string]int
usedIDs map[int]bool
events []string
}
func (m *PrinterMonitorSystem) generateEventID(server string, alarm string) int {
// Initialize eventid map if it is nil
if m.eventid == nil {
m.eventid = make(map[string]int)
}
// Initialize usedIDs map if it is nil
if m.usedIDs == nil {
m.usedIDs = make(map[int]bool)
}
// Check if server+alarm is already in the map
if _, ok := m.eventid[server+alarm]; ok {
return m.eventid[server+alarm]
}
// Generate a random integer
for {
id := rand.Intn(1000000)
if !m.usedIDs[id] {
m.usedIDs[id] = true
m.eventid[server+alarm] = id
return id
}
}
}
func (m *PrinterMonitorSystem) handleAlarm(server string, alarm string, time float64) {
m.Lock()
defer m.Unlock()
// Get time in unix epoch format
e := fmt.Sprintf("%.0f,%s,%s,%v\n", time*60, server, alarm, m.generateEventID(server, alarm))
m.events = append(m.events, e)
}
func (m *PrinterMonitorSystem) WriteEvents(fileName string) {
// Delete the events file if it exists
if _, err := os.Stat(fileName); err == nil {
os.Remove(fileName)
}
// File where events will be saved
f, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer f.Close()
// Write the CSV header for the events file
_, err = f.WriteString("time,server,alarm,eventid\n")
if err != nil {
panic(err)
}
for _, e := range m.events {
_, err := f.WriteString(e)
if err != nil {
panic(err)
}
}
}