Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 将Nagios配置拆分为多个文件_File_Configuration_Split_Nagios - Fatal编程技术网

File 将Nagios配置拆分为多个文件

File 将Nagios配置拆分为多个文件,file,configuration,split,nagios,File,Configuration,Split,Nagios,NagiosQL创建一个名为servicemplates.cfg的文件。 为了更方便地分发某些选定模板,我想将任何服务定义拆分为一个单独的文件。 示例servicetemplates.cfg define service { name imap_service use generic_service check_command

NagiosQL创建一个名为servicemplates.cfg的文件。 为了更方便地分发某些选定模板,我想将任何服务定义拆分为一个单独的文件。 示例servicetemplates.cfg

define service {
   name                                     imap_service
   use                                      generic_service
   check_command                            check_service_imap
   register                                 0
}

define service {
       name                                     ldapserver_ldap_service
       service_description                      LDAP
       use                                      generic_service
       check_command                            check_service_ldap
       icon_image                               ldapserver.png
       register                                 0

}
我喜欢的是某种解析器,它可以创建模板的“名称”之类的文件,例如。Gimap_service.cfg和ldapserver_ldap_service.cfg。
每个文件都必须包含整个定义(定义服务{…})

以下代码对我来说是一个解决方案,它可能并不完美,因为它需要Nagios对象配置的特定语法。 第一个参数必须是Nagios配置文件的文件名

<?php
if (empty($argv[1])) {
        echo "First parameter: File to Nagios object configuration\n";
        exit(1);
}

$starttag=0;
$outfile='';
$outtext='';
$inputfile=$argv[1];
$cfgfile = file($inputfile) or exit(2);

foreach ($cfgfile as $line_num => $line) {
        # $outtext will be reset if define ... { is found
        $outtext.=$line;
        # Start tag of a new section "define ... {"
        if (preg_match("/.*define.*{/i", $line)) {
                $starttag=1;
                $outtext=$line;
        }
        # Split the line with name. The parameter after name is the later filename
        if (preg_match("/name[\s]+[\w]+/", $line) && $starttag=1) {
                $keywords=preg_split("/[\s]+/", $line);
                $outfile=$keywords[2];
                $outfile.=".cfg";
                $outfile=str_replace(' ', '_', $outfile);
        }
        # End tag of a new section "}"
        if (preg_match("/.*}/", $line) && $starttag=1) {
                $starttag=0;
                echo "Writing {$outfile}\n";
                file_put_contents($outfile, $outtext);
        }
}

echo "Read lines {$line_num} from {$inputfile}\n";
?>