Module: Hostmon::Helpers

Includes:
Models
Defined in:
lib/hostmon/helpers.rb

Instance Method Summary collapse

Instance Method Details

#aggregate_bugzilla(check) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hostmon/helpers.rb', line 38

def aggregate_bugzilla(check)
  return "" unless settings.config.bugzilla_config
  url = settings.config.bugzilla_config[:bugzilla_url]
  h_url = settings.config.bugzilla_config[:hostlint_url]
  failing = check.hosts_failing.map(&:first).size
  failing += check.hosts_check_failing.map(&:first).size
  succeeding = check.hosts_ok.map(&:first).size
  fields = { :short_desc => "[hostlint] hosts failing #{check}",
    :comment => "#{failing.size}/#{failing + succeeding} hosts failing at report time (#{Time.now}).
  See #{h_url}/check/#{check}",
    :keyword => ""
  }
  url_parts = []
  fields.each do |k, v|
    [v].flatten.each do |v2|
      url_parts << "#{URI.escape(k.to_s)}=#{URI.escape(v2.to_s)}"
    end
  end
  url += "&amp;" + url_parts.join("&amp;")
  "<a href=\"#{url}\" target=\"_blank\" style=\"font-size:small;\">[file an aggregate bug]</a>"
end

#append_query_string(str) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/hostmon/helpers.rb', line 9

def append_query_string(str)
  v = str.dup
  # CGI::escapeHTML()?
  unless request.query_string.empty?
    v << "?#{request.query_string.gsub("&", "&amp;")}"
  end
  return v
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hostmon/helpers.rb', line 60

def bugzilla_link(report)
  return "" unless settings.config.bugzilla_config

  # info should have host, cluster, report_time
  url = settings.config.bugzilla_config[:bugzilla_url]
  h_url = settings.config.bugzilla_config[:hostlint_url]
  desc = "[hostlint ##{report.hostlint_id}] " +
    "#{report.host}.#{report.cluster}: #{report.name}"
  fields = { :short_desc => desc,
    :comment => "hostlint link: #{h_url}/report/#{report.name}##{report.hostlint_id}
#{report.body}",
    :keyword => ""
  }
  url_parts = []
  fields.each do |k, v|
    [v].flatten.each do |v2|
      url_parts << "#{URI.escape(k.to_s)}=#{URI.escape(v2.to_s)}"
    end
  end
  url += "&amp;" + url_parts.join("&amp;")
  "<a href=\"#{url}\" target=\"_blank\" style=\"font-size:small;\">file a bug</a>"
end


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hostmon/helpers.rb', line 83

def bugzilla_search_link(check)
  return "" unless settings.config.bugzilla_config

  url = settings.config.bugzilla_config[:bugzilla_search_url]
  desc = "[hostlint ##{check.hostlint_id}] " +
    "#{check.host}.#{check.cluster}: #{check.name}"
  fields = { :short_desc => desc }
  url_parts = []
  fields.each do |k, v|
    [v].flatten.each do |v2|
      url_parts << "#{URI.escape(k.to_s)}=#{URI.escape(v2.to_s)}"
    end
  end
  url += "&amp;" + url_parts.join("&amp;")
  "<a href=\"#{url}\" target=\"_blank\" style=\"font-size:small;\">search</a>"
end

#check_status_to_hosts_mapObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hostmon/helpers.rb', line 163

def check_status_to_hosts_map
  bucket = {}
  @hosts.each do |h|
    type = @status == Host::OK ? :checks_succeeding : :checks_failing
    h.send(type).each do |c|
      bucket[c.name] ||= []
      bucket[c.name] << h
    end
  end
  bucket
end


107
108
109
# File 'lib/hostmon/helpers.rb', line 107

def checklink(check)
  "<a href=\"/check/#{check}\">#{check}</a>"
end

fixme failing vs failed



5
6
7
# File 'lib/hostmon/helpers.rb', line 5

def cluster_link(cluster)
  "/cluster/#{cluster}"
end

#color(status) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/hostmon/helpers.rb', line 18

def color(status)
  map = {:CHECK_OK => "green",
    :CHECK_FAILED => "red"}
  "[" + status.map do |s|
    "<span style=\"color:#{map[s]||'orange'}\">#{s}</span>"
  end.join(', ') + "]"
end

#css_urlObject



31
32
33
34
35
36
# File 'lib/hostmon/helpers.rb', line 31

def css_url
  style = File.join(settings.root, "public/style.css")
  mtime = File.mtime(style).to_i.to_s
  return \
  %Q[<link href="/style.css?#{mtime}" rel="stylesheet" type="text/css">]
end

#host_helper(host) ⇒ Object

fixme de-duplication



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/hostmon/helpers.rb', line 145

def host_helper(host)
  ok = host.succeeding.size
  failed = host.failing.size
  check_failed = host.checks_failing.size
  total = ok + failed + check_failed
  if failed + check_failed != 0

    ol = append_query_string("/host/#{host.cluster}/#{host}/#{Host::OK}")
    fl = append_query_string("/host/#{host.cluster}/#{host}/#{Host::FAIL}")
    cfl = append_query_string("/host/#{host.cluster}/#{host}/#{Host::EXEC_FAIL}")

    "<span><a style=\"color:green\" href=\"#{ol}\">#{ok}</a></span>" +
      "/<span><a style=\"color:red\" href=\"#{fl}\">#{failed}</a></span>" +
      "/<span><a style=\"color:orange\" href=\"#{cfl}\">#{check_failed}</a></span>" +
      " <span>(#{total})</span>"
  end
end

when single cluster view, don’t display cluster in hostname



101
102
103
104
105
# File 'lib/hostmon/helpers.rb', line 101

def hostlink(host, single=false)
  str = "<a href=\"/host/#{host.cluster}/#{host}\">#{host}"
  str << ".#{host.cluster}" unless single
  str << "</a>"
end

#hosts_selector(hosts) ⇒ Object



111
112
113
114
# File 'lib/hostmon/helpers.rb', line 111

def hosts_selector(hosts)
  @hosts = hosts
  erb :'partials/hosts_selector', :layout => false
end

#rstatus(report) ⇒ Object



26
27
28
29
# File 'lib/hostmon/helpers.rb', line 26

def rstatus(report)
  j = report.ok? ? "ok" : "error"
  "<#{j}>#{color(report.status)}</#{j}> #{bugzilla_link(report) if j=="error" } #{bugzilla_search_link(report) if j=="error" }"
end

#stats_helper(check) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hostmon/helpers.rb', line 120

def stats_helper(check)
  total = ok = failed = check_failed = 0
  Check.find(check).host_map.each do |h, r|
    r = r.last
    total += 1
    if r.ok?
      ok += 1
    elsif r.failed?
      failed += 1
    else
      check_failed += 1
    end
  end

  ol = append_query_string("/check/#{check}/#{Host::OK}")
  fl = append_query_string("/check/#{check}/#{Host::FAIL}")
  cfl = append_query_string("/check/#{check}/#{Host::EXEC_FAIL}")

  "<span><a style=\"color:green\" href=\"#{ol}\">#{ok}</a></span>" +
    "/<span><a style=\"color:red\" href=\"#{fl}\">#{failed}</a></span>" +
    "/<span><a style=\"color:orange\" href=\"#{cfl}\">#{check_failed}</a></span>" +
    " <span>(#{total})</span>"
end

#status_selectorObject



116
117
118
# File 'lib/hostmon/helpers.rb', line 116

def status_selector()
  erb :'partials/status_selector', :layout => false
end