-
Notifications
You must be signed in to change notification settings - Fork 0
/
webservice.php
executable file
·295 lines (214 loc) · 6.84 KB
/
webservice.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
<?php
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
// Convertisseur d'octet
// $entree = 'octet' ou 'kotect'
function convert_octet($intOctet, $entree, $intPrecision = 2)
{
if ($entree == 'octet')
{
if ($intOctet >= 1073741824)
{
$resultat = $intOctet / 1073741824;
$resultat_unit = ' Go';
}
elseif ($intOctet >= 1048576)
{
$resultat = $intOctet / 1048576;
$resultat_unit = ' Mo';
}
else
{
$resultat = $intOctet / 1024;
$resultat_unit = ' Ko';
}
}
elseif ($entree == 'koctet')
{
if ($intOctet >= 1073741824)
{
$resultat = $intOctet / 1073741824;
$resultat_unit = ' To';
}
elseif ($intOctet >= 1048576)
{
$resultat = $intOctet / 1048576;
$resultat_unit = ' Go';
}
elseif ($intOctet >= 1024)
{
$resultat = $intOctet / 1024;
$resultat_unit = ' Mo';
}
else
{
$resultat = $intOctet;
$resultat_unit = ' Ko';
}
}
return round($resultat, $intPrecision).$resultat_unit;
}
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
// Retourne l'uptime
// echo uptime();
function uptime()
{
$result = NULL;
$fd = fopen('/proc/uptime', 'r');
$ar_buf = split(' ', fgets($fd, 4096));
fclose($fd);
$sys_ticks = trim($ar_buf[0]);
$min = $sys_ticks / 60;
$heures = $min / 60;
$jours = floor($heures / 24);
$heures = floor($heures - ($jours * 24));
$min = floor($min - ($jours * 60 * 24) - ($heures * 60));
$text['jours'] = 'day(s)';
$text['heures'] = 'hour(s)';
$text['minutes'] = 'minute(s)';
if ($jours != 0)
{
if ($jours < 2) $text['jours'] = 'day(s)';
$result .= $jours.' '.$text['jours'].' ';
}
if ($heures != 0)
{
if ($heures < 2) $text['heures'] = 'hour(s)';
$result .= $heures.' '.$text['heures'].' ';
}
if ($min < 2) $text['minutes'] = 'minute(s)';
$result .= $min.' '.$text['minutes'];
return $result;
}
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
// Retourne la charge du CPU
// echo loadaverage();
function loadaverage()
{
exec('uptime', $exec); // On exécute la commande "uptime"
preg_match('/(load.*)/', $exec[0], $LA); // On cherche le load average
$result = strstr($LA[1], ':'); // On cherche les :
$result = substr($result, 1); // On les enlève
return $result;
}
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
// Retourne les infos sur la Ram en fonction du $choix
// Ex. : echo meminfo('pourcent_used');
function meminfo()
{
$meminfo = file('/proc/meminfo');
for ($i = 0; $i < count($meminfo); $i++)
{
list($item, $data) = split(':', $meminfo[$i], 2);
$item = chop($item);
$data = chop($data);
if ($item == 'MemTotal') $total_mem = $data;
if ($item == 'MemFree') $free_mem = $data;
if ($item == 'SwapTotal') $total_swap = $data;
if ($item == 'SwapFree') $free_swap = $data;
if ($item == 'Buffers') $buffer_mem = $data;
if ($item == 'Cached') $cache_mem = $data;
if ($item == 'MemShared') $shared_mem = $data;
}
$used_mem = $total_mem - $free_mem;
$used_swap = $total_swap - $free_swap;
$pourcent_free = round($free_mem / $total_mem * 100).'%';
$pourcent_used = (100 - $pourcent_free).'%';
$pourcent_swap = round(($total_swap - $free_swap) / $total_swap * 100).'%';
$pourcent_swap_free = (100 - $pourcent_swap).'%';
$pourcent_buff = round($buffer_mem / $total_mem * 100).'%';
$pourcent_cach = round($cache_mem / $total_mem * 100).'%';
$pourcent_shar = round($shared_mem / $total_mem * 100).'%';
$used_mem = str_replace('kB', '', $used_mem);
$used_swap = str_replace('kB', '', $used_swap);
$total_mem = str_replace('kB', '', $total_mem);
$total_swap = str_replace('kB', '', $total_swap);
$used_mem = convert_octet($used_mem, 'koctet');
$used_swap = convert_octet($used_swap, 'koctet');
$free_mem = convert_octet($free_mem, 'koctet');
$free_swap = convert_octet($free_swap, 'koctet');
$total_mem = convert_octet($total_mem, 'koctet');
$total_swap = convert_octet($total_swap, 'koctet');
$array = array("used_mem" => $used_mem, "used_swap" => $used_swap, "free_mem" => $free_mem,
"free_swap" => $free_swap, "total_mem" => $total_mem, "total_swap" => $total_swap);
return $array;
}
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
// Retourne le statut d'un port (ONLINE ou OFFLINE)
// echo check_port(21);
function check_port($port, $tempsmax = 5)
{
$ip = 'localhost';
$sock = @fsockopen($ip, $port, $num, $error, $tempsmax);
if ($sock)
{
fclose($sock);
return 1;
}
else
{
return 0;
}
}
// Retourne les fréquences de CPU (Samuel Salas)
function getfrequencies()
{
$cpuclk = @file_get_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cpuclk");
$lcdclk = exec("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_lcdclk");
$cpuclk1 = substr($cpuclk, 5,9);
$cpuclk2 = substr($cpuclk, 20,9);
$cpuclk3 = substr($cpuclk, 34,9);
$cpuclk4 = substr($cpuclk, 49,9);
$arrayclk = array("CPU"=>$cpuclk1,"EMI"=>$cpuclk2,"APBX"=>$cpuclk3,"APBH"=>$cpuclk4,"LCD"=>$lcdclk);
return $arrayclk;
}
// Retourne les infos de CPU (Samuel Salas)
function getcpuinfos()
{
$cpuinfo = @file_get_contents("/proc/cpuinfo");
return $cpuinfo;
}
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
# WEBSERVICE
###############################################################################################################
if ($_GET["query"] == "uptime")
{
$uptime = array("uptime", uptime());
echo json_encode($uptime);
}
else if ($_GET["query"] == "loadaverage")
{
$loadaverage = array("loadaverage", loadaverage());
echo json_encode($loadaverage);
}
else if ($_GET["query"] == "meminfo")
{
$meminfo = meminfo();
echo json_encode($meminfo);
}
else if ($_GET["query"] == "services")
{
$ports = array("ssh" => check_port(22), "http" => check_port(80),"ftp" => check_port(21));
echo json_encode($ports);
}
else if ($_GET["query"] == "frequencies")
{
$frequencies = getfrequencies();
echo json_encode($frequencies);
}
else if ($_GET["query"] == "cpuinfos")
{
$getcpuinfos = getcpuinfos();
echo json_encode($getcpuinfos);
}
else
{
$all = array( "uptime"=> uptime(),
"loadaverage" => loadaverage(),
"meminfo" => meminfo(),
"ports" => array("ssh" => check_port(22), "http" => check_port(80), "ftp" => check_port(21)),
"frequencies" => getfrequencies(),
"cpuinfos" => getcpuinfos()
);
echo json_encode($all);
}
?>