Class: Solanum::Source::Certificate

Inherits:
Solanum::Source show all
Defined in:
lib/solanum/source/certificate.rb

Instance Attribute Summary collapse

Attributes inherited from Solanum::Source

#attributes, #period, #type

Instance Method Summary collapse

Methods inherited from Solanum::Source

#next_run

Constructor Details

#initialize(opts) ⇒ Certificate

Returns a new instance of Certificate.



10
11
12
13
14
15
16
17
# File 'lib/solanum/source/certificate.rb', line 10

def initialize(opts)
  super(opts)
  @host = opts['host'] or raise "No host provided"
  @port = opts['port'] || 443
  @hostname = opts['hostname'] || @host
  @ca_cert = opts['ca_cert']
  @expiry_states = opts['expiry_states'] || {}
end

Instance Attribute Details

#ca_certObject (readonly)

Returns the value of attribute ca_cert.



7
8
9
# File 'lib/solanum/source/certificate.rb', line 7

def ca_cert
  @ca_cert
end

#expiry_statesObject (readonly)

Returns the value of attribute expiry_states.



7
8
9
# File 'lib/solanum/source/certificate.rb', line 7

def expiry_states
  @expiry_states
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/solanum/source/certificate.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/solanum/source/certificate.rb', line 7

def port
  @port
end

Instance Method Details

#collect!Object

Collect metric events.



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
# File 'lib/solanum/source/certificate.rb', line 48

def collect!
  events = []
  prefix = "certificate #{@hostname}"

  begin
    connect do |ssl|
      cert = ssl.peer_cert

      # Connect okay.
      events << {
        service: "#{prefix} connect",
        metric: 1,
        state: 'ok',
        description: "Certificate for #{@hostname} verified successfully",
      }

      # Certificate expiration time.
      #puts cert.inspect
      expiry = cert.not_after - Time.now
      expiry_days = expiry/86400
      events << {
        service: "#{prefix} expiry",
        metric: expiry_days,
        state: state_under(@expiry_states, expiry_days),
        description: "Certificate for #{@hostname} expires in #{duration_str(expiry)}",
      }
    end
  rescue => e
    # Connect error.
    events << {
      service: "#{prefix} connect",
      metric: 1,
      state: 'critical',
      description: "Error connecting to #{@hostname}: #{e}",
    }
  end

  events
end

#connectObject



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
# File 'lib/solanum/source/certificate.rb', line 20

def connect
  # Configure context.
  ctx = OpenSSL::SSL::SSLContext.new
  #ctx.verify_hostname = true  # Only in ruby 2.x?

  if @ca_cert
    ctx.ca_file = @ca_cert
    ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  # Open socket connection.
  sock = TCPSocket.new(@host, @port)
  ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
  ssl.sync_close = true
  ssl.hostname = @hostname
  ssl.connect

  yield ssl if block_given?
ensure
  if ssl
    ssl.close
  elsif sock
    sock.close
  end
end