Module: ZabbixRubyClient::Plugins::Mysqlcommand

Extended by:
Mysqlcommand
Included in:
Mysqlcommand
Defined in:
lib/zabbix-ruby-client/plugins/mysqlcommand.rb

Instance Method Summary collapse

Instance Method Details

#collect(*args) ⇒ Object



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
38
39
40
# File 'lib/zabbix-ruby-client/plugins/mysqlcommand.rb', line 8

def collect(*args)
  host = args.shift
  app = args.shift
  mysqldb = args.shift
  mysqlargs = args.shift
  mysqlcommand = "mysql #{mysqlargs} -s --skip-column-names -e \"%s\" #{mysqldb}"
  back = []
  Hash[*args].each do |name, command|
    time = Time.now.to_i
    comm = sprintf(mysqlcommand, command.gsub(/"/,'\"'))
    res = `#{comm}`
    if $?.to_i == 0
      if name[0] == "_"
        res.each_line do |line|
          label, value = line,split("\t")
          back << "#{host} app.#{app}[#{name[1..-1]},#{label}] #{time} #{value}"
        end
      elsif name[/,/]
        res = res.split("\t")
        name.split(',').each_with_index do |n, i|
          back << "#{host} app.#{app}[#{n}] #{time} #{res[i]}"
        end
      else
        back << "#{host} app.#{app}[#{name}] #{time} #{res}"
      end
    else
      Log.warn "The connection failed."
      return []
    end

  end
  return back
end