#!/usr/bin/ruby

require 'symbiosis/monitor/check'
require 'symbiosis/host'

# check apache's running and listening on http and https
class ApacheCheck < Symbiosis::Monitor::Check
  def initialize(connections)
    super pid_file: '/var/run/apache2/apache2.pid',
          init_script: '/etc/init.d/apache2',
          unit_name: 'apache2',
          connections: connections
  end

  def do_response_check(responses)
    raise Errno::EPROTO, "Unexpected response '#{responses.first}'" unless responses.first =~ /^HTTP\/\d+/
  end
end

connections = %w[http https].map do |proto|
  Symbiosis::Monitor::TCPConnection.new(
    Symbiosis::Host.primary_ip.to_s,
    proto,
    ["OPTIONS / HTTP/1.0\r\n", "Host: localhost\r\n", "\r\n", nil],
    'https' == proto
  )
end

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