check_mk conf import

User / Community contributed content: modifications, patches, extensions, modules, scripts etc.
Locked
Chris oder so

check_mk conf import

Post by Chris oder so » Tue Sep 24, 2013 18:03

Hi @all
I'm working on a project.
I try to import check_mk configurationfiles to NConf.
The problem: After the imports I can't generate the configuration.
It only shows the nav and head of the nconfpage, but the mainpart is blanc.

waht I did:
I created a menu in NConf where I can enter a IP of a host,
wehn I do that, it executes a php-script which creates a check_mk configuration for this host (in main.mk).
after this, check_mk gets executed with the parameters -II and -U (the icinga configuration gets created).

then the script imports with the "bin/add_items_from_nagios.pl" every Conf-object one by one in nconf.

on the end it delete the icinga configuration (which check_mk has genarated).


The Host and Services and so are now all correctly in the NConf. But like I wrote, the nconf don't generate the configuration anymore.

I hope i describe it understandable for you.

Here's the Code's, sorry it is written chaotic :)

template_loader.php (loads the check_mk template to nconf):

Code: Select all

<?php

	echo "<b>Lade Konfiguration von Template.</b><br>\n";

	// Definiere Variablen

	$s="";	// <- so stehen lassen!

	$icingaconffile="/etc/icinga/objects/check_mk.d/check_mk_templates.cfg";

	$tmpdir="./conf";

	$conffile="$tmpdir/temp.cfg";

	$templateconf="/etc/icinga/objects/check_mk.d/check_mk_templates.cfg";

	$importplugin="../bin/add_items_from_nagios.pl";

	$icingaconffile=$templateconf;

	//$s=" -s"; //kommentiert für NomalModus, nicht kommentiert für simulationsmodus





	// Importiere die neuen ConfFiles

	$problem = array(

		0 => "define command {\n#  command_name	check-mk\n#  command_line /usr/bin/check_mk \$HOSTNAME$ \$HOSTADDRESS$ \n#}",

 		1 => "\\\n"

	);

	$highelight = array(

		0 => "[ERROR]",

 		1 => "Finished"

	);

	$class = array(

		5 => "host",

 		7 => "service",

		6 => "servicedependency", 

		4 => "hostgroup", 

		3 => "command", 

		1 => "contact",

		2 => "contactgroup", 

		0 => "timeperiod"

	);

	if(file_exists($icingaconffile)){

		//Move The Icinga Conf File in tmpdir:

		shell_exec("sudo chmod 777 $icingaconffile");

		shell_exec("sudo chmod 777 $tmpdir");

		//echo shell_exec("whoami");

		shell_exec("sudo chown wwwrun $icingaconffile");

		shell_exec("sudo chown wwwrun $tmpdir");

		rename("$icingaconffile", "$tmpdir/IcingaConf.cfg");

		//shell_exec("mv $icingaconffile $tmpdir/IcingaConf.cfg");



		//Read the Conf File to a Var (& remove problems):

		$IcingaConf = file_get_contents("$tmpdir/IcingaConf.cfg");

		$i=0;

		while($problem[$i]){

			$IcingaConf = str_replace($problem[$i],"",$IcingaConf);

			$i++;

		}



		// Start the Workflow

		$i=0;	

		while($class[$i]){

			if($define_explode = explode("define ".$class[$i]." {", $IcingaConf)){

				$j=1;

				while($define_explode[$j]){

					//Aufteile, zuerst Inhalt rechts von { wählen, danach Inhalt links von }

					//$FromStart = explode("{", $define_explode[$j]);

					//$TillEnd = explode("}", $FromStart[1]);

					$TillEnd = explode("}", $define_explode[$j]);



					// Configuration neu zusammensetzten

					if($class[$i] == "contact"){

						$ADD="email			  dummy@umb.ch\n";

					}else{

						$ADD="";

					}

					$ConfChapter="define ".$class[$i]." {".$TillEnd[0]."$ADD}\n\n";

					shell_exec("sudo rm $conffile");

					$file = fopen($conffile,"a+");

					fwrite($file,$ConfChapter);

					fclose($file);

					if(file_exists($conffile)){

						if($class[$i]=="command"){

							$ImportCommand="$importplugin -f $conffile -c "."checkcommand"." $s";

						}else{

							if($class[$i]=="host"){

								$ImportCommand="$importplugin -f $conffile -c "."host-template"." $s";

							}else{

								if($class[$i]=="service"){

									$ImportCommand="$importplugin -f $conffile -c "."service-template"." $s";

								}else{

									$ImportCommand="$importplugin -f $conffile -c ".$class[$i]." $s";

								}

							}

						}

						if($class[$i]=="servicedependency"){

							$ImportCommand="$importplugin -f $conffile -c "."service-dependency"." $s";

						}



						echo "<b>".$class[$i].":</b><br>\n";

						$output = str_replace("\n","<br>\n",shell_exec("sudo $ImportCommand"))."<br>\n";

						$o=0;

						while($highelight[$o]){

							$output = str_replace($highelight[$o],"<b>".$highelight[$o]."</b>",$output);

							$o++;

						}

						echo $output;

					}

					//count up

					$j++;

				}

			}

			$i++;

		}



		//Delete Icinga Conf

		shell_exec("rm $tmpdir/IcingaConf.cfg");	$i++;

	}

