summaryrefslogtreecommitdiff
path: root/libs/memory.php
blob: d2826f272dbdf71eda7a481c3b90970a1f7023b9 (plain)
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
<?php
require '../autoload.php';

$free = 0;
$cached = 0;

if (shell_exec('cat /proc/meminfo'))
{
    $free    = shell_exec('grep MemFree /proc/meminfo | awk \'{print $2}\'');
    $buffers = shell_exec('grep Buffers /proc/meminfo | awk \'{print $2}\'');
    $cached  = $buffers + shell_exec('grep Cached /proc/meminfo | awk \'{print $2}\'');
}

// Total
if (!($total = shell_exec('grep MemTotal /proc/meminfo | awk \'{print $2}\'')))
{
    $total = 0;
}

// Used
$used = $total - $free - $cached;

// Percent used
$percent_used = 0;
if ($total > 0)
    $percent_used = round($used / $total * 100);


$datas = array(
    'used'          => Misc::getSize($used * 1024),
    'free'          => Misc::getSize($free * 1024),
    'cached '       => Misc::getSize($cached * 1024),
    'total'         => Misc::getSize($total * 1024),
    'percent_used'  => $percent_used,
);

echo json_encode($datas);