Dateien hochladen nach „libs“

This commit is contained in:
M_Viper 2023-02-25 02:06:41 +01:00
parent 074411eeb5
commit 542006d923
11 changed files with 561 additions and 0 deletions

91
libs/cpu.php Normal file
View File

@ -0,0 +1,91 @@
<?php
require '../autoload.php';
$Config = new Config();
// Number of cores
$num_cores = Misc::getCpuCoresNumber();
// CPU info
$model = 'N.A';
$frequency = 'N.A';
$cache = 'N.A';
$bogomips = 'N.A';
$temp = 'N.A';
if ($cpuinfo = shell_exec('cat /proc/cpuinfo'))
{
$processors = preg_split('/\s?\n\s?\n/', trim($cpuinfo));
foreach ($processors as $processor)
{
$details = preg_split('/\n/', $processor, -1, PREG_SPLIT_NO_EMPTY);
foreach ($details as $detail)
{
list($key, $value) = preg_split('/\s*:\s*/', trim($detail));
switch (strtolower($key))
{
case 'model name':
case 'cpu model':
case 'cpu':
case 'processor':
$model = $value;
break;
case 'cpu mhz':
case 'clock':
$frequency = $value.' MHz';
break;
case 'cache size':
case 'l2 cache':
$cache = $value;
break;
case 'bogomips':
$bogomips = $value;
break;
}
}
}
}
if ($frequency == 'N.A')
{
if ($f = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq'))
{
$f = $f / 1000;
$frequency = $f.' MHz';
}
}
// CPU Temp
if ($Config->get('cpu:enable_temperature'))
{
if (exec('/usr/bin/sensors | grep -E "^(CPU Temp|Core 0)" | cut -d \'+\' -f2 | cut -d \'.\' -f1', $t))
{
if (isset($t[0]))
$temp = $t[0].' °C';
}
else
{
if (exec('cat /sys/class/thermal/thermal_zone0/temp', $t))
{
$temp = round($t[0] / 1000).' °C';
}
}
}
$datas = array(
'model' => $model,
'num_cores' => $num_cores,
'frequency' => $frequency,
'cache' => $cache,
'bogomips' => $bogomips,
'temp' => $temp,
);
echo json_encode($datas);

52
libs/disk.php Normal file
View File

@ -0,0 +1,52 @@
<?php
require '../autoload.php';
$Config = new Config();
$datas = array();
if (!(exec('/bin/df -T | awk -v c=`/bin/df -T | grep -bo "Type" | awk -F: \'{print $2}\'` \'{print substr($0,c);}\' | tail -n +2 | awk \'{print $1","$2","$3","$4","$5","$6","$7}\'', $df)))
{
$datas[] = array(
'total' => 'N.A',
'used' => 'N.A',
'free' => 'N.A',
'percent_used' => 0,
'mount' => 'N.A',
'filesystem' => 'N.A',
);
}
else
{
$mounted_points = array();
$key = 0;
foreach ($df as $mounted)
{
list($filesystem, $type, $total, $used, $free, $percent, $mount) = explode(',', $mounted);
if (strpos($type, 'tmpfs') !== false && $Config->get('disk:show_tmpfs') === false)
continue;
if (!in_array($mount, $mounted_points))
{
$mounted_points[] = trim($mount);
$datas[$key] = array(
'total' => Misc::getSize($total * 1024),
'used' => Misc::getSize($used * 1024),
'free' => Misc::getSize($free * 1024),
'percent_used' => trim($percent, '%'),
'mount' => $mount,
);
if ($Config->get('disk:show_filesystem'))
$datas[$key]['filesystem'] = $filesystem;
}
$key++;
}
}
echo json_encode($datas);

33
libs/last_login.php Normal file
View File

@ -0,0 +1,33 @@
<?php
require '../autoload.php';
$Config = new Config();
$datas = array();
if ($Config->get('last_login:enable'))
{
if (!(exec('/usr/bin/lastlog --time 365 | /usr/bin/awk -F\' \' \'{ print $1";"$5, $4, $8, $6}\'', $users)))
{
$datas[] = array(
'user' => 'N.A',
'date' => 'N.A',
);
}
else
{
$max = $Config->get('last_login:max');
for ($i = 1; $i < count($users) && $i <= $max; $i++)
{
list($user, $date) = explode(';', $users[$i]);
$datas[] = array(
'user' => $user,
'date' => $date,
);
}
}
}
echo json_encode($datas);

30
libs/load_average.php Normal file
View File

@ -0,0 +1,30 @@
<?php
require '../autoload.php';
if (!($load_tmp = shell_exec('cat /proc/loadavg | awk \'{print $1","$2","$3}\'')))
{
$load = array(0, 0, 0);
}
else
{
// Number of cores
$cores = Misc::getCpuCoresNumber();
$load_exp = explode(',', $load_tmp);
$load = array_map(
function ($value, $cores) {
$v = (int)($value * 100 / $cores);
if ($v > 100)
$v = 100;
return $v;
},
$load_exp,
array_fill(0, 3, $cores)
);
}
$datas = $load;
echo json_encode($datas);

37
libs/memory.php Normal file
View File

@ -0,0 +1,37 @@
<?php
require '../autoload.php';
$free = 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 = shell_exec('grep Cached /proc/meminfo | awk \'{print $2}\'');
$free = (int)$free + (int)$buffers + (int)$cached;
}
// Total
if (!($total = shell_exec('grep MemTotal /proc/meminfo | awk \'{print $2}\'')))
{
$total = 0;
}
// Used
$used = $total - $free;
// Percent used
$percent_used = 0;
if ($total > 0)
$percent_used = 100 - (round($free / $total * 100));
$datas = array(
'used' => Misc::getSize($used * 1024),
'free' => Misc::getSize($free * 1024),
'total' => Misc::getSize($total * 1024),
'percent_used' => $percent_used,
);
echo json_encode($datas);