echo "<b>Ende</b><br>\n";

echo "<a href='../setHost.php'> > Gehe zur&uuml;ck zu NConf < </a><br>\n";

?>
loader.php (import the check_mk host configuration to nconf):

Code: Select all

<?php

if(isset($_GET['ip']) && $_GET['ip']!=""){

	// Die IP des Hosts

	$ip=$_GET['ip'];



	echo "<b>Lade Konfiguration von $ip.</b><br>\n";

	// Definiere Variablen
	$s="";	// <- so stehen lassen!

	$icingaconffile="/etc/icinga/objects/check_mk.d/check_mk_objects.cfg";

	$tmpdir="./conf";

	$conffile="$tmpdir/temp.cfg";

	$templateconf="/etc/icinga/objects/check_mk.d/check_mk_templates.cfg";

	$importplugin="../bin/add_items_from_nagios.pl";
	$main_mk_path="/etc/check_mk/main.mk";
	$cmk_path="/usr/bin/check_mk";
	//$s=" -s"; //kommentiert für NomalModus, nicht kommentiert für simulationsmodus



	// Editiere Check_mk Konfiguration

	$main_mk="all_hosts = ['".$ip."']";
	shell_exec("sudo rm $main_mk_path");
	shell_exec("sudo touch $main_mk_path");
	shell_exec("sudo chmod 777 $main_mk_path");
	$file = fopen($main_mk_path,"a+");

	fwrite($file,$main_mk);

	fclose($file);
	$file = fopen("./hosts.mk","a+");

	fwrite($file,",'$ip'");

	fclose($file);


	// Aktualisiere Check_mk
	echo "<b>Aktualisiere Check_mk:</b><br>\n<br>\n";

	echo shell_exec("sudo $cmk_path -II");


	// Erstelle Check_mk Konfigurations Files
	echo "<b>Erstelle Check_mk Konfigurations Files:</b><br>\n<br>\n";

	shell_exec("sudo $cmk_path -U");


	// Löschen des Templatefile

	shell_exec("sudo rm $templateconf");

	// Setzte check_mk zurück
	$ips = file_get_contents("./hosts.mk");
	$main_mk = "all_hosts = [".$ips."]";
	$file = fopen($main_mk_path,"a+");

	fwrite($file,$main_mk);

	fclose($file);


	// Splitte das Conf File

		//include('./icingaconf_split.php'); // Nun im Importteil integriert

	



	// Importiere die neuen ConfFiles

	$problem = array(

		0 => "define command {\n#  command_name	check-mk\n#  command_line /usr/bin/check_mk \$HOSTNAME$ \$HOSTADDRESS$ \n#}",

 		1 => "\\\n"

	);

	$highelight = array(

		0 => "[ERROR]",

 		1 => "Finished"

	);

	$class = array(

		5 => "host",

 		7 => "service",

		6 => "servicedependency", 

		4 => "hostgroup", 

		3 => "command", 

		1 => "contact",

		2 => "contactgroup", 

		0 => "timeperiod"

	);

	if(file_exists($icingaconffile)){
		echo "<b>Lade Konfiguration in NConf:</b><br>\n<br>\n";

		//Move The Icinga Conf File in tmpdir:

		shell_exec("sudo chmod 777 $icingaconffile");

		shell_exec("sudo chmod 777 $tmpdir");

		//echo shell_exec("whoami");

		shell_exec("sudo chown wwwrun $icingaconffile");

		shell_exec("sudo chown wwwrun $tmpdir");
		//shell_exec("cp $icingaconffile $tmpdir/DEBUG.cfg");

		rename("$icingaconffile", "$tmpdir/IcingaConf.cfg");

		//shell_exec("mv $icingaconffile $tmpdir/IcingaConf.cfg");



		//Read the Conf File to a Var (& remove problems):

		$IcingaConf = file_get_contents("$tmpdir/IcingaConf.cfg");

		$i=0;

		while($problem[$i]){

			$IcingaConf = str_replace($problem[$i],"",$IcingaConf);

			$i++;

		}



		// Start the Workflow

		$i=0;	

		while($class[$i]){

			if($define_explode = explode("define ".$class[$i]." {", $IcingaConf)){

				$j=1;

				while($define_explode[$j]){

					//Aufteile, zuerst Inhalt rechts von { wählen, danach Inhalt links von }

					//$FromStart = explode("{", $define_explode[$j]);

					//$TillEnd = explode("}", $FromStart[1]);

					$TillEnd = explode("}", $define_explode[$j]);



					// Configuration neu zusammensetzten

					if($class[$i] == "contact"){

						$ADD="email			  dummy@umb.ch\n";

					}else{

						if($class[$i] == "service"){			
							if(strpos($TillEnd[0], "service_description")=== false){

								$ADD="service_description			  EvtlPing_PH".time()."\n";
							}

						}else{

							$ADD="";

						}

					}

					$ConfChapter="define ".$class[$i]." {".$TillEnd[0]."$ADD}\n\n";
//echo $ConfChapter;

					shell_exec("sudo rm $conffile");

					$file = fopen($conffile,"a+");

					fwrite($file,$ConfChapter);

					fclose($file);

					if(file_exists($conffile)){

						if($class[$i]=="command"){

							$ImportCommand="$importplugin -f $conffile -c "."checkcommand"." $s";

						}else{

							$ImportCommand="$importplugin -f $conffile -c ".$class[$i]." $s";

						}
						if($class[$i]=="servicedependency"){

							$ImportCommand="$importplugin -f $conffile -c "."service-dependency"." $s";

						}

						echo "<b>".$class[$i].":</b><br>\n";

						$output = str_replace("\n","<br>\n",shell_exec("sudo $ImportCommand"))."<br>\n";

						$o=0;

						while($highelight[$o]){

							$output = str_replace($highelight[$o],"<b>".$highelight[$o]."</b>",$output);

							$o++;

						}

						echo $output;

					}

					//count up

					$j++;

				}

			}

			$i++;

		}



		//Delete Icinga Conf

		shell_exec("rm $tmpdir/IcingaConf.cfg");	$i++;

	}

}else{

	echo "Es wurde keine IP eingetragen.<br>\n";

}

