Class: Sensu::Dashboard::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/sensu-dashboard/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.invalid_settings(reason, details = {}) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/sensu-dashboard/server.rb', line 43

def invalid_settings(reason, details={})
  $logger.fatal('invalid settings', {
    :reason => reason
  }.merge(details))
  $logger.fatal('SENSU DASHBOARD NOT RUNNING!')
  exit 2
end

.run(options = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/sensu-dashboard/server.rb', line 35

def run(options={})
  EM::run do
    setup(options)
    start
    trap_signals
  end
end

.setup(options = {}) ⇒ Object



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
# File 'lib/sensu-dashboard/server.rb', line 51

def setup(options={})
  base = Sensu::Base.new(options)
  $logger = base.logger
  settings = base.settings
  if settings[:dashboard]
    unless settings[:dashboard].is_a?(Hash)
      invalid_settings('dashboard must be a hash')
    end
  end
  $dashboard_settings = settings[:dashboard] || Hash.new
  $dashboard_settings[:port] ||= 8080
  $dashboard_settings[:poll_frequency] ||= 10
  $api_settings = $dashboard_settings[:api] || settings[:api] || Hash.new
  $api_settings[:host] ||= 'localhost'
  $api_settings[:port] ||= 4567
  unless $dashboard_settings[:port].is_a?(Integer)
    invalid_settings('dashboard port must be an integer', {
      :settings => $dashboard_settings
    })
  end
  unless $dashboard_settings[:poll_frequency].is_a?(Integer)
    invalid_settings('dashboard poll frequency must be an integer', {
      :settings => $dashboard_settings
    })
  end
  base.setup_process
  $api_url = 'http://' + $api_settings[:host] + ':' + $api_settings[:port].to_s
  $api_options = {:head => {'Accept' => 'application/json'}}
  if $api_settings[:user] && $api_settings[:password]
    $api_options.merge!(:head => {:authorization => [$api_settings[:user], $api_settings[:password]]})
  end
end

.startObject



84
85
86
87
88
# File 'lib/sensu-dashboard/server.rb', line 84

def start
  Thin::Logging.silent = true
  bind = $dashboard_settings[:bind] || '0.0.0.0'
  Thin::Server.start(bind, $dashboard_settings[:port], self)
end

.stopObject



90
91
92
93
# File 'lib/sensu-dashboard/server.rb', line 90

def stop
  $logger.warn('stopping')
  EM::stop_event_loop
end

.trap_signalsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sensu-dashboard/server.rb', line 95

def trap_signals
  $signals = Array.new
  Sensu::STOP_SIGNALS.each do |signal|
    Signal.trap(signal) do
      $signals << signal
    end
  end
  EM::PeriodicTimer.new(1) do
    signal = $signals.shift
    if Sensu::STOP_SIGNALS.include?(signal)
      $logger.warn('received signal', {
        :signal => signal
      })
      stop
    end
  end
end

Instance Method Details

#request_log_lineObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/sensu-dashboard/server.rb', line 114

def request_log_line
  $logger.info([env['REQUEST_METHOD'], env['REQUEST_PATH']].join(' '), {
    :remote_address => env['REMOTE_ADDR'],
    :user_agent => env['HTTP_USER_AGENT'],
    :request_method => env['REQUEST_METHOD'],
    :request_uri => env['REQUEST_URI'],
    :request_body =>  env['rack.input'].read
  })
  env['rack.input'].rewind
end