module NewRelicManagement::Manager
> Manager
Methods¶ ↑
Public Instance Methods
add_to_alert(entities, condition_id, type = 'Server')
click to toggle source
# File lib/newrelic-management/manager.rb, line 55 def add_to_alert(entities, condition_id, type = 'Server') return if entities.empty? Notifier.add_servers(entities) Array(entities).each do |entity| Client.alert_add_entity(entity, condition_id, type) end end
delete_from_alert(entities, condition_id, type = 'Server')
click to toggle source
# File lib/newrelic-management/manager.rb, line 63 def delete_from_alert(entities, condition_id, type = 'Server') return if entities.empty? Notifier.remove_servers(entities) Array(entities).each do |entity| Client.alert_delete_entity(entity, condition_id, type) end end
find_alert(alert)
click to toggle source
find_alert_conditions(alert)
click to toggle source
find_excluded(excluded)
click to toggle source
> Find Servers which should be excluded from Management¶ ↑
# File lib/newrelic-management/manager.rb, line 128 def find_excluded(excluded) result = [] Array(excluded).each do |exclude| if exclude.include?(':') find_labeled(exclude).each { |x| result << x } next end res = Client.get_server(exclude) result << res['id'] if res end result end
find_labeled(labels, match_any = Config.transient[:alert_match_any])
click to toggle source
> Find Servers Matching a Label¶ ↑
> Example: find_labeled
(['Role:API', 'Environment:Production'])¶ ↑
# File lib/newrelic-management/manager.rb, line 151 def find_labeled(labels, match_any = Config.transient[:alert_match_any]) # rubocop: disable AbcSize list = list_labels labeled = [] Array(labels).select do |lbl| list.select { |x| x['key'].casecmp(lbl).zero? }.each do |mtch| labeled.push(Array(mtch['links']['servers'])) end end unless match_any # => Array(labeled) should contain one array per label # => # => If it does not, it means the label is missing or misspelled return [] unless labeled.count == Array(labels).count # => Return Only those matching All Labels return Util.common_array(labeled) end labeled.flatten.uniq end
list_alert_conditions(policy_id)
click to toggle source
list_alerts()
click to toggle source
list_labels()
click to toggle source
list_nonreporting_servers()
click to toggle source
# File lib/newrelic-management/manager.rb, line 114 def list_nonreporting_servers list_servers.reject { |server| server[:reporting] } end
list_servers()
click to toggle source
> Servers with the oldest `last_reported_at` will be at the top¶ ↑
# File lib/newrelic-management/manager.rb, line 101 def list_servers Util.cachier('list_servers') do Client.servers.sort_by { |hsh| hsh['last_reported_at'] }.collect do |server| { name: server['name'], last_reported_at: server['last_reported_at'], id: server['id'], reporting: server['reporting'] } end end end
manage_alert(alert, labels, exclude = [])
click to toggle source
# File lib/newrelic-management/manager.rb, line 38 def manage_alert(alert, labels, exclude = []) # rubocop: disable AbcSize conditions = find_alert_conditions(alert) || return tagged_entities = find_labeled(labels) excluded = find_excluded(exclude) conditions.each do |condition| next unless condition['type'] == 'servers_metric' existing_entities = condition['entities'] to_add = tagged_entities.map(&:to_i) - existing_entities.map(&:to_i) - excluded to_delete = excluded & existing_entities.map(&:to_i) add_to_alert(to_add, condition['id']) delete_from_alert(to_delete, condition['id']) end end
manage_alerts()
click to toggle source
> Manage Alerts¶ ↑
# File lib/newrelic-management/manager.rb, line 28 def manage_alerts Array(Config.alerts).each do |alert| # => Set the Filtering Policy Config.transient[:alert_match_any] = alert[:match_any] ? true : false # => Manage the Alerts manage_alert(alert[:name], alert[:labels], alert[:exclude]) end end
remove_nonreporting_servers(keeptime = nil)
click to toggle source
> Remove Non-Reporting Servers¶ ↑
# File lib/newrelic-management/manager.rb, line 119 def remove_nonreporting_servers(keeptime = nil) list_nonreporting_servers.each do |server| next if keeptime && Time.parse(server[:last_reported_at]) >= Time.now - ChronicDuration.parse(keeptime) Notifier.msg(server[:name], 'Removing Stale, Non-Reporting Server') Client.delete_server(server[:id]) end end