[xASECO] Addon for chat command: /server | shows current Server CPU & Memory usage

Discuss everything about Xymph's Aseco flavoured server control scripts for TM Forever / classic TMN and for TM² Canyon.

Moderators: Xymph, TM-Patrol

Post Reply
User avatar
L3cKy
smooth traffic navigator
smooth traffic navigator
Posts: 192
Joined: 05 Sep 2007 13:55
Owned TM-games: TMUF
Location: Berlin
Contact:

[xASECO] Addon for chat command: /server | shows current Server CPU & Memory usage

Post by L3cKy » 26 Jun 2019 00:45

NOTE: This works ONLY with Linux/Ubuntu Servers

This little feature will show you the current Server CPU and Memory usage on chat command /server

Click for Preview

ToDo List:

1. Open xaseco/plugins/chat.server.php and add after line 44 following code:
//Under line: "$uptime = $aseco->server->uptime - ($updays * 24 * 3600);"

Code: Select all

// get CPU usage
$exec_loads = sys_getloadavg();
$exec_cores = trim(shell_exec("grep -P '^processor' /proc/cpuinfo|wc -l"));
$cpu = round($exec_loads[1]/($exec_cores + 1)*100, 0) . '%';

// get Memory usage
$exec_free = explode("\n", trim(shell_exec('free')));
$get_mem = preg_split("/[\s]+/", $exec_free[1]);
$mem = number_format(round($get_mem[2]/1024/1024, 2), 2) . ' GB / ' . number_format(round($get_mem[1]/1024/1024, 2), 2).' GB';
$mem_free = round($get_mem[2]/$get_mem[1]*100, 0) . '%';

// remove Model Name (Only the Lines)
$file = file('/proc/cpuinfo');
$proc_details = $file[4];
$proc_details = substr($proc_details, 13);

// count free
$cpu_free = 100-$cpu;
$mem_free = 100-$mem_free;

2. Search for following line and replace it:
//This will increase the lines for the server comment to prevent an out of screen window

Code: Select all

$multicmt = explode(LF, wordwrap($comment, 35, LF . '...'));

// into

$multicmt = explode(LF, wordwrap($comment, 250, LF . ''));

3. Search for following lines and replace the code:

Code: Select all

if ($admin_contact) {
   $stats[] = array('Admin Contact', '{#black}' . $admin_contact);
}
$stats[] = array();

// replace it into

if ($cpu_free > '20') {
	$cpu_free = '$0c0'.$cpu_free.'%{#black}';
} else {
	$cpu_free = '$f00'.$cpu_free.'%{#black}';
}

if ($mem_free > '20') {
	$mem_free = '$0c0'.$mem_free.'%{#black}';
} else {
	$mem_free = '$f00'.$mem_free.'%{#black}';
}
//Server Info
$stats[] = array('Server Info:', '{#black}' . $proc_details);
$stats[] = array('', '{#black}CPU: '.$cpu.' (Free: '.$cpu_free.')');
$stats[] = array('', '{#black}RAM: '.$mem.' (Free: '.$mem_free.')');

if ($admin_contact) {
   $stats[] = array('Admin Contact', '{#black}' . $admin_contact);
}
$stats[] = array();


Greetings L3cKy

Post Reply