<commit_before>module Bipbip

  class Plugin::FastcgiPhpOpcache < Plugin

    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 => 'opcache_hit_rate', :type => 'gauge', :unit => '%'},
      ]
    end

    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)

      stats_memory = stats['memory_usage']
      stats_statistics = stats['opcache_statistics']
      {
          :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,
          :opcache_hit_rate => stats_statistics['opcache_hit_rate'].to_i,
      }
    end
  end
end<commit_msg>Extended metrics with "missees", "hits", "oom_restart"<commit_after>module Bipbip

  class Plugin::FastcgiPhpOpcache < Plugin

    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 => 'opcache_hit_rate', :type => 'gauge', :unit => '%'},
      ]
    end

    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)

      stats_memory = stats['memory_usage']
      stats_statistics = stats['opcache_statistics']
      {
          :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,
          :opcache_hit_rate => stats_statistics['opcache_hit_rate'].to_i,
          :misses => stats_statistics['misses'].to_i,
          :hits => stats_statistics['hits'].to_i,
          :oom_restarts => stats_statistics['oom_restarts'].to_i,
      }
    end
  end
end
{'fraction_non_alphanumeric': 0.16177924217462933, 'fraction_numerical': 0.0019769357495881385, 'mean_word_length': 3.048, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4541537', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1015, 'n_words': 172}