Class: LogStash::Inputs::Zenoss

Inherits:
RabbitMQ show all
Defined in:
lib/logstash/inputs/zenoss.rb

Overview

Read Zenoss events from the zenoss.zenevents fanout exchange.

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes inherited from Base

#params, #threadable

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from RabbitMQ

#initialize

Methods included from RabbitMQ::BunnyImpl

#consume, #setup, #teardown

Methods included from RabbitMQ::MarchHareImpl

#teardown

Methods inherited from Threadable

#initialize

Methods inherited from Base

#initialize, #tag

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Inputs::RabbitMQ

Instance Method Details

#registerObject



35
36
37
38
39
# File 'lib/logstash/inputs/zenoss.rb', line 35

def register
  super
  require "logstash/util/zenoss"
  require "bunny"
end

#run(queue) ⇒ Object

def register



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
# File 'lib/logstash/inputs/zenoss.rb', line 41

def run(queue)
  begin
    zep = Org::Zenoss::Protobufs::Zep

    @logger.debug("Connecting with RabbitMQ settings #{@rabbitmq_settings.inspect}")
    @bunny = Bunny.new(@rabbitmq_settings)
    return if terminating?
    @bunny.start
    @bunny.qos({:prefetch_count => @prefetch_count})

    @arguments_hash = Hash[*@arguments]

    @logger.debug("Setting up queue #{@name.inspect}")
    @queue = @bunny.queue(@name, {
      :durable => @durable,
      :auto_delete => @auto_delete,
      :exclusive => @exclusive,
      :arguments => @arguments_hash
    })

    @queue.bind(@exchange, :key => @key)

    @queue.subscribe({:ack => @ack}) do |data|

      # Zenoss can optionally compress message payloads.
      if data[:header].content_encoding == "deflate"
        data[:payload] = Zlib::Inflate.inflate(data[:payload])
      end

      # Decode the payload into an EventSummary.
      summary = zep::EventSummary.decode(data[:payload])

      # This should never happen, but skip it if it does.
      next unless summary.occurrence.length > 0

      occurrence = summary.occurrence[0]
      #timestamp = DateTime.strptime(occurrence.created_time.to_s, "%Q").to_s
      timestamp = Time.at(occurrence.created_time / 1000.0)

      # LogStash event properties.
      event = LogStash::Event.new(
        "@timestamp" => timestamp,
        "type" => @type,
        "host" => occurrence.actor.element_title,
        "message" => occurrence.message,
      )
      decorate(event)

      # Direct mappings from summary.
      %w{uuid}.each do |property|
        property_value = occurrence.send property
        if !property_value.nil?
          event[property] = property_value
        end
      end

      # Direct mappings from occurrence.
      %w{
        fingerprint event_class event_class_key event_key event_group agent
        syslog_facility nt_event_code monitor
      }.each do |property|
        property_value = occurrence.send property
        if !property_value.nil?
          event[property] = property_value
        end
      end

      # Enum Mappings.
      event["severity"] = zep::EventSeverity.constants[occurrence.severity]

      if !occurrence.status.nil?
        event["status"] = zep::EventStatus.constants[occurrence.status]
      end

      if !occurrence.syslog_priority.nil?
        event["syslog_priority"] = zep::SyslogPriority.constants[
          occurrence.syslog_priority]
      end

      # Extra Details.
      if !occurrence.details.nil?
        occurrence.details.each do |detail|
          if detail.value.length == 1
            event[detail.name] = detail.value[0]
          else
            event[detail.name] = detail.value
          end
        end
      end

      queue << event
    end # @queue.subscribe

  rescue *[Bunny::ConnectionError, Bunny::ServerDownError] => e
    @logger.error("RabbitMQ connection error, will reconnect: #{e}")
    # Sleep for a bit before retrying.
    # TODO(sissel): Write 'backoff' method?
    sleep(1)
    retry
  end # begin/rescue
end