class Riemann::Tools::NginxStatus

Public Class Methods

new() click to toggle source
# File lib/riemann/tools/nginx_status.rb, line 28
def initialize
  @uri = URI.parse(opts[:uri])

  # sample response:
  #
  # Active connections: 1
  # server accepts handled requests
  #  39 39 39
  # Reading: 0 Writing: 1 Waiting: 0
  @keys = %w[active accepted handled requests reading writing waiting]
  @re = /Active connections: (\d+) \n.+\n (\d+) (\d+) (\d+) \nReading: (\d+) Writing: (\d+) Waiting: (\d+)/m
end

Public Instance Methods

state(key, value) click to toggle source
# File lib/riemann/tools/nginx_status.rb, line 41
def state(key, value)
  if opts.key? "#{key}_critical".to_sym
    critical_threshold = opts["#{key}_critical".to_sym]
    return 'critical' if critical_threshold.positive? && (value >= critical_threshold)
  end

  if opts.key? "#{key}_warning".to_sym
    warning_threshold = opts["#{key}_warning".to_sym]
    return 'warning' if warning_threshold.positive? && (value >= warning_threshold)
  end

  'ok'
end
tick() click to toggle source
# File lib/riemann/tools/nginx_status.rb, line 55
def tick
  response = nil
  begin
    response = ::Net::HTTP.new(@uri.host, @uri.port).get(@uri, { 'user-agent' => opts[:user_agent] }).body
  rescue StandardError => e
    report(
      service: 'nginx health',
      state: 'critical',
      description: "Connection error: #{e.class} - #{e.message}",
    )
  end

  return if response.nil?

  report(
    service: 'nginx health',
    state: 'ok',
    description: 'Nginx status connection ok',
  )

  values = @re.match(response).to_a[1, 7].map(&:to_i)

  @keys.zip(values).each do |key, value|
    report({
             service: "nginx #{key}",
             metric: value,
             state: state(key, value),
             tags: ['nginx'],
           })
  end
end