diff options
-rw-r--r-- | bin/mauvesend | 2 | ||||
-rw-r--r-- | debian/control | 5 | ||||
-rw-r--r-- | lib/mauve/sender.rb | 79 | ||||
-rw-r--r-- | test/tc_mauve_sender.rb | 79 |
4 files changed, 146 insertions, 19 deletions
diff --git a/bin/mauvesend b/bin/mauvesend index edb8e33..a61af49 100644 --- a/bin/mauvesend +++ b/bin/mauvesend @@ -318,8 +318,6 @@ begin error "Cannot send update -- not all libraries are available" if update.nil? error "No alerts specified" unless !update.alert.empty? || update.replace - update.transmission_id = rand(2**63) - Mauve::Sender.new(ARGV).send(update, verbose) rescue ArgumentError => ae diff --git a/debian/control b/debian/control index 599ec3d..798f058 100644 --- a/debian/control +++ b/debian/control @@ -9,8 +9,9 @@ Standards-Version: 3.9.1 Package: mauvealert-client Architecture: all Depends: ruby1.8, - mauvealert-common (>= 3.4.0), + mauvealert-common (>= 3.8.0), ${misc:Depends} +Recommends: liblocale-ruby1.8 | ruby-locale Description: Mauve network alert system -- client Mauve is a network alert system for system and network administrators. You can use it to quickly set up ad-hoc monitoring for a variety of services, and @@ -22,7 +23,7 @@ Description: Mauve network alert system -- client Package: mauvealert-server Architecture: all Pre-Depends: libjs-jquery -Depends: mauvealert-common (>= 3.4.0), +Depends: mauvealert-common (>= 3.8.0), adduser, libdm-core-ruby1.8 (>= 1.2.0), libdm-migrations-ruby1.8 (>= 1.2.0), diff --git a/lib/mauve/sender.rb b/lib/mauve/sender.rb index c84dcb0..054313e 100644 --- a/lib/mauve/sender.rb +++ b/lib/mauve/sender.rb @@ -1,8 +1,21 @@ # encoding: UTF-8 require 'ipaddr' require 'socket' +begin + require 'locale' +rescue LoadError + # Do nothing -- these are bonus libraries :) +end + +begin + require 'iconv' +rescue LoadError + # Do nothing -- these are bonus libraries :) +end + require 'mauve/mauve_resolv' require 'mauve/mauve_time' +require 'mauve/proto' module Mauve # @@ -129,32 +142,68 @@ module Mauve end - # Send an update. - # - # @param [Mauve::Proto] update The update to send - # @param [Integer] vebose The verbosity -- higher is more. + # Sanitise all fields in an update, such that when we send, they are + # normal. + # # - # @return [Integer] the number of packets sent. - def send(update, verbose=0) - + def sanitize(update) # # Must have a source, so default to hostname if user doesn't care update.source ||= Socket.gethostname - + + # + # Check the locale charset. This is to maximise the amout of information + # mauve receives, rather than provide proper sanitised data for the server. + # + from_charset = (Locale.current.charset || Locale.charset) if defined?(Locale) + from_charset ||= "UTF-8" + # - # Make sure all alerts default to "-r now" + # # + update.each_field do |field, value| + # + # Make sure all string fields are UTF8 -- to ensure the maximal amount of information is sent. + # + update.__send__("#{field.name}=", Iconv.conv("UTF-8//IGNORE", from_charset, value)) if value.is_a?(String) and defined?(Iconv) + end + update.alert.each do |alert| - next if alert.raise_time || alert.clear_time - alert.raise_time = Time.now.to_i + # + # Make sure all alerts default to "-r now" + # + alert.raise_time = Time.now.to_i unless (alert.raise_time > 0 or alert.clear_time > 0) + + alert.each_field do |field, value| + # + # Make sure all string fields are UTF8 -- to ensure the maximal amount of information is sent. + # + alert.__send__("#{field.name}=", Iconv.conv("UTF-8//IGNORE", from_charset, value)) if value.is_a?(String) and defined?(Iconv) + end end - + + # + # Make sure we set the transmission time and ID. + # + update.transmission_time = Time.now.to_i if update.transmission_time.nil? or update.transmission_time == 0 + update.transmission_id = rand(2**63) if update.transmission_id.nil? or update.transmission_id == 0 + + update + end + + # Send an update. + # + # @param [Mauve::Proto] update The update to send + # @param [Integer] vebose The verbosity -- higher is more. + # + # @return [Integer] the number of packets sent. + def send(update, verbose=0) # - # Make sure we set the transmission time + # Clean up the update, and set any missing fields. # - update.transmission_time = Time.now.to_i + update = sanitise(update) - data = update.serialize_to_string + data = sanitize(update).serialize_to_string if verbose == 1 summary = "#{update.transmission_id} from #{update.source}" diff --git a/test/tc_mauve_sender.rb b/test/tc_mauve_sender.rb new file mode 100644 index 0000000..7db06c1 --- /dev/null +++ b/test/tc_mauve_sender.rb @@ -0,0 +1,79 @@ +$:.unshift "../lib" + +require 'th_mauve_resolv' +require 'test/unit' +require 'pp' +require 'timecop' +require 'mauve/sender' +require 'locale' +require 'iconv' + + +class TcMauveSender < Test::Unit::TestCase + include Mauve + + def setup + Timecop.freeze(Time.local(2011,8,1,0,0,0,0)) + end + + def teardown + Timecop.return + end + + def test_sanitise + Locale.clear + Locale.current = "en_GB.ISO-8859-1" + + # + # Set up a couple of crazy sources. + # + utf8_source = "Å ðîßtáñt plàñët" + iso88591_source = Iconv.conv(Locale.current.charset, "UTF-8", utf8_source) + + # + # Make sure our two sources are distinct + # + assert(iso88591_source != utf8_source) + + sender = Sender.new("test-1.example.com") + update = Mauve::Proto::AlertUpdate.new + update.source = iso88591_source + update.replace = false + + alert = Mauve::Proto::Alert.new + update.alert << alert + + alert_cleared = Mauve::Proto::Alert.new + update.alert << alert_cleared + alert_cleared.clear_time = Time.now.to_i + + # + # Make sure the update has the correct source + # + assert_equal(iso88591_source, update.source) + + # + # Sanitize + # + update = sender.sanitize(update) + + # + # Now make sure the sanitization has changed it back to UTF-8 + # + assert_equal(utf8_source, update.source) + + # + # Now make sure the transmission time + id have been set + # + assert_equal(Time.now.to_i, update.transmission_time) + assert_kind_of(Integer, update.transmission_id) + + # + # Make sure that the alert has its raise time set by default + # + assert_equal(Time.now.to_i, alert.raise_time) + assert_equal(0, alert_cleared.raise_time) + end + + +end |