首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby Array into Array语法

Ruby Array into Array语法
EN

Stack Overflow用户
提问于 2014-09-19 14:40:17
回答 2查看 166关注 0票数 0

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

电子邮件-包含LotusServer和POP3的数组-服务器

其中每一个都包含不同的值,如host_nameservice_descriptionsuptime duration……

我需要将json输出发送回表格,它的工作是显示POP3Server和LotusServer,但是,其想法是要显示主机组。

我试图将这些数组推入名为latest的新数组中,并将其发送回表中,但我似乎没有得到正确的语法。我是ruby的新手,也许有人能给我一些提示或者帮我解决这个问题?

下面是一些代码,可以更好地解释我遇到的问题:

代码语言:javascript
复制
# 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

这是我得到的输出:

代码语言:javascript
复制
[{"cols":[{"value":"\"usv\""},{"value":"\OK"","class":"icinga-status icinga-status-ok"}]},{"cols":[{"value":"\"usv\"","class":"icinga-servicename"},{"value":"9h 47m 3s","class":"icinga-duration"}]}]

我得到了一个Table小部件。例如,显示"E-Mail“,然后显示一个复选标记或一个十字,以查看它是向下还是向上。那么下一个entry Network也是一样的。这些条目中的每个条目都有不同的主机,例如,对于电子邮件POP3服务器和Lotus Server,它们都具有不同的状态Up/down、正常运行时间、host_name等。因此,当其中一个主机出现问题时,它应该在电子邮件旁边的列表中显示一个十字,如果所有状态都正常,则应该进行检查。

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

先谢谢你,法比安

EN

回答 2

Stack Overflow用户

发布于 2014-09-19 17:40:01

也许你可以避免使用case,因为你在里面重复相同的代码。

代码语言:javascript
复制
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可以显示:

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2014-09-19 18:42:05

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25927830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档