解析Nagios插件的Json输出

解析Nagios插件的Json输出,json,parsing,plugins,nagios,Json,Parsing,Plugins,Nagios,我搜索了这个网站,尽可能多地用谷歌搜索,但似乎找不到有效的解决方案 我让Nagios core在家里运行一个监控项目。status.dat使用我安装的插件在web服务器上转换为Json文件 此插件的输出如下所示: { "programStatus": { "modified_host_attributes": "0", "modified_service_attributes": "0", "nagios_pid": "983", "daemon_mode

我搜索了这个网站,尽可能多地用谷歌搜索,但似乎找不到有效的解决方案

我让Nagios core在家里运行一个监控项目。status.dat使用我安装的插件在web服务器上转换为Json文件

此插件的输出如下所示:

  {
  "programStatus": {
    "modified_host_attributes": "0",
    "modified_service_attributes": "0",
    "nagios_pid": "983",
    "daemon_mode": "1",
    "program_start": "1414556165",
    "last_log_rotation": "0",
    "enable_notifications": "1",
    "active_service_checks_enabled": "1",
    "passive_service_checks_enabled": "1",
    "active_host_checks_enabled": "1",
    "passive_host_checks_enabled": "1",
    "enable_event_handlers": "1",
    "obsess_over_services": "0",
    "obsess_over_hosts": "0",
    "check_service_freshness": "1",
    "check_host_freshness": "0",
    "enable_flap_detection": "1",
    "process_performance_data": "0",
    "global_host_event_handler": "",
    "global_service_event_handler": "",
    "next_comment_id": "1",
    "next_downtime_id": "1",
    "next_event_id": "77",
    "next_problem_id": "23",
    "next_notification_id": "304",
    "active_scheduled_host_check_stats": "1,5,5",
    "active_ondemand_host_check_stats": "0,0,0",
    "passive_host_check_stats": "0,0,0",
    "active_scheduled_service_check_stats": "3,11,11",
    "active_ondemand_service_check_stats": "0,0,0",
    "passive_service_check_stats": "0,0,0",
    "cached_host_check_stats": "0,0,0",
    "cached_service_check_stats": "0,0,0",
    "external_command_stats": "0,0,0",
    "parallel_host_check_stats": "1,5,5",
    "serial_host_check_stats": "0,0,0"
  },
  "hosts": {
   "localhost": {
      "host_name": "localhost",
      "modified_attributes": "0",
      "check_command": "check-host-alive",
      "check_period": "24x7",
      "notification_period": "workhours",
      "check_interval": "5.000000",
      "retry_interval": "1.000000",
      "event_handler": "",
      "has_been_checked": "1",
      "should_be_scheduled": "1",
      "check_execution_time": "4.007",
      "check_latency": "1.279",
      "check_type": "0",
      "current_state": "0",
      "last_hard_state": "0",
      "last_event_id": "0",
      "current_event_id": "0",
      "current_problem_id": "0",
      "last_problem_id": "0",
      "plugin_output": "PING OK - Packet loss = 0%, RTA = 0.08 ms",
      "long_plugin_output": "",
      "performance_data": "rta=0.076000ms;3000.000000;5000.000000;0.000000 pl=0%;80;100;0",
      "last_check": "1414556166",
      "next_check": "1414556470",
      "check_options": "0",
      "current_attempt": "1",
      "max_attempts": "10",
      "state_type": "1",
      "last_state_change": "1411951605",
      "last_hard_state_change": "1411951605",
      "last_time_up": "1414556170",
      "last_time_down": "0",
      "last_time_unreachable": "0",
      "last_notification": "0",
      "next_notification": "0",
      "no_more_notifications": "0",
      "current_notification_number": "0",
      "current_notification_id": "0",
      "notifications_enabled": "1",
      "problem_has_been_acknowledged": "0",
      "acknowledgement_type": "0",
      "active_checks_enabled": "1",
      "passive_checks_enabled": "1",
      "event_handler_enabled": "1",
      "flap_detection_enabled": "1",
      "process_performance_data": "1",
      "obsess": "1",
      "last_update": "1414556456",
      "is_flapping": "0",
      "percent_state_change": "0.00",
      "scheduled_downtime_depth": "0"
   },
   "test-vm": {
      "host_name": "test-vm",
      "modified_attributes": "0",
      "check_command": "check-host-alive",
      "check_period": "24x7",
      "notification_period": "workhours",
      "check_interval": "5.000000",
      "retry_interval": "1.000000",
      "event_handler": "",
      "has_been_checked": "1",
      "should_be_scheduled": "1",
      "check_execution_time": "3.001",
      "check_latency": "0.000",
      "check_type": "0",
      "current_state": "1",
      "last_hard_state": "1",
      "last_event_id": "70",
      "current_event_id": "72",
      "current_problem_id": "19",
      "last_problem_id": "10",
      "plugin_output": "CRITICAL - Host Unreachable (192.168.56.4)",
      "long_plugin_output": "",
      "performance_data": "",
      "last_check": "1414556437",
      "next_check": "1414556740",
      "check_options": "0",
      "current_attempt": "1",
      "max_attempts": "10",
      "state_type": "1",
      "last_state_change": "1413873683",
      "last_hard_state_change": "1413873683",
      "last_time_up": "1413873142",
      "last_time_down": "1414556440",
      "last_time_unreachable": "0",
      "last_notification": "1414556268",
      "next_notification": "1414563468",
      "no_more_notifications": "0",
      "current_notification_number": "2",
      "current_notification_id": "301",
      "notifications_enabled": "1",
      "problem_has_been_acknowledged": "0",
      "acknowledgement_type": "0",
      "active_checks_enabled": "1",
      "passive_checks_enabled": "1",
      "event_handler_enabled": "1",
      "flap_detection_enabled": "1",
      "process_performance_data": "1",
      "obsess": "1",
      "last_update": "1414556456",
      "is_flapping": "0",
      "percent_state_change": "0.00",
      "scheduled_downtime_depth": "0"
   },
   "winserver": {
      "host_name": "winserver",
      "modified_attributes": "0",
      "check_command": "check-host-alive",
      "check_period": "24x7",
      "notification_period": "24x7",
      "check_interval": "5.000000",
      "retry_interval": "1.000000",
      "event_handler": "",
      "has_been_checked": "1",
      "should_be_scheduled": "1",
      "check_execution_time": "4.004",
      "check_latency": "0.000",
      "check_type": "0",
      "current_state": "0",
      "last_hard_state": "0",
      "last_event_id": "75",
      "current_event_id": "76",
      "current_problem_id": "0",
      "last_problem_id": "20",
      "plugin_output": "PING OK - Packet loss = 0%, RTA = 0.44 ms",
      "long_plugin_output": "",
      "performance_data": "rta=0.438000ms;3000.000000;5000.000000;0.000000 pl=0%;80;100;0",
      "last_check": "1414556380",
      "next_check": "1414556684",
      "check_options": "0",
      "current_attempt": "1",
      "max_attempts": "10",
      "state_type": "1",
      "last_state_change": "1414556303",
      "last_hard_state_change": "1414556303",
      "last_time_up": "1414556384",
      "last_time_down": "1414556303",
      "last_time_unreachable": "0",
      "last_notification": "1414556303",
      "next_notification": "1414558103",
      "no_more_notifications": "0",
      "current_notification_number": "0",
      "current_notification_id": "302",
      "notifications_enabled": "1",
      "problem_has_been_acknowledged": "0",
      "acknowledgement_type": "0",
      "active_checks_enabled": "1",
      "passive_checks_enabled": "1",
      "event_handler_enabled": "1",
      "flap_detection_enabled": "1",
      "process_performance_data": "1",
      "obsess": "1",
      "last_update": "1414556456",
      "is_flapping": "0",
      "percent_state_change": "5.99",
      "scheduled_downtime_depth": "0"
   }
  },
并永远与主机信息

现在,我使用rainmeter web解析器和一个名为rainregex的正则表达式助手很容易地解析了这个问题

我想做的是在网页上使用这些信息

我尝试过使用jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>

<script>
$.getJSON("http://<serverIP>/nagios/statusJson.php", function( data){ 
 var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});

 $( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
</script>

        </script>

$.getJSON(“http:///nagios/statusJson.php“,函数(数据){
var项目=[];
$。每个(数据、函数(键、值){
items.push(“
  • ”+val+“
  • ”); }); $(“
      ”{ “类”:“我的新列表”, html:items.join(“”) }).附于(“主体”); });
    但是我无法让它从php文件中提取信息


    我不确定我做错了什么。

    嗨,我自己设法解决了这个问题

    我对JQuery不够熟练,所以我决定使用Python脚本解析Json数据并将其更新为SQL表,从这里我可以轻松地使用PHP查询SQL数据库中的数据


    无论如何,谢谢大家。当我完成脚本润色后,我会很快编辑此评论。

    如果您仍然对解决方案感到好奇,这可能会帮助您:

  • 如果从php文件中提取,则必须确保回显创建的json编码数组:
  • echo json_编码($postData)

  • 您还可以跳过使用php的解析,直接从Nagios RESTful API获取信息(我假设您也安装了API插件)。从那里,您可以使用jquery脚本解析和输出json

  • 您提到的PHP文件在哪里?很抱歉,我忘记更改代码$.getJSON(“http:///nagios/statusJson.php,函数(数据){理想情况下我想做:$.getJSON(“http://:password@/nagios/statusJson.php”),函数(数据){但我不确定它是否支持@Raptor