/** Returns a list of templates. @param array $templatepaths list of requested templates. @return array list of templates. **/ public function loadTemplates(array $templatepaths, $loader) { $templates = array(); $hasChanged = false; foreach ($templatepaths as $path) { $fileAge = filemtime($path); if ($this->templates != null && array_key_exists($path, $this->templates) && $this->templates[$path][FILE_AGE_KEY] >= $fileAge) { array_push($templates, $this->templates[$path][RULE_KEY]); continue; } $template = call_user_func($loader, $path); if ($template == null) { \histou\Debug::add("The template: {$path} is not valid PHP!"); } else { $hasChanged = true; $this->templates[$path][FILE_AGE_KEY] = $fileAge; $this->templates[$path][RULE_KEY] = $template->getRule(); array_push($templates, $template); } } if ($hasChanged) { $this->saveCache(); } return $templates; }
public function testBasics() { $this->assertSame(\histou\Debug::isEnable(), false); \histou\Debug::enable(); $this->assertSame(\histou\Debug::isEnable(), true); $this->assertSame(\histou\Debug::printBoolean(true), "true"); $this->assertSame(\histou\Debug::printBoolean(false), "false"); \histou\Debug::add("foo"); $this->assertSame("#### foo\n", \histou\Debug::getLogAsMarkdown()); \histou\Debug::add("bar"); $this->assertSame("#### foo\n#### bar\n", \histou\Debug::getLogAsMarkdown()); }
/** Uses the php -l command to test if a file contains valid PHP code. @param string $filePath path to the file to check. @return bool. **/ public static function isFileValidPHP($filePath) { //TODO:test if php content. e.g. just foo would work... $cmd = \histou\Basic::$phpCommand . " -l {$filePath} 2>&1"; $process = proc_open($cmd, \histou\Basic::$descriptorSpec, $pipes); if (!is_resource($process)) { \histou\Debug::add("Error: Could not start: {$cmd}"); // @codeCoverageIgnore return false; // @codeCoverageIgnore } $syntaxCheck = stream_get_contents($pipes[1]); fclose($pipes[1]); $returnCode = proc_close($process); if (substr($syntaxCheck, 1, 12) == "Parse error:") { \histou\Debug::add("Syntaxcheck: " . $syntaxCheck); } if ($returnCode != 0) { \histou\Debug::add("Error: " . $syntaxCheck); } return $returnCode == 0; }
} } else { // find the one $template = \histou\template\Template::findDefaultTemplate($templates, \histou\Basic::$specificTemplate); } // load ForecastTemplates $forecastTemplateFiles = \histou\Folder::loadFolders(array(FORECAST_TEMPLATE_FOLDER)); $forcastTemplateCache = new \histou\template\cache('forecast'); $forecastTemplates = $forcastTemplateCache->loadTemplates($forecastTemplateFiles, '\\histou\\template\\loader::loadForecastTemplate'); usort($forecastTemplates, '\\histou\\template\\Template::compare'); $fValid = !empty($forecastTemplates) && $forecastTemplates[0]->isValid(); \histou\Debug::add("ForecastTemplate order:"); foreach ($forecastTemplates as $ftemplate) { \histou\Debug::add($ftemplate); } \histou\Debug::add("Is the first ForecastTemplate valid: " . \histou\Debug::printBoolean($fValid) . "\n"); if ($fValid) { $forecastTemplate = $forecastTemplates[0]; $className = get_class($forecastTemplate); if ($className == 'histou\\template\\Rule') { $forecast = \histou\template\loader::loadForecastTemplate($forecastTemplate->getFileName(), true); } if (isset($forecast)) { $forecast->setForecastDurations(); } } if (isset($template) && !empty($template)) { $className = get_class($template); if ($className == 'histou\\template\\Rule') { $dashboard = \histou\template\loader::loadTemplate($template->getFileName(), true)->generateDashboard($perfData); } elseif ($className == 'histou\\template\\Template' || $className == 'histou\\template\\SimpleTemplate') {
/** Expects a filename and parses the file, to return a rule and a dashbord lambda. @param string $file Path to file. @return array. **/ public static function parseSimpleTemplate($file) { $lines = file($file, FILE_SKIP_EMPTY_LINES); $foundJson = false; $ruleHits = array(); $dashboard = ""; foreach ($lines as $line) { if (substr(trim($line), 0, 1) == "#") { //Comment found continue; } if ($foundJson) { $dashboard .= $line; } else { if (sizeof($ruleHits) != 4) { //Searching for Ruleset foreach (array('host', 'service', 'command', 'perfLabel') as $type) { if (preg_match(";^\\s*{$type}\\s*=\\s*(.*?)\$;", $line, $hit)) { if ($type == 'perfLabel') { $ruleHits[$type] = str_getcsv($hit[1]); foreach ($ruleHits[$type] as &$label) { $label = trim($label); } } else { $ruleHits[$type] = trim($hit[1]); } } } } if (preg_match(";^\\s*{;", $line)) { //Found dashboard beginning $foundJson = true; $dashboard .= $line; } } } $rule = new Rule($ruleHits['host'], $ruleHits['service'], $ruleHits['command'], $ruleHits['perfLabel']); $genTemplate = function ($perfData) use($dashboard) { $jsonDashboard = static::isStringValidJson($dashboard); if ($jsonDashboard === null) { \histou\Debug::enable(); return \histou\Debug::errorMarkdownDashboard('#The Template given was not valid json!'); } $oldPerfData = array('host' => array(), 'service' => array(), 'command' => array()); if ($jsonDashboard && array_key_exists('rows', $jsonDashboard)) { foreach ($jsonDashboard['rows'] as &$row) { if (array_key_exists('panels', $row)) { foreach ($row['panels'] as &$panel) { // remove PanelTitel if needed if (\histou\Basic::$disablePanelTitle) { $panel['title'] = ''; } // get old Perfdata if (array_key_exists('targets', $panel)) { foreach ($panel['targets'] as $target) { if (array_key_exists('tags', $target)) { foreach ($target['tags'] as $tag) { $key = $tag['key']; if (array_key_exists($key, $oldPerfData)) { array_push($oldPerfData[$key], $tag['value']); } } } } } } } } } $dashboard = json_encode($jsonDashboard); foreach ($oldPerfData as $label => $value) { if (sizeof($value) > 0) { $counted = array_count_values($value); $oldPerfData[$label] = array_search(max($counted), $counted); } } //Test if hostname != service != command if so replace them $perfDataSize = sizeof($oldPerfData); $perfDataKeys = array_keys($oldPerfData); $replaced = false; for ($i = 0; $i < $perfDataSize; $i++) { if ($oldPerfData[$perfDataKeys[$i]] != $oldPerfData[$perfDataKeys[($i + 1) % $perfDataSize]] && $oldPerfData[$perfDataKeys[$i]] != $oldPerfData[$perfDataKeys[($i + 2) % $perfDataSize]] && array_key_exists($perfDataKeys[$i], $oldPerfData) && array_key_exists($perfDataKeys[$i], $perfData) && $oldPerfData[$perfDataKeys[$i]] != $perfData[$perfDataKeys[$i]]) { $dashboard = str_replace($oldPerfData[$perfDataKeys[$i]], $perfData[$perfDataKeys[$i]], $dashboard); $replaced = true; } } if (!$replaced) { \histou\Debug::add('# Nothing replace because hostname, service, command are equal in the template'); } return $dashboard; }; return array($rule, $genTemplate); }