0

大家好,我目前正试图让我的仪表板上的表格显示一个应该如下所示的数组。

电子邮件 - 包含 LotusServer 和 POP3-Server 的数组

每一个都包含不同的值,如 a host_name, service_descriptions, uptime duration...

我需要将一个 json 输出发送回表,它可以自己显示 POP3-Server 和 LotusServer,但想法是显示主机组。

我正在尝试将这些数组推送到一个名为 latest 的新数组中并将其发送回表中,但我似乎没有正确理解语法。我对 ruby​​ 很陌生,也许有人可以给我一个提示或帮助我解决这个问题?

这里有一些代码可以更好地解释我卡在哪里:

# get the url to download the status.cgi which contains the values

def request_status(url, user, pass, type)
  case type
  when "host"
    url_part = "style=hostdetail"
  when "service"
    url_part = "host=all&hoststatustypes=3"
  else
    throw "status type '" + type + "' is not supported!"
  end

  uri = URI.parse(url + "?" + url_part + "&nostatusheader&jsonoutput&sorttype=1&sortoption=6")

  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  if (user and pass)
    request.basic_auth(user, pass)
  end
  response = http.request(request)
  return JSON.parse(response.body)["status"][type+"_status"]
end


  # when service_description in status("usv") push the values into the usv Array
  # after that push the usv Array into the latest Array <-- Problem 
  case status["service_description"]
  when "usv"
      usv.push({ cols: [
        { value: status['host_name'].to_json},
        { value: status['status'].to_json, class: 'icinga-status icinga-status-'+status['status'].downcase },
      ]})
      usv.push({ cols: [
        { value: status['service_description'].to_json, class: 'icinga-servicename' },
        { value: status['duration'].gsub(/^0d\s+(0h\s+)?/, ''), class: 'icinga-duration' }
      ]})
      latest.push({ cols:[
        { value: usv.to_json},
      ]})
  when "Disk Space"
      disk.push({ cols: [
        { value: status['host_name']},
        { value: status['status'], class: 'icinga-status icinga-status-'+status['status'].downcase },
      ]})
      disk.push({ cols: [
        { value: status['service_description'], class: 'icinga-servicename' },
        { value: status['duration'].gsub(/^0d\s+(0h\s+)?/, ''), class: 'icinga-duration' }
      ]})
  end

这是我得到的输出:

[{"cols":[{"value":"\"usv\""},{"value":"\OK"","class":"icinga-status icinga-status-ok"}]},{"cols":[{"value":"\"usv\"","class":"icinga-servicename"},{"value":"9h 47m 3s","class":"icinga-duration"}]}]

我有一个表格小部件。显示例如“电子邮件”然后检查或交叉以查看它是向下还是向上。然后下一个条目网络相同。这些条目中的每一个都有不同的主机,例如电子邮件 POP3 服务器和 Lotus 服务器,它们都有不同的状态 Up/down、uptime、host_name 等。因此,当其中一个主机出现问题时,如果所有状态都正常,它应该在电子邮件旁边的列表中显示一个叉号,它应该是一个检查。

问题是如何访问 latest[usv]['host_name'] 中的内容,例如我计划显示组列表并检查每个组的 Up/down 状态和/或其他问题中的任何错误分别。

预先感谢您

4

2 回答 2

0

我尽可能地减少了代码,最后弄清楚了状态代表什么......

require "net/https"
require "uri"


SCHEDULER.every '15s', :first_in => 0 do |job|

icinga_user = settings.icinga_user
icinga_pass = settings.icinga_pass

#host

uri = URI.parse("http://localhost/cgi-bin/icinga/status.cgi?style=hostdetail&nostatusheader&jsonoutput&sorttype=1&sortoption=6")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth(icinga_user, icinga_pass)
response = http.request(request)
    status = JSON.parse(response.body)["status"]["host_status"]

    rows = []

    status.each { |status|

    case status['host_name']
    when "usv"
    rows.push({ cols: [
      { value: status['host_name']}
    ]})
    end

}

send_event('icinga-hosts-latest', {
  rows: rows
  })
end
于 2014-09-19T10:42:05.057 回答
0

也许您可以避免case,因为您在其中重复相同的代码。

groups = []

['usv', 'Disk Space'].each do |group|
  groups << { status['service_description'] => {
                host_name: status['host_name'],
                status: status['status'],
                status_class: "icinga-status icinga-status-#{status['status'].downcase}",
                service_description: status['service_description'],
                service_description_class: 'icinga-servicename',
                duration: status['duration'].gsub(/^0d\s+(0h\s+)?/, ''),
                duration_class: 'icinga-duration'
              }
            }
end

return groups.to_json

当您查看 JSON 时,使用 jQuery 可以显示:

var response = $.parseJSON(response.responseText);
var response_html = "<thead> 
                      <tr>
                        <th> Host Name </th>
                        <th> Status </th>
                        <th> Service description </th>
                        <th> Duration </th>
                      </tr>
                    </thead>";
response_html +=  "<tbody>";

for (var i = 0; i < response.length; i++ ) {
  response_html +=  "<tr>";
  // Run upto 3. Since, there are 4 columns.
  for (var j = 0; j < 4; j++){
    response_html += "<td>" + response[i].host_name + "</td>";
    response_html += "<td class='" + response[i].status_class + "'>" + response[i].status + "</td>";
    response_html += "<td class='" + response[i].service_description_class + "'>" + response[i].service_description + "</td>";
    response_html += "<td class='" + response[i].duration_class + "'>" + response[i].duration + "</td>";
  }
  response_html += "</tr>";
}

response_html +=  "</tbody>";
$('table#services').html(response_html);
于 2014-09-19T09:40:00.960 回答