2014-09-19 38 views
0

大家好我目前正試圖讓我的儀表板上的表顯示一個應該看起來像這樣的數組。紅寶石陣列轉換爲陣列語法

電子郵件 - 含LotusServer和POP3服務器陣列

每個包含類似host_nameservice_descriptionsuptime duration ...

我需要一個JSON輸出發回表,不同的價值觀那些其努力自己顯示POP3服務器和LotusServer,但是,想法是顯示主機組。

我想推入那些數組到最新的一個新的數組,並將它發送回表,但我似乎沒有得到正確的語法。我對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 Server,它們都具有不同的狀態Up/Down,正常運行時間,host_name等。因此,當其中一臺主機出現問題時,如果所有狀態均正常,則應在電子郵件旁邊的列表中顯示一個十字,這應該是一個檢查。

問題是我如何訪問最新的[usv] ['host_name']中的東西,例如,我計劃顯示組的列表並檢查Up/Down狀態和/或其他分別針對每個組的問題。

預先感謝您 費邊

+0

我很抱歉,但我不明白你在問什麼。你到底想要發生什麼? – Surya 2014-09-19 06:54:56

+0

輸出應該是組名,在這種情況下,應該包含值的數組「USV」或「磁盤空間」,以便我可以檢查主機的Up/Down狀態。問題是我怎樣才能訪問最新的[usv] ['host_name']中的東西,例如 我打算顯示組的列表並檢查Up/Down狀態和/或其他問題中的任何錯誤對於每個組分別 – 2014-09-19 06:59:03

+0

您可以發佈有問題的預期輸出以便更好地理解? – Surya 2014-09-19 07:01:00

回答

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); 
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 
+0

歡迎來到本站! [請僅使用Answers來回答問題](// meta.stackoverflow.com/q/92107)。您應該[編輯]原始問題以添加其他信息。 – Mogsdad 2016-04-20 22:07:44