Skip to content

Commit

Permalink
Created the class to process requests
Browse files Browse the repository at this point in the history
  • Loading branch information
simultsop committed Oct 18, 2018
1 parent 63121e7 commit 026c276
Showing 1 changed file with 113 additions and 34 deletions.
147 changes: 113 additions & 34 deletions info/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,120 @@
include_once $basePath . "/bootstrap.php";
validateApiRequest();

// Providing some Machine Info at a glance
$meminfo = file_exists("/proc/meminfo") ? file("/proc/meminfo") : 0;
$networkInfo = shell_exec("ifconfig -a");
$linuxInfo = array(
"os" => php_uname("s") . " " . php_uname("r"), //OS and version
"hostname" => php_uname("n"),
"uptime" => readableTimeticks(),
"totalDisk" => disk_total_space("/") / 1024, // Returns kilobytes
"freeDisk" => disk_free_space("/") / 1024, // Returns kilobytes
"totalMemory" => intval(preg_replace("/[^0-9]/", "", $meminfo[0])), // Returns kilobytes
"freeMemory" => intval(preg_replace("/[^0-9]/", "", $meminfo[1])), // Returns kilobytes
"ftp" => (getservbyport(21, "tcp")=="ftp") ? true : false, // Checking if port is default
"sftp" => (getservbyport(22, "tcp")=="ssh") ? true : false, // Checking if port is default
"telnet" => (getservbyport(23, "tcp")=="telnet") ? true : false, // Checking if port is default
"snmp" => (getservbyport(161, "udp")=="snmp") ? true : false, // Checking if port is default
"mysql" => (getservbyport(3306, "tcp")=="mysql") ? true : false, // Checking if port is default
"address" => mapNetworkInfo($networkInfo, "address"),
"subnet" => mapNetworkInfo($networkInfo, "subnet"),
"gateway" => mapNetworkInfo($networkInfo, "gateway"),
);

if(!is_resource(@fsockopen("127.0.0.1", 21))) { $linuxInfo['ftp'] = false; }
if(!is_resource(@fsockopen("127.0.0.1", 22))) { $linuxInfo['sftp'] = false; }
if(!is_resource(@fsockopen("127.0.0.1", 23))) { $linuxInfo['telnet'] = false; }
if(!is_resource(@fsockopen("127.0.0.1", 161))) { $linuxInfo['snmp'] = false; }
if(!is_resource(@fsockopen("127.0.0.1", 3306))) { $linuxInfo['mysql'] = false; }

function mapNetworkInfo($networkInfo, $info) {
$map = array('address'=>'inet addr:', 'subnet'=>'Mask:', 'gateway'=>'Bcast:');
$result = explode($map[$info], $networkInfo);
$result = explode(" ", array_pop($result));
$result = array_shift($result);
return trim($result);
Class MachineInfo {

public function info() {
$machineInfo = array(
"about" => $this->about(false),
"stats" => $this->stats(false),
);

returnJson($machineInfo);
}

public function about($returnJson = true) {
$about = array(
"os" => php_uname("s") . " " . php_uname("r"), //OS and version
"hostname" => php_uname("n"),
"network" => $this->network(false)
);

if($returnJson) {
returnJson($about);
} else {
return $about;
}
}

public function network($returnJson = true) {
$networkInfo = (shell_exec("ifconfig eth0")) ? shell_exec("ifconfig eth0") : "";
$networkInfo = array(
"address" => mapNetworkInfo($networkInfo, "address"),
"subnet" => mapNetworkInfo($networkInfo, "subnet"),
"gateway" => mapNetworkInfo($networkInfo, "gateway"),
);

if($returnJson) {
returnJson($networkInfo);
} else {
return $networkInfo;
}
}

public function stats($returnJson = true) {
$statsInfo = array(
"uptime" => $this->uptime(false),
"disk" => $this->disk(false),
"memory" => $this->memory(false),
"ports" => $this->ports(false),
);

if($returnJson) {
returnJson($statsInfo);
} else {
return $statsInfo;
}
}

public function uptime($returnJson = true) {
$uptimeInfo = readableTimeticks();

if($returnJson) {
returnJson($uptimeInfo);
} else {
return $uptimeInfo;
}
}

public function disk($returnJson = true) {
$diskInfo = array(
"total" => formatBytes(disk_total_space("/")),
"free" => formatBytes(disk_free_space("/")),
"used" => formatBytes((disk_total_space("/") - disk_free_space("/")) )
);

if($returnJson) {
returnJson($diskInfo);
} else {
return $diskInfo;
}
}

public function memory($returnJson = true) {
$memoryInfo = file_exists("/proc/meminfo") ? file("/proc/meminfo") : array(0,0);
$memoryInfo = array(
"total" => formatKiloBytes(intval(preg_replace("/[^0-9]/", "", $memoryInfo[0]))),
"free" => formatKiloBytes(intval(preg_replace("/[^0-9]/", "", $memoryInfo[1]))),
"used" => formatKiloBytes(intval(preg_replace("/[^0-9]/", "", $memoryInfo[0])) - intval(preg_replace("/[^0-9]/", "", $memoryInfo[1]))),
);

if($returnJson) {
returnJson($memoryInfo);
} else {
return $memoryInfo;
}
}

public function ports($returnJson = true) {
$portsInfo = array(
"ftp" => (is_resource(@fsockopen("127.0.0.1", 21)) && getservbyport(21, "tcp")=="ftp") ? true : false,
"sftp" => (is_resource(@fsockopen("127.0.0.1", 22)) && getservbyport(22, "tcp")=="ssh") ? true : false,
"telnet"=> (is_resource(@fsockopen("127.0.0.1", 23)) && getservbyport(23, "tcp")=="telnet") ? true : false,
"snmp" => (is_resource(@fsockopen("127.0.0.1", 161)) && getservbyport(161, "udp")=="snmp") ? true : false,
"mysql" => (is_resource(@fsockopen("127.0.0.1", 3306)) && getservbyport(3306, "tcp")=="mysql") ? true : false,
);

if($returnJson) {
returnJson($portsInfo);
} else {
return $portsInfo;
}
}

}

echo json_encode($linuxInfo);
$action = (isset($_GET["action"])) ? $_GET["action"] : "info";
$machineInfo = new MachineInfo();
$machineInfo->$action();

?>

0 comments on commit 026c276

Please sign in to comment.