-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (170 loc) · 4.86 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"time"
"github.com/bytemine/go-icinga2"
"github.com/bytemine/go-icinga2/event"
"github.com/bytemine/icinga2rt/rt"
)
const version = "0.2.2"
const icingaQueueName = "icinga2rt"
var writeExample = flag.Bool("example", false, "write example configuration file as icinga2rt.json.example to current directory")
var configFile = flag.String("config", "/etc/bytemine/icinga2rt.json", "configuration file")
var debug = flag.Bool("debug", false, "debug mode, print log messages")
var debugEvents = flag.Bool("debugevents", false, "print received events")
var showVersion = flag.Bool("version", false, "display version and exit")
var exportCache = flag.String("exportCache", "", "export contents of cache to this file, and quit")
var importCache = flag.String("importCache", "", "import contents of cache from this file, and quit")
// openEventStreamer connects to the icinga2 API, exponentially backing off when the connection fails
func openEventStreamer(retries int, icingaClient *icinga2.Client, queue string, filter string, streamtype ...event.StreamType) (io.Reader, error) {
exp := uint(0)
var err error
for tries := 0; tries < retries; tries++ {
if *debug {
log.Printf("main: connecting to icinga, try: %v", tries+1)
}
var r io.Reader
r, err = icingaClient.EventStream(queue, filter, streamtype...)
if err != nil {
if *debug {
log.Printf("main: couldn't connect to icinga: %v", err)
log.Printf("main: waiting %v seconds before trying again.", 1<<exp)
}
time.Sleep(time.Duration(1<<exp) * time.Second)
exp++
continue
}
return r, nil
}
return nil, err
}
// rtClient interface enables to use a dummy client for testing.
type rtClient interface {
Ticket(int) (*rt.Ticket, error)
NewTicket(*rt.Ticket) (*rt.Ticket, error)
UpdateTicket(*rt.Ticket) (*rt.Ticket, error)
CommentTicket(int, string) error
}
func main() {
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
if *writeExample {
err := saveConfig("icinga2rt.json.example", &defaultConfig)
if err != nil {
log.Fatal("FATAL: init:", err)
}
os.Exit(0)
}
conf, err := loadConfig(*configFile)
if err != nil {
log.Fatalf("FATAL: init: Couldn't open config file %v: %v", *configFile, err)
}
if err := checkConfig(conf); err != nil {
log.Fatal("FATAL: init:", err)
}
eventCache, err := openCache(conf.Cache.File)
if err != nil {
log.Fatal("FATAL: init:", err)
}
if *exportCache != "" {
var f io.WriteCloser
if *exportCache == "-" {
f = os.Stdout
} else {
f, err = os.Create(*exportCache)
if err != nil {
log.Fatal("FATAL: export:", err)
}
}
_, err := eventCache.WriteTo(f)
if err != nil {
log.Fatal("FATAL: export:", err)
}
f.Close()
eventCache.Close()
os.Exit(0)
}
if *importCache != "" {
var f io.ReadCloser
if *importCache == "-" {
f = os.Stdin
} else {
f, err = os.Open(*importCache)
if err != nil {
log.Fatal("FATAL: import:", err)
}
}
_, err := eventCache.ReadFrom(f)
if err != nil {
log.Fatal("FATAL: import:", err)
}
f.Close()
eventCache.Close()
os.Exit(0)
}
rtClient, err := rt.NewClient(conf.RT.URL, conf.RT.User, conf.RT.Password, conf.RT.Insecure)
if err != nil {
log.Fatal("FATAL: init:", err)
}
tu := newTicketUpdater(eventCache, rtClient, conf.Ticket.mappings, conf.Ticket.Nobody, conf.Ticket.Queue, conf.Ticket.ClosedStatus)
icingaClient, err := icinga2.NewClient(conf.Icinga.URL, conf.Icinga.User, conf.Icinga.Password, conf.Icinga.Insecure)
if err != nil {
log.Fatal("FATAL: init:", err)
}
r, err := openEventStreamer(conf.Icinga.Retries, icingaClient, icingaQueueName, conf.Icinga.Filter, event.StreamTypeNotification)
if err != nil {
log.Fatal("FATAL: init:", err)
}
dec := json.NewDecoder(r)
for {
var x event.Notification
err := dec.Decode(&x)
if err != nil {
if *debug {
log.Printf("main: %v", err)
log.Printf("main: trying to reconnect to icinga.")
}
r, err := openEventStreamer(conf.Icinga.Retries, icingaClient, icingaQueueName, conf.Icinga.Filter, event.StreamTypeNotification)
if err != nil {
log.Fatal("FATAL: main:", err)
}
dec = json.NewDecoder(r)
continue
}
if *debug && *debugEvents {
buf, err := json.Marshal(x)
if err != nil {
log.Fatal("FATAL: main:", err)
}
log.Println("main: event stream:", string(buf))
}
// filter the notification
if conf.Icinga.LocalFilter.All != nil && !conf.Icinga.LocalFilter.All.Match(x) {
if *debug {
log.Println("main: event didn't match All filter")
}
continue
}
if conf.Icinga.LocalFilter.Any != nil && !conf.Icinga.LocalFilter.Any.Match(x) {
if *debug {
log.Println("main: event didn't match Any filter")
}
continue
}
if *debug {
log.Println("main: event matched filters")
}
err = tu.update(&x)
if err != nil {
log.Fatal("FATAL: main:", err)
}
}
}