echo "<b>Ende</b><br>\n";

echo "<a href='../setHost.php'> > Gehe zur&uuml;ck zu NConf < </a><br>\n";

?>
Add to menu_administration.html:

Code: Select all

<tr>
                <td>
                    <div class="link_with_tag">check_mk</div>
                </td>
                <td align="right">
                    <div align="right"><b>
                         <a href="#" >---</a>
                         / <a href="setHost.php" >Add</a>
                    </b></div>
                </td>
            </tr>
setHost.php:

Code: Select all

<?php
require_once 'include/head.php';


?>

<!-- jQuery part -->
<script type="text/javascript">
    $(document).ready(function(){
        // highlight the previouse applied template
        $('.previously_applied').hover(function() {
            $("td[id='"+$(this).attr("id")+"_first']").toggleClass("ui-state-highlight");
        });
    });

</script>

<?php


// Set previous (referer) page if that actual item in detail view will be deleted
if ( empty($_SERVER["HTTP_REFERER"]) AND isset($_SESSION["after_delete_page"]) ) {
    message($debug, "referer not set, seems to be from a delete operation, go to after_delete_page");
    $from_url = $_SESSION["after_delete_page"];
}elseif ( isset($_SERVER["HTTP_REFERER"]) AND preg_match('/detail\.php/', $_SERVER["HTTP_REFERER"]) ){
    message($debug, "detail.php matched");
    $from_url = $_SERVER["HTTP_REFERER"];
}elseif( isset($_SERVER["HTTP_REFERER"]) AND preg_match('/modify_item\.php/', $_SERVER["HTTP_REFERER"]) ){
    # coming from editing, do still a forward to the after_delete_page
    $from_url = $_SESSION["after_delete_page"];
}elseif( isset($_SERVER["HTTP_REFERER"]) AND preg_match('/'.preg_quote("add_item_step2.php").'/', $_SERVER["HTTP_REFERER"]) ){
    # coming from an add (entry exists), do still a forward to the after_delete_page
    $from_url = $_SESSION["after_delete_page"];
}elseif( !empty($_SERVER["HTTP_REFERER"]) ){
    message($debug, "not from detail.php or modify, setting referer");
    $_SESSION["after_delete_page"] = $_SERVER["HTTP_REFERER"];
    $from_url = $_SERVER["HTTP_REFERER"];
}else{
    # direct opening of this file, or no referer, go back to overview.php
    $from_url = "index.php";
}

set_page();

# Normal detail page
echo '<div style="width: 500px;" class="relative">';

    echo '<div class="ui-nconf-header ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">';

        echo '<div><h2>Lade Check_mk Konfiguration</h2></div>';
        echo '<div id="ui-nconf-icon-bar">';
        echo '</div>';
    echo '</div>';
    echo '<div class="ui-nconf-content ui-widget-content ui-corner-bottom">';

    $colgroup = '<colgroup>
                    <col width="150">
                    <col>
                 </colgroup>';


    echo '<table class="ui-nconf-table ui-nconf-max-width">';
    echo $colgroup;
    

    echo '<tbody>';

   
	echo "ACHTUNG: Der Host darf nicht in NConf vorhanden sein!\n";
?>
	<form action="cmkToNconf/loader.php" method="GET">
	<tr>
		<td class="color_list2">IP-Adresse:</td>
		<td class="color_list1 highlight">
				<input type="text" name="ip">
		</td>
	</tr>
	<tr>
		<td class="color_list2"><input type="reset" value=" Abbrechen"></td>
		<td class="color_list1 highlight"><input type="submit" value=" Laden "></td>
	</tr>
        </form>
        
<?php
    echo '</table>';
    echo '</div>';

echo '</div>';

mysql_close($dbh);
require_once 'include/foot.php';

?>
I hope for your help :)

Locked