Ruby Array into Array语法

时间:2014-09-19 06:40:17

标签: ruby-on-rails ruby arrays dashing

大家好,我目前正试图让仪表板上的表格显示一个看起来像这样的数组。

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

其中每个包含不同的值,例如host_nameservice_descriptionsuptime duration ......

我需要将json输出发送回表,它会自行显示POP3-Server和LotusServer,但其目的是显示主机组。

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

这里有一些代码可以更好地解释我遇到的问题:

# 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等。因此,当其中一个主机出现问题时,它应该在电子邮件旁边的列表中显示一个十字,如果所有状态都正常则应该进行检查。

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

提前谢谢你 费边

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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