forked from RobboRob/allsky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.php
254 lines (234 loc) · 10.9 KB
/
system.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
<?php
/**
*
* Find the version of the Raspberry Pi
* Currently only used for the system information page but may useful elsewhere
*
*/
function RPiVersion()
{
// Lookup table from http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/
$revisions = array(
'0002' => 'Model B Revision 1.0',
'0003' => 'Model B Revision 1.0 + ECN0001',
'0004' => 'Model B Revision 2.0 (256 MB)',
'0005' => 'Model B Revision 2.0 (256 MB)',
'0006' => 'Model B Revision 2.0 (256 MB)',
'0007' => 'Model A',
'0008' => 'Model A',
'0009' => 'Model A',
'000d' => 'Model B Revision 2.0 (512 MB)',
'000e' => 'Model B Revision 2.0 (512 MB)',
'000f' => 'Model B Revision 2.0 (512 MB)',
'0010' => 'Model B+',
'0013' => 'Model B+',
'0011' => 'Compute Module',
'0012' => 'Model A+',
'a01041' => 'a01041',
'a21041' => 'a21041',
'900092' => 'PiZero 1.2',
'900093' => 'PiZero 1.3',
'9000c1' => 'PiZero W',
'a02082' => 'Pi 3 Model B',
'a22082' => 'Pi 3 Model B',
'a020d3' => 'Pi 3 Model B+',
'a03111' => 'Model 4B Revision 1.1 (1 GB)',
'b03111' => 'Model 4B Revision 1.1 (2 GB)',
'b03112' => 'Model 4B Revision 1.2 (2 GB)',
'c03111' => 'Model 4B Revision 1.1 (4 GB)',
'c03112' => 'Model 4B Revision 1.2 (4 GB)',
'd03114' => 'Model 4B Revision 1.4 (8 GB)'
);
exec('cat /proc/cpuinfo', $cpuinfo_array);
$rev = trim(array_pop(explode(':', array_pop(preg_grep("/^Revision/", $cpuinfo_array)))));
if (array_key_exists($rev, $revisions)) {
return $revisions[$rev];
} else {
return 'Unknown Pi';
}
}
function formatSize($bytes)
{
$types = array('B', 'KB', 'MB', 'GB', 'TB');
for ($i = 0; $bytes >= 1024 && $i < (count($types) - 1); $bytes /= 1024, $i++) ;
return (round($bytes, 2) . " " . $types[$i]);
}
/**
*
*
*/
function DisplaySystem()
{
// hostname
exec("hostname -f", $hostarray);
$hostname = $hostarray[0];
// uptime
$uparray = explode(" ", exec("cat /proc/uptime"));
$seconds = round($uparray[0], 0);
$minutes = $seconds / 60;
$hours = $minutes / 60;
$days = floor($hours / 24);
$hours = floor($hours - ($days * 24));
$minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60));
$uptime = '';
if ($days != 0) {
$uptime .= $days . ' day' . (($days > 1) ? 's ' : ' ');
}
if ($hours != 0) {
$uptime .= $hours . ' hour' . (($hours > 1) ? 's ' : ' ');
}
if ($minutes != 0) {
$uptime .= $minutes . ' minute' . (($minutes > 1) ? 's ' : ' ');
}
// mem used
exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray);
$memused = floor($memarray[0]);
// check for memused being unreasonably low, if so repeat expecting modern output of "free" command
if ($memused < 0.1) {
unset($memarray);
exec("free -m | awk '/Mem:/ { total=$2 } /Mem:/ { used=$3 } END { print used/total*100}'", $memarray);
$memused = floor($memarray[0]);
}
// Disk usage
// File Usage
/* get disk space free (in bytes) */
$df = disk_free_space("/var/www");
/* and get disk space total (in bytes) */
$dt = disk_total_space("/var/www");
/* now we calculate the disk space used (in bytes) */
$du = $dt - $df;
/* percentage of disk used - this will be used to also set the width % of the progress bar */
$dp = sprintf('%.2f', ($du / $dt) * 100);
/* and we formate the size from bytes to MB, GB, etc. */
$df = formatSize($df);
$du = formatSize($du);
$dt = formatSize($dt);
if ($memused > 90) {
$memused_status = "danger";
} elseif ($memused > 75) {
$memused_status = "warning";
} elseif ($memused > 0) {
$memused_status = "success";
}
// cpu load
$cores = exec("grep -c ^processor /proc/cpuinfo");
$loadavg = exec("awk '{print $1}' /proc/loadavg");
$cpuload = floor(($loadavg * 100) / $cores);
if ($cpuload > 90) {
$cpuload_status = "danger";
} elseif ($cpuload > 75) {
$cpuload_status = "warning";
} elseif ($cpuload > 0) {
$cpuload_status = "success";
}
// temperature
$temperature = round(exec("awk '{print $1/1000}' /sys/class/thermal/thermal_zone0/temp"), 2);
if ($temperature > 70 || $temperature < 0) {
$temperature_status = "danger";
} elseif ($temperature > 60 || $temperature < 10) {
$temperature_status = "warning";
} else {
$temperature_status = "success";
}
// disk usage
if ($dp > 90) {
$disk_usage_status = "danger";
} elseif ($dp > 70 && $dp < 90) {
$disk_usage_status = "warning";
} else {
$disk_usage_status = "success";
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-cube fa-fw"></i> System</div>
<div class="panel-body">
<?php
if (isset($_POST['system_reboot'])) {
echo '<div class="alert alert-warning">System Rebooting Now!</div>';
$result = shell_exec("sudo /sbin/reboot");
}
if (isset($_POST['system_shutdown'])) {
echo '<div class="alert alert-warning">System Shutting Down Now!</div>';
$result = shell_exec("sudo /sbin/shutdown -h now");
}
if (isset($_POST['service_start'])) {
echo '<div class="alert alert-warning">Allsky service started</div>';
$result = shell_exec("sudo /bin/systemctl start allsky_RPiHQ");
}
if (isset($_POST['service_stop'])) {
echo '<div class="alert alert-warning">Allsky service stopped</div>';
$result = shell_exec("sudo /bin/systemctl stop allsky_RPiHQ");
}
?>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h4>System Information</h4>
<div class="info-item">Hostname</div>
<?php echo $hostname ?></br>
<div class="info-item">Pi Revision</div>
<?php echo RPiVersion() ?></br>
<div class="info-item">Uptime</div>
<?php echo $uptime ?></br>
<div class="info-item">SD Card</div>
<?php echo "$dt ($df free)" ?></br></br>
<div class="info-item">Memory Used</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $memused_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $memused ?>" aria-valuemin="0"
aria-valuemax="100"
style="width: <?php echo $memused ?>%;"><?php echo $memused ?>%
</div>
</div>
<div class="info-item">CPU Load</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $cpuload_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $cpuload ?>" aria-valuemin="0"
aria-valuemax="100"
style="width: <?php echo $cpuload ?>%;"><?php echo $cpuload ?>%
</div>
</div>
<div class="info-item">CPU Temperature</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $temperature_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $temperature ?>" aria-valuemin="0"
aria-valuemax="100"
style="width: <?php echo $temperature ?>%;"><?php echo $temperature ?>°C
</div>
</div>
<div class="info-item">Disk Usage</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $disk_usage_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $dp ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $dp ?>%;"><?php echo $dp ?>%
</div>
</div>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
</div><!-- /.row -->
<form action="?page=system_info" method="POST">
<div style="margin-bottom: 20px">
<button type="button" class="btn btn-outline btn-primary" onclick="document.location.reload(true)"><i class="fa fa-sync-alt"></i> Refresh</button>
</div>
<div style="margin-bottom: 15px">
<button type="submit" class="btn btn-success" style="margin-bottom:5px" name="service_start"/><i class="fa fa-play"></i> Start Allsky</button>
<button type="submit" class="btn btn-danger" style="margin-bottom:5px" name="service_stop"/><i class="fa fa-stop"></i> Stop Allsky</button>
</div>
<button type="submit" class="btn btn-warning" style="margin-bottom:5px" name="system_reboot"/><i class="fa fa-power-off"></i> Reboot Raspberry Pi</button>
<button type="submit" class="btn btn-warning" style="margin-bottom:5px" name="system_shutdown"/><i class="fa fa-plug"></i> Shutdown Raspberry Pi</button>
</form>
</div><!-- /.panel-body -->
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
?>