Class: Bipbip::Plugin::Nginx

Inherits:
Bipbip::Plugin show all
Defined in:
lib/bipbip/plugin/nginx.rb

Instance Attribute Summary

Attributes inherited from Bipbip::Plugin

#config, #frequency, #metric_group, #name, #tags

Instance Method Summary collapse

Methods inherited from Bipbip::Plugin

factory, factory_from_plugin, #initialize, #metrics_names, #run, #source_identifier

Methods included from InterruptibleSleep

#interrupt_sleep, #interruptible_sleep

Constructor Details

This class inherits a constructor from Bipbip::Plugin

Instance Method Details

#match_or_fail(string, regexp) ⇒ Object

Parameters:

  • string (String)
  • regexp (Regexp)


46
47
48
49
50
51
52
# 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_schemaObject



3
4
5
6
7
8
9
10
11
12
13
14
# 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

#monitorObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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