#!/usr/bin/ruby

require 'symbiosis/monitor/check'

# check that exim4 is running & the tcp connection is running
class Exim4Check < Symbiosis::Monitor::Check
  def initialize(connections)
    super pid_file: '/var/run/exim4/exim.pid',
          init_script: '/etc/init.d/exim4',
          unit_name: 'exim4',
          connections: connections
  end

  def do_tcpresponse_check(responses)
    bad = responses.find { |l| l !~ /^2\d+[ -]/ }
    raise "Unexpected response '#{bad}'" unless bad.nil?
  end
end

resp = /^\d+\s/

connections = %w[smtp ssmtp].map do |proto|
  Symbiosis::Monitor::TCPConnection.new(
    'localhost',
    proto,
    [resp, "EHLO localhost\r\n", resp, "QUIT\r\n", resp],
    'ssmtp' == proto
  )
end

exit Exim4Check.new(connections).do_check if $PROGRAM_NAME == __FILE__
