0

こんにちは、私は現在、ダッシュボードのテーブルに次のような配列を表示させようとしています。

Email - LotusServer と POP3-Server を含む配列

host_nameservice_descriptionsuptime duration...のような異なる値を含むそれぞれ

json 出力をテーブルに送り返す必要があります。これは、POP3 サーバーと 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"}]}]

テーブル ウィジェットを取得しました。たとえば「E-Mail」を表示してから、チェックまたはバツを表示して、それがダウンかアップかを確認します。次に、次のエントリ Network はそれと同じです。これらの各エントリには、たとえば E-Mail POP3 サーバーと Lotus サーバーのように、すべて異なる状態の Up/down、uptime、host_name などの異なるホストがあります。そのため、これらのホストの 1 つに問題がある場合、すべての状態が OK であれば、電子メールの横のリストに十字が表示されます。これはチェックされているはずです。

問題は、最新の[usv] ['host_name']にあるものにどのようにアクセスできるかです。たとえば、グループのリストを表示し、各グループのアップ/ダウン状態のエラーやその他の問題をチェックする予定ですそれぞれ。

事前にありがとうファビアン

4

2 に答える 2

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 に答える
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 に答える