115
libs/network.php Normal file
View File

@ -0,0 +1,115 @@
<?php
require '../autoload.php';
$datas = array();
$network = array();
// Possible commands for ifconfig and ip
$commands = array(
'ifconfig' => array('ifconfig', '/sbin/ifconfig', '/usr/bin/ifconfig', '/usr/sbin/ifconfig'),
'ip' => array('ip', '/bin/ip', '/sbin/ip', '/usr/bin/ip', '/usr/sbin/ip'),
);
// Returns command line for retreive interfaces
function getInterfacesCommand($commands)
{
$ifconfig = Misc::whichCommand($commands['ifconfig'], ' | awk -F \'[/ |: ]\' \'{print $1}\' | sed -e \'/^$/d\'');
if (!empty($ifconfig))
{
return $ifconfig;
}
else
{
$ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);
if (!empty($ip_cmd))
{
return $ip_cmd.' -oneline link show | awk \'{print $2}\' | sed "s/://"';
}
else
{
return null;
}
}
}
// Returns command line for retreive IP address from interface name
function getIpCommand($commands, $interface)
{
$ifconfig = Misc::whichCommand($commands['ifconfig'], ' '.$interface.' | awk \'/inet / {print $2}\' | cut -d \':\' -f2');
if (!empty($ifconfig))
{
return $ifconfig;
}
else
{
$ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);
if (!empty($ip_cmd))
{
return 'for family in inet inet6; do '.
$ip_cmd.' -oneline -family $family addr show '.$interface.' | grep -v fe80 | awk \'{print $4}\' | sed "s/\/.*//"; ' .
'done';
}
else
{
return null;
}
}
}
$getInterfaces_cmd = getInterfacesCommand($commands);
if (is_null($getInterfaces_cmd) || !(exec($getInterfaces_cmd, $getInterfaces)))
{
$datas[] = array('interface' => 'N.A', 'ip' => 'N.A');
}
else
{
foreach ($getInterfaces as $name)
{
$ip = null;
$getIp_cmd = getIpCommand($commands, $name);
if (is_null($getIp_cmd) || !(exec($getIp_cmd, $ip)))
{
$network[] = array(
'name' => $name,
'ip' => 'N.A',
);
}
else
{
if (!isset($ip[0]))
$ip[0] = '';
$network[] = array(
'name' => $name,
'ip' => $ip[0],
);
}
}
foreach ($network as $interface)
{
// Get transmit and receive datas by interface
exec('cat /sys/class/net/'.$interface['name'].'/statistics/tx_bytes', $getBandwidth_tx);
exec('cat /sys/class/net/'.$interface['name'].'/statistics/rx_bytes', $getBandwidth_rx);
$datas[] = array(
'interface' => $interface['name'],
'ip' => $interface['ip'],
'transmit' => Misc::getSize($getBandwidth_tx[0]),
'receive' => Misc::getSize($getBandwidth_rx[0]),
);
unset($getBandwidth_tx, $getBandwidth_rx);
}
}
echo json_encode($datas);

30
libs/ping.php Normal file
View File

