Пример #1
0
func (cp *ConsumerPool) run() chan []common.MapStr {
	conn, err := amqp.Dial(cp.ServerURI)
	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()

	events := make(chan []common.MapStr)
	selCases := make([]reflect.SelectCase, len(cp.Consumers))

	for i, c := range cp.Consumers {
		consumerChan := c.Run(ch)

		selCases[i].Dir = reflect.SelectRecv
		selCases[i].Chan = reflect.ValueOf(consumerChan)
	}

	go func() {
		for {
			_, recv, recvOK := reflect.Select(selCases)
			if recvOK {
				events <- recv.Interface().([]common.MapStr)
			}
		}
	}()

	return events
}
Пример #2
0
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")
}
Пример #3
0
func amqpConnect() (*amqp.Connection, *amqp.Channel) {
	conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/")
	utils.FailOnError(err, "Failed to connect to RabbitMQ")

	ch, err := conn.Channel()
	utils.FailOnError(err, "Failed to open a channel")

	return conn, ch
}
Пример #4
0
/*
Run ...
*/
func (c *Consumer) Run(ch *amqp.Channel) chan []common.MapStr {
	events := make(chan []common.MapStr)
	// TODO: make exchange part of config
	deliveries, err := ch.Consume("test", *c.cfg.Name, false, false, false, false, nil)
	utils.FailOnError(err, "Failed to register consumer")
	go c.consume(deliveries, events)
	return events
}
Пример #5
0
func (p *Publisher) send(ch *amqp.Channel, msg string) {
	err := (*ch).Publish(p.exch, p.routingKey, false, true, amqp.Publishing{
		DeliveryMode: amqp.Persistent,
		ContentType:  "text/plain",
		Body:         []byte(msg),
	})
	utils.FailOnError(err, "Failed to publish message")
}
Пример #6
0
func firehosePublisher(exch string, routingKey string, ch *amqp.Channel) {
	for i := 0; i < 1000; i++ {
		err := (*ch).Publish(exch, routingKey, false, true, amqp.Publishing{
			DeliveryMode: amqp.Persistent,
			ContentType:  "text/plain",
			Body:         []byte(fmt.Sprintf("{\"test\": %d}", i)),
		})
		utils.FailOnError(err, "Failed to send message")
	}
}
Пример #7
0
// 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
}
Пример #8
0
func (cp *ConsumerPool) init(cfg *config.AmqpConfig) {
	cp.ServerURI = *cfg.ServerURI
	cp.Consumers = make([]*Consumer, len(*cfg.Channels))

	for i, chConfig := range *cfg.Channels {
		c := new(Consumer)
		c.Init(&chConfig, func(b []byte) common.MapStr {
			e := new(common.MapStr)
			err := json.Unmarshal(b, e)
			utils.FailOnError(err, "Failed to marshal event")
			return *e
		})
		cp.Consumers[i] = c
	}
}
Пример #9
0
// 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
}
Пример #10
0
func newBeat(cfgFile string, client *MockClient) (*Amqpbeat, *beat.Beat) {
	wd, err := os.Getwd()
	utils.FailOnError(err, "Could not determine working directory")

	ab := &Amqpbeat{}
	b := beat.NewBeat("amqpbeat", "0.0.0", ab)
	ab.ConfigWithFile(b, fmt.Sprintf("%s/../test/config/%s", wd, cfgFile))

	if client == nil {
		client = &MockClient{
			eventsPublished: func(event []common.MapStr, beat *Amqpbeat) {
			},
		}
	}

	client.beat = ab
	b.Events = client
	ab.Setup(b)

	return ab, b
}
Пример #11
0
// ConfigWithFile ...
func (ab *Amqpbeat) ConfigWithFile(b *beat.Beat, path string) error {
	// Config loading goes here
	err := cfgfile.Read(&ab.config, path)
	utils.FailOnError(err, "Error reading configuration file")
	return nil
}
Пример #12
0
func newPublisher(exch string, routingKey string, ch *amqp.Channel) *Publisher {
	_, err := ch.QueueDeclare(routingKey, false, true, false, false, nil)
	utils.FailOnError(err, fmt.Sprintf("Failed to declare queue %s", routingKey))
	return &Publisher{exch: exch, routingKey: routingKey, ch: ch}
}