Class: Fluent::SpectrumInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_spectrum.rb

Overview

error checking in every section

Instance Method Summary collapse

Constructor Details

#initializeSpectrumInput

Returns a new instance of SpectrumInput.



16
17
18
19
20
# File 'lib/fluent/plugin/in_spectrum.rb', line 16

def initialize
  require 'rest_client'
  require 'json'
  super
end

Instance Method Details

#configure(conf) ⇒ Object

def initialize



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
# File 'lib/fluent/plugin/in_spectrum.rb', line 22

def configure(conf)
  super 
  @conf = conf
  # map of Spectrum attribute codes to names
  @spectrum_access_code={
    "0x11f9c" => "ALARM_ID",
    "0x11f4e" => "CREATION_DATE",
    "0x11f56" => "SEVERITY",
    "0x12b4c" => "ALARM_TITLE",
    "0x1006e" => "HOSTNAME",
    "0x12d7f" => "IP_ADDRESS",
    "0x1296e" => "ORIGINATING_EVENT_ATTR",
    "0x10000" => "MODEL_STRING",  
    "0x11f4d" => "ACKNOWLEDGED",
    "0x11f4f" => "ALARM_STATUS",
    "0x11fc5" => "OCCURRENCES",
    "0x11f57" => "TROUBLE_SHOOTER",
    "0x11f9b" => "USER_CLEARABLE",
    "0x12022" => "TROUBLE_TICKET_ID",
    "0x12942" => "PERSISTENT",
    "0x12adb" => "GC_NAME",
    "0x57f0105" => "Custom_Project",
    "0x11f4d" => "ACKNOWLEDGED",
    #{}"0x11f51" => "CLEARED_BY_USER_NAME",
    #{}"0x11f52" => "EVENT_ID_LIST",
    #{}"0x11f53" => "MODEL_HANDLE",
    #{}"0x11f54" => "PRIMARY_ALARM",
    #{}"0x11fc4" => "ALARM_SOURCE",
    #{}"0x11fc6" => "TROUBLE_SHOOTER_MH",
    #{}"0x12a6c" => "TROUBLE_SHOOTER_EMAIL",
    #{}"0x1290d" => "IMPACT_SEVERITY",
    #{}"0x1290e" => "IMPACT_SCOPE",
    #{}"0x1298a" => "IMPACT_TYPE_LIST",
    #{}"0x12948" => "DIAGNOSIS_LOG",
    #{}"0x129aa" => "MODEL_ID",
    #{}"0x129ab" => "MODEL_TYPE_ID",
    #{}"0x129af" => "CLEAR_DATE",
    #{}"0x12a04" => "SYMPTOM_LIST_ATTR",
    #{}"0x12a6f" => "EVENT_SYMPTOM_LIST_ATTR",
    #{}"0x12a05" => "CAUSE_LIST_ATTR",
    #{}"0x12a06" => "SYMPTOM_COUNT_ATTR",
    #{}"0x12a70" => "EVENT_SYMPTOM_COUNT_ATTR",
    #{}"0x12a07" => "CAUSE_COUNT_ATTR",
    #{}"0x12a63" => "WEB_CONTEXT_URL",
    #{}"0x12a6b" => "COMBINED_IMPACT_TYPE_LIST",
    #{}"0x11f50" => "CAUSE_CODE",
    #{}"0x10009" => "SECURITY_STRING"
  }
  # Create XML chunk for attributes we care about
  @attr_of_interest=""
  @spectrum_access_code.each do |key, array|
    @attr_of_interest += " <rs:requested-attribute id=\"#{key}\"/>"
  end
  # Setup URL Resource
  @url = 'http://' + @endpoint.to_s + '/spectrum/restful/alarms'
  def spectrumEnd
    RestClient::Resource.new(@url,@user,@pass)
  end
end

#inputObject

def run



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
# File 'lib/fluent/plugin/in_spectrum.rb', line 101

