-
Notifications
You must be signed in to change notification settings - Fork 30
/
Collector.go
165 lines (149 loc) · 4.74 KB
/
Collector.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
package livestatus
import (
"fmt"
"github.com/griesbacher/nagflux/collector"
"github.com/griesbacher/nagflux/data"
"github.com/griesbacher/nagflux/logging"
"github.com/kdar/factorlog"
"time"
)
//Collector fetches data from livestatus.
type Collector struct {
quit chan bool
jobs map[data.Datatype]chan collector.Printable
livestatusConnector *Connector
log *factorlog.FactorLog
}
const (
//Updateinterval on livestatus data.
intervalToCheckLivestatus = time.Duration(2) * time.Minute
//QueryForNotifications livestatusquery for notifications.
QueryForNotifications = `GET log
Columns: type time contact_name message
Filter: type ~ .*NOTIFICATION
Filter: time < %d
Negate:
OutputFormat: csv
`
//QueryForComments livestatusquery for comments
QueryForComments = `GET comments
Columns: host_name service_display_name comment entry_time author entry_type
Filter: entry_time > %d
OutputFormat: csv
`
//QueryForDowntimes livestatusquery for downtimes
QueryForDowntimes = `GET downtimes
Columns: host_name service_display_name comment entry_time author end_time
Filter: entry_time > %d
OutputFormat: csv
`
)
//NewLivestatusCollector constructor, which also starts it immediately.
func NewLivestatusCollector(jobs map[data.Datatype]chan collector.Printable, livestatusConnector *Connector) *Collector {
live := &Collector{make(chan bool, 2), jobs, livestatusConnector, logging.GetLogger()}
go live.run()
return live
}
//Stop signals the collector to stop.
func (live *Collector) Stop() {
live.quit <- true
<-live.quit
live.log.Debug("LivestatusCollector stoped")
}
//Loop which checks livestats for data or waits to quit.
func (live Collector) run() {
live.queryData()
for {
select {
case <-live.quit:
live.quit <- true
return
case <-time.After(intervalToCheckLivestatus):
live.queryData()
}
}
}
//Queries livestatus and returns the data to the gobal queue
func (live Collector) queryData() {
printables := make(chan collector.Printable)
finished := make(chan bool)
go live.requestPrintablesFromLivestatus(QueryForNotifications, true, printables, finished)
go live.requestPrintablesFromLivestatus(QueryForComments, true, printables, finished)
go live.requestPrintablesFromLivestatus(QueryForDowntimes, true, printables, finished)
jobsFinished := 0
for jobsFinished < 3*len(live.jobs) {
for _, j := range live.jobs {
select {
case job := <-printables:
j <- job
case <-finished:
jobsFinished++
case <-time.After(intervalToCheckLivestatus / 3):
live.log.Debug("requestPrintablesFromLivestatus timed out")
}
}
}
}
func (live Collector) requestPrintablesFromLivestatus(query string, addTimestampToQuery bool, printables chan collector.Printable, outerFinish chan bool) {
queryWithTimestamp := query
if addTimestampToQuery {
queryWithTimestamp = addTimestampToLivestatusQuery(query)
}
csv := make(chan []string)
finished := make(chan bool)
go live.livestatusConnector.connectToLivestatus(queryWithTimestamp, csv, finished)
for {
select {
case line := <-csv:
switch query {
case QueryForNotifications:
if printable := live.handleQueryForNotifications(line); printable != nil {
printables <- printable
} else {
live.log.Warn("The notification type is unkown:" + line[0])
}
case QueryForComments:
if len(line) == 6 {
printables <- CommentData{Data{line[0], line[1], line[2], line[3], line[4]}, line[5]}
} else {
live.log.Warn("QueryForComments out of range", line)
}
case QueryForDowntimes:
if len(line) == 6 {
printables <- DowntimeData{Data{line[0], line[1], line[2], line[3], line[4]}, line[5]}
} else {
live.log.Warn("QueryForDowntimes out of range", line)
}
default:
live.log.Fatal("Found unkown query type" + query)
}
case <-finished:
outerFinish <- true
return
case <-time.After(intervalToCheckLivestatus / 3):
live.log.Warn("connectToLivestatus timed out")
}
}
}
func addTimestampToLivestatusQuery(query string) string {
return fmt.Sprintf(query, time.Now().Add(intervalToCheckLivestatus/100*-150).Unix())
}
func (live Collector) handleQueryForNotifications(line []string) *NotificationData {
switch line[0] {
case "HOST NOTIFICATION":
if len(line) == 10 {
//Custom
return &NotificationData{Data{line[4], "", line[9], line[1], line[8]}, line[0], line[5]}
} else if len(line) == 9 {
return &NotificationData{Data{line[4], "", line[7], line[1], line[2]}, line[0], line[5]}
}
case "SERVICE NOTIFICATION":
if len(line) == 11 {
//Custom
return &NotificationData{Data{line[4], line[5], line[10], line[1], line[9]}, line[0], line[6]}
} else if len(line) == 10 {
return &NotificationData{Data{line[4], line[5], line[8], line[1], line[2]}, line[0], line[6]}
}
}
return nil
}