module Xcal module Parktronic module Routes module Alarms # Fetches latest alarms # # ==== Parameters # * +page+ page number, defaults to 1 # * +per_page+ per page value, defaults to 100 # # ==== Examples # api.get_paged_alarms # api.get_paged_alarms(page: 2, per_page: 5) # api.alarms # api.alarms(page: 2, per_page: 5) def get_paged_alarms(args = {}) args.merge!(:access_token => access_token) response = get_response("/#{api_version}/alarms?#{URI.encode_www_form(args)}") generic_response = Xcal::Parktronic::GenericResponse.new(response.body) response.code == '200' && generic_response.has_key?(:alarms) ? generic_response.alarms.map { |alarm| Xcal::Parktronic::GenericResponse.new(alarm.alarm, self) } : generic_response end alias :alarms :get_paged_alarms # Fetches alarm with specific ID # # ==== Parameters # * +id+ alarm ID # # ==== Examples # api.get_alarm(2) def get_alarm(id) response = get_response("/#{api_version}/alarms/#{id}?access_token=#{access_token}") generic_response = Xcal::Parktronic::GenericResponse.new(response.body) response.code == '200' ? Xcal::Parktronic::GenericResponse.new(generic_response.alarm, self) : generic_response end alias :alarm :get_alarm # Posts new alarm # # ==== Parameters # * +alarm+ hash of Alarm data. # * +events+ array of Events or leave blank for creating alarm without events. # # ==== Examples # # Creates only Alarm, without Events # api.post_alarm(alarm: {name: 'alarm name', originating_system: 'Source System', impact_level: 'low'}) # # Creates Alarm with Events # alarm_params = {name: 'alarm name', originating_system: 'Source System', impact_level: 'low'} # event_1_params = {subject: 'EventSubj', description: 'EventDesc', host_impacted: 'host', initiated_at: '2013-11-22T01:00:24Z', service_impacted: 'EventSvc', incident_status: 'CRITICAL'} # api.post_alarm( alarm: alarm_params, events: [event_1_params, event_2_params, ...] ) def post_alarm(args = {}) url_path = args[:events].nil? ? 'alarms/create_without_events' : 'alarms' request = Net::HTTP::Post.new("/#{api_version}/#{url_path}", 'Content-Type' => 'application/json') params = { access_token: access_token, alarm: args[:alarm] } params.merge!(events: args[:events]) unless args[:events].nil? request.body = params.to_json response = http.start { |net| net.request(request) } Xcal::Parktronic::GenericResponse.new(response.body) end # Update alarm with specific ID # # ==== Parameters # * +id+ alarm ID # * +params+ alarm params # # ==== Examples # api.update_alarm(2, name: 'new name') # api.update_alarm(5, name: 'new name', originating_system: 'new system', description: 'test') def update_alarm(id, params) request = Net::HTTP::Patch.new("/#{api_version}/alarms/#{id}", 'Content-Type' => 'application/json') request.body = { access_token: access_token, alarm: params }.to_json response = http.start { |net| net.request(request) } Xcal::Parktronic::GenericResponse.new(response.body) end end # Find all Alarm Tags # # ==== Parameters # * +id+ alarm ID # # ==== Examples # api.get_alarm_tags(2) # api.alarm_tags(2) def get_alarm_tags(id) response = get_response("/#{api_version}/alarms/#{id}/tags?#{URI.encode_www_form(access_token: access_token)}") generic_response = Xcal::Parktronic::GenericResponse.new(response.body, self) response.code == '200' && generic_response.has_key?(:tags) ? generic_response.tags.map(&:tag) : generic_response end alias :alarm_tags :get_alarm_tags # Search unclosed alarm by name or tag or originating system with time range # # ==== Parameters # * +name+ string, alarm name # * +tagged_with+ string or array, tag list # * +time_range+ integer, time range in hours, defaults to 24 hours # * +originating_system+ string or array, originating system name(s), blank by default (example: 'Op5_Nagios' or 'Op5_Nagios Splunk' or ['Op5_Nagios', 'Splunk']) # * +page+ integer, page number, defaults to 1 # * +per_page+ integer, per page value, defaults to 100 # * +where+ 'where' hash of conditions for searching # * +extends+ array, may include: latest_event, latest_unclosed_event, with_unclosed_events, with_most_recent_events, top_alarms, select_brouha_open # # ==== Examples # api.search_alarm(name: 'test alarm') # api.search_alarm(tagged_with: 'test alarm', time_range: 120) # api.search_alarm(where: {impact_level: 'Unknown', originating_system: 'test'}, time_range: 120) # api.search_alarm(extends: ['latest_event', 'with_most_recent_events'], time_range: 120) def search_alarm(args = {}) custom_scopes = { with_most_recent_events: [] } #Filter filter = { alarms: '' } custom_filter = args[:originating_system] custom_filter = custom_filter.split(' ') if custom_filter.is_a?(String) filter = { alarms: custom_filter.map{|os| "originating_system = '#{os}'"}.join(' OR ') } unless custom_filter.nil? #Scopes (time range) custom_scopes[:within_event_date_range] = [(Time.now - (60 * (60 * args.fetch(:time_range, 24)))).strftime('%Y-%m-%d %H:%M:%S'), Time.now.strftime('%Y-%m-%d %H:%M:%S')] #Tags custom_scopes[:tagged_with] = args[:tagged_with] unless args[:tagged_with].nil? # Params for request scopes = URI.escape(custom_scopes.to_json) search = URI.escape({name: args.fetch(:name, '')}.to_json) extends = URI.escape(args.fetch(:extends, []).to_json) filter = URI.escape(args.fetch(:filter, filter).to_json) where = URI.escape(args.fetch(:where, '').to_json) page = args.fetch(:page, 1) per_page = args.fetch(:per_page, 100) response = get_response("/#{api_version}/alarms/search?page=#{page}&per_page=#{per_page}&scopes=#{scopes}&search=#{search}&extends=#{extends}&filter=#{filter}&where=#{where}&access_token=#{access_token}") generic_response = Xcal::Parktronic::GenericResponse.new(response.body) response.code == '200' && generic_response.has_key?(:alarms) ? generic_response.alarms.map(&:alarm) : generic_response end end end end