aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/mauvesend2
-rw-r--r--debian/control5
-rw-r--r--lib/dm-sqlite-adapter-with-mutex.rb15
-rw-r--r--lib/mauve/person.rb4
-rw-r--r--lib/mauve/sender.rb79
-rw-r--r--test/tc_mauve_database_peculiarities.rb9
-rw-r--r--test/tc_mauve_sender.rb79
-rw-r--r--test/th_mauve.rb9
8 files changed, 164 insertions, 38 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/dm-sqlite-adapter-with-mutex.rb b/lib/dm-sqlite-adapter-with-mutex.rb
index 2842c5e..6c5c022 100644
--- a/lib/dm-sqlite-adapter-with-mutex.rb
+++ b/lib/dm-sqlite-adapter-with-mutex.rb
@@ -5,20 +5,11 @@
require 'dm-sqlite-adapter'
require 'monitor'
-ADAPTER = DataMapper::Adapters::SqliteAdapter
+class DataMapper::Adapters::SqliteAdapter
-# better way to alias a private method? (other than "don't"? :) )
-ADAPTER.__send__(:alias_method, :initialize_old, :initialize)
-ADAPTER.__send__(:undef_method, :initialize)
-ADAPTER.__send__(:alias_method, :with_connection_old, :with_connection)
-ADAPTER.__send__(:undef_method, :with_connection)
+ include MonitorMixin
-class ADAPTER
-
- def initialize(*a)
- extend(MonitorMixin)
- initialize_old(*a)
- end
+ alias_method :with_connection_old, :with_connection
private
diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb
index a4acaf0..c1c12c6 100644
--- a/lib/mauve/person.rb
+++ b/lib/mauve/person.rb
@@ -169,7 +169,7 @@ module Mauve
#
notification_method = Configuration.current.notification_methods[name.to_s]
- @logger.warn "Notification method '#{name}' not defined (#{@person.username})" if notification_method.nil?
+ logger.warn "Notification method '#{name}' not defined (#{@person.username})" if notification_method.nil?
#
# Work out the destination
@@ -182,7 +182,7 @@ module Mauve
destination = nil
end
- @logger.warn "#{name} destination for #{@person.username} not set" if destination.nil?
+ logger.warn "#{name} destination for #{@person.username} not set" if destination.nil?
if args.first.is_a?(Hash)
conditions = @base_conditions.merge(args.pop)
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_database_peculiarities.rb b/test/tc_mauve_database_peculiarities.rb
index 9eb612e..b29bd95 100644
--- a/test/tc_mauve_database_peculiarities.rb
+++ b/test/tc_mauve_database_peculiarities.rb
@@ -81,5 +81,14 @@ class TcMauveDatabaseSqlite3Peculiarities < TcMauveDatabasePeculiarities
# @pg_conn = PGconn.open(:dbname => @temp_db)
@db_url = "sqlite3::memory:"
end
+
+ #
+ # This just makes sure our mixin has been added to the SqliteAdapter.
+ #
+ def test_has_mixin
+ assert DataMapper::Adapters::SqliteAdapter.private_instance_methods.include?("with_connection_old")
+ assert DataMapper::Adapters::SqliteAdapter.public_instance_methods.include?("synchronize")
+ end
+
end
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
diff --git a/test/th_mauve.rb b/test/th_mauve.rb
index 99eaaed..4ec8dc5 100644
--- a/test/th_mauve.rb
+++ b/test/th_mauve.rb
@@ -1,5 +1,4 @@
require 'test/unit'
-require 'mauve/datamapper'
require 'timecop'
require 'log4r'
require 'pp'
@@ -94,12 +93,12 @@ module Mauve
end
def setup_database
- DataMapper::Model.raise_on_save_failure = true
+ DataMapper::Model.raise_on_save_failure = true if defined?(DataMapper::Model)
end
def teardown_database
- DataObjects::Pooling.pools.each{|pool| pool.dispose}
- end
+ DataObjects::Pooling.pools.each{|pool| pool.dispose} if defined?(DataObjects::Pooling)
+ end
def setup_time
Timecop.freeze(Time.local(2011,8,1,0,0,0,0))
@@ -110,7 +109,7 @@ module Mauve
end
def reset_mauve_configuration
- Mauve::Configuration.current = Mauve::Configuration.new
+ Mauve::Configuration.current = Mauve::Configuration.new if defined?(Mauve::Configuration)
end
def reset_all_singletons