aboutsummaryrefslogtreecommitdiff
path: root/bin/mauveserver
blob: 4ac5a6dfc290af9e2dafc240d055feae6c2863d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/ruby1.8
# NAME
# 
#  mauveserver -- receive alerts from station(s) around the network
#
# SYNOPSIS
#
#  mauveserver [ -h | --help ] [<configuration file>]
#
# OPTIONS
#
#  -h, --help            Show a help message
#
#  <configuration file>  File to load the configuration from
# 
# SEE ALSO
#
#  mauveclient(1), mauveconsole(1)
#
# AUTHOR
# 
#  Patrick J Cherry <patrick@bytemark.co.uk>
#

def error(msg)
  STDERR.print "*** Error: #{msg}\n"
  STDERR.print "*** For help, type: #{$0} -h\n"
  exit 1
end

begin
  eval "Proc.new { |a,&b| }"
rescue SyntaxError => no_blocks_with_procs
  error "mauveserver must have Ruby 1.8.7 or later"
end

# CAUTION! Kwality kode.
#
if ARGV.any?{|a| a =~ /--?h(elp)?/}
  # Open the file, stripping the shebang line
  lines = File.open(__FILE__){|fh| fh.readlines}[1..-1]

  lines.each do |line|
  line.chomp!
    break if line.empty?
    puts line[2..-1].to_s
  end

  exit 0
end


configuration_file = ARGV.shift
configuration_file = [".", "/etc/mauvealert/"].collect{|x| File.join("mauveserver.conf") }.find{|d| File.file?(d)} if configuration_file.nil?
configuration_file = File.expand_path(configuration_file)

unless File.file?(configuration_file)
  error "Configuration file #{configuration_file} not found\n"
end

require 'mauve/configuration'

begin
  Mauve::Configuration.current = Mauve::ConfigurationBuilder.load(configuration_file)
rescue StandardError => ex
  error ex.message
end

%w(HUP).each do |sig|
  trap(sig) do
    Mauve::Server.instance.logger.warn "#{sig} signal received.  Restarting."
    Mauve::Server.instance.stop

    #
    # Reload configuration
    #
    begin
      new_config = Mauve::ConfigurationBuilder.load(configuration_file)
      Mauve::Configuration.current = new_config
    rescue BuildException => ex
      Mauve::Server.instance.logger.error "Reconfiguration failed: #{ex.to_s}.  Sticking with old one."
    end
    Mauve::Server.instance.logger.warn "Restarting."
    Mauve::Server.instance.start
  end
end

%w(USR1).each do |sig|
  trap(sig) do
    Mauve::Server.instance.logger.warn "#{sig} signal received. Re-opening logs."

    Log4r::Outputter.each_outputter do |old|
      next unless old.is_a?(Log4r::FileOutputter)

      new = Log4r::FileOutputter.new(old.name, {:filename => old.filename, :trunc => false})
      new.formatter = old.formatter
      new.level     = old.level

      Mauve::Server.instance.logger.outputters << new
      Mauve::Server.instance.logger.outputters.delete(old)
      old.close

      Mauve::Server.instance.logger.info "Opened #{new.filename}."
    end
  end
end

%w(QUIT TERM INT).each do |sig|
  trap(sig) do
    Mauve::Server.instance.logger.warn "#{sig} signal received.  Stopping."
    Mauve::Server.instance.stop
    exit 0
  end
end

begin
  Mauve::Server.instance.run
rescue StandardError => ex
  error ex.message
end