Class: Sensu::Socket

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/sensu/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amqObject

Returns the value of attribute amq.



3
4
5
# File 'lib/sensu/socket.rb', line 3

def amq
  @amq
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/sensu/socket.rb', line 3

def logger
  @logger
end

#replyObject

Returns the value of attribute reply.



3
4
5
# File 'lib/sensu/socket.rb', line 3

def reply
  @reply
end

#settingsObject

Returns the value of attribute settings.



3
4
5
# File 'lib/sensu/socket.rb', line 3

def settings
  @settings
end

Instance Method Details

#receive_data(data) ⇒ Object



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
# File 'lib/sensu/socket.rb', line 11

def receive_data(data)
  if data =~ /[\x80-\xff]/n
    @logger.warn('socket received non-ascii characters')
    respond('invalid')
  elsif data.strip == 'ping'
    @logger.debug('socket received ping')
    respond('pong')
  else
    @logger.debug('socket received data', {
      :data => data
    })
    begin
      check = Oj.load(data)
      check[:issued] = Time.now.to_i
      check[:status] ||= 0
      validates = [
        check[:name] =~ /^[\w\.-]+$/,
        check[:output].is_a?(String),
        check[:status].is_a?(Integer)
      ].all?
      if validates
        payload = {
          :client => @settings[:client][:name],
          :check => check
        }
        @logger.info('publishing check result', {
          :payload => payload
        })
        @amq.direct('results').publish(Oj.dump(payload))
        respond('ok')
      else
        @logger.warn('invalid check result', {
          :check => check
        })
        respond('invalid')
      end
    rescue Oj::ParseError => error
      @logger.warn('check result must be valid json', {
        :data => data,
        :error => error.to_s
      })
      respond('invalid')
    end
  end
end

#respond(data) ⇒ Object



5
6
7
8
9
# File 'lib/sensu/socket.rb', line 5

def respond(data)
  unless @reply == false
    send_data(data)
  end
end