<NME> init.php
<BEF> <?php
/*
 * Holy Lance
 * https://github.com/lincanbin/Holy-Lance
 *
 * Copyright 2016 Canbin Lin (lincanbin@hotmail.com)
 * http://www.94cb.com/
 *
 * Licensed under the MIT License:
 * https://opensource.org/licenses/MIT
 * 
 * A Linux Resource / Performance Monitor based on PHP. 
 */

function exec_command($command)
{
	exec($command, $temp);
	return empty($temp) ? "" : $temp[0];
}

function exec_command_all($command)
{
	exec($command, $temp);
	return implode("\n", $temp);
}

function get_cpu_info_map($cpu_info_val)
{
	$result = array();
check_password();


exec("cat /proc/net/dev | grep \":\" | awk -F ':' '{gsub(\" \", \"\"); if ($2 > 0) print $1}'", $network_cards);
exec("cat /proc/diskstats | awk '{if ($4 > 0) print $3}'", $disk);
$cpu_info = array(
	'cpu_name' => trim(shell_exec('cat /proc/cpuinfo | grep name | cut -f2 -d: | head -1')), // CPU名称
	'cpu_num' => trim(shell_exec('cat /proc/cpuinfo | grep "physical id"| sort | uniq | wc -l')), // CPU个数（X路CPU）
	'cpu_core_num' => trim(shell_exec('cat /proc/cpuinfo | grep "cores" | uniq | awk -F ":" \'{print $2}\'')), // CPU核心数
	'cpu_processor_num' => trim(shell_exec('cat /proc/cpuinfo | grep "processor" | wc -l')), // CPU逻辑处理器个数
	'cpu_frequency' => trim(shell_exec('cat /proc/cpuinfo | grep MHz | uniq | awk -F ":" \'{print $2}\'')), // CPU 频率
);
$all_cpu_info = array_map("get_cpu_info_map", explode("\n\n", trim(shell_exec('cat /proc/cpuinfo'))));
$memory_info = get_mem_info_map(explode("\n", trim(shell_exec('cat /proc/meminfo'))));
$network_info = array();
foreach ($network_cards as $eth) {
	$network_info[$eth]['ip'] = explode("\n", trim(shell_exec("ip addr show " . $eth . " | grep 'inet' | awk '{print $2}' 2> /dev/null; if [[ $? -ne 0 ]]; then ifconfig " . $eth . " | grep 'inet' | sed 's/addr://g' | awk '{print $2}'; fi")));
}
$system_env = array(
	'status' => true,
	'version' => 1,
	'system_name' => trim(shell_exec('cat /etc/*-release | head -n1')),
exec("cat /proc/net/dev | grep \":\" | awk '{gsub(\":\", \"\");print $1}'", $network_cards);
exec("cat /proc/diskstats | awk '{print $3}'", $disk);
$cpu_info = array(
	'cpu_name' => exec_command('cat /proc/cpuinfo | grep name | cut -f2 -d: | head -1'), // CPU名称
	'cpu_num' => exec_command('cat /proc/cpuinfo | grep "physical id"| sort | uniq | wc -l'), // CPU个数（X路CPU）
	'cpu_core_num' => exec_command('cat /proc/cpuinfo | grep "cores" | uniq | awk -F ":" \'{print $2}\''), // CPU核心数
	'cpu_processor_num' => exec_command('cat /proc/cpuinfo | grep "processor" | wc -l'), // CPU逻辑处理器个数
	'cpu_frequency' => exec_command('cat /proc/cpuinfo | grep MHz | uniq | awk -F ":" \'{print $2}\''), // CPU 频率
);
$all_cpu_info = array_map("get_cpu_info_map", explode("\n\n", trim(exec_command_all('cat /proc/cpuinfo'))));
$memory_info = get_mem_info_map(explode("\n", trim(exec_command_all('cat /proc/meminfo'))));
$network_info = array();
foreach ($network_cards as $eth) {
	$network_info[$eth]['ip'] = explode("\n", exec_command_all("ifconfig " . $eth . " | grep 'inet' | awk '{ print $2}'"));
}
$system_env = array(
	'version' => 1,
	'system_name' => exec_command('cat /etc/*-release | head -n1'),
	'psssword_require' => false,
	'cpu_info' => $cpu_info,
	'cpu' => $all_cpu_info,

<MSG> Code refactoring.

<DFF> @@ -12,18 +12,6 @@
  * A Linux Resource / Performance Monitor based on PHP. 
  */
 
-function exec_command($command)
-{
-	exec($command, $temp);
-	return empty($temp) ? "" : $temp[0];
-}
-
-function exec_command_all($command)
-{
-	exec($command, $temp);
-	return implode("\n", $temp);
-}
-
 function get_cpu_info_map($cpu_info_val)
 {
 	$result = array();
@@ -52,21 +40,21 @@ header('Content-type: application/json');
 exec("cat /proc/net/dev | grep \":\" | awk '{gsub(\":\", \"\");print $1}'", $network_cards);
 exec("cat /proc/diskstats | awk '{print $3}'", $disk);
 $cpu_info = array(
-	'cpu_name' => exec_command('cat /proc/cpuinfo | grep name | cut -f2 -d: | head -1'), // CPU名称
-	'cpu_num' => exec_command('cat /proc/cpuinfo | grep "physical id"| sort | uniq | wc -l'), // CPU个数（X路CPU）
-	'cpu_core_num' => exec_command('cat /proc/cpuinfo | grep "cores" | uniq | awk -F ":" \'{print $2}\''), // CPU核心数
-	'cpu_processor_num' => exec_command('cat /proc/cpuinfo | grep "processor" | wc -l'), // CPU逻辑处理器个数
-	'cpu_frequency' => exec_command('cat /proc/cpuinfo | grep MHz | uniq | awk -F ":" \'{print $2}\''), // CPU 频率
+	'cpu_name' => trim(shell_exec('cat /proc/cpuinfo | grep name | cut -f2 -d: | head -1')), // CPU名称
+	'cpu_num' => trim(shell_exec('cat /proc/cpuinfo | grep "physical id"| sort | uniq | wc -l')), // CPU个数（X路CPU）
+	'cpu_core_num' => trim(shell_exec('cat /proc/cpuinfo | grep "cores" | uniq | awk -F ":" \'{print $2}\'')), // CPU核心数
+	'cpu_processor_num' => trim(shell_exec('cat /proc/cpuinfo | grep "processor" | wc -l')), // CPU逻辑处理器个数
+	'cpu_frequency' => trim(shell_exec('cat /proc/cpuinfo | grep MHz | uniq | awk -F ":" \'{print $2}\'')), // CPU 频率
 );
-$all_cpu_info = array_map("get_cpu_info_map", explode("\n\n", trim(exec_command_all('cat /proc/cpuinfo'))));
-$memory_info = get_mem_info_map(explode("\n", trim(exec_command_all('cat /proc/meminfo'))));
+$all_cpu_info = array_map("get_cpu_info_map", explode("\n\n", trim(shell_exec('cat /proc/cpuinfo'))));
+$memory_info = get_mem_info_map(explode("\n", trim(shell_exec('cat /proc/meminfo'))));
 $network_info = array();
 foreach ($network_cards as $eth) {
-	$network_info[$eth]['ip'] = explode("\n", exec_command_all("ifconfig " . $eth . " | grep 'inet' | awk '{ print $2}'"));
+	$network_info[$eth]['ip'] = explode("\n", trim(shell_exec("ifconfig " . $eth . " | grep 'inet' | awk '{ print $2}'")));
 }
 $system_env = array(
 	'version' => 1,
-	'system_name' => exec_command('cat /etc/*-release | head -n1'),
+	'system_name' => trim(shell_exec('cat /etc/*-release | head -n1')),
 	'psssword_require' => false,
 	'cpu_info' => $cpu_info,
 	'cpu' => $all_cpu_info,

Code refactoring.