class Bipbip::Plugin::Nginx

Public Instance Methods

match_or_fail(string, regexp) click to toggle source

@param [String] string @param [Regexp] regexp

# File lib/bipbip/plugin/nginx.rb, line 46
def match_or_fail(string, regexp)
  match_data = regexp.match(string)
  if match_data.nil?
    raise "Data `#{string}` doesn't match pattern `#{regexp}`."
  end
  match_data
end
metrics_schema() click to toggle source
# File lib/bipbip/plugin/nginx.rb, line 3
def metrics_schema
  [
    { name: 'connections_accepts', type: 'counter', unit: 'Connections' },
    { name: 'connections_handled', type: 'counter', unit: 'Connections' },
    { name: 'connections_dropped', type: 'counter', unit: 'Connections' },
    { name: 'connections_requests', type: 'counter', unit: 'Requests' },
    { name: 'active_total', type: 'gauge', unit: 'Connections' },
    { name: 'active_reading', type: 'gauge', unit: 'Connections' },
    { name: 'active_writing', type: 'gauge', unit: 'Connections' },
    { name: 'active_waiting', type: 'gauge', unit: 'Connections' }
  ]
end
monitor() click to toggle source
# File lib/bipbip/plugin/nginx.rb, line 16
def monitor
  uri = URI.parse(config['url'])
  response = Net::HTTP.get_response(uri)

  raise "Invalid response from server at #{config['url']}" unless response.code == '200'

  lines = response.body.split(/\r*\n/)
  lines.map(&:strip!)

  data = {}

  stats_connections = match_or_fail(lines[2], /^(\d+) (\d+) (\d+)$/)
  data[:connections_accepts] = stats_connections[1].to_i
  data[:connections_handled] = stats_connections[2].to_i
  data[:connections_dropped] = data[:connections_accepts] - data[:connections_handled]
  data[:connections_requests] = stats_connections[3].to_i

  stats_active_total = match_or_fail(lines[0], /^Active connections: (\d+)$/)
  data[:active_total] = stats_active_total[1].to_i

  stats_active = match_or_fail(lines[3], /^Reading: (\d+) Writing: (\d+) Waiting: (\d+)$/)
  data[:active_reading] = stats_active[1].to_i
  data[:active_writing] = stats_active[2].to_i
  data[:active_waiting] = stats_active[3].to_i

  data
end