@ -0,0 +1,30 @@
<?php
require '../autoload.php';
$Config = new Config();
$datas = array();
if (count($Config->get('ping:hosts')) > 0)
$hosts = $Config->get('ping:hosts');
else
$hosts = array('google.com', 'wikipedia.org');
foreach ($hosts as $host)
{
exec('/bin/ping -qc 1 '.$host.' | awk -F/ \'/^rtt/ { print $5 }\'', $result);
if (!isset($result[0]))
{
$result[0] = 0;
}
$datas[] = array(
'host' => $host,
'ping' => $result[0],
);
unset($result);
}
echo json_encode($datas);

35
libs/server.php Normal file
View File

@ -0,0 +1,35 @@
<?php
require '../autoload.php';
$Config = new Config();
$datas = array();
$available_protocols = array('tcp', 'udp');
$show_port = $Config->get('server:show_port');
if (count($Config->get('server:list')) > 0)
{
foreach ($Config->get('server:list') as $service)
{
$host = $service['host'];
$port = $service['port'];
$name = $service['name'];
$protocol = isset($service['protocol']) && in_array($service['protocol'], $available_protocols) ? $service['protocol'] : 'tcp';
if (Misc::scanPort($host, $port, $protocol))
$status = 1;
else
$status = 0;
$datas[] = array(
'port' => $show_port === true ? $port : '',
'name' => $name,
'status' => $status,
);
}
}
echo json_encode($datas);

35
libs/services.php Normal file
View File

@ -0,0 +1,35 @@
<?php
require '../autoload.php';
$Config = new Config();
$datas = array();
$available_protocols = array('tcp', 'udp');
$show_port = $Config->get('services:show_port');
if (count($Config->get('services:list')) > 0)
{
foreach ($Config->get('services:list') as $service)
{
$host = $service['host'];
$port = $service['port'];
$name = $service['name'];
$protocol = isset($service['protocol']) && in_array($service['protocol'], $available_protocols) ? $service['protocol'] : 'tcp';
if (Misc::scanPort($host, $port, $protocol))
$status = 1;
else
$status = 0;
$datas[] = array(
'port' => $show_port === true ? $port : '',
'name' => $name,
'status' => $status,
);
}
}
echo json_encode($datas);

32
libs/swap.php Normal file
View File

@ -0,0 +1,32 @@
<?php
require '../autoload.php';
// Free
if (!($free = shell_exec('grep SwapFree /proc/meminfo | awk \'{print $2}\'')))
{
$free = 0;
}
// Total
if (!($total = shell_exec('grep SwapTotal /proc/meminfo | awk \'{print $2}\'')))
{
$total = 0;
}
// Used
$used = $total - $free;
// Percent used
$percent_used = 0;
if ($total > 0)
$percent_used = 100 - (round($free / $total * 100));
$datas = array(
'used' => Misc::getSize($used * 1024),
'free' => Misc::getSize($free * 1024),
'total' => Misc::getSize($total * 1024),
'percent_used' => $percent_used,
);
echo json_encode($datas);

71
libs/system.php Normal file
View File

@ -0,0 +1,71 @@
<?php
require '../autoload.php';
// Hostname
$hostname = php_uname('n');
// OS
if (!($os = shell_exec('/usr/bin/lsb_release -ds | cut -d= -f2 | tr -d \'"\'')))
{
if(!($os = shell_exec('cat /etc/system-release | cut -d= -f2 | tr -d \'"\'')))
{
if (!($os = shell_exec('find /etc/*-release -type f -exec cat {} \; | grep PRETTY_NAME | tail -n 1 | cut -d= -f2 | tr -d \'"\'')))
{
$os = 'N.A';
}
}
}
$os = trim($os, '"');
$os = str_replace("\n", '', $os);
// Kernel
if (!($kernel = shell_exec('/bin/uname -r')))
{
$kernel = 'N.A';
}
// Uptime
if (!($totalSeconds = shell_exec('/usr/bin/cut -d. -f1 /proc/uptime')))
{
$uptime = 'N.A';
}
else
{
$uptime = Misc::getHumanTime($totalSeconds);
}
// Last boot
if (!($upt_tmp = shell_exec('cat /proc/uptime')))
{
$last_boot = 'N.A';
}
else
{
$upt = explode(' ', $upt_tmp);
$last_boot = date('Y-m-d H:i:s', time() - intval($upt[0]));
}
// Current users
if (!($current_users = shell_exec('who -u | awk \'{ print $1 }\' | wc -l')))
{
$current_users = 'N.A';
}
// Server datetime
if (!($server_date = shell_exec('/bin/date')))
{
$server_date = date('Y-m-d H:i:s');
}
$datas = array(
'hostname' => $hostname,
'os' => $os,
'kernel' => $kernel,
'uptime' => $uptime,
'last_boot' => $last_boot,
'current_users' => $current_users,
'server_date' => $server_date,
);
echo json_encode($datas);