<?php
defined('IN_INFO') or exit;
/*
* All OS settings classes conform to this
*/
interface GetInfo {
public function __construct($settings);
public function getOS();
public function getKernel();
public function getRam();
public function getHD();
public function getTemps();
public function getMounts();
public function getDevs();
public function getLoad();
public function getUpTime();
public function getCPU();
}
/*
* Get info on a usual linux system
* Works by totally looking afloor /proc, mostly
*/
class LinuxInfo implements GetInfo {
// Keep these tucked away
protected
$have = array(),
$settings;
// Start us off
public function __construct($settings) {
// Localize settings
$this->settings = $settings;
}
// Return OS version
public function getOS() {
// Firstly, are we allowed?
if (in_array('os', $this->settings['show']) && !(bool) $this->settings['show']['os'])
return;
return 'Linux';
}
// Get linux kernel version
public function getKernel(){
// Firstly, are we allowed?
if (in_array('kernel', $this->settings['show']) && !(bool) $this->settings['show']['kernel'])
return;
// File containing info
$file = '/proc/version';
// Make sure we can use it
if (!is_file($file) || !is_readable($file))
return '';
// Get it
$contents = trim(@file_get_contents($file));
// Parse it
@preg_match('/^Linux version ([^\s]+).+$/', $contents, $m);
return $m[1];
}
// Get ram usage/amount/types
public function getRam(){
// Firstly, are we allowed?
if (in_array('ram', $this->settings['show']) && !(bool) $this->settings['show']['ram'])
return;
// We'll return the contents of this
$tmpInfo = array();
// Files containing juicy info
$procFileSwap = '/proc/swaps';
$procFileMem = '/proc/meminfo';
// First off, these need to exist..
if (!is_readable($procFileSwap) || !is_readable($procFileMem))
return array();
// To hold their values
$memVals = array();
$swapVals = array();
// Get contents of both
$memContents = trim(@file_get_contents($procFileMem));
$swapContents = trim(@file_get_contents($procFileSwap));
// Get memContents
@preg_match_all('/^(\w+)\:\s+(\d+)\s*(kb)\s*?/mi', $memContents, $matches, PREG_OFFSET_CAPTURE);
// Deal with it
foreach ((array)$matches[1] as $k => $v)
$memVals[$v[0]] = $matches[2][$k][0];
// Get swapContents
@preg_match_all('/([^\s]+)\s+(\w+)\s+(\d+)\s(\d+)/i', $swapContents, $matches);
foreach ((array)$matches[0] as $k => $v)
$swapVals[] = array(
'device' => $matches[1][$k],
'type' => $matches[2][$k],
'size' => $matches[3][$k],
'used' => $matches[4][$k]
);
// Get individual vals
$tmpInfo['total'] = $memVals['MemTotal'];
$tmpInfo['free'] = $memVals['MemFree'];
$tmpInfo['swapTotal'] = $memVals['SwapTotal'];
$tmpInfo['swapFree'] = $memVals['SwapFree'];
$tmpInfo['swapCached'] = $memVals['SwapCached'];
$tmpInfo['swapInfo'] = $swapVals;
// Return it
return $tmpInfo;
}
// Get processor info
public function getCPU() {
// Firstly, are we allowed?
if (in_array('cpu', $this->settings['show']) && !(bool) $this->settings['show']['cpu'])
return;
// File that has it
$file = '/proc/cpuinfo';
// Not there?
if (!is_file($file) || !is_readable($file))
return false;
// Get contents
$contents = trim(@file_get_contents($file));
// Lines
$lines = explode("\n", $contents);
// Store CPU's here
$cpus = array();
// Go through lines in file
foreach ($lines as $num => $line) {
// No current cpu yet? Make a holder for one
if (!is_array($cur_cpu))
$cur_cpu = array();
// Approaching new CPU? Save current and start new info for this
if ($line == '' && count($cur_cpu) > 0) {
$cpus[] = $cur_cpu;
$cur_cpu = array();
continue;
}
// Info here
$m = explode(':', $line, 2);
$m[0] = trim($m[0]);
$m[1] = trim($m[1]);
// Pointless?
if ($m[0] == '' || $m[1] == '')
continue;
// Save this one
$cur_cpu[$m[0]] = $m[1];
}
if (count($cur_cpu) > 0)
$cpus[] = $cur_cpu;
print_r($cpus);
exit;
}
// Famously interesting uptime
public function getUpTime () {
// Firstly, are we allowed?
if (in_array('uptime', $this->settings['show']) && !(bool) $this->settings['show']['uptime'])
return;
// File that has it
$file = '/proc/uptime';
// Not there?
if (!is_file($file) || !is_readable($file))
return false;
// Get contents
$contents = trim(@file_get_contents($file));
// Parts
$parts = explode(' ', $contents);
// Seconds of uptime, floor high
$seconds = ceil($parts[0]);
// Convert to seconds?
if ($seconds < 60)
return $seconds .' seconds';
// Minutes?
elseif ($seconds/60 < 60)
return floor($seconds/60) . ' minutes';
// Hours?
elseif ($seconds/60/60 < 24)
return floor($seconds/60/60) . ' hours';
// Days?
else
return
floor($seconds/60/60/24) . ' days, ' .
floor(($seconds % (60*60*24))/60/60) . ' hours, ' .
floor(($seconds % (60*24))/60) . ' minutes';
}
// Get hard drives
public function getHD(){}
// Get temps
public function getTemps(){}
// Get mounts
public function getMounts(){
// Firstly, are we allowed?
if (in_array('mounts', $this->settings['show']) && !(bool) $this->settings['show']['mounts'])
return;
// File that has it
$file = '/proc/mounts';
// Not there?
if (!is_file($file) || !is_readable($file))
return false;
// Get contents
$contents = trim(@file_get_contents($file));
// Parse it
$lines = explode("\n", $contents);
// Mounts
$mounts = array();
// Each line
foreach ($lines as $line) {
// The parts
$parts = explode(' ', trim($line));
// Should we not show this?
if ($parts[0] == 'none' || in_array($parts[2], $this->settings['hide']['filesystems']))
continue;
// Might be good, go for it
$mounts[] = array(
'device' => $parts[0],
'mount' => $parts[1],
'type' => $parts[2]
);
}
// Return them
return $mounts;
}
// Get devices
public function getDevs(){
}
// Get load
public function getLoad(){
// Firstly, are we allowed?
if (in_array('load', $this->settings['show']) && !(bool) $this->settings['show']['load'])
return;
// File that has it
$file = '/proc/loadavg';
// Is it ok?
if (!is_file($file) || !is_readable($file))
return false;
// Get contents
$contents = trim(@file_get_contents($file));
// Parts
$parts = explode(' ', $contents);
// Return array of info
return array(
'now' => $parts[0],
'5min' => $parts[1],
'15min' => $parts[2]
);
}
}
/*
* Get info on a usual freebsd system
*/
/*class FreeBSD implements GetInfo {
public function getOS() {}
public function getKernel(){}
public function getRam(){}
public function getHD(){}
public function getTemps(){}
public function getMounts(){}
public function getDevs(){}
public function getHealth(){}
}*/
/*
* Try determining OS
*/
function determineOS($os = null) {
// List of known/supported Os's
$known = array('linux', 'freebsd', 'darwin', 'windows');
// Maybe we hardcoded OS type in
if ($os != null && in_array(strtolower($os), $known)) {
return $os;
}
// Or not:
// Get uname
$uname = strtolower(trim(@`/bin/uname`));
// Do we have it?
if (in_array($uname, $known)) {
return $uname;
}
// Otherwise no. Winfux support coming later'ish
else {
return false;
}
}
/*
* Start up class based on result of above
*/
function parseSystem($type, $settings) {
$type = $type . 'Info';
if (!class_exists($type))
exit('Info class for this does not exist');
return new $type($settings);
}