<commit_before>#!/usr/bin/env ruby
#
# MySQL Select Count Metric
#
# Creates a graphite-formatted metric for the first value of a result set from a MySQL query.
#
# Copyright 2017 <NAME> <<EMAIL>>
# Copyright 2018 <NAME> <<EMAIL>>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-plugin/metric/cli'
require 'mysql'
require 'inifile'

class MysqlQueryCountMetric < Sensu::Plugin::Metric::CLI::Graphite
  option :host,
         short: '-h HOST',
         long: '--host HOST',
         description: 'MySQL Host to connect to',
         required: true

  option :port,
         short: '-P PORT',
         long: '--port PORT',
         description: 'MySQL Port to connect to',
         proc: proc(&:to_i),
         default: 3306

  option :username,
         short: '-u USERNAME',
         long: '--user USERNAME',
         description: 'MySQL Username'

  option :password,
         short: '-p PASSWORD',
         long: '--pass PASSWORD',
         description: 'MySQL password'

  option :database,
         short: '-d DATABASE',
         long: '--database DATABASE',
         description: 'MySQL database',
         default: ''

  option :ini,
         short: '-i',
         long: '--ini VALUE',
         description: 'My.cnf ini file'

  option :ini_section,
         description: 'Section in my.cnf ini file',
         long: '--ini-section VALUE',
         default: 'client'

  option :socket,
         short: '-S SOCKET',
         long: '--socket SOCKET',
         description: 'MySQL Unix socket to connect to'

  option :name,
         short: '-n NAME',
         long: '--name NAME',
         description: 'Metric name for a configured handler',
         default: 'mysql.query_count'

  option :query,
         short: '-q SELECT_COUNT_QUERY',
         long: '--query SELECT_COUNT_QUERY',
         description: 'Query to execute',
         required: true

  def run
    if config[:ini]
      ini = IniFile.load(config[:ini])
      section = ini[config[:ini_section]]
      db_user = section['user']
      db_pass = section['password']
    else
      db_user = config[:username]
      db_pass = config[:password]
    end
    raise "invalid query : #{config[:query]}" unless config[:query] =~ /^select\s+count\(\s*\*\s*\)/i

    db = Mysql.real_connect(config[:host], db_user, db_pass, config[:database], config[:port], config[:socket])
    count = db.query(config[:query]).fetch_row[0].to_i

    output config[:name], count
    ok

  rescue Mysql::Error => e
    errstr = "Error code: #{e.errno} Error message: #{e.error}"
    critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')

  rescue StandardError => e
    critical "unhandled exception: #{e}"

  ensure
    db.close if db
  end
end<commit_msg>appease the cops<commit_after>#!/usr/bin/env ruby
#
# MySQL Select Count Metric
#
# Creates a graphite-formatted metric for the first value of a result set from a MySQL query.
#
# Copyright 2017 <NAME> <<EMAIL>>
# Copyright 2018 <NAME> <<EMAIL>>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-plugin/metric/cli'
require 'mysql'
require 'inifile'

class MysqlQueryCountMetric < Sensu::Plugin::Metric::CLI::Graphite
  option :host,
         short: '-h HOST',
         long: '--host HOST',
         description: 'MySQL Host to connect to',
         required: true

  option :port,
         short: '-P PORT',
         long: '--port PORT',
         description: 'MySQL Port to connect to',
         proc: proc(&:to_i),
         default: 3306

  option :username,
         short: '-u USERNAME',
         long: '--user USERNAME',
         description: 'MySQL Username'

  option :password,
         short: '-p PASSWORD',
         long: '--pass PASSWORD',
         description: 'MySQL password'

  option :database,
         short: '-d DATABASE',
         long: '--database DATABASE',
         description: 'MySQL database',
         default: ''

  option :ini,
         short: '-i',
         long: '--ini VALUE',
         description: 'My.cnf ini file'

  option :ini_section,
         description: 'Section in my.cnf ini file',
         long: '--ini-section VALUE',
         default: 'client'

  option :socket,
         short: '-S SOCKET',
         long: '--socket SOCKET',
         description: 'MySQL Unix socket to connect to'

  option :name,
         short: '-n NAME',
         long: '--name NAME',
         description: 'Metric name for a configured handler',
         default: 'mysql.query_count'

  option :query,
         short: '-q SELECT_COUNT_QUERY',
         long: '--query SELECT_COUNT_QUERY',
         description: 'Query to execute',
         required: true

  def run
    if config[:ini]
      ini = IniFile.load(config[:ini])
      section = ini[config[:ini_section]]
      db_user = section['user']
      db_pass = section['password']
    else
      db_user = config[:username]
      db_pass = config[:password]
    end
    raise "invalid query : #{config[:query]}" unless config[:query] =~ /^select\s+count\(\s*\*\s*\)/i

    db = Mysql.real_connect(config[:host], db_user, db_pass, config[:database], config[:port], config[:socket])
    count = db.query(config[:query]).fetch_row[0].to_i

    output config[:name], count
    ok
  rescue Mysql::Error => e
    errstr = "Error code: #{e.errno} Error message: #{e.error}"
    critical "#{errstr} SQLSTATE: #{e.sqlstate}" if e.respond_to?('sqlstate')
  rescue StandardError => e
    critical "unhandled exception: #{e}"
  ensure
    db.close if db
  end
end
{'fraction_non_alphanumeric': 0.13611811307166005, 'fraction_numerical': 0.004681310767014764, 'mean_word_length': 2.484943538268507, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '343394', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1691, 'n_words': 517}