Class: Sabisu::Server::Event
- Inherits:
-
Object
- Object
- Sabisu::Server::Event
- Defined in:
- lib/sabisu/event.rb
Overview
class for sensu events
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
return all docs.
- .clear_events(events) ⇒ Object
-
.search(query, options = {}) ⇒ Object
Example: Event.search(“client:cheftest* AND status:1”, :bookmark => ‘ABCD36’, :limit => 10, :sort => [ ‘status<string>’, ‘-client<string>’, ‘-issued<number>’ ]).
- .stale(params) ⇒ Object
- .update_design_doc ⇒ Object
Instance Method Summary collapse
-
#initialize(fields) ⇒ Event
constructor
takes a hash and maps it to the fields defined in FIELDS.
- #to_hash ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(fields) ⇒ Event
takes a hash and maps it to the fields defined in FIELDS
162 163 164 |
# File 'lib/sabisu/event.rb', line 162 def initialize(fields) fields.each { |k, v| send("#{k}=", v) } if fields end |
Class Method Details
.all(options = {}) ⇒ Object
return all docs
7 8 9 10 11 12 |
# File 'lib/sabisu/event.rb', line 7 def self.all( = {}) = { skip: 0, limit: nil, sort: [] }.merge() .delete_if { |k, v| v.nil? || v == [] } CURRENT_DB.all_docs(.merge(include_docs: true, start_key: '"a"')) end |
.clear_events(events) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sabisu/event.rb', line 133 def self.clear_events(events) events.each do |event| puts "Deleting #{event[:client]}/#{event[:check]}" begin doc = CURRENT_DB.get("#{event[:client]}/#{event[:check]}") doc.destroy rescue RestClient::Conflict # there been a conflict, ignore puts 'doc conflict' rescue RestClient::ResourceNotFound # looks like its already deleted, noop puts 'doc not found' end end end |
.search(query, options = {}) ⇒ Object
Example: Event.search(“client:cheftest* AND status:1”, :bookmark => ‘ABCD36’,
:limit => 10,
:sort => [ 'status<string>', '-client<string>', '-issued<number>' ])
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 |
# File 'lib/sabisu/event.rb', line 17 def self.search(query, = {}) # define facet range params for status attribute ranges = { status: {} } ranges[:status][:OK] = '[0 TO 0]' ranges[:status][:Warning] = '[1 TO 1]' ranges[:status][:Critical] = '[2 TO 2]' ranges[:status][:Unknown] = '[3 TO 3]' # define facet count param counts = FIELDS.map do |field| field[:name] if field.key?(:facet) && field[:facet] == true end.compact = { bookmark: nil, limit: nil, sort: [], include_docs: true, ranges: ranges.to_json, counts: counts }.merge() .delete_if { |k, v| v.nil? || v == [] } # because couchrest doesn't handle arrays correctly [:counts] = [:counts].to_s unless [:counts].nil? [:sort] = [:sort].to_s unless [:sort].nil? CURRENT_DB.view('_design/sabisu/_search/all_fields', .merge(q: query)) end |
.stale(params) ⇒ Object
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 |
# File 'lib/sabisu/event.rb', line 81 def self.stale(params) # get cloudant events and put them into a hash cloudant_events = {} cloudant_events_tmp = all['rows'] cloudant_events_tmp.each do |event| event = event['doc']['event'] client = event['client']['name'] check = event['check']['name'] cloudant_events[client] = {} unless cloudant_events.key?(client) cloudant_events[client][check] = { status: event['check']['status'], output: event['check']['output'] } end ############################################## # get sensu event and put them into a hash sensu = Sensu.new rawdata = sensu.request(method: 'GET', ssl: true, path: '/events') sensu_events_tmp = JSON.parse(rawdata.body) sensu_events = {} sensu_events_tmp.each do |event| client = event['client'] check = event['check'] sensu_events[client] = {} unless sensu_events.key?(client) sensu_events[client][check] = { status: event['status'], output: event['output'] } end ############################################## stale = cloudant_events.deep_diff(sensu_events) # get list of cloudant docs to be deleted (marked as recovered by sensu) # this is done by iterating over the deep_diff and finding places where # there is no sensu alert for a client/check (ie nil) if params.key?('clear_recovered') && params['clear_recovered'].to_s == 'true' clear_list = cloudant_events_tmp.each.map do |event| client = event['doc']['event']['client']['name'] check = event['doc']['event']['check']['name'] unless sensu_events.key?(client) && sensu_events[client].key?(check) { client: client, check: check } end end.compact clear_events(clear_list) end stale end |
.update_design_doc ⇒ Object
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 |
# File 'lib/sabisu/event.rb', line 44 def self.update_design_doc # create search indexes fields = FIELDS.map do |field| next if field.key?(:index) && field[:index] == false field[:facet] == true ? facet = ", { 'facet': true }" : '' " if (typeof(doc.event.#{field[:path]}) !== 'undefined' && doc.event.#{field[:path]} !== null){ index('#{field[:name]}', doc.event.#{field[:path]}#{facet}); }" end search_function = " function(doc) { index('default', doc._id); #{fields.join(' ')} }" search_indexes = { all_fields: { analyzer: 'whitespace', index: search_function } } # save the design doc only if it has changed or doesn't exist begin doc = CURRENT_DB.get('_design/sabisu') if doc[:indexes] != search_indexes doc[:languages] = 'javascript' doc[:indexes] = search_indexes CURRENT_DB.save_doc(doc) end rescue ::RestClient::Conflict # ignore this puts 'doc conflict' rescue ::RestClient::ResourceNotFound CURRENT_DB.save_doc( '_id' => '_design/sabisu', :language => 'javascript', :indexes => search_indexes ) end end |
Instance Method Details
#to_hash ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/sabisu/event.rb', line 149 def to_hash hash = {} instance_variables.each do |var| hash[var.to_s.delete('@')] = instance_variable_get(var) end hash end |
#to_json ⇒ Object
157 158 159 |
# File 'lib/sabisu/event.rb', line 157 def to_json JSON.pretty_generate(to_hash) end |