Class: GRI::List
- Includes:
- FormatHelper, Utils
- Defined in:
- lib/gri/list.rb
Constant Summary collapse
- SORT_PROC =
{ 'a'=>proc {|aa, bb| a, = aa; b, = bb (a =~ /\A[\.\d]+$/ and b =~ /\A[\.\d]+$/) ? ipstr2bin(a) <=> ipstr2bin(b) : a <=> b }, 'l'=>proc {|a, b| (a[1]['sysLocation'] || "\377") <=> (b[1]['sysLocation'] || "\377") }, 'u'=>Proc.new {|a, b| (a[1]['sysUpTime'].to_i) <=> (b[1]['sysUpTime'].to_i) }, 'v'=>Proc.new {|a, b| (a[1]['_ver'] || "\377") <=> (b[1]['_ver'] || "\377") } }
Constants included from FormatHelper
FormatHelper::HTML_ESCAPE, FormatHelper::HTML_ESCAPE_ONCE_REGEXP
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #format_rowstr(basename, sysinfo) ⇒ Object
- #grep_graph(dirs, hosts, params) ⇒ Object
-
#initialize(options = {}) ⇒ List
constructor
A new instance of List.
- #load_sysdb(dirs) ⇒ Object
- #mk_regexp(str) ⇒ Object
- #sysdb_list(dirs, sysdb, params) ⇒ Object
- #template ⇒ Object
Methods included from Utils
get_prop, key_encode, load_records, parse_host_key, parse_key, search_records, update_ltsv_file, url_encode
Methods included from FormatHelper
#check_box, #escape_once, #h, #hidden, #mk_query, #mk_tag, #popup_menu, #radio_button, #render, #td, #text_field, #to_scalestr, #u, #url_to
Constructor Details
#initialize(options = {}) ⇒ List
Returns a new instance of List.
30 31 32 33 |
# File 'lib/gri/list.rb', line 30 def initialize ={} @options = @urs = !!([:use_regexp_search]) end |
Class Method Details
.ipstr2bin(str) ⇒ Object
26 27 28 |
# File 'lib/gri/list.rb', line 26 def self.ipstr2bin str str.split('.').map {|s|s.to_i}.pack('C4') end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/gri/list.rb', line 35 def call env req = Rack::Request.new env params = req.params dirs = @options[:dirs] sysdb = load_sysdb(dirs) rescue {} if params['op'] == 'comp' shash = {} grep_results = grep_graph dirs, sysdb.keys, params for h0 in grep_results.keys.sort for data_name, host, key in grep_results[h0] (shash[data_name] ||= []).push "#{host}_#{key}" end end if shash.keys.size == 0 url = url_to '' else key, = shash.keys rstr = shash[key].map {|hkey| "r=#{hkey}"}.join('&') query = "?p=#{params['p']}&s=#{key}&#{rstr}" url = url_to query end return [302, {'Location'=>url}, []] end ly_ary = [['0', 'none'], ['1', 'day'], ['7', 'week'], ['31', 'month'], ['366', 'year']] (ly_ary.assoc(params['ly']) || ly_ary[0])[2] = true sort_ary = [['a', 'hostname'], #['n','sysName'], ['l','location'], ['u','uptime'], ['v','version']] (sort_ary.assoc(params['sort']) || sort_ary[0])[2] = true @title = 'list' body = render(Grapher.layout) {render template, binding} [200, {'Content-type' => 'text/html'}, [body]] end |
#format_rowstr(basename, sysinfo) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/gri/list.rb', line 128 def format_rowstr basename, sysinfo mtime = sysinfo['_mtime'].to_i now = Time.now.to_i if mtime < (now - 24*3600) hostname = "(#{basename})" else hostname = basename end list_format = @options[:list_format] || "%-28_H%-18M %-18V %L" rowstr = list_format.gsub(/(%([-+]?([ \d]+)?(\.\d+)?))(_)?([A-Z%])/) { form = $1 + 's' linkflag = $5 case $6 when 'H' s = form % hostname when 'L' s = form % sysinfo['sysLocation'] when 'M' s = form % sysinfo['_firm'] when 'N' s = form % sysinfo['sysName'] when 'S' mtime = sysinfo['_mtime'].to_i if mtime > (now - 3600) s = Time.at(mtime).strftime '%H:%M' else s = '?' end s = form % s when 'U' if mtime > (now - 24*3600) ustr = ''#format_uptime sysinfo['sysUpTime'] else ustr = '' end s = form % ustr when 'V' s = form % sysinfo['_ver'] else s = '' end if linkflag == '_' ss = s.strip s = "<a href=\"#{h url_to(basename)}\">#{h ss}</a>" + ' ' * (s.size - ss.size) end s } rowstr end |
#grep_graph(dirs, hosts, params) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gri/list.rb', line 106 def grep_graph dirs, hosts, params re_name = mk_regexp params['n'] re_descr = mk_regexp params['d'] re_host = mk_regexp params['h'] hostres = {} for host in hosts next if params['h'].present? and host !~ re_host dir, h = search_records dirs, host if h for key in h.keys.sort prop = get_prop h[key] next unless re_name === prop[:name] next unless re_descr === (descr = prop[:description] || '') data_name, index = parse_key key (hostres[host] ||= []).push [data_name, host, key, prop[:name], prop[:description]] end end end hostres end |
#load_sysdb(dirs) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/gri/list.rb', line 73 def load_sysdb dirs sysdb = {} for dir in dirs values = LTSV.load_from_file(dir + '/.sysdb/sysdb.txt') values.inject(sysdb) {|h, v| v['_host'] and (h[v['_host']] = v); h} end sysdb end |
#mk_regexp(str) ⇒ Object
101 102 103 104 |
# File 'lib/gri/list.rb', line 101 def mk_regexp str str = @urs ? str.to_s.gsub(/[#]/) {|c| "\\#{c}"} : Regexp.quote(str.to_s) Regexp.new str, Regexp::IGNORECASE end |
#sysdb_list(dirs, sysdb, params) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gri/list.rb', line 82 def sysdb_list dirs, sysdb, params hosts = sysdb.keys if params['n'].present? or params['d'].present? grep_results = grep_graph dirs, hosts, params end grep_results ||= {} sort_proc = SORT_PROC[params['sort']] || SORT_PROC['a'] hlines = [] for host, sysinfo in sysdb.sort(&sort_proc) next if params['h'].present? and host !~ mk_regexp(params['h']) next if sysinfo['sysDescr'] and params['sysdescr'] and sysinfo['sysDescr'] !~ mk_regexp(params['sysdescr']) line = format_rowstr host, sysinfo hlines.push [host, line, grep_results[host]] end hlines end |
#template ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/gri/list.rb', line 180 def template <<'EOS' <% if sysdb -%> <% if params['search'] == '1' -%> <form enctype="application/x-www-form-urlencoded" method="get" action="<%=h url_to '' %>"> <table> <% for hname, vname in [['Hostname', 'h'], #['sysName', 'sysname'], ['sysDescr', 'sysdescr'], ['I/F or Name', 'n'], ['Description', 'd'],] -%> <tr> <td class=text-right><%= hname %> :</td> <td><%= text_field vname, params[vname], 40 %></td> </tr> <% end -%> <tr> <td class=text-right>sort by:</td> <td><%= popup_menu 'sort', 'form-control', *sort_ary %></td> </tr> <tr> <td class=text-right>graph:</td> <td><%= popup_menu 'ly', 'form-control', *ly_ary %></td> </tr> </table> <input type="hidden" name="start" value="<%=h params['start'] %>"> <input type="hidden" name="search" value="1"> <input class="btn btn-primary btn-sm" type="submit" value="submit"> </form> <% if params['n'].present? or params['d'].present? -%> <% ary = [['', 'sum'], ['s', 'stack'], ['v', 'overlay']] -%> <% while item = ary.shift -%> <% q = mk_query :op=>'comp', :p=>item[0], :h=>params['h'], :n=>params['n'], :d=>params['d'], :sysdescr=>params['sysdescr'] -%> <a href="<%=h url_to q %>"><%= item[1] %></a> <% if ary.size > 0 then %> | <% end -%> <% end -%> <% end -%> <% else -%> <a href="<%= url_to '?search=1'%>">Search</a> <% end -%> <pre> <% for host, line, gres in sysdb_list dirs, sysdb, params -%> <span class="line"><%= line %></span> <% if gres -%> <% for data_name, h2, key, name, description in gres -%> <% url = "?r=#{h2}_#{key}" + (params['grp'] ? "&grp=#{params['grp']}" : "") -%> - <a href="<%=h url_to(url) %>"><%=h name %></a> <%= description %> <% unless (ly = params['ly'].to_i).zero? -%> <img src="<%=h url%>&z=s&stime=-<%= ly * 24 * 3600 %>"/><br/> <% end -%> <% end -%> <% end -%> <% end -%> </pre> <% end -%> EOS end |