public function testErrorMarkdownDashboard() { $this->init(); $panel = new \histou\grafana\TextPanel('', 1); $panel->setMode(\histou\grafana\TextPanel::MARKDOWN); $panel->setContent("foo"); $row = new \histou\grafana\Row("ERROR"); $row->addPanel($panel); $dashboard = new \histou\grafana\dashboard\dashboardInfluxdb('Error'); $dashboard->addRow($row); $this->assertEquals($dashboard, \histou\Debug::errorMarkdownDashboard("foo")); }
public static function testConfig() { //test php command $cmd = static::$phpCommand . " -h 2>&1"; $process = proc_open($cmd, \histou\Basic::$descriptorSpec, $pipes); if (!is_resource($process)) { \histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard("# Error: Could not start: {$cmd}")); // @codeCoverageIgnore return 1; // @codeCoverageIgnore } stream_get_contents($pipes[1]); fclose($pipes[1]); $returnCode = proc_close($process); if ($returnCode != 0) { \histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard("# '" . $cmd . "' did not return with returncode 0. Maybe the phpCommand is not set properly."), 1); return 1; } return 0; }
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') { $dashboard = $template->generateDashboard($perfData); } else { \histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard("# unkown class {$className}"), 1); } if ($dashboard == null) { \histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard('# Template did not return a dashboard!'), 1); } else { \histou\Basic::returnData($dashboard, 0); } } else { \histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard('# No template found!'), 1); }
/** 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); }