Repository navigation
Expand file tree
/
Copy pathmonitask.php
More file actions
301 lines (269 loc) · 9.5 KB
/
Copy pathmonitask.php
File metadata and controls
301 lines (269 loc) · 9.5 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
use Plugins\CsvStore;
use Plugins\Export;
use Plugins\GChartsExport;
use Plugins\Store;
use Plugins\TextExport;
class Monitask
{
// global vars of ini file
const VAR_INCLUDE = 'include';
// main sections
const SECTION_DATASTORE = 'datastore';
const SECTION_EXPORT = 'export';
const SECTION_COMMANDS = 'commands';
public static $platform; // "freebsd"
public static $architecture; // "amd64"
public static $ini; // {ini file contents}
public static $includes = []; // {"file-full-name":true, ...}
public static $commands = []; // {"cmd-name":"command", ...}
public static $metrics = []; // {"metric-name":["time", "value"], ...}
public static $items = []; // {"block-name":[], ...}
/** @var Store */
public static $store;
/** @var Export */
public static $export;
/**
* @param string $config_file
* @return bool
*/
public static function init($config_file)
{
putenv('LANG=C');
define('DIR', __DIR__);
self::$platform = strtolower(trim(`uname`));
self::$architecture = strtolower(trim(`uname -p`));
if (self::$ini = self::parseIni($config_file)) {
// CORE DIRECTIVES
// configure datastore
$datastore = Lib::arrayExtract(self::$ini, self::SECTION_DATASTORE);
if ($ds_type = Lib::arrayExtract($datastore, Store::VAR_TYPE)) {
switch ($ds_type) {
case CsvStore::TYPE_CSV:
self::$store = new CsvStore($datastore);
break;
default:
error_log(
sprintf(
"%s is not an implemented engine, use '%s'",
$ds_type,
CsvStore::TYPE_CSV
)
);
return false;
}
if (!empty(self::$store->error)) {
error_log(self::$store->error);
return false;
}
}
// configure export
$export = Lib::arrayExtract(self::$ini, self::SECTION_EXPORT);
if (isset($export[Export::VAR_TYPE])) {
$ex_type = Lib::arrayExtract($export, Export::VAR_TYPE);
switch ($ex_type) {
case TextExport::TYPE_TEXT:
self::$export = new TextExport($export);
break;
case GChartsExport::TYPE_GCHARTS:
self::$export = new GChartsExport($export);
break;
default:
error_log(
sprintf(
"%s is not an implemented engine, use '%s' or '%s'",
$ex_type,
TextExport::TYPE_TEXT,
GChartsExport::TYPE_GCHARTS
)
);
return false;
}
if (!empty(self::$export->error)) {
error_log(self::$export->error);
return false;
}
}
// COMMON DIRECTIVES
self::processIni(self::$ini);
return true;
} else {
error_log("error parsing $config_file");
return false;
}
}
/**
* @param string $config_file
* @return array|false
*/
public static function parseIni($config_file)
{
if ($config_fullname = realpath($config_file)
and $ini = parse_ini_file($config_fullname, true)
) {
$path = pathinfo($config_fullname);
// merge platform config
$platf_fullname =
(isset($path['dirname']) ? $path['dirname'] . DIRECTORY_SEPARATOR : '') .
"{$path['filename']}-" . self::$platform .
(isset($path['extension']) ? ".{$path['extension']}" : '');
if (file_exists($platf_fullname)
and $ini_platf = parse_ini_file($platf_fullname, true)
) {
$ini = array_replace_recursive($ini, $ini_platf);
}
// merge platform-architecture config
$platf_arch_fullname =
(isset($path['dirname']) ? $path['dirname'] . DIRECTORY_SEPARATOR : '') .
"{$path['filename']}-" . self::$platform . '-' . self::$architecture .
(isset($path['extension']) ? ".{$path['extension']}" : '');
if (file_exists($platf_arch_fullname)
and $ini_platf_arch = parse_ini_file($platf_arch_fullname, true)
) {
$ini = array_replace_recursive($ini, $ini_platf_arch);
}
return $ini;
} else {
return false;
}
}
/**
* @param array $ini
* @return bool
*/
public static function processIni($ini)
{
// merge commands
$commands = Lib::arrayExtract($ini, self::SECTION_COMMANDS);
if ($commands) {
self::$commands = array_replace_recursive(self::$commands, $commands);
}
// get includes
$includes = Lib::arrayExtract($ini, self::VAR_INCLUDE);
// get block name and start periods
$block = Lib::arrayExtract($ini, Store::VAR_BLOCK);
// import items
if (!empty($ini)) {
self::$items[$block] = $ini;
}
// process includes
if ($includes) {
foreach ((array)$includes as $include) {
$file = realpath($include);
if ($file and $ini2 = self::parseIni($file)) {
if (empty(self::$includes[$file])) {
self::$includes[$file] = true;
self::processIni($ini2);
}
} else {
error_log("include file $include does not exist or cannot be parsed");
}
}
}
return true;
}
/**
* @return bool
*/
public static function createStore()
{
if (empty(self::$store)) {
error_log('[' . self::SECTION_DATASTORE . '] section is not configured');
return false;
} elseif (self::$store->create()) {
return true;
} else {
error_log(self::$store->error);
return false;
}
}
/**
* @return bool
*/
public static function outputTemplate()
{
if (empty(self::$export)) {
error_log('[' . self::SECTION_EXPORT . '] section is not configured');
return false;
} elseif (empty(self::$store)) {
error_log('[' . self::SECTION_DATASTORE . '] section is not configured');
return false;
} elseif (self::$export->template(self::$items, self::$store)) {
return true;
} else {
error_log(self::$export->error);
return false;
}
}
/**
* @return bool
*/
public static function collectMetrics()
{
if (empty(self::$store)) {
error_log('[' . self::SECTION_DATASTORE . '] section is not configured');
return false;
} elseif (empty(self::$store->periods)) {
error_log(Store::VAR_PERIOD . ' directive is not set');
return false;
}
$errors = [];
foreach (self::$commands as $key => $cmd) {
// run commands
if ($cmd) {
$errorlevel = 0;
$output = [];
exec($cmd, $output, $errorlevel);
if ($errorlevel) {
$errors[] = "error executing $key: $cmd";
} elseif (empty($output)) {
// noop
} elseif (preg_match_all('/^(\S+)\s+(.+)$/m', implode(PHP_EOL, $output), $m)) {
// collect metrics
foreach ($m[1] as $k => $v) {
self::$metrics[$v] = rtrim($m[2][$k]);
}
} else {
$errors[] = "unformatted output from $key: $cmd";
}
}
}
// store metrics
if (self::$metrics
and !self::$store->insertMetrics($_SERVER['REQUEST_TIME'], self::$metrics)
) {
$errors[] = self::$store->error;
}
if ($errors) {
error_log(implode(PHP_EOL, $errors));
return false;
}
return true;
}
/**
* @return bool
*/
public static function exportData()
{
if (empty(self::$export)) {
error_log('[' . self::SECTION_EXPORT . '] section is not configured');
return false;
} elseif (empty(self::$items)) {
error_log('export items are not configured');
return false;
} elseif (empty(self::$store->periods)) {
error_log(Store::VAR_PERIOD . ' directive is not set');
return false;
} elseif (self::$store->load()) {
if (self::$export->export(self::$items, self::$store)) {
return true;
} else {
error_log(self::$export->error ?: 'error exporting data');
return false;
}
} else {
error_log(self::$store->error ?: 'error loading data from datastore');
return false;
}
}
}