aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/web_interface.rb
blob: 4569cb6c13725bd644d48ea66b22021290b0ad51 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# encoding: UTF-8
require 'sinatra/base'
require 'sinatra-partials'
require 'haml'
require 'rack'
require 'rack-flash'

if !defined?(JRUBY_VERSION)
  require 'thin'
end

module Mauve
  # Our Sinatra app proper
  #
  class WebInterface < Sinatra::Base
  
    class PleaseAuthenticate < Exception; end
  
    use Rack::Session::Cookie, :expire_after => 604800 # 7 days in seconds

    enable :sessions

    use Rack::Flash
    
    set :root, "/usr/share/mauve"
    set :views, "#{root}/views"
    set :public, "#{root}/static"
    set :static, true
    set :show_exceptions, true

    logger = Log4r::Logger.new("Mauve::WebInterface")

    set :logging, true
    set :logger, logger
    set :dump_errors, true      # ...will dump errors to the log
    set :raise_errors, false    # ...will not let exceptions out to main program
    set :show_exceptions, false # ...will not show exceptions
    
    ########################################################################
    
    before do
      @person = Configuration.current.people[session['username']]
      @title = "Mauve alert panel"
    end
    
    get '/' do
      redirect '/alerts'
    end
    
    ########################################################################
    
    ## Checks the identity of the person via a password.
    #
    # The password can be either the SSO or a local one defined 
    # in the configuration file.
    #
    post '/login' do
      usr = params['username']
      pwd = params['password']
      ret_sso = helper_auth_SSO(usr, pwd)
      ret_loc = helper_auth_local(usr, pwd)
      if "success" == ret_sso or "success" == ret_loc
        session['username'] = usr
      else
        flash['error'] =<<__MSG
