class Checkson::Check::Certificate

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method Checkson::Check::Base::new
# File lib/checkson/checks/certificate.rb, line 6
def initialize(opts = {})
  @opts = (@opts || {}).merge(opts)
  @opts[:port] ||= 443
  super()
end

Public Instance Methods

check() click to toggle source
# File lib/checkson/checks/certificate.rb, line 12
def check
  raise ArgumentError, 'No options given' unless @opts[:domain] && @opts[:port] || @opts[:certfile] || @opts[:leftdays]

  @opts[:domain] ? http_check : file_check
end

Protected Instance Methods

cert_check(cert) click to toggle source
# File lib/checkson/checks/certificate.rb, line 20
def cert_check(cert)
  time_left = @opts[:leftdays] * 86_400
  failed! if Time.now + time_left > cert.not_after
end
file_check() click to toggle source
# File lib/checkson/checks/certificate.rb, line 25
def file_check
  require 'openssl'
  begin
    raw = File.read @opts[:certfile] if File.file? @opts[:certfile]
    certificate = OpenSSL::X509::Certificate.new raw
    cert_check certificate
  rescue StandardError
    failed!
  end
end
http_check() click to toggle source
# File lib/checkson/checks/certificate.rb, line 36
def http_check
  require 'net/http'
  require 'openssl'

  uri = URI::HTTPS.build(host: @opts[:domain], port: @opts[:port])
  begin
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
    cert = response.peer_cert

    cert_check cert
  rescue StandardError
    failed!
  end
end