如何将 Nagios 数据导出为 CSV?

How to export Nagios data into CSV?

我是新手,正在为我的项目做这件事。我能够成功安装和监控nagios。但我需要将这些数据导出到 csv 中。任何人都可以帮助我吗?

非常感谢xx

您可以在 nagios.cfg 配置文件中设置 host_perfdata_fileservice_perfdata_file 指令,以 host_perfdata_file_template 指定的格式将性能数据输出到指定的文件路径和 service_perfdata_file_template 指令。

Writing Performance Data To Files

You can have Nagios write all host and service performance data directly to text files using the host_perfdata_file and service_perfdata_file options. The format in which host and service performance data is written to those files is determined by the host_perfdata_file_template and service_perfdata_file_template options.

An example file format template for service performance data might look like this:

service_perfdata_file_template=[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$

By default, the text files will be opened in "append" mode. If you need to change the modes to "write" or "non-blocking read/write" (useful when writing to pipes), you can use the host_perfdata_file_mode and service_perfdata_file_mode options.

Additionally, you can have Nagios periodically execute commands to periocially process the performance data files (e.g. rotate them) using the host_perfdata_file_processing_command and service_perfdata_file_processing_command options. The interval at which these commands are executed are governed by the host_perfdata_file_processing_interval and service_perfdata_file_processing_interval options, respectively.

来源:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/perfdata.html

Performance Data Processing Option

Format:   process_performance_data=<0/1>
Example:  process_performance_data=1
This value determines whether or not Nagios will process host and service check performance data.

0 = Don't process performance data (default)
1 = Process performance data

Host Performance Data Processing Command

Format:   host_perfdata_command=<command>
Example:  host_perfdata_command=process-host-perfdata

This option allows you to specify a command to be run after every host check to process host performance data that may be returned from the check. The command argument is the short name of a command definition that you define in your object configuration file. This command is only executed if the process_performance_data option is enabled globally and if the process_perf_data directive in the host definition is enabled.

Service Performance Data Processing Command

Format:   service_perfdata_command=<command>
Example:  service_perfdata_command=process-service-perfdata

This option allows you to specify a command to be run after every service check to process service performance data that may be returned from the check. The command argument is the short name of a command definition that you define in your object configuration file. This command is only executed if the process_performance_data option is enabled globally and if the process_perf_data directive in the service definition is enabled.

Host Performance Data File

Format:   host_perfdata_file=<file_name>
Example:  host_perfdata_file=/usr/local/nagios/var/host-perfdata.dat

This option allows you to specify a file to which host performance data will be written after every host check. Data will be written to the performance file as specified by the host_perfdata_file_template option. Performance data is only written to this file if the process_performance_data option is enabled globally and if the process_perf_data directive in the host definition is enabled.

Service Performance Data File

Format:   service_perfdata_file=<file_name>
Example:  service_perfdata_file=/usr/local/nagios/var/service-perfdata.dat

This option allows you to specify a file to which service performance data will be written after every service check. Data will be written to the performance file as specified by the service_perfdata_file_template option. Performance data is only written to this file if the process_performance_data option is enabled globally and if the process_perf_data directive in the service definition is enabled.

Host Performance Data File Template

Format:   host_perfdata_file_template=<template>
Example:  host_perfdata_file_template=[HOSTPERFDATA]\t$TIMET$\t$HOSTNAME$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$

This option determines what (and how) data is written to the host performance data file. The template may contain macros, special characters (\t for tab, \r for carriage return, \n for newline) and plain text. A newline is automatically added after each write to the performance data file.

Service Performance Data File Template

Format:   service_perfdata_file_template=<template>
Example:  service_perfdata_file_template=[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$

This option determines what (and how) data is written to the service performance data file. The template may contain macros, special characters (\t for tab, \r for carriage return, \n for newline) and plain text. A newline is automatically added after each write to the performance data file.

来源:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/configmain.html#process_performance_data

注意: 如果您已按照 "Bulk Mode" 中的说明设置 pnp4nagios,您可能已经完成了此操作。在这种情况下,您只需要参考您在 host_perfdata_fileservice_perfdata_file 中指定的路径。但如果没有,这里是你如何为 pnp4nagios 做的:

Processing of performance data has to be enabled in nagios.cfg

 process_performance_data=1

Additionally some new directives are required

#
# service performance data
#
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file

#
# host performance data starting with Nagios 3.0
# 
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file

来源:https://docs.pnp4nagios.org/pnp-0.6/config#bulk_mode

编辑:这是按需生成 CSV 数据的更简单方法。

  • 浏览到 http:///nagios/cgi-bin/avail.cgi
  • 填写向导的步骤。
  • 请务必选中第 3 个屏幕上的 "Output in CSV Format" 复选框。
  • 单击 "Create Availability Report!" 按钮。
  • CSV 文件将生成并下载到您的浏览器中。