class PoiseMonit::Resources::Monit::Resource

A `monit` resource to install and configure Monit.

@provides monit @action enable @action disable @action start @action stop @action restart @action reload @example

monit 'monit'

Public Instance Methods

confd_path() click to toggle source

@!attribute [r] confd_path

Path to the conf.d/ directory.
@return [String]
# File lib/poise_monit/resources/monit.rb, line 122
def confd_path
  ::File.join(path, 'conf.d')
end
config_path() click to toggle source

@!attribute [r] config_path

Path to the Monit configuration file.
@return [String]
# File lib/poise_monit/resources/monit.rb, line 129
def config_path
  ::File.join(path, 'monitrc')
end
httpd_is_unix?() click to toggle source

@!attribute [r] httpd_is_unix?

Are we using a Unix socket or TCP socket for Monit's HTTPD?
@return [Boolean]
# File lib/poise_monit/resources/monit.rb, line 143
def httpd_is_unix?
  httpd_port.to_s[0] == '/'
end
monit_binary() click to toggle source

The path to the `monit` binary for this Monit installation. This is an output property.

@return [String] @example

execute "#{resources('monit[monit]').monit_binary} start myapp"
# File lib/poise_monit/resources/monit.rb, line 153
def monit_binary
  provider_for_action(:monit_binary).monit_binary
end
password_path() click to toggle source

@!attribute [r] password_path

Path to the CLI password state tracking file.
@return [String]
# File lib/poise_monit/resources/monit.rb, line 136
def password_path
  ::File.join(var_path, '.cli-password')
end

Private Instance Methods

default_httpd_password() click to toggle source

Default HTTP password for CLI authentication.

@return [String]

# File lib/poise_monit/resources/monit.rb, line 171
def default_httpd_password
  if httpd_is_unix?
    # Unix sockets have ACLs already, don't need a password.
    nil
  elsif ::File.exist?(password_path)
    # Strip is just to be safe in case a user edits the file.
    IO.read(password_path).strip
  else
    # Monit offers no protections against brute force so use a long one.
    SecureRandom.hex(64)
  end
end
default_httpd_port() click to toggle source

Default HTTP port. Use a Unix socket if possible for security reasons.

# File lib/poise_monit/resources/monit.rb, line 160
def default_httpd_port
  if monit_version && UNIXSOCKET_VERSION.satisfied_by?(monit_version)
    monit_name_path('/var/run/%{name}.sock')
  else
    2812
  end
end
default_logfile() click to toggle source

Default logfile path.

@return [String]

# File lib/poise_monit/resources/monit.rb, line 187
def default_logfile
  monit_name_path('/var/log/%{name}.log')
end
default_path() click to toggle source

Default config path.

@return [String]

# File lib/poise_monit/resources/monit.rb, line 194
def default_path
  monit_name_path('/etc/%{name}')
end
default_pidfile() click to toggle source

Default pidfile path. This MUST NOT be the same as the pidfile used by the sysvinit or dummy providers or monit will refuse to start. It also can't be /dev/null or something tricky because the client uses it to check if the daemon is running. I hate monit.

@return [String]

# File lib/poise_monit/resources/monit.rb, line 204
def default_pidfile
  monit_name_path('/var/run/%{name}_real.pid')
end
default_var_path() click to toggle source

Default var files path.

@return [String]

# File lib/poise_monit/resources/monit.rb, line 211
def default_var_path
  monit_name_path('/var/lib/%{name}')
end
monit_name_path(path) click to toggle source

Interpolate the name of this monit instance in to a path.

@api private @param path [String] A string with a %{name} template in it. @return [String]

# File lib/poise_monit/resources/monit.rb, line 236
def monit_name_path(path)
  name = if service_name == 'monit'
    'monit'
  else
    # If we are using a non-default service name, put that in the path.
    "monit-#{service_name}"
  end
  path % {name: name}
end
monit_version() click to toggle source

Find the version of Monit installed. Returns nil if monit is not installed.

@api private @return [String, nil]

# File lib/poise_monit/resources/monit.rb, line 220
def monit_version
  @monit_version ||= begin
    cmd = shell_out([monit_binary, '-V'])
    if !cmd.error? && /version ([0-9.]+)$/ =~ cmd.stdout
      Gem::Version.create($1)
    else
      nil
    end
  end
end