Exemple #1
0
 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());
 }
Exemple #2
0
 /**
     Parses the GET parameter.
     @return null.
     **/
 public static function parsArgs()
 {
     if (static::$parsed) {
         return;
     }
     static::$parsed = true;
     $shortopts = "";
     $longopts = array("host:", "service:", "command:", "perf_label:", "request:");
     $args = getopt($shortopts, $longopts);
     $input = file_get_contents('php://input');
     if (!empty($input)) {
         // @codeCoverageIgnore
         static::$request = json_decode($input, true);
         // @codeCoverageIgnore
     } elseif (isset($args['request']) && !empty($args['request'])) {
         // @codeCoverageIgnore
         static::$request = json_decode($args['request'], true);
         // @codeCoverageIgnore
     }
     // @codeCoverageIgnore
     if (!static::$request) {
         if (isset($_GET['host']) && !empty($_GET['host'])) {
             define("HOST", $_GET["host"]);
         } elseif (isset($args['host']) && !empty($args['host'])) {
             define("HOST", $args["host"]);
             // @codeCoverageIgnore
         } else {
             // @codeCoverageIgnore
             \histou\Basic::returnData('Hostname is missing!', 1, 'Hostname is missing!');
         }
         if (isset($_GET['service']) && !empty($_GET['service'])) {
             define("SERVICE", $_GET["service"]);
         } elseif (isset($args['service']) && !empty($args['service'])) {
             define("SERVICE", $args["service"]);
             // @codeCoverageIgnore
         } else {
             // @codeCoverageIgnore
             define("SERVICE", HOSTCHECK_ALIAS);
         }
         if (isset($_GET['command']) && !empty($_GET['command'])) {
             define("COMMAND", $_GET["command"]);
         } elseif (isset($args['command']) && !empty($args['command'])) {
             define("COMMAND", $args["command"]);
             // @codeCoverageIgnore
         }
         // @codeCoverageIgnore
         if (isset($_GET['perf_label']) && !empty($_GET['perf_label'])) {
             global $PERF_LABEL;
             $PERF_LABEL = $_GET["perf_label"];
         } elseif (isset($args['perf_label']) && !empty($args['perf_label'])) {
             global $PERF_LABEL;
             $PERF_LABEL = $args["perf_label"];
             // @codeCoverageIgnore
         }
         // @codeCoverageIgnore
         if (isset($PERF_LABEL) && !is_array($PERF_LABEL)) {
             $PERF_LABEL = array($PERF_LABEL);
         }
     }
     if (isset($_GET['disablePanelTitel'])) {
         static::$disablePanelTitel = true;
     }
     if (isset($_GET['debug'])) {
         \histou\Debug::enable();
     }
     if (isset($_GET['height']) && !empty($_GET['height'])) {
         static::$height = $_GET["height"];
     }
     if (isset($_GET['legend']) && !empty($_GET['legend']) && $_GET["legend"] == "false") {
         define("SHOW_LEGEND", false);
     } else {
         define("SHOW_LEGEND", true);
     }
     if (isset($_GET['annotations']) && !empty($_GET['annotations']) && $_GET["annotations"] == "true") {
         define("SHOW_ANNOTATION", true);
     } else {
         define("SHOW_ANNOTATION", false);
     }
     if (isset($_GET['specificTemplate']) && !empty($_GET['specificTemplate'])) {
         static::$specificTemplate = $_GET["specificTemplate"];
     }
     if (static::$specificTemplate != "" && isset($_GET['disablePerfdataLookup'])) {
         static::$disablePerfdataLookup = true;
     }
 }
Exemple #3
0
 public function testReturnData()
 {
     $this->init();
     \histou\Debug::enable();
     $dashboard = new \histou\grafana\dashboard\DashboardInfluxDB('foo');
     ob_start();
     \histou\Basic::returnData($dashboard);
     $out1 = ob_get_contents();
     ob_end_clean();
     $this->assertSame($this->emptyDashboard, $out1);
     $_GET["callback"] = 1;
     ob_start();
     \histou\Basic::returnData('{"foo":"bar"}');
     $out2 = ob_get_contents();
     ob_end_clean();
     $this->assertSame('1({"foo":"bar"})', $out2);
     ob_start();
     \histou\Basic::returnData(1);
     $out3 = ob_get_contents();
     ob_end_clean();
     $this->assertContains("<pre>Don't know what to do with this type: integer", $out3);
 }
Exemple #4
0
 /**
     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);
 }