Class: Bipbip::Plugin::FastcgiPhpOpcache

Inherits:
Bipbip::Plugin show all
Defined in:
lib/bipbip/plugin/fastcgi_php_opcache.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

#metrics_schemaObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/bipbip/plugin/fastcgi_php_opcache.rb', line 3

def metrics_schema
  [
    { name: 'free_memory', type: 'gauge', unit: 'b' },
    { name: 'current_wasted_percentage', type: 'gauge', unit: '%' },
    { name: 'num_cached_keys', type: 'gauge', unit: 'Keys' },
    { name: 'hit_rate', type: 'gauge', unit: '%' },
    { name: 'misses', type: 'counter', unit: 'Misses' },
    { name: 'hits', type: 'counter', unit: 'Hits' },
    { name: 'oom_restarts', type: 'counter', unit: 'Restarts' }
  ]
end

#monitorObject



15
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
43
44
45
46
47
# File 'lib/bipbip/plugin/fastcgi_php_opcache.rb', line 15

def monitor
  authority = config['host'].to_s + ':' + config['port'].to_s
  path = File.join(Bipbip::Helper.data_path, 'php-opcache-status.php')

  env_backup = ENV.to_hash
  ENV['REQUEST_METHOD'] = 'GET'
  ENV['SCRIPT_NAME'] = File.basename(path)
  ENV['SCRIPT_FILENAME'] = path
  response = `cgi-fcgi -bind -connect #{authority.shellescape} 2>&1`
  ENV.replace(env_backup)

  body = response.split(/\r?\n\r?\n/)[1]
  raise "FastCGI response has no body: #{response}" unless body
  stats = JSON.parse(body)
  raise "FastCGI response has no stats (opcache disabled?): #{stats.inspect}" unless stats

  @data_previous ||= stats

  stats_memory = stats['memory_usage']
  stats_statistics = stats['opcache_statistics']
  hit_rate = hit_rate(stats)

  @data_previous = stats
  {
    free_memory: stats_memory['free_memory'].to_i,
    current_wasted_percentage: stats_memory['current_wasted_percentage'].to_i,
    num_cached_keys: stats_statistics['num_cached_keys'].to_i,
    hit_rate: hit_rate,
    misses: stats_statistics['misses'].to_i,
    hits: stats_statistics['hits'].to_i,
    oom_restarts: stats_statistics['oom_restarts'].to_i
  }
end