简体   繁体   English

使用 PHP 控制 Nagios

[英]Working with PHP to control Nagios

To monitor an host and some services with Nagios, we have to edit the text file with something like:要使用 Nagios 监控主机和某些服务,我们必须使用以下内容编辑文本文件:

define host{
use <template>
host_name <name>
alias <alias>
address <IP> }

define service{
use <template>
host_name <name>
service_description <desc>
check_command <command> }

I have an interface where I can do something like that with PHP language.我有一个界面,我可以用 PHP 语言做类似的事情。 I have 2 functions addServiceNagios($hostname, $service, $command) and addHostNagios($hostname, $hostIP) where the information is placed in the file.我有 2 个函数 addServiceNagios($hostname, $service, $command) 和 addHostNagios($hostname, $hostIP) ,其中信息放置在文件中。 Now I have a big problem:现在我有一个大问题:

The information is always placed no matter what.. If the host already exists, or if the services are already assigned to hosts.无论如何都会放置信息。如果主机已经存在,或者服务已经分配给主机。 What's the best approach to check if the host is already defined?检查主机是否已定义的最佳方法是什么? And if the services are associated?如果服务相关联? And if I want to erase the services that are associated to the host?如果我想删除与主机关联的服务? Thanks in advance.提前致谢。

I would build a parser that parses the syntax from the file so is able to display the same data in a more accessible data-structure like an array.我将构建一个解析器来解析文件中的语法,以便能够以更易于访问的数据结构(如数组)显示相同的数据。

Depending on how large the files are, the array could store additional information like offsets etc.. However it's easier next to the parser to write a dumper that can dump this array structure back to file.根据文件的大小,数组可以存储额外的信息,如偏移量等。但是,在解析器旁边编写一个可以将该数组结构转储回文件的转储程序会更容易。 So each time the whole file is rewritten but with the actual data.因此,每次重写整个文件时都会使用实际数据。

You can then perform add and remove actions on the array structure easily while taking care that reading and writing the file works.然后,您可以轻松地对数组结构执行添加和删除操作,同时注意读取和写入文件的工作。

So you've seperated the problems from each other.所以你已经把问题彼此分开了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM