forked from sh2/dstat2graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstat2graphs.php
74 lines (63 loc) · 1.98 KB
/
dstat2graphs.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
<?php
// error_reporting(E_ALL|E_STRICT);
function get_report_dir() {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$report_dir = 'reports/' . date('Ymd-His_');
for ($i = 0; $i < 8; $i++) {
$report_dir .= $chars{mt_rand(0, strlen($chars) - 1)};
}
return $report_dir;
}
function validate_number($input, $min, $max, $default) {
if (preg_match('/^\d+$/D', $input)) {
if ($input < $min) {
return $min;
} elseif ($input > $max) {
return $max;
} else {
return $input;
}
} else {
return $default;
}
}
$csv_file = escapeshellarg($_FILES['csv_file']['tmp_name']);
$report_dir = get_report_dir();
$width = validate_number($_POST['width'], 64, 1024, 512);
$height = validate_number($_POST['height'], 64, 1024, 192);
$disk_limit = validate_number($_POST['disk_limit'], 0, PHP_INT_MAX, 0);
$net_limit = validate_number($_POST['net_limit'], 0, PHP_INT_MAX, 0);
$offset = validate_number($_POST['offset'], 0, PHP_INT_MAX, 0);
$duration = validate_number($_POST['duration'], 0, PHP_INT_MAX, 0);
$message = '';
if (is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
exec("perl dstat2graphs.pl {$csv_file} {$report_dir} {$width} {$height} {$disk_limit} {$net_limit} {$offset} {$duration} 2>&1",
$output, $return_var);
if ($return_var == 0) {
header("Location: {$report_dir}/");
exit();
} else {
foreach ($output as $line) {
$message .= htmlspecialchars($line) . "<br />\n";
}
}
} else {
if (($_FILES['csv_file']['error'] == UPLOAD_ERR_INI_SIZE)
|| ($_FILES['csv_file']['error'] == UPLOAD_ERR_FORM_SIZE)) {
$message = "File size limit exceeded.\n";
} else {
$message = "Failed to upload file.\n";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Error - dstat2graphs</title>
</head>
<body>
<p>
<?php echo $message; ?>
</p>
</body>
</html>