Skip to content

Instantly share code, notes, and snippets.

@benlemasurier
Last active August 29, 2015 14:03
Show Gist options
  • Save benlemasurier/baecbb7f666b8d750b85 to your computer and use it in GitHub Desktop.
Save benlemasurier/baecbb7f666b8d750b85 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# check-smtp
# ===
#
# This is a simple SMTP check script for Sensu
#
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'net/smtp'
require 'sensu-plugin/check/cli'
require 'timeout'
class CheckSMTP < Sensu::Plugin::Check::CLI
option :host,
:description => 'Mail server host. Default: localhost',
:short => '-h HOST',
:long => '--host HOST',
:default => 'localhost'
option :port,
:description => 'Mail server port. Default: 25',
:short => '-p PORT',
:long => '--port PORT',
:default => '25'
option :user,
:description => 'Username. Default: test',
:short => '-u USER',
:long => '--user USER',
:default => 'test'
option :password,
:description => 'Password. Default: test',
:short => '-P PASSWORD',
:long => '--password PASSWORD',
:default => 'test'
option :domain,
:description => 'Mail from domain. Default: example.com',
:short => '-d DOMAIN',
:long => '--domain DOMAIN',
:default => 'example.com'
option :tls,
:description => 'Enable SSL/TLS. Default: false',
:short => '-t',
:long => '--tls',
:default => false,
:boolean => true
def run
p config
begin
Timeout.timeout(15) do
smtp = Net::SMTP.new(config[:host], config[:port])
smtp.enable_starttls unless !config[:tls]
smtp.start(config[:domain], config[:user], config[:password], :login)
end
ok
rescue Timeout::Error
critical "SMTP connection timed out"
rescue OpenSSL::SSL::SSLError => e
critical "SSL Error: #{e.message}"
rescue => e
critical "Error: #{e.message}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment