Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins

## [Unreleased]

### Added
- `check-ssh-cert.rb` can now calculate expiry in hours, instead of days.

### Changed
- Removed centos build from .bonsai.yml

Expand Down
39 changes: 26 additions & 13 deletions bin/check-ssl-cert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# for details.
#

require 'date'
require 'time'
require 'openssl'
require 'sensu-plugin/check/cli'

Expand All @@ -38,15 +38,15 @@
#
class CheckSSLCert < Sensu::Plugin::Check::CLI
option :critical,
description: 'Numbers of days left',
description: 'Time (hours or days) left',
short: '-c',
long: '--critical DAYS',
long: '--critical TIME',
required: true

option :warning,
description: 'Numbers of days left',
description: 'Time (hours or days) left',
short: '-w',
long: '--warning DAYS',
long: '--warning TIME',
required: true

option :pem,
Expand Down Expand Up @@ -79,6 +79,11 @@ class CheckSSLCert < Sensu::Plugin::Check::CLI
short: '-S',
long: '--pass '

option :hours,
description: 'Calculate expiry in hours, instead of days. Useful for short-lived (<24h) ACME certs',
short: '-H',
long: '--hours'

def ssl_cert_expiry
`openssl s_client -servername #{config[:servername]} -connect #{config[:host]}:#{config[:port]} < /dev/null 2>&1 | openssl x509 -enddate -noout`.split('=').last
end
Expand Down Expand Up @@ -117,16 +122,24 @@ def run
ssl_cert_expiry
end

days_until = (Date.parse(expiry.to_s) - Date.today).to_i
time_delta = Time.parse(expiry.to_s) - Time.now

if config[:hours]
time_delta_check = (time_delta / 3600).floor
time_check_unit = 'hours'
else
time_delta_check = (time_delta / 86_400).floor
time_check_unit = 'days'
end

if days_until < 0 # rubocop:disable Style/NumericPredicate
critical "Expired #{days_until.abs} days ago"
elsif days_until < config[:critical].to_i
critical "#{days_until} days left"
elsif days_until < config[:warning].to_i
warning "#{days_until} days left"
if time_delta_check < 0 # rubocop:disable Style/NumericPredicate
critical "Expired #{time_delta_check} #{time_check_unit} ago"
elsif time_delta_check < config[:critical].to_i
critical "#{time_delta_check} #{time_check_unit} left"
elsif time_delta_check < config[:warning].to_i
warning "#{time_delta_check} #{time_check_unit} left"
else
ok "#{days_until} days left"
ok "#{time_delta_check} #{time_check_unit} left"
end
end
end