Class: SlashPort::Component::Mysql

Inherits:
SlashPort::Component show all
Defined in:
app/models/components/mysql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SlashPort::Component

#_want, attribute, attributes, #attributes, class_initialize, components, config, #configs, configs, disable, enable, #get_attributes, get_attributes, #get_configs, get_configs, #get_things, get_things, inherited, is_enabled?, label, #path

Constructor Details

#initializeMysql

Returns a new instance of Mysql.



26
27
28
29
30
31
32
33
34
# File 'app/models/components/mysql.rb', line 26

def initialize
  super
  begin
    @db = Sequel.connect("mysql://slashport@localhost")
  rescue Sequel::AdapterNotFound => e
    puts "Disabling #{self.class.label} component: missing mysql Sequel adapter: #{e.inspect}"
    self.class.disable
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'app/models/components/mysql.rb', line 6

def host
  @host
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'app/models/components/mysql.rb', line 8

def password
  @password
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'app/models/components/mysql.rb', line 7

def user
  @user
end

Instance Method Details

#ConfigGetVariablesObject

end MysqlStats



97
98
99
100
101
102
103
104
# File 'app/models/components/mysql.rb', line 97

def ConfigGetVariables
  result = @db["show variables"]
  data = Hash.new
  result.map do |row|
    data[row[:Variable_name]] = row[:Value]
  end
  return data
end

#MasterStatusObject

end MysqlOK



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/components/mysql.rb', line 48

def MasterStatus
  data = []
  tuple = SlashPort::Tuple.new
  begin
    result = @db["show master status"].map[0]
    result.each do |key, val|
      tuple.data[key.to_s.downcase] = val
    end
    data << tuple
  rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError
    return nil
  end
  return data
end

#MysqlOKObject



36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/components/mysql.rb', line 36

def MysqlOK
  tuple = SlashPort::Tuple.new
  begin
    # dummy query just to test our connection
    @db["show variables like 'x'"].map
    tuple.data["healthy"] = 1
  rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError
    tuple.data["healthy"] = 0
  end
  return tuple
end

#MysqlStatsObject

end MasterStatus



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/components/mysql.rb', line 82

def MysqlStats
  tuple = SlashPort::Tuple.new
  begin
    result = @db["show global status"]
    result.map do |row|
      value = row[:Value]
      # use actual value if Float fails.
      tuple.data[row[:Variable_name].downcase] = (Float(value) rescue value)
    end
  rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError
    return nil
  end
  return tuple
end

#SlaveStatusObject

end MasterStatus



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/components/mysql.rb', line 63

def SlaveStatus
  tuple = SlashPort::Tuple.new
  begin
    result = @db["show slave status"].map[0]
    ret = Hash.new
    if result == nil
      # this host is not a slave
      tuple.data["is_slave"] = false
    else
      result.each do |key, val|
        tuple.data[key.to_s.downcase] = val
      end
    end
  rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError
    return nil
  end
  return tuple
end