/
amqpbeat.go
129 lines (109 loc) · 2.72 KB
/
amqpbeat.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
package main
import (
// "bufio"
"encoding/json"
"fmt"
"os"
"time"
"github.com/elastic/libbeat/cfgfile"
"github.com/elastic/libbeat/logp"
cfg "github.com/robinpercy/amqpbeat/config"
"github.com/robinpercy/amqpbeat/utils"
// "github.com/elastic/libbeat/cfgfile"
// "encoding/json"
"github.com/elastic/libbeat/beat"
"github.com/elastic/libbeat/common"
"github.com/elastic/libbeat/publisher"
"github.com/streadway/amqp"
)
// Amqpbeat is a beat.Beater implementation that consumes events from one or
// more AMQP channels
type Amqpbeat struct {
events publisher.Client
config cfg.Settings
}
func startConsuming(uri string, qName string, payloads chan<- []byte) {
conn, err := amqp.Dial(uri)
utils.FailOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
utils.FailOnError(err, "Failed to open a channel")
defer conn.Close()
q, err := ch.Consume(qName, "", false, false, false, false, nil)
utils.FailOnError(err, "Failed to register consumer")
// TODO: dryrun value to config
dryrun := true
maxMsgs := 10
count := 1
for m := range q {
payloads <- m.Body
if dryrun == true {
m.Nack(false, true)
count++
if count >= maxMsgs {
break
}
} else {
m.Ack(false)
}
}
close(payloads)
fmt.Println("Finished consuming")
}
// Config extracts settings from the config file
func (mb *Amqpbeat) Config(b *beat.Beat) error {
// Config loading goes here
err := cfgfile.Read(&mb.config, "")
utils.FailOnError(err, "Error reading configuration file")
//if err != nil {
// logp.Err("Error reading configuration file: %v", err)
// return err
// }
logp.Debug("amq", " is configured")
return nil
}
// Setup ...
func (mb *Amqpbeat) Setup(b *beat.Beat) error {
mb.events = b.Events
logp.Debug("ampqbeat", " is setup")
return nil
}
// Run ...
func (mb *Amqpbeat) Run(b *beat.Beat) error {
args := os.Args
payloads := make(chan []byte)
fmt.Println(mb.config.AmqpInput)
fmt.Println((*mb.config.AmqpInput.Channels)[0])
fmt.Println((*mb.config.AmqpInput.Channels)[0].Name)
go startConsuming(args[1], *(*mb.config.AmqpInput.Channels)[0].Name, payloads)
for p := range payloads {
var event map[string]interface{}
err := json.Unmarshal(p, &event)
utils.FailOnError(err, "Failed to unmarshal value")
event["@timestamp"] = common.Time(time.Now())
event["type"] = "openstack"
fmt.Println(event)
mb.events.PublishEvent(event, publisher.Sync)
}
return nil
}
// Cleanup ...
func (mb *Amqpbeat) Cleanup(b *beat.Beat) error {
return nil
}
// Stop ...
func (mb *Amqpbeat) Stop() {
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
mb := &Amqpbeat{}
b := beat.NewBeat("amqpbeat", "0.1", mb)
b.CommandLineSetup()
b.LoadConfig()
mb.Config(b)
b.Run()
}