Module: Cawcaw::Command::Postgresql::Table

Defined in:
lib/cawcaw/command/postgresql/table.rb

Class Method Summary collapse

Class Method Details

.run(argv, input_stream = $stdin, output_stream = $stdout) ⇒ Object



5
6
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
38
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cawcaw/command/postgresql/table.rb', line 5

def self.run(argv, input_stream=$stdin, output_stream=$stdout)
  
  params = Cawcaw.parse_opts(argv)
  
  params[:graph_title] ||= "PostgreSQL Records"
  params[:graph_args] ||= "--base 1000"
  params[:graph_vlabel] ||= "records"
  params[:graph_category] ||= "postgresql"
  params[:graph_info] ||= "PostgreSQL record size"
  
  bytes_flag = (params[:graph_vlabel] == "bytes")
  
  params[:adapter] = "postgresql"
  params[:host] ||= "localhost"
  params[:port] ||= 5432
  
  params[:label_warning] ||=   50000000
  params[:label_critical] ||= 100000000
  
  if params[:stdin_flag]
    table_paths = []
    while line = input_stream.gets
      line.chomp!
      table_paths.push line
    end
  else
    table_paths = argv.shift
    table_paths = table_paths.split(/,/) unless table_paths.nil?
    table_paths ||= []
    table_paths.delete("")
  end
  command = argv.shift
  
  if table_paths == []
    Cawcaw.logger.error("PostgreSQL table name is not set")
    return 1
  end
  
  case command
  when "autoconf"
    output_stream.puts "yes"
  when "config"
    output_stream.puts "graph_title \#{params[:graph_title]}\ngraph_args \#{params[:graph_args]}\ngraph_vlabel \#{params[:graph_vlabel]}\ngraph_category \#{params[:graph_category]}\ngraph_info \#{params[:graph_info]}\n    EOF\n    \n    label_draw = \"AREA\"\n    \n    table_paths.each do |table_path|\n      label = table_path.gsub(/[^a-zA-Z0-9]/, \"_\").downcase\n      output_stream.puts <<-EOF\n\#{label}.label \#{table_path}\n\#{label}.info \#{table_path} size\n\#{label}.draw \#{label_draw}\n\#{label}.warning \#{params[:label_warning]}\n\#{label}.critical \#{params[:label_critical]}\n    EOF\n      label_draw = \"STACK\"\n    end\n  else\n    \n    ActiveRecord::Base.establish_connection(params)\n\n    full_table_paths = {}\n    table_paths.each do |table_path|\n      table_path_pair = table_path.split(/\\./)\n      if /[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)?/ !~ table_path\n        Cawcaw.logger.error(\"PostgreSQL table name '\#{table_path}' is invalid\")\n        return 1\n      end\n      table_name = table_path_pair.pop\n      schema_name = table_path_pair.pop || \"public\"\n      full_table_paths[table_path] = \"\#{schema_name}.\#{table_name}\"\n    end\n    \n    if params[:stats_flag]\n      count_sql = <<-EOF\nselect nspname || '.' || relname as full_table_path, reltuples, relpages\nfrom pg_class\ninner join pg_namespace\non pg_class.relnamespace = pg_namespace.oid\nwhere relkind='r'\n  and (\n    \#{\n      table_paths.map{|table_path|\n\"(nspname || '.' || relname) = \#{ActiveRecord::Base.sanitize(full_table_paths[table_path])}\"\n      }.join(\" or \")\n    }\n  )\n      EOF\n    else\n      count_sql = <<-EOF\n\#{\n  table_paths.map{|table_path|\n    \"(select \#{ActiveRecord::Base.sanitize(full_table_paths[table_path])} as full_table_path, count(*) as reltuples from \#{full_table_paths[table_path]})\"\n  }.join(\" union \")\n}\n      EOF\n    end\n\n    \n    results = ActiveRecord::Base.connection.execute count_sql\n    table_sizes = {}\n    results.each do |record|\n      table_path = record[\"full_table_path\"]\n      if bytes_flag\n        table_sizes[table_path] = record[\"relpages\"].to_i * 8\n      else\n        table_sizes[table_path] = record[\"reltuples\"].to_i\n      end\n    end\n    table_paths.each do |table_path|\n      label = table_path.gsub(/[^a-zA-Z0-9]/, \"_\").downcase\n      table_size = table_sizes[full_table_paths[table_path]]\n      output_stream.puts \"\#{label}.value \#{table_size}\"\n    end\n  end\n  \n  return 0\nend\n"