【问题标题】:Parse Json output from Nagios plugin从 Nagios 插件解析 Json 输出
【发布时间】:2014-10-29 04:31:32
【问题描述】:

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

我在家中运行 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 文件中提取信息。

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

【问题讨论】:

  • 你提到的PHP文件在哪里?
  • 对不起,我忘了更改代码 $.getJSON("http:///nagios/statusJson.php", function(data){ 理想情况下我想做:$.getJSON ("http://:password@/nagios/statusJson.php", function(data){ 但我不确定它是否支持@Raptor

标签: json parsing plugins nagios


【解决方案1】:

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

  1. 如果您从 php 文件中提取,则必须确保回显您创建的 json 编码数组:

回显 json_encode($postData);

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

【讨论】:

    【解决方案2】:

    您好,我自己解决了这个问题。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多