-
Notifications
You must be signed in to change notification settings - Fork 158
/
diagnostic.php
360 lines (319 loc) · 13.2 KB
/
diagnostic.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
include_once("./config.php");
include_once("./lib/loader.php");
$sent_ok = 0;
if (preg_match('/ru/is', $_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$lang_page_title = 'Отправка информации для диагностики';
$lang_comments = 'Комментарии';
$lang_comments_note = 'Пожалуйста, опишите детали инцидента:';
$lang_tech_details = 'Техническая информация';
$lang_tech_details_note = 'Формируется автоматически';
$lang_tech_details_review = 'Просмотреть';
$lang_attach_log = 'приложить последние лог-файлы (это может занять некоторое дополнительное время)';
$lang_send = 'Отправить';
$lang_sent_ok = 'Спасибо. Данные отправлены.';
$lang_sent_ok_url = 'Отправленная диагностическая информация доступна по следующей уникальной ссылке:';
$lang_sent_ok_note = '(информация приватная и доступна только по указанной ссылке)';
} else {
$lang_page_title = 'Submit diagnostic details';
$lang_comments = 'Comments';
$lang_comments_note = 'Please, describe the incident details:';
$lang_tech_details = 'Technical Data';
$lang_tech_details_note = 'Generated automatically';
$lang_tech_details_review = 'Review';
$lang_attach_log = 'attach latest log-files (it can take some time to pack it)';
$lang_send = 'Send';
$lang_sent_ok = 'Thank you. Data has been sent.';
$lang_sent_ok_url = 'Diagnostic information sent can be reviewed by following uniq URL:';
$lang_sent_ok_note = '(information is kept private and available only by the URL above)';
}
function collectData()
{
$result = array();
$result['unixtime'] = time();
$result['timestamp'] = date('Y-m-d H:i:s', $result['unixtime']);
if (defined('MASTER_UPDATE_URL')) {
$result['update_url'] = MASTER_UPDATE_URL;
} else {
$result['update_url'] = 'Default';
}
$result['reboot'] = array();
if (file_exists('./reboot')) {
$reboot_started = filemtime('./reboot');
$result['reboot']['status'] = 'initiated';
$result['reboot']['started'] = date('Y-m-d H:i:s', $reboot_started);
$result['reboot']['since_started'] = $result['unixtime'] - $reboot_started;
} else {
$result['reboot']['status'] = 'ok';
}
if (IsWindowsOS()) {
$os = 'Windows';
} else {
$os = trim(exec("uname -a"));
if (!$os) {
$os = 'Linux';
}
}
$result['OS'] = $os;
$result['locale'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$result['PHP']['version'] = phpversion();
$result['PHP']['extensions'] = get_loaded_extensions();
$server_keys = array('SERVER_SOFTWARE','GATEWAY_INTERFACE','DOCUMENT_ROOT','FCGI_ROLE','USER','HOME','HTTP_HOST');
foreach($server_keys as $key) {
if (isset($_SERVER[$key])) {
$result['SERVER'][$key]=$_SERVER[$key];
}
}
$cycles = array();
if ($lib_dir = opendir("./scripts")) {
while (($lib_file = readdir($lib_dir)) !== false) {
if ((preg_match("/^cycle_.+?\.php$/", $lib_file)))
$cycles[$lib_file] = array();
}
closedir($lib_dir);
}
$result['cycles'] = $cycles;
global $db;
if ($db) {
$result['LatestUpdateId'] = gg('LatestUpdateId');
$result['LatestUpdateTimestamp'] = gg('LatestUpdateTimestamp');
include_once("./load_settings.php");
$result['DB']['connection'] = 'OK';
$tables = SQLSelect("SHOW TABLE STATUS;");
$result['DB']['tables'] = $tables;
$sys_errors = SQLSelect("SELECT ID,CODE,LATEST_UPDATE,ACTIVE FROM system_errors ORDER BY LATEST_UPDATE DESC");
$result['active_errors'] = 0;
if ($sys_errors[0]['ID']) {
foreach ($sys_errors as $v) {
$errors_data = SQLSelect("SELECT COMMENTS,ADDED FROM system_errors_data WHERE ERROR_ID=" . $v['ID'] . " ORDER BY ADDED DESC LIMIT 5");
$v['DATA'] = $errors_data;
if ($v['ACTIVE']) {
$result['active_errors'] += $v['ACTIVE'];
}
unset($v['ID']);
$result['SYSTEM_ERRORS'][] = $v;
}
}
foreach ($result['cycles'] as $path => $v) {
if (preg_match('/(cycle_.+?)\.php/is', $path, $m)) {
$title = $m[1];
if (getGlobal($title . 'Disabled')) {
$result['cycles'][$path]['disabled'] = 1;
}
if (getGlobal($title . 'Control')) {
$result['cycles'][$path]['control'] = getGlobal($title . 'Control');
}
$cycle_run_update = getGlobal($title . 'Run');
if ($cycle_run_update) {
$result['cycles'][$path]['run'] = $cycle_run_update;
$time_passed = ($result['unixtime'] - $cycle_run_update);
$result['cycles'][$path]['since_update'] = $time_passed;
if ($time_passed > 5 * 60) {
$result['cycles'][$path]['status'] = 'stopped';
} else {
$result['cycles'][$path]['status'] = 'running';
}
} else {
$result['cycles'][$path]['status'] = 'offline';
}
}
}
} else {
$result['DB']['connection'] = 'Failed';
}
if (!IsWindowsOS()) {
$max_usage = 90; //%
$output = array();
exec('df -h', $output);
$result['DF_OUTPUT'] = implode("\n", $output);
foreach ($output as $line) {
if (preg_match('/(\d+)% (\/.+)/', $line, $m)) {
$proc = $m[1];
$path = trim($m[2]);
if ($path == '') continue;
$result['SPACE_AVAILABLE'][$path]['usage_proc'] = $proc;
if ($proc > $max_usage) {
$result['SPACE_AVAILABLE'][$path]['status'] = 'low';
} else {
$result['SPACE_AVAILABLE'][$path]['status'] = 'ok';
}
}
}
}
$result['language'] = SETTINGS_SITE_LANGUAGE;
$result['timezone'] = SETTINGS_SITE_TIMEZONE;
return $result;
}
$comments = '';
if (isset($_POST['send'])) {
$data = gr('data');
if (!$data) {
$data = json_encode(collectData());
}
$comments = gr('comments');
$tar_name = '';
if ($_POST['include_log']) {
// add latest log files to archive
mkdir('./cms/saverestore/temp', 0777);
mkdir('./cms/saverestore/temp/cms', 0777);
mkdir('./cms/saverestore/temp/cms/debmes', 0777);
$log_expire = 24 * 60 * 60;
if (defined('SETTINGS_SYSTEM_DEBMES_PATH') && SETTINGS_SYSTEM_DEBMES_PATH != '') {
$log_path = SETTINGS_SYSTEM_DEBMES_PATH;
} elseif (defined('LOG_DIRECTORY') && LOG_DIRECTORY != '') {
$log_path = LOG_DIRECTORY;
} else {
$log_path = ROOT . 'cms/debmes';
}
$files = scandir($log_path);
foreach ($files as $file) {
if (is_file($log_path . '/' . $file) && (time() - filemtime($log_path . '/' . $file) < $log_expire)) {
copy($log_path . '/' . $file, './cms/saverestore/temp/cms/debmes/' . $file);
}
}
$tar_name .= 'diagnostic_' . date('Y-m-d__h-i-s');
$tar_name .= IsWindowsOS() ? '.tar' : '.tgz';
if (IsWindowsOS()) {
$result = exec('tar.exe --strip-components=2 -C ./cms/saverestore/temp/ -cvf ./cms/saverestore/' . $tar_name . ' ./');
$new_name = str_replace('.tar', '.tar.gz', $tar_name);
$result = exec('gzip.exe ./cms/saverestore/' . $tar_name);
if (file_exists('./cms/saverestore/' . $new_name)) {
$tar_name = $new_name;
}
} else {
chdir(ROOT . 'cms/saverestore/temp');
exec('tar cvzf ../' . $tar_name . ' .');
chdir('../../../');
}
removeTree('./cms/saverestore/temp');
}
$url = 'https://connect.smartliving.ru/market/';
$fields = array(
'op' => 'diagnostic',
'data' => $data,
'comments' => $comments
);
if ($_POST['code']) {
$fields['code'] = $code;
}
if ($tar_name != '') {
if (!function_exists('getCurlValue')) {
function getCurlValue($filename, $contentType, $postname)
{
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}
$value = "@" . $filename . ";filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}
return $value;
}
}
$cfile = getCurlValue(ROOT . 'cms/saverestore/' . $tar_name, 'application/tar+gzip', $tar_name);
$fields['datafile'] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec($ch);
if (curl_errno($ch) && !$background) {
$errorInfo = curl_error($ch);
$info = curl_getinfo($ch);
echo("Failed sending data. Finished with error: \n" . $errorInfo . "\n" . json_encode($info));
exit;
}
curl_close($ch);
if ($_POST['code']) {
header("Content-type:text/json");
echo $result;
exit;
}
if ($result != '') {
$data = json_decode($result, true);
if (is_array($data) && $data['status'] == 'ok') {
$sent_ok = 1;
$sent_details_url = $data['url'];
}
}
/*
if (!$sent_ok) {
echo "Failed to send data. ";echo $result;exit;
}
*/
}
$result = collectData();
if ($_GET['ajax'] == 1) {
//echo '<pre>';
//var_dump($result);
foreach ($result["cycles"] as $key => $value) {
if (isset($value['run'])) $result["cycles"][$key]['run_time'] = date('d.m.Y H:i:s', $value['run']);
}
//echo '<pre>';
//var_dump($result["cycles"]);
echo json_encode($result);
die();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php echo $lang_page_title; ?></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1><?php echo $lang_page_title; ?></h1>
<?php if ($sent_ok) { ?>
<div class="alert alert-success"><?php echo $lang_sent_ok; ?></div>
<div>
<?php echo $lang_sent_ok_url; ?>
<br/>
<big><a href="<?php echo $sent_details_url; ?>"><?php echo $sent_details_url; ?></a></big>
<br/>
<small><?php echo $lang_sent_ok_note; ?></small>
</div>
<?php } else { ?>
<form method="post" class="form">
<h3><?php echo $lang_comments; ?></h3>
<div>
<?php echo $lang_comments_note; ?><br/>
<textarea name="comments" class="form-control" rows="3"><?php echo $comments; ?></textarea>
</div>
<h3><?php echo $lang_tech_details; ?></h3>
<div>
<?php echo $lang_tech_details_note; ?> <a href="#"
onclick="$('#diagnostic_data').toggle();return false;"><?php echo $lang_tech_details_review; ?></a><br/>
<div id="diagnostic_data" style="display:none">
<textarea name="data" class="form-control"
rows="8"><?php echo json_encode($result, JSON_PRETTY_PRINT); ?></textarea>
</div>
</div>
<div>
<label><input type="checkbox" name="include_log" value="1"> <?php echo $lang_attach_log; ?></label>
</div>
<div>
<input type="submit" class="btn btn-success" name="send" value="<?php echo $lang_send; ?>">
</div>
</form>
<?php } ?>
</div>
</body>
</html>