Class: Bipbip::Plugin::Mysql

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

Instance Method Summary collapse

Methods inherited from Bipbip::Plugin

#initialize, #interrupt, #interrupted?, #metric_identifier, #metrics_names, #name, #run

Methods included from InterruptibleSleep

#interrupt_sleep, #interruptible_sleep

Constructor Details

This class inherits a constructor from Bipbip::Plugin

Instance Method Details

#metrics_schemaObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bipbip/plugin/mysql.rb', line 7

def metrics_schema
  [
      {:name => 'max_connections', :type => 'ce_gauge', :unit => 'Connections'},
      {:name => 'Max_used_connections', :type => 'ce_gauge', :unit => 'Connections'},
      {:name => 'Connections', :type => 'ce_counter', :unit => 'Connections'},
      {:name => 'Threads_connected', :type => 'ce_gauge', :unit => 'Threads'},

      {:name => 'Seconds_Behind_Master', :type => 'ce_gauge', :unit => 'Seconds'},

      {:name => 'Created_tmp_disk_tables', :type => 'ce_counter', :unit => 'Tables'},

      {:name => 'Queries', :type => 'ce_counter', :unit => 'Queries'},
      {:name => 'Slow_queries', :type => 'ce_counter', :unit => 'Queries'},

      {:name => 'Bytes_received', :type => 'ce_counter', :unit => 'b'},
      {:name => 'Bytes_sent', :type => 'ce_counter', :unit => 'b'},

      {:name => 'Table_locks_immediate', :type => 'ce_counter', :unit => 'Locks'},
      {:name => 'Table_locks_waited', :type => 'ce_counter', :unit => 'Locks'},

      {:name => 'Processlist', :type => 'ce_gauge', :unit => 'Processes'},
      {:name => 'Processlist_Locked', :type => 'ce_gauge', :unit => 'Processes'},
      {:name => 'Processlist_Sending_data', :type => 'ce_gauge', :unit => 'Processes'},

      {:name => 'Com_select', :type => 'ce_counter', :unit => 'Commands'},
      {:name => 'Com_delete', :type => 'ce_counter', :unit => 'Commands'},
      {:name => 'Com_insert', :type => 'ce_counter', :unit => 'Commands'},
      {:name => 'Com_update', :type => 'ce_counter', :unit => 'Commands'},
      {:name => 'Com_replace', :type => 'ce_counter', :unit => 'Commands'},
  ]
end

#monitor(server) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bipbip/plugin/mysql.rb', line 39

def monitor(server)
  mysql = Mysql2::Client.new(
      :host => server['hostname'],
      :port => server['port'],
      :username => server['username'],
      :password => server['password']
  )

  stats = Hash.new(0)

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

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

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

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

  data = {}
  metrics_names.each do |key|
    data[key] = stats[key]
  end
  data
end