<commit_before><?php
/*
 * Copyright 2005-2011 MERETHIS
 * Centreon is developped by : <NAME> and <NAME> under
 * GPL Licence 2.0.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation ; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give MERETHIS
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of MERETHIS choice, provided that
 * MERETHIS also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : <EMAIL>
 *
 * SVN : $URL$
 * SVN : $Id$
 *
 */
	if (!isset($oreon))
		exit;

	function _TestRPNInfinityLoop () {
                global $form;
                $gsvs = NULL;
                if (isset($form))
                        $gsvs = $form->getSubmitValues();

		if ( $gsvs["vmetric_name"] != NULL && preg_match("/".$gsvs["vmetric_name"]."/",$gsvs["rpn_function"]) ) {
			return false;
		} else {
			return true;
		}
	}


	function NameTestExistence ($vmetric_name = NULL, $index_id = NULL) {
		global $pearDB, $pearDBO, $form;
		$gsvs = NULL;
		if (isset($form))
			$gsvs = $form->getSubmitValues();

		$sql = "SELECT vmetric_id FROM virtual_metrics WHERE ";
		$sql .= "vmetric_name = '".($vmetric_name == NULL ? $gsvs["vmetric_name"] : $vmetric_name)."' ";
		$sql .= "AND index_id = '".($index_id == NULL ? $gsvs["index_id"] : $index_id)."'";
		$DBRESULT = $pearDB->query($sql);
		if (PEAR::isError($DBRESULT))
			print "DB Error : ".$DBRESULT->getDebugInfo();
		$vmetric = $DBRESULT->fetchRow();
		$nR = $DBRESULT->numRows();
		$DBRESULT->free();

		$sql = "SELECT metric_id FROM metrics WHERE ";
		$sql .= "metric_name = '".($vmetric_name == NULL ? $gsvs["vmetric_name"] : $vmetric_name)."' ";
		$sql .= "AND index_id = '".($index_id == NULL ? $gsvs["index_id"] : $index_id)."'";
		$DBRESULT = $pearDBO->query($sql);
		if (PEAR::isError($DBRESULT))
			print "DB Error : ".$DBRESULT->getDebugInfo();
		$metric = $DBRESULT->fetchRow();
		$nR = $nR + $DBRESULT->numRows();
		$DBRESULT->free();

		if ( $nR >= 1 && $vmetric["vmetric_id"] != $gsvs["vmetric_id"] || isset($metric["metric_id"]) )
			return false;
		else
			return true;
	}

	function deleteVirtualMetricInDB ($vmetrics = array())	{
		global $pearDB;
		foreach($vmetrics as $key => $value){
			$DBRESULT = $pearDB->query("DELETE FROM virtual_metrics WHERE vmetric_id = '".$key."'");
			if (PEAR::isError($DBRESULT))
				print "DB Error : ".$DBRESULT->getDebugInfo();
		}
	}

	function multipleVirtualMetricInDB ($vmetrics = array(), $nbrDup = array())	{
		global $pearDB;
		foreach($vmetrics as $key=>$value) {
			$DBRESULT = $pearDB->query("SELECT * FROM virtual_metrics WHERE vmetric_id = '".$key."' LIMIT 1");

			if (PEAR::isError($DBRESULT))
				print "DB Error : ".$DBRESULT->getDebugInfo();
			$row = $DBRESULT->fetchRow();
			$row["vmetric_id"] = '';
			for ($i = 1; $i <= $nbrDup[$key]; $i++)	{
				$val = null;
				foreach ($row as $key2=>$value2)	{
					$key2 == "index_id" ? $i_id = $value2 : null;
					if ($key2 == "vmetric_name") {
						$count = 1;
						$v_name = $value2."_".$count;
						while (!NameTestExistence($v_name,$i_id)) {
							$count++;
							$v_name = $value2."_".$count;
						}
						$value2 = $v_name;
					}
					$val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2!=NULL?("'".$value2."'"):"NULL");
				}
				$val ? $rq = "INSERT INTO virtual_metrics VALUES (".$val.")" : $rq = null;
				$DBRESULT2 = $pearDB->query($rq);
				if (PEAR::isError($DBRESULT2))
					print "DB Error : ".$DBRESULT2->getDebugInfo();
			}
		}
	}

	function updateVirtualMetricInDB ($vmetric_id = NULL)	{
		if (!$vmetric_id) return;
		updateVirtualMetric($vmetric_id);
	}

	function insertVirtualMetricInDB ()	{
		$vmetric_id = insertVirtualMetric();
		return ($vmetric_id);
	}

	function insertVirtualMetric()	{
		global $form, $pearDB;
		$h_id = NULL;
		$s_id = NULL;
		$ret = array();
		$ret = $form->getSubmitValues();
		$rq = "INSERT INTO `virtual_metrics` ( `vmetric_id` , `index_id`, `vmetric_name`, `def_type` , `rpn_function`, `unit_name` , `warn`, `crit`, `hidden` , `comment` , `vmetric_activate`, `ck_state`) ";
		$rq .= "VALUES ( NULL, ";
                isset($ret["index_id"]) && $ret["index_id"] != NULL ? $rq .= "'".$ret["index_id"]."', ": $rq .= "NULL, ";
		isset($ret["vmetric_name"]) && $ret["vmetric_name"] != NULL ? $rq .= "'".htmlentities($ret["vmetric_name"], ENT_QUOTES, "UTF-8")."', ": $rq .= "NULL, ";
		isset($ret["def_type"]) && $ret["def_type"] != NULL ? $rq .= "'".$ret["def_type"]."', ": $rq .= "NULL, ";
		isset($ret["rpn_function"]) && $ret["rpn_function"] != NULL ? $rq .= "'".$ret["rpn_function"]."', ": $rq .= "NULL, ";
		isset($ret["unit_name"]) && $ret["unit_name"] != NULL ? $rq .= "'".$ret["unit_name"]."', ": $rq .= "NULL, ";
		isset($ret["warn"]) && $ret["warn"] != NULL ? $rq .= "'".$ret["warn"]."', ": $rq .= "NULL, ";
		isset($ret["crit"]) && $ret["crit"] != NULL ? $rq .= "'".$ret["crit"]."', ": $rq .= "NULL, ";
		isset($ret["vhidden"]) && $ret["vhidden"] != NULL ? $rq .= "'".$ret["vhidden"]."', ": $rq .= "NULL, ";
		isset($ret["comment"]) && $ret["comment"] != NULL ? $rq .= "'".htmlentities($ret["comment"], ENT_QUOTES, "UTF-8")."', ": $rq .= "NULL, ";
		$rq .= "NULL, NULL";
		$rq .= ")";
`echo "Insert VMetric 'RQ' : $rq" >> /opt/app/centreon/www/include/views/graphs/virtualMetrics/DB.log`;
		$DBRESULT = $pearDB->query($rq);
		$DBRESULT = $pearDB->query("SELECT MAX(vmetric_id) FROM virtual_metrics");
		$vmetric_id = $DBRESULT->fetchRow();
		return ($vmetric_id["MAX(vmetric_id)"]);
	}

	function updateVirtualMetric($vmetric_id = null)	{
		if (!$vmetric_id) return;
		global $form, $pearDB;
		$ret = array();
		$ret = $form->getSubmitValues();
		$rq = "UPDATE virtual_metrics ";
		$rq .= "SET `index_id` = ";
                isset($ret["index_id"]) && $ret["index_id"] != NULL ? $rq .= "'".$ret["index_id"]."', ": $rq .= "NULL, ";
		$rq .=  "vmetric_name = ";
		isset($ret["vmetric_name"]) && $ret["vmetric_name"] != NULL ? $rq .= "'".htmlentities($ret["vmetric_name"], ENT_QUOTES, "UTF-8")."', ": $rq .= "NULL, ";
		$rq .=  "def_type = ";
		isset($ret["def_type"]) && $ret["def_type"] != NULL ? $rq .= "'".$ret["def_type"]."', ": $rq .= "NULL, ";
		$rq .=  "rpn_function = ";
		isset($ret["rpn_function"]) && $ret["rpn_function"] != NULL ? $rq .= "'".$ret["rpn_function"]."', ": $rq .= "NULL, ";<commit_msg>* Fix #3649 : Bad logging action (thanks <NAME>)<commit_after><?php
/*
 * Copyright 2005-2011 MERETHIS
 * Centreon is developped by : <NAME> and <NAME> under
 * GPL Licence 2.0.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation ; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give MERETHIS
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of MERETHIS choice, provided that
 * MERETHIS also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : <EMAIL>
 *
 * SVN : $URL$
 * SVN : $Id$
 *
 */
	if (!isset($oreon))
		exit;

	function _TestRPNInfinityLoop () {
                global $form;
                $gsvs = NULL;
                if (isset($form))
                        $gsvs = $form->getSubmitValues();

		if ( $gsvs["vmetric_name"] != NULL && preg_match("/".$gsvs["vmetric_name"]."/",$gsvs["rpn_function"]) ) {
			return false;
		} else {
			return true;
		}
	}


	function NameTestExistence ($vmetric_name = NULL, $index_id = NULL) {
		global $pearDB, $pearDBO, $form;
		$gsvs = NULL;
		if (isset($form))
			$gsvs = $form->getSubmitValues();

		$sql = "SELECT vmetric_id FROM virtual_metrics WHERE ";
		$sql .= "vmetric_name = '".($vmetric_name == NULL ? $gsvs["vmetric_name"] : $vmetric_name)."' ";
		$sql .= "AND index_id = '".($index_id == NULL ? $gsvs["index_id"] : $index_id)."'";
		$DBRESULT = $pearDB->query($sql);
		if (PEAR::isError($DBRESULT))
			print "DB Error : ".$DBRESULT->getDebugInfo();
		$vmetric = $DBRESULT->fetchRow();
		$nR = $DBRESULT->numRows();
		$DBRESULT->free();

		$sql = "SELECT metric_id FROM metrics WHERE ";
		$sql .= "metric_name = '".($vmetric_name == NULL ? $gsvs["vmetric_name"] : $vmetric_name)."' ";
		$sql .= "AND index_id = '".($index_id == NULL ? $gsvs["index_id"] : $index_id)."'";
		$DBRESULT = $pearDBO->query($sql);
		if (PEAR::isError($DBRESULT))
			print "DB Error : ".$DBRESULT->getDebugInfo();
		$metric = $DBRESULT->fetchRow();
		$nR = $nR + $DBRESULT->numRows();
		$DBRESULT->free();

		if ( $nR >= 1 && $vmetric["vmetric_id"] != $gsvs["vmetric_id"] || isset($metric["metric_id"]) )
			return false;
		else
			return true;
	}

	function deleteVirtualMetricInDB ($vmetrics = array())	{
		global $pearDB;
		foreach($vmetrics as $key => $value){
			$DBRESULT = $pearDB->query("DELETE FROM virtual_metrics WHERE vmetric_id = '".$key."'");
			if (PEAR::isError($DBRESULT))
				print "DB Error : ".$DBRESULT->getDebugInfo();
		}
	}

	function multipleVirtualMetricInDB ($vmetrics = array(), $nbrDup = array())	{
		global $pearDB;
		foreach($vmetrics as $key=>$value) {
			$DBRESULT = $pearDB->query("SELECT * FROM virtual_metrics WHERE vmetric_id = '".$key."' LIMIT 1");

			if (PEAR::isError($DBRESULT))
				print "DB Error : ".$DBRESULT->getDebugInfo();
			$row = $DBRESULT->fetchRow();
			$row["vmetric_id"] = '';
			for ($i = 1; $i <= $nbrDup[$key]; $i++)	{
				$val = null;
				foreach ($row as $key2=>$value2)	{
					$key2 == "index_id" ? $i_id = $value2 : null;
					if ($key2 == "vmetric_name") {
						$count = 1;
						$v_name = $value2."_".$count;
						while (!NameTestExistence($v_name,$i_id)) {
							$count++;
							$v_name = $value2."_".$count;
						}
						$value2 = $v_name;
					}
					$val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2!=NULL?("'".$value2."'"):"NULL");
				}
				$val ? $rq = "INSERT INTO virtual_metrics VALUES (".$val.")" : $rq = null;
				$DBRESULT2 = $pearDB->query($rq);
				if (PEAR::isError($DBRESULT2))
					print "DB Error : ".$DBRESULT2->getDebugInfo();
			}
		}
	}

	function updateVirtualMetricInDB ($vmetric_id = NULL)	{
		if (!$vmetric_id) return;
		updateVirtualMetric($vmetric_id);
	}

	function insertVirtualMetricInDB ()	{
		$vmetric_id = insertVirtualMetric();
		return ($vmetric_id);
	}

	function insertVirtualMetric()	{
		global $form, $pearDB, $centreon;
		$h_id = NULL;
		$s_id = NULL;
		$ret = array();
		$ret = $form->getSubmitValues();
		$rq = "INSERT INTO `virtual_metrics` ( `vmetric_id` , `index_id`, `vmetric_name`, `def_type` , `rpn_function`, `unit_name` , `warn`, `crit`, `hidden` , `comment` , `vmetric_activate`, `ck_state`) ";
		$rq .= "VALUES ( NULL, ";
                isset($ret["index_id"]) && $ret["index_id"] != NULL ? $rq .= "'".$ret["index_id"]."', ": $rq .= "NULL, ";
		isset($ret["vmetric_name"]) && $ret["vmetric_name"] != NULL ? $rq .= "'".htmlentities($ret["vmetric_name"], ENT_QUOTES, "UTF-8")."', ": $rq .= "NULL, ";
		isset($ret["def_type"]) && $ret["def_type"] != NULL ? $rq .= "'".$ret["def_type"]."', ": $rq .= "NULL, ";
		isset($ret["rpn_function"]) && $ret["rpn_function"] != NULL ? $rq .= "'".$ret["rpn_function"]."', ": $rq .= "NULL, ";
		isset($ret["unit_name"]) && $ret["unit_name"] != NULL ? $rq .= "'".$ret["unit_name"]."', ": $rq .= "NULL, ";
		isset($ret["warn"]) && $ret["warn"] != NULL ? $rq .= "'".$ret["warn"]."', ": $rq .= "NULL, ";
		isset($ret["crit"]) && $ret["crit"] != NULL ? $rq .= "'".$ret["crit"]."', ": $rq .= "NULL, ";
		isset($ret["vhidden"]) && $ret["vhidden"] != NULL ? $rq .= "'".$ret["vhidden"]."', ": $rq .= "NULL, ";
		isset($ret["comment"]) && $ret["comment"] != NULL ? $rq .= "'".htmlentities($ret["comment"], ENT_QUOTES, "UTF-8")."', ": $rq .= "NULL, ";
		$rq .= "NULL, NULL";
		$rq .= ")";
		if ($centreon->optGen["debug_rrdtool"] == "1") {
		    $debug_path = realpath($centreon->optGen["debug_path"]);
		    error_log("[" . date("d/m/Y H:s") ."] VIRTUAL METRIC : $rq \n", 3, $debug_path . "/rrdtool.log");
		}
		$DBRESULT = $pearDB->query($rq);
		$DBRESULT = $pearDB->query("SELECT MAX(vmetric_id) FROM virtual_metrics");
		$vmetric_id = $DBRESULT->fetchRow();
		return ($vmetric_id["MAX(vmetric_id)"]);
	}

	function updateVirtualMetric($vmetric_id = null)	{
		if (!$vmetric_id) return;
		global $form, $pearDB;
		$ret = array();
		$ret = $form->getSubmitValues();
		$rq = "UPDATE virtual_metrics ";
		$rq .= "SET `index_id` = ";
                isset($ret["index_id"]) && $ret["index_id"] != NULL ? $rq .= "'".$ret["index_id"]."', ": $rq .= "NULL, ";
		$rq .=  "vmetric_name = ";
		isset($ret["vmetric_name"]) && $ret["vmetric_name"] != NULL ? $rq .= "'".htmlentities($ret["vmetric_name"], ENT_QUOTES, "UTF-8")."', ": $rq .= "NULL, ";
		$rq .=  "def_type = ";
		isset($ret["def_type"]) && $ret["def_type"] != NULL ? $rq .= "'".$ret["def_type"]."', ": $rq .= "NULL, ";
		$rq .=  "rpn_function = ";
		isset($ret["rpn_function"]) && $ret["rpn_function"] != NULL ? $rq .= "'".$ret["rpn_function"]."', ": $rq .= "NULL, ";
{'fraction_non_alphanumeric': 0.20607007945812167, 'fraction_numerical': 0.004689331770222743, 'mean_word_length': 3.8059467918622847, 'pattern_counts': {'":': 26, '<': 16, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2274118', 'n_tokens_mistral': 5760, 'n_tokens_neox': 5354, 'n_words': 1369}