<hr /> <img src="/images/error.png" /> <br />
ACCESS DENIED  <br />
#{ret_sso} <br />
#{ret_loc} <hr />
__MSG
      end
      redirect '/alerts'
    end
    
    get '/logout' do
      session.delete('username')
      redirect '/alerts'
    end
    
    get '/alerts' do 
      #now = MauveTime.now.to_f
      please_authenticate()
      find_active_alerts()
      #pp MauveTime.now.to_f - now
      haml(:alerts2)
    end
    
    get '/_alert_summary' do
      find_active_alerts; partial("alert_summary")
    end

    get '/_alert_counts' do 
      find_active_alerts; partial("alert_counts")
    end

    get '/_head' do 
      find_active_alerts()
      partial("head")
    end
  
    get '/alert/:id/detail' do
      please_authenticate
    
      content_type("text/html") # I think
      Alert.get(params[:id]).detail
    end
    
    get '/alert/:id' do
      please_authenticate
      find_active_alerts
      @alert = Alert.get(params['id'])
      haml :alert
    end
    
    post '/alert/:id/acknowledge' do
      please_authenticate
      
      alert = Alert.get(params[:id])
      if alert.acknowledged?
        alert.unacknowledge!
      else
        alert.acknowledge!(@person, 0)
      end
      content_type("application/json")
      alert.to_json
    end
    
    # Note that :until must be in seconds.
    post '/alert/acknowledge/:id/:until' do
      #now = MauveTime.now.to_f
      please_authenticate

      alert = Alert.get(params[:id])
      alert.acknowledge!(@person, params[:until].to_i())
      
      #print "Acknowledge request was processed in #{MauveTime.now.to_f - now} seconds\n"
      content_type("application/json")
      alert.to_json
    end

    post '/alert/:id/raise' do
      #now = MauveTime.now.to_f
      please_authenticate
      
      alert = Alert.get(params[:id])
      alert.raise!
      #print "Raise request was processed in #{MauveTime.now.to_f - now} seconds\n"
      content_type("application/json")
      alert.to_json
    end
    
    post '/alert/:id/clear' do
      please_authenticate
      
      alert = Alert.get(params[:id])
      alert.clear!
      content_type("application/json")
      alert.to_json
    end

    post '/alert/:id/toggleDetailView' do 
      please_authenticate
      
      alert = Alert.get(params[:id])
      if nil != alert
        id = params[:id].to_i()
        session[:display_alerts][id] = (true == session[:display_alerts][id])? false : true
        content_type("application/json")
        'all is good'.to_json
      end
    end

    post '/alert/fold/:subject' do 
      please_authenticate
      
      session[:display_folding][params[:subject]] = (true == session[:display_folding][params[:subject]])? false : true
      content_type("application/json")
      'all is good'.to_json
    end

    ########################################################################
    
    get '/preferences' do
      please_authenticate
      find_active_alerts
      haml :preferences
    end
    
    ########################################################################
    
    get '/events' do
      please_authenticate
      find_active_alerts
      find_recent_alerts
      haml :events
    end
    
    ########################################################################
    
    helpers do
      include Sinatra::Partials
      
      def please_authenticate
        raise PleaseAuthenticate.new unless @person
      end
      
      def find_active_alerts

        # FIXME: make sure alerts only appear once some better way
        #@urgent = AlertGroup.all_alerts_by_level(:urgent)
        #@normal = AlertGroup.all_alerts_by_level(:normal) - @urgent
        #@low = AlertGroup.all_alerts_by_level(:low) - @normal - @urgent
        ook = Alert.get_all()
        @urgent = ook[:urgent]
        @normal = ook[:normal]
        @low = ook[:low]

        # Get groups of alerts and count those acknowledged.
        @grouped_ack_urgent = Hash.new()
        @grouped_ack_normal = Hash.new()
        @grouped_ack_low = Hash.new()
        @grouped_new_urgent = Hash.new()
        @grouped_new_normal = Hash.new()
        @grouped_new_low = Hash.new()
        @count_ack = Hash.new()
        @count_ack[:urgent] = self.group_alerts(@grouped_ack_urgent, 
                                                @grouped_new_urgent,
                                                @urgent)
        @count_ack[:normal] = self.group_alerts(@grouped_ack_normal,
                                                @grouped_new_normal,
                                                @normal)
        @count_ack[:low] = self.group_alerts(@grouped_ack_low,
                                             @grouped_new_low,
                                             @low)
        @grouped_ack = Hash.new()
        @grouped_new = Hash.new()
        @grouped_ack_urgent.each_pair {|k,v| @grouped_ack[k] = v}
        @grouped_ack_normal.each_pair {|k,v| @grouped_ack[k] = v}
        @grouped_ack_low.each_pair {|k,v| @grouped_ack[k] = v}
        @grouped_new_urgent.each_pair {|k,v| @grouped_new[k] = v}
        @grouped_new_normal.each_pair {|k,v| @grouped_new[k] = v}
        @grouped_new_low.each_pair {|k,v| @grouped_new[k] = v}
      end
      
      ## Fill two hashs with alerts that are acknowledged or not.
      # @param [Hash] ack Acknowledge hash.
      # @param [Hash] new Unacknowledged (aka new) hash.
      # @param [List] list List of alerts.
      # @return [Fixnum] The count of acknowledged alerts.
      def group_alerts(ack, new, list)
        count = 0
        list.each do |alert| 
          #key = alert.source + '::' + alert.subject
          key = alert.subject
          if true == alert.acknowledged?
            count += 1
            ack[key] = Array.new() if false == ack.has_key?(key)
            ack[key] << alert
          else
            new[key] = Array.new() if false == new.has_key?(key)
            new[key] << alert
          end
          if false == session[:display_alerts].has_key?(alert.id)
            session[:display_alerts][alert.id] = false
          end
          if false == session[:display_folding].has_key?(key)
            session[:display_folding][key] = false 
          end
          #session[:display_alerts][alert.id] = true if false == session[:display_alerts].has_key?(alert.id)
          #session[:display_folding][key] = true if false == session[:display_folding].has_key?(key)
          new.each_key {|k| new[k].sort!{|a,b| a.summary <=> b.summary} }
          ack.each_key {|k| ack[k].sort!{|a,b| a.summary <=> b.summary} }
        end
        return count
      end

      def find_recent_alerts
        since = params['since'] ? MauveTime.parse(params['since']) : (MauveTime.now-86400)
        @alerts = Alert.all(:updated_at.gt => since, :order => [:raised_at.desc, :cleared_at.desc, :acknowledged_at.desc, :updated_at.desc, ])
      end
      
      def cycle(*list)
        @cycle ||= 0
        @cycle = (@cycle + 1) % list.length
        list[@cycle]
      end

      ## Test for authentication with SSO.
      #
      def helper_auth_SSO (usr, pwd)
        auth = AuthSourceBytemark.new()
        begin
          return "success" if true == auth.authenticate(usr,pwd)
          return "SSO did not regcognise your login/password combination."
        rescue ArgumentError => ex
          return "SSO argument error: #{ex.message}"
        rescue => ex
          return "SSO generic error: #{ex.message}"
        end
      end

      ## Test for authentication with configuration file parameter.
      #
      def helper_auth_local (usr, pwd)
        person = Configuration.current.people[params['username']]
        return "I did not recognise your local login details." if !person
        return "I did not recognise your local password." if Digest::SHA1.hexdigest(params['password']) != person.password
        return "success"
      end

    end
    
    ########################################################################
    
    error PleaseAuthenticate do
      status 403
      session[:display_alerts] = Hash.new()
      session[:display_folding] = Hash.new()
      haml :please_authenticate
    end
    
    ########################################################################
    # @see http://stackoverflow.com/questions/2239240/use-rackcommonlogger-in-sinatra
    def call(env)
      if true == @logger.nil?
        @logger = Log4r::Logger.new("mauve::Rack")
      end
      env['rack.errors'] = RackErrorsProxy.new(@logger)
      super(env)
    end
    
  end

end