从Nagios插件中解析Json输出

时间:2014-10-29 04:31:33

标签: json parsing plugins nagios

我已经搜索过这个网站并尽可能多地搜索,但似乎无法找到有效的解决方案。

我在家里运行Nagios核心进行监控项目。使用我安装的插件将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>

但是我无法从php文件中获取信息。

我不确定我在这里做错了什么。

2 个答案:

答案 0 :(得分:0)

您好我自己管理解决此问题。

我在JQuery中不够流利,所以我决定使用Python脚本来解析Json数据并将其更新为SQL表,从这里我可以轻松地使用PHP来查询SQL DB中的数据。

无论如何,谢谢大家。我很快就完成了对这个剧本的修改。

答案 1 :(得分:0)

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

  1. 如果您从php文件中提取,则必须确保回显您创建的json编码数组:
  2. echo json_encode($ postData);

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