diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-03-22 17:31:27 +0000 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-03-22 17:31:27 +0000 |
commit | 24554e44478fadb3c86dea76773d456c5284596d (patch) | |
tree | 817319e8d2f579a8f690d756d0fb62e74100ac30 | |
parent | 53c5b4412c46dd289eeac08a9a4768aaf9d069ff (diff) |
Rejigged caching of alert_group, and the way source lists are searched, to
(hopefully) cut down on DNS resolving.
-rw-r--r-- | lib/mauve/alert.rb | 124 | ||||
-rw-r--r-- | lib/mauve/mauve_resolv.rb | 8 | ||||
-rw-r--r-- | lib/mauve/source_list.rb | 37 |
3 files changed, 117 insertions, 52 deletions
diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index d867ccd..edaa2ad 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -146,15 +146,18 @@ module Mauve # # Find the AlertGroup by name if we've got a cached value # - @alert_group = AlertGroup.all.find{|a| self.cached_alert_group == a.name} if @alert_group.nil? and self.cached_alert_group + alert_group = AlertGroup.all.find{|a| self.cached_alert_group == a.name} if self.cached_alert_group - # - # If we've not found the alert group by name, or the object hasn't been - # saved since the alert group was last resolved, look for it again. - # - @alert_group = AlertGroup.matches(self).first if @alert_group.nil? + if alert_group.nil? + # + # If we've not found the alert group by name look for it again, the + # proper way. + # + alert_group = AlertGroup.matches(self).first + self.cached_alert_group = alert_group.name unless alert_group.nil? + end - @alert_group + alert_group end # Pick out the source lists that match this alert by subject. @@ -169,32 +172,53 @@ module Mauve # @param [String] listname # @return [Boolean] def in_source_list?(listname) - list = Mauve::Configuration.current.source_lists[listname] - return false unless list.is_a?(SourceList) - self.subject_hosts_and_ips.any?{|host| list.includes?(host) } - end + source_list = Mauve::Configuration.current.source_lists[listname] + return false unless source_list.is_a?(SourceList) - def subject_hosts_and_ips - if @subject_hosts_and_ips.nil? or @subject_hosts_and_ips_last_resolved_at.nil? or (Time.now - 1800) > @subject_hosts_and_ips_last_resolved_at - host = self.subject - # - # Pick out hostnames from URIs. - # - if host =~ /^[a-z][a-z0-9+-]+:\/\// - begin - uri = URI.parse(host) - host = uri.host unless uri.host.nil? - rescue URI::InvalidURIError => ex - # ugh - logger.warn "Did not recognise URI #{host}" - end + host = self.subject + + # + # Pick out hostnames from URIs. + # + if host =~ /^[a-z][a-z0-9+-]+:\/\// + begin + uri = URI.parse(host) + host = uri.host unless uri.host.nil? + rescue URI::InvalidURIError => ex + # ugh + logger.warn "Did not recognise URI #{host}" end + end - @subject_hosts_and_ips = ([host] + MauveResolv.get_ips_for(host)).flatten - @subject_hosts_and_ips_last_resolved_at = Time.now + return true if source_list.list.any? do |l| + case l + when String + host == l + when Regexp + host =~ l + when IPAddr + begin + l.include?(IPAddr.new(host)) + rescue ArgumentError + # rescue random IPAddr argument errors + false + end + else + false + end end - @subject_hosts_and_ips + return false unless source_list.list.any?{|l| l.is_a?(IPAddr)} + + @subject_ips ||= MauveResolv.get_ips_for(host).collect{|i| IPAddr.new(i)} + + return false if @subject_ips.nil? or @subject_ips.empty? + + return source_list.list.select{|i| i.is_a?(IPAddr)}.any? do |list_ip| + @subject_ips.any?{|ip| list_ip.include?(ip)} + end + + return false end # Returns the alert level @@ -229,6 +253,38 @@ module Mauve # # @return [String] def detail; attribute_get(:detail) || "_No detail set._" ; end + + # + # Set the subject -- this clears the cached_alert_group. + # + def subject=(s) + attribute_set(:subject, s) + self.cached_alert_group = nil + end + + # + # Set the detail -- this clears the cached_alert_group. + # + def detail=(s) + attribute_set(:detail, s) + self.cached_alert_group = nil + end + + # + # Set the source -- this clears the cached_alert_group. + # + def source=(s) + attribute_set(:source, s) + self.cached_alert_group = nil + end + + # + # Set the summary -- this clears the cached_alert_group. + # + def summary=(s) + attribute_set(:summary, s) + self.cached_alert_group = nil + end protected @@ -326,6 +382,10 @@ module Mauve true end + # + # + # + # Remove all history for an alert, when an alert is destroyed. # # @@ -372,9 +432,8 @@ module Mauve # # Re-cache the alert group. # - @alert_group = nil self.cached_alert_group = nil - self.cached_alert_group = self.alert_group.name + self.alert_group unless save logger.error("Couldn't save #{self}") @@ -428,9 +487,8 @@ module Mauve # # Re-cache the alert group. # - @alert_group = nil self.cached_alert_group = nil - self.cached_alert_group = self.alert_group.name + self.alert_group logger.info("Postponing raise of #{self} until #{postpone_until} as it was last updated in a prior run of Mauve.") else @@ -446,7 +504,7 @@ module Mauve # # Cache the alert group, but only if not already set. # - self.cached_alert_group = self.alert_group.name if self.cached_alert_group.nil? + self.alert_group end unless save diff --git a/lib/mauve/mauve_resolv.rb b/lib/mauve/mauve_resolv.rb index a9c7526..8ce8969 100644 --- a/lib/mauve/mauve_resolv.rb +++ b/lib/mauve/mauve_resolv.rb @@ -15,12 +15,10 @@ module Mauve # @return [Array] Array of IP addresses, as Strings. # def get_ips_for(host) - pp host ips = [] - @count ||= 0 Resolv::DNS.open do |dns| %w(A AAAA).each do |type| - @count += 1 + self.count += 1 if $debug begin ips += dns.getresources(host, Resolv::DNS::Resource::IN.const_get(type)).collect{|a| a.address.to_s} rescue Resolv::ResolvError, Resolv::ResolvTimeout => e @@ -35,6 +33,10 @@ module Mauve @count ||= 0 end + def count=(c) + @count = c + end + # @return [Log4r::Logger] def logger @logger ||= Log4r::Logger.new(self.to_s) diff --git a/lib/mauve/source_list.rb b/lib/mauve/source_list.rb index 63a7aa6..a6baa2b 100644 --- a/lib/mauve/source_list.rb +++ b/lib/mauve/source_list.rb @@ -19,7 +19,7 @@ module Mauve # class SourceList - attr_reader :label, :list, :last_resolved_at + attr_reader :label, :last_resolved_at ## Default contructor. def initialize (label) @@ -101,6 +101,15 @@ module Mauve @logger ||= Log4r::Logger.new self.class.to_s end + def list + # + # Redo resolution every thirty minutes + # + resolve if @resolved_list.empty? or @last_resolved_at.nil? or (Time.now - 1800) > @last_resolved_at + + @resolved_list + end + # # Return whether or not a list contains a source. # @@ -118,11 +127,6 @@ module Mauve # @return [Boolean] def includes?(host) # - # Redo resolution every thirty minutes - # - resolve if @resolved_list.empty? or @last_resolved_at.nil? or (Time.now - 1800) > @last_resolved_at - - # # Pick out hostnames from URIs. # if host =~ /^[a-z][a-z0-9+-]+:\/\// @@ -135,7 +139,7 @@ module Mauve end end - return true if @resolved_list.any? do |l| + return true if self.list.any? do |l| case l when String host == l @@ -153,15 +157,15 @@ module Mauve end end -# return false unless @resolved_list.any?{|l| l.is_a?(IPAddr)} -# -# ips = MauveResolv.get_ips_for(host).collect{|i| IPAddr.new(i)} -# -# return false if ips.empty? -# -# return @resolved_list.select{|i| i.is_a?(IPAddr)}.any? do |list_ip| -# ips.any?{|ip| list_ip.include?(ip)} -# end + return false unless self.list.any?{|l| l.is_a?(IPAddr)} + + ips = MauveResolv.get_ips_for(host).collect{|i| IPAddr.new(i)} + + return false if ips.empty? + + return self.list.select{|i| i.is_a?(IPAddr)}.any? do |list_ip| + ips.any?{|ip| list_ip.include?(ip)} + end return false end @@ -181,6 +185,7 @@ module Mauve host end end + @resolved_list = new_list.flatten end end |