-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd.go
229 lines (219 loc) · 5.97 KB
/
cmd.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"github.com/bangunindo/trap2json/correlate"
"github.com/bangunindo/trap2json/forwarder"
"github.com/bangunindo/trap2json/metrics"
"github.com/bangunindo/trap2json/snmp"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
"io"
"net/http"
"os"
"os/signal"
"path"
"strconv"
"strings"
"sync"
"syscall"
)
func GetSnmptrapDProcess() (*os.Process, error) {
f, err := os.Open(snmp.PidFilePath)
if err != nil {
return nil, errors.Wrap(err, "failed opening pid file")
}
defer f.Close()
pidRaw, err := io.ReadAll(f)
if err != nil {
return nil, errors.Wrap(err, "failed reading pid file")
}
pid, err := strconv.Atoi(strings.TrimSpace(string(pidRaw)))
if err != nil {
return nil, errors.Wrap(err, "corrupted pid file")
}
process, err := os.FindProcess(pid)
if err != nil {
return nil, errors.Wrap(err, "can't find process")
}
return process, nil
}
func TerminateSnmptrapD() {
logger := log.With().Str("module", "snmptrapd_monitor").Logger()
logger.Info().Msg("terminating snmptrapd process")
process, err := GetSnmptrapDProcess()
if err != nil {
logger.Warn().
Err(err).
Msg("failed getting snmptrapd process")
return
}
err = process.Signal(syscall.SIGTERM)
if err != nil {
logger.Warn().
Err(err).
Msg("failed sending terminate signal")
return
}
_, err = process.Wait()
if err != nil {
logger.Warn().
Err(err).
Msg("can't terminate process")
return
}
logger.Info().Msg("snmptrapd terminated")
}
// SplitAt implements bufio.SplitFunc to split byte stream using predefined substring
func SplitAt(substring string) func(data []byte, atEOF bool) (advance int, token []byte, err error) {
searchBytes := []byte(substring)
searchLen := len(searchBytes)
return func(data []byte, atEOF bool) (advance int, token []byte, err error) {
dataLen := len(data)
if atEOF && dataLen == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, searchBytes); i >= 0 {
return i + searchLen, data[0:i], nil
}
if atEOF {
return dataLen, data, nil
}
return 0, nil, nil
}
}
func Run(ctx context.Context, c config, r io.Reader, noSnmpTrapD bool) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
signal.Notify(sig, syscall.SIGTERM)
log.Info().Msg("loading MIBs")
if loadedModules, err := snmp.InitMIBTranslator(path.Join(defaultConfigPath, "mibs")); err != nil {
log.Warn().Err(err).Msg("failed initiating MIB parser, some data might be unavailable")
} else {
log.Trace().Strs("modules", loadedModules).Msg("MIB modules loaded")
}
topWg := new(sync.WaitGroup)
var promServer *http.Server
if c.Prometheus.Enable {
http.Handle(c.Prometheus.Path, promhttp.Handler())
addr := fmt.Sprintf(":%d", c.Prometheus.Port)
promServer = &http.Server{
Addr: addr,
}
// start prometheus exporter
topWg.Add(1)
go func() {
defer topWg.Done()
log.Info().Msgf("starting prometheus exporter at %s", addr)
if err := promServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("prometheus exporter failed to start")
}
}()
// and terminate when terminate signal received
go func() {
<-ctx.Done()
_ = promServer.Shutdown(context.Background())
log.Info().Msg("prometheus exporter exited")
}()
}
go func() {
select {
case <-sig:
log.Info().Msg("received termination signal")
cancel()
case <-ctx.Done():
}
}()
if !noSnmpTrapD {
topWg.Add(1)
// terminate snmptrapd when we receive terminate signal
go func() {
defer topWg.Done()
<-ctx.Done()
TerminateSnmptrapD()
}()
}
parseChan := make(chan []byte)
forwarderChan := make(chan *snmp.Message)
var parseSendChan chan<- *snmp.Message
parseWg := new(sync.WaitGroup)
correlateWg := new(sync.WaitGroup)
forwarderWg := new(sync.WaitGroup)
var corr *correlate.Correlate
var err error
if c.ParseWorkers <= 0 {
c.ParseWorkers = 1
}
if c.Correlate.Enable {
if c.Correlate.Workers <= 0 {
c.Correlate.Workers = 1
}
corr, err = correlate.NewCorrelate(c.Correlate, correlateWg, forwarderChan)
if err != nil {
log.Fatal().Err(err).Msg("correlate failed to start")
}
parseSendChan = corr.SendChannel()
for i := 0; i < c.Correlate.Workers; i++ {
correlateWg.Add(1)
go corr.CorrelateWorker()
}
} else {
parseSendChan = forwarderChan
}
for i := 0; i < c.ParseWorkers; i++ {
parseWg.Add(1)
go snmp.ParserWorker(i+1, parseWg, parseChan, parseSendChan)
}
forwarderWg.Add(1)
go forwarder.StartForwarders(forwarderWg, c.Forwarders, forwarderChan)
bufferSize, err := c.SnmpTrapD.GetBufferSize()
if err != nil {
log.Warn().Err(err).Msg("failed parsing snmptrapd.buffer_size")
bufferSize = snmp.DefaultBufferSize
}
buf := make([]byte, bufferSize)
scanner := bufio.NewScanner(r)
scanner.Split(SplitAt(c.SnmpTrapD.MagicEnd))
scanner.Buffer(buf, bufferSize)
magicBegin := []byte(c.SnmpTrapD.MagicBegin)
magicBeginLen := len(magicBegin)
log.Info().Msg("trap2json started")
// Scan() will stop when snmptrapd is successfully terminated since it will
// close os.Stdin stream
for scanner.Scan() {
metrics.SnmpTrapDProcessed.Inc()
line := scanner.Bytes()
metrics.SnmpTrapDProcessedBytes.Add(float64(len(line)))
log.Trace().Bytes("data", line).Msg("received data")
idx := bytes.LastIndex(line, magicBegin)
if idx < 0 {
log.Debug().Bytes("data", line).Msg("dropping data")
metrics.SnmpTrapDDropped.Inc()
continue
}
msg := make([]byte, len(line)-magicBeginLen-idx)
copy(msg, line[idx+magicBeginLen:])
log.Trace().Bytes("data", msg).Msg("sending data")
parseChan <- msg
metrics.SnmpTrapDSucceeded.Inc()
}
if err := scanner.Err(); err != nil {
log.Error().Err(err).Msg("scanner error")
// in case of scanner error, cancel should be called manually
cancel()
}
// drain all channels
close(parseChan)
parseWg.Wait()
corr.Close()
correlateWg.Wait()
close(forwarderChan)
forwarderWg.Wait()
cancel()
topWg.Wait()
}