Module: ZabbixRubyClient::Plugins::Postgres

Extended by:
Postgres
Included in:
Postgres
Defined in:
lib/zabbix-ruby-client/plugins/postgres.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
41
42
43
44
45
46
47
48
49
50
# File 'lib/zabbix-ruby-client/plugins/postgres.rb', line 8

def collect(*args)
  host = args[0]
  psqlargs = args[1]
  dbname = args[2]
  # be sure to have this in the config file
  # args: [ "-U username -h localhost", "dbname" ]
  # and setup username in a ~/.pgpass (0600) file for the zrc user
  # localhost:5432:dbname:username:password
  psqlstatus = `psql #{psqlargs} -A -t -c "select * from pg_stat_database where datname='#{dbname}'" #{dbname}`
  if $?.to_i == 0
    status = get_status(psqlstatus)
  else
    Log.warn "The connection failed."
    return []
  end
  psqlactivity = `psql #{psqlargs} -A -t -c "select state, count(*) from pg_stat_activity group by state" #{dbname}`
  if $?.to_i == 0
    activity = get_activity(psqlactivity)
  else
    Log.warn "The connection failed."
    return []
  end
  psqlwriter = `psql #{psqlargs} -A -t -c "select * from pg_stat_bgwriter" #{dbname}`
  if $?.to_i == 0
    writer = get_writer(psqlwriter)
  else
    Log.warn "The connection failed."
    return []
  end

  time = Time.now.to_i
  back = []
  status.each do |e,v|
    back << "#{host} postgres.#{e}[#{dbname}] #{time} #{v}"
  end
  activity.each do |e,v|
    back << "#{host} postgres.connections.#{e} #{time} #{v}"
  end
  writer.each do |e,v|
    back << "#{host} postgres.#{e} #{time} #{v}"
  end
  return back
end

#discover(*args) ⇒ Object



52
53
54
55
# File 'lib/zabbix-ruby-client/plugins/postgres.rb', line 52

def discover(*args)
  dbname = args[1]
  [ "postgres.db.discovery", "{\"{#DBNAME}\": \"#{dbname}\"}" ]
end

#get_activity(status) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/zabbix-ruby-client/plugins/postgres.rb', line 79

def get_activity(status)
  ar = { "active" => "0", "idle" => "0", "idle_in_transaction" => "0"}
  status.each_line.reduce([]) do |a,l|
    els = l.split("|").map(&:strip)
    ar[els[0].gsub(" ","_")] = els[1]
  end
  ar
end

#get_status(status) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zabbix-ruby-client/plugins/postgres.rb', line 57

def get_status(status)
  ret = {}
  els = status.split "|"
  ret["numbackends"] = els[2]
  ret["xact_commit"] = els[3]
  ret["xact_rollback"] = els[4]
  ret["blks_read"] = els[5]
  ret["blks_hit"] = els[6]
  ret["tup_returned"] = els[7]
  ret["tup_fetched"] = els[8]
  ret["tup_inserted"] = els[9]
  ret["tup_updated"] = els[10]
  ret["tup_deleted"] = els[11]
  ret["conflicts"] = els[12]
  ret["temp_files"] = els[13]
  ret["temp_bytes"] = els[14]
  ret["deadlocks"] = els[15]
  ret["blk_read_time"] = els[16]
  ret["blk_write_time"] = els[17]
  ret
end

#get_writer(status) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zabbix-ruby-client/plugins/postgres.rb', line 88

def get_writer(status)
  ret = {}
  els = status.split "|"
  ret["checkpoints_timed"] = els[1]
  ret["checkpoints_req"] = els[1]
  ret["checkpoint_write_time"] = els[2]
  ret["checkpoint_sync_time"] = els[3]
  ret["buffers_checkpoint"] = els[4]
  ret["buffers_clean"] = els[5]
  ret["maxwritten_clean"] = els[6]
  ret["buffers_backend"] = els[7]
  ret["buffers_backend_fsync"] = els[8]
  ret["buffers_alloc"] = els[9]
  ret
end