def input
  alertStartTime = Engine.now.to_i - @interval.to_i 
  $log.info "Spectrum :: Polling for alerts for time period: #{alertStartTime.to_i} - #{Engine.now.to_i}"

  # function to UTF8 encode
  def to_utf8(str)
    str = str.force_encoding('UTF-8')
    return str if str.valid_encoding?
    str.encode("UTF-8", 'binary', invalid: :replace, undef: :replace, replace: '')
  end

  # XML for spectrum post
  @xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  <rs:alarm-request throttlesize=\"10000\"
  xmlns:rs=\"http://www.ca.com/spectrum/restful/schema/request\"
  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  xsi:schemaLocation=\"http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd \">
  <rs:attribute-filter>
    <search-criteria xmlns=\"http://www.ca.com/spectrum/restful/schema/filter\">
    <filtered-models>
      <greater-than>
        <attribute id=\"0x11f4e\">
          <value> #{alertStartTime} </value>
        </attribute>
      </greater-than>
    </filtered-models>
    </search-criteria>
  </rs:attribute-filter>
  #{@attr_of_interest}
  </rs:alarm-request>"

  # Post to Spectrum and parse results  
  responsePost=spectrumEnd.post @xml,:content_type => 'application/xml',:accept => 'application/json'
  body = JSON.parse(responsePost.body)
  # Only continue if we have alarms in results
  if body['ns1.alarm-response-list']['@total-alarms'].to_i > 1
    $log.info "Spectrum :: returned #{body['ns1.alarm-response-list']['@total-alarms'].to_i} alarms"
    # iterate through alarms in results
    body['ns1.alarm-response-list']['ns1.alarm-responses']['ns1.alarm'].each do |alarm|
      record_hash = Hash.new # temp hash to hold attributes of alarm
      record_hash['event_type'] = @tag.to_s
      record_hash['intermediary_source'] = @endpoint.to_s
      # iterate though alarm attributes and add to temp hash  
      alarm['ns1.attribute'].each do |attribute|
        record_hash[@spectrum_access_code[attribute['@id'].to_s].to_s] = to_utf8(attribute['$'].to_s)
      end
      # include raw?
      if @include_raw.to_s == "true"  
        record_hash['raw'] = alarm  
      end
      Engine.emit(@tag, record_hash['CREATION_DATE'].to_i,record_hash)
    end
  elsif body['ns1.alarm-response-list']['@total-alarms'].to_i == 1
    $log.info "Spectrum :: returned #{body['ns1.alarm-response-list']['@total-alarms'].to_i} alarms"
    record_hash = Hash.new # temp hash to hold attributes of alarm
    record_hash['event_type'] = @tag.to_s
    record_hash['intermediary_source'] = @endpoint.to_s
    # iterate though alarm attributes and add to temp hash  
    body['ns1.alarm-response-list']['ns1.alarm-responses']['ns1.alarm']['ns1.attribute'].each do |attribute|
      record_hash[@spectrum_access_code[attribute['@id'].to_s].to_s] = to_utf8(attribute['$'].to_s)
    end
    # include raw?
    if @include_raw.to_s == "true"  
      record_hash['raw'] = alarm  
    end
    Engine.emit(@tag, record_hash['CREATION_DATE'].to_i,record_hash)
  end  
end

#runObject

def shutdown



97
98
99
# File 'lib/fluent/plugin/in_spectrum.rb', line 97

def run
  @loop.run
end

#shutdownObject

def start



91
92
93
94
95
# File 'lib/fluent/plugin/in_spectrum.rb', line 91

def shutdown
  super
  @loop.stop
  @thread.join
end

#spectrumEndObject



77
78
79
# File 'lib/fluent/plugin/in_spectrum.rb', line 77

def spectrumEnd
  RestClient::Resource.new(@url,@user,@pass)
end

#startObject

def configure



82
83
84
85
86
87
88
89
# File 'lib/fluent/plugin/in_spectrum.rb', line 82

def start 
  super 
  @loop = Coolio::Loop.new
  timer_trigger = TimerWatcher.new(@interval, true, &method(:input))
  timer_trigger.attach(@loop)
  @thread = Thread.new(&method(:run))
  $log.info "Spectrum :: starting poller, endpoint #{@endpoint} interval #{@interval}"
end

#to_utf8(str) ⇒ Object

function to UTF8 encode



106
107
108
109
110
# File 'lib/fluent/plugin/in_spectrum.rb', line 106

def to_utf8(str)
  str = str.force_encoding('UTF-8')
  return str if str.valid_encoding?
  str.encode("UTF-8", 'binary', invalid: :replace, undef: :replace, replace: '')
end