NSCA 被动模式插件

  • 版本 2.9.x

  • 需要安装 nagios

  • 调用流程

    • 远程主机 send_nsca 插件 -> nagios 主机上的 NSCA daemon 服务
  • 组成结构

    • send_nsca : 客户端插件, 安装在远程主机上, 用法发送指定格式的数据到 NSCA daemon 服务上
    • NSCA daemon : 运行在 Nagios 主机上的守护服务

一、部署 NSCA daemon 服务

  1. 1. 创建账号
  2. # 创建 nagios 账号, 并且分配 nagios 到 nagios 组中
  3. useradd nagios
  4. usermod -a -G nagios nagios
  5. 2. 下载 NSCA(Nagios 主机)
  6. # 下载
  7. cd /usr/local/src
  8. sudo wget https://nchc.dl.sourceforge.net/project/nagios/nsca-2.x/nsca-2.9.2/nsca-2.9.2.tar.gz
  9. sudo tar -zxvf nsca-2.9.2.tar.gz
  10. # 编译
  11. cd nsca-2.9.2
  12. ./configure
  13. make all
  14. # 出现如下信息
  15. NSCA port: 5667
  16. NSCA user: nagios
  17. NSCA group: nagios
  18. # 编译检查正确执行以后, 会出现如下信息
  19. src/ 目录下生成两个程序 nsca(服务进程) send_nsca(客户端)
  20. sample-config/ 中会有 nsca.cfg send_nsca.cfg 的配置文件
  21. 3. 部署 NSCA 和配置文件
  22. # 部署 NSCA (send_nsca 和 nsca)
  23. cp src/send_nsca src/nsca /usr/local/nagios/bin/
  24. chown nagios:nagios /usr/local/nagios/bin/nsca /usr/local/nagios/bin/send_nsca
  25. # 部署配置文件
  26. mkdir -p /usr/local/nagios/etc/nsca
  27. cp sample-config/* /usr/local/nagios/etc/nsca/
  28. chown nagios:nagios /usr/local/nagios/etc/nsca/*
  29. 4. NSCA daemon 配置
  30. # 修改配置文件: etc/nsca/nsca.cfg
  31. vim etc/nsca/nsca.cfg
  32. # 端口
  33. server_port=5667
  34. # 监听地址
  35. server_address=0.0.0.0
  36. # 开启 debug 模式, 用于调试。部署的时候调整到 debug=0
  37. debug=1
  38. # 命令文件, 这是守护进程的 Nagios 命令文件的位置
  39. # 注意文件路径
  40. command_file=/usr/local/nagios/var/rw/nagios.cmd
  41. # 备用转储文件, 这是用来指定守护进程应该的另一个文件
  42. # 注意文件路径
  43. alternate_dump_file=/usr/local/nagios/var/rw/nsca.dump
  44. # 聚合写选项
  45. aggregate_writes=1
  46. # password: 通讯密码, 与 send_nsca 客户端定义相同的密码, send_nsca 的配置文件在 etc/nsca/send_nsca.cfg
  47. password=xxxx.com
  48. 5. NSCA daemon 启动服务
  49. # 语法 bin/nsca --help
  50. Usage: bin/nsca -c <config_file> [mode]
  51. Options:
  52. <config_file> = Name of config file to use
  53. [mode] = Determines how NSCA should run. Valid modes:
  54. --inetd = Run as a service under inetd or xinetd
  55. --daemon = Run as a standalone multi-process daemon
  56. --single = Run as a standalone single-process daemon (default)
  57. # 启动服务
  58. /usr/local/nagios/bin/nsca -c /usr/local/nagios/etc/nsca/nsca.cfg --single
  59. # 监控日志
  60. tail -f /var/log/message
  61. # 查看进程是否存在
  62. ps -axu | grep nsca
  63. # 查看端口是否存在
  64. netstat -tunlp | grep 5667

二、 Nagios 与 NSCA daemon 服务打通

  1. 1. Nagios services 配置 nsca 脚本
  2. # 修改配置文件
  3. vim etc/objects/services.cfg
  4. # 定义 service, 使用 check_dummy 插件, 接收 send_nsca 客户端发送过来的数据
  5. define service{
  6. # 使用的模板
  7. use service_nsca_template
  8. # 定义用于接收 send_nsca 信息的主机名
  9. host_name serverTest
  10. # 定义用于接收 send_nsca 信息的主机描述
  11. service_description nscaTest
  12. # 定义 <脚本名称> check_dummy, 用于处理 send_nsca 发送来的数据
  13. # check_dummy: 这个脚本, 就是 commands.cfg 配置中定义的<脚本名称>
  14. check_command check_dummy!0
  15. # 是否发送通知
  16. notifications_enabled 1
  17. contact_groups MonitorGroup
  18. }
  19. 2. Nagios commands 配置 nsca <脚本名称>
  20. # 修改配置文件
  21. vim etc/objects/commands.cfg
  22. # 定义 commands, 定义 <脚本名称> check_dummy, 处理 send_nsca 客户端发送过来的数据
  23. # check_dummy: 插件是一个简单的翻译插件, 只能处理 4 个参数状态分别是 0,1,3,663, 对应了 (OK、WARNING、CRITICAL、UNKNOWN), 例如: check_dummy 0 对应就是 OK
  24. define command{
  25. command_name check_dummy
  26. command_line $USER1$/check_dummy $ARG1$
  27. }
  28. 3. 重启 nagios 服务
  29. service nagios restart

二、send_nsca 远程主机部署

  1. # 语法 bin/send_nsca --help
  2. Usage: bin/send_nsca -H <host_address> [-p port] [-to to_sec] [-d delim] [-c config_file]
  3. Options:
  4. <host_address> = The IP address of the host running the NSCA daemon
  5. [port] = The port on which the daemon is running - default is 5667
  6. [to_sec] = Number of seconds before connection attempt times out.
  7. (default timeout is 10 seconds)
  8. [delim] = Delimiter to use when parsing input (defaults to a tab)
  9. [config_file] = Name of config file to use
  10. Service Checks:
  11. <host_name>[tab]<svc_description>[tab]<return_code>[tab]<plugin_output>[newline]
  12. Host Checks:
  13. <host_name>[tab]<return_code>[tab]<plugin_output>[newline]
  14. # 案例: 发送数据到 NSCA daemon 中, 注意其中的 serverTest,nscaTest 必须与 services.cfg 中定义的 service 相同, 才可接受到数据
  15. echo -e "serverTest,nscaTest,0,test" | bin/send_nsca -H dw0 -p 5667 -c /usr/local/nagios/etc/nsca/send_nsca.cfg -d ","