class Bipbip::Plugin::Mysql

Public Instance Methods

metrics_schema() click to toggle source
# File lib/bipbip/plugin/mysql.rb, line 5
def metrics_schema
  [
    { name: 'Max_used_connections', type: 'gauge', unit: 'Connections' },
    { name: 'Connections', type: 'counter', unit: 'Connections' },
    { name: 'Threads_connected', type: 'gauge', unit: 'Threads' },

    { name: 'Slave_running', type: 'gauge', unit: 'Boolean' },
    { name: 'Seconds_Behind_Master', type: 'gauge', unit: 'Seconds' },

    { name: 'Created_tmp_disk_tables', type: 'counter', unit: 'Tables' },

    { name: 'Queries', type: 'counter', unit: 'Queries' },
    { name: 'Slow_queries', type: 'counter', unit: 'Queries' },

    { name: 'Table_locks_immediate', type: 'counter', unit: 'Locks' },
    { name: 'Table_locks_waited', type: 'counter', unit: 'Locks' },

    { name: 'Processlist', type: 'gauge', unit: 'Processes' },
    { name: 'Processlist_Locked', type: 'gauge', unit: 'Processes' },

    { name: 'Com_select', type: 'counter', unit: 'Commands' },
    { name: 'Com_delete', type: 'counter', unit: 'Commands' },
    { name: 'Com_insert', type: 'counter', unit: 'Commands' },
    { name: 'Com_update', type: 'counter', unit: 'Commands' },
    { name: 'Com_replace', type: 'counter', unit: 'Commands' }
  ]
end
monitor() click to toggle source
# File lib/bipbip/plugin/mysql.rb, line 33
def monitor
  mysql = Mysql2::Client.new(
    host: config['hostname'],
    port: config['port'],
    username: config['username'],
    password: config['password']
  )

  stats = Hash.new(0)

  mysql.query('SHOW GLOBAL STATUS').each do |v|
    stats[v['Variable_name']] = v['Value']
  end

  mysql.query('SHOW VARIABLES').each do |v|
    stats[v['Variable_name']] = v['Value']
  end

  slave_status = mysql.query('SHOW SLAVE STATUS').first
  if slave_status
    stats['Seconds_Behind_Master'] = slave_status['Seconds_Behind_Master']

    if slave_status.key?('Slave_IO_Running') && slave_status.key?('Slave_SQL_Running')
      stats['Slave_running'] = ('Yes' === slave_status['Slave_IO_Running'] && 'Yes' === slave_status['Slave_SQL_Running']) ? 'Yes' : 'No'
    end
  end

  processlist = mysql.query('SHOW PROCESSLIST')
  stats['Processlist'] = processlist.count
  processlist.each do |process|
    state = process['State'].to_s
    stats['Processlist_' + state.sub(' ', '_')] += 1 unless state.empty?
  end

  mysql.close

  data = {}
  metrics_schema.each do |metric|
    name = metric[:name]
    unit = metric[:unit]
    data[name] = if 'Boolean' == unit
                   ('ON' === stats[name] || 'Yes' === stats[name] ? 1 : 0)
                 else
                   stats[name].to_i
                 end
  end
  data
end