-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.php
176 lines (157 loc) · 5.78 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
This is the main file.
It returns an json Object when requested per jsonp or some debug output else.
PHP version 5
@category Main_File
@package Default
@author Philip Griesbacher <[email protected]>
@license http://opensource.org/licenses/gpl-license.php GNU Public License
@link https://github.com/ConSol/histou
**/
require_once('histou/bootstrap.php');
set_error_handler(
function ($errno, $errstr, $errfile, $errline, $errcontext = null) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
);
//Set path to config file and parse it
if(isset($_SERVER['OMD_ROOT'])){
$ini_path = $_SERVER['OMD_ROOT'].'/etc/histou/histou.ini';
}
else{
$ini_path = 'histou.ini';
}
\histou\Basic::parsIni($ini_path);
//Parse commandline and get parameter
\histou\Basic::parsArgs();
//Test config
$returnCode = \histou\Basic::testConfig();
if (isset($returnCode) && $returnCode != 0){
exit($returnCode);
}
header("access-control-allow-origin: *");
//Disable warnings
//error_reporting(E_ALL ^ E_WARNING);
//error_reporting(0);
ini_set('default_socket_timeout', DEFAULT_SOCKET_TIMEOUT);
$perfData = array('host' => HOST, 'service' => SERVICE);
if (!\histou\Basic::$disablePerfdataLookup){
// database load perfdata
$database = null;
if (DATABASE_TYPE == INFLUXDB) {
$database = new \histou\database\Influxdb(URL);
}elseif(DATABASE_TYPE == VICTORIAMETRICS){
$database = new \histou\database\Victoriametrics(URL);
}else{
\histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard('# Unsupported database'), 1);
}
$request = $database->fetchPerfData();
if (empty($request)) {
\histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard('# Database not reachable or empty result'), 1);
exit(0);
}
if(empty($request['data']) && empty($request['series'])) {
\histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard("# Error: result does not contain data\n```".print_r($request, true)), 1);
exit(0);
}
\histou\Debug::add('request out: '. print_r ($request,true)."\n");
$perfData = $database->filterPerfdata(
$request,
HOST,
SERVICE
);
$perfDataSize = 0;
if(is_array($perfData)) {
$perfDataSize = sizeof($perfData);
}
if ($perfDataSize < 4) {
if ($perfDataSize == 1) {
\histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard('# Database Error: '.$perfData), 1);
exit(1);
} else {
\histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard("# Host / Service not found in Database\n```".print_r ($request,true)), 1);
exit(1);
}
}
}
// load templates
$templateFiles = \histou\Folder::loadFolders(
array(CUSTOM_TEMPLATE_FOLDER, DEFAULT_TEMPLATE_FOLDER)
);
$templateCache = new \histou\template\cache('templates');
$templates = $templateCache->loadTemplates($templateFiles, '\histou\template\loader::loadTemplate');
if (sizeof($templates) == 0) {
\histou\Basic::returnData(\histou\Debug::errorMarkdownDashboard('# Could not load templates!'), 1);
exit(1);
}
if (\histou\Basic::$specificTemplate == ""){
// search for the best
//save databaseresult to rule
\histou\template\Rule::setCheck(
$perfData['host'],
$perfData['service'],
$perfData['command'],
array_keys($perfData['perfLabel'])
);
usort($templates, '\histou\template\Template::compare');
$valid = $templates[0]->isValid();
\histou\Debug::add("Template order:");
foreach ($templates as $template) {
\histou\Debug::add($template);
}
\histou\Debug::add("Is the first template valid: ".\histou\Debug::printBoolean($valid)."\n");
\histou\Debug::add("Data: ".print_r($perfData, true));
if ($valid) {
$template = $templates[0];
} else {
$template = \histou\template\Template::findDefaultTemplate($templates, 'default.php');
}
}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') {
$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);
}