RE: Hardware Detection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This works on Linux/Unix boxen. On windows... You're on your own.


//generate the global array here so we can re-use it independant of output
format
$device['os_ver'] = exec("/bin/uname -a");

$temp = preg_split("/\s+/",exec("/sbin/ifconfig -a eth0 | /bin/grep
HWaddr"), -1, PREG_SPLIT_NO_EMPTY);
$device['MAC_addr_eth0'] = $temp[4];

$temp = preg_split("/[\s:]+/",exec("/sbin/ifconfig -a eth0 | /bin/grep
\"inet addr:\""), -1, PREG_SPLIT_NO_EMPTY);
$device['dot_quad_ip_eth0'] = $temp[2];

$temp = preg_split("/\s+/",exec("/sbin/ifconfig -a eth1 2>&1 | /bin/grep
HWaddr"), -1, PREG_SPLIT_NO_EMPTY);
$device['MAC_addr_eth1'] = $temp[4];

$temp = preg_split("/[\s:]+/",exec("/sbin/ifconfig -a eth1 2>&1 | /bin/grep
\"inet addr:\""), -1, PREG_SPLIT_NO_EMPTY);
$device['dot_quad_ip_eth1'] = $temp[2];

# look at /var for now as that seems to be where the bulk of our data is
stored.
$temp = preg_split("/\s+/",exec("/bin/df | /bin/grep hda1"), -1,
PREG_SPLIT_NO_EMPTY);
$device['hd_size'] =  $temp[1];
$device['hd_used'] =  $temp[2];

$temp = preg_split("/\s+/",exec("/usr/bin/free | /bin/grep Mem"), -1,
PREG_SPLIT_NO_EMPTY);
$device['ram_total'] = $temp[1];
$device['ram_used'] = $temp[2];

$temp = preg_split("/:/",exec("/bin/cat /proc/cpuinfo | /bin/grep 'model
name'"), -1, PREG_SPLIT_NO_EMPTY);
$temp1 = preg_split("/\s\s/",ltrim($temp[1]), -1, PREG_SPLIT_NO_EMPTY);
$device['cpu_type'] = $temp1[0];
$temp = preg_split("/:/",exec("/bin/cat /proc/cpuinfo | /bin/grep 'cpu
MHz'"), -1, PREG_SPLIT_NO_EMPTY);
$device['cpu_mhz'] = ltrim($temp[1]);

$temp = preg_split("/:/",substr(exec("/usr/bin/lspci | grep 'VGA'"), 8), -1,
PREG_SPLIT_NO_EMPTY);
$device['video'] = $temp[1];

$temp = preg_split("/:/",substr(exec("/usr/bin/lspci | grep 'Host'"), 8),
-1, PREG_SPLIT_NO_EMPTY);
$device['mobo'] = $temp[1];

$ethernet = `/usr/bin/lspci | grep 'Ethernet'`;
$temp = split("\n",$ethernet);
$temp1 = preg_split("/:/",substr($temp[0],8), -1, PREG_SPLIT_NO_EMPTY);
$device['nic_eth0'] = substr($temp1[1],1);
$temp1 = preg_split("/:/",substr($temp[1],8), -1, PREG_SPLIT_NO_EMPTY);
$device['nic_eth1'] = substr($temp1[1],1);

$device['proc_count'] = trim(`ps auxw | wc -l`);

$device['someprocessd'] = trim(`ps axuw | grep "someprocessd" | grep -v
"grep" | wc -l`);

# Returns the uptime of a Linux system by parsing through /proc/uptime. 
# It returns a 4-field array (days, hours, minutes, seconds). 
# I typically use it like: 
# $ut = linuxUptime(); 
# echo "Time since last reboot: $ut[0] days, $ut[1] hours, $ut[2] minutes"; 
$ut = strtok( exec(  "cat /proc/uptime" ),  "." ); 
$days = sprintf(  "%d", ($ut/(3600*24)) ); 
$hours = sprintf(  "%2d", ( ($ut % (3600*24)) / 3600) ); 
$min = sprintf(  "%2d", ($ut % (3600*24) % 3600)/60  ); 
$sec = sprintf(  "%2d", ($ut % (3600*24) % 3600)%60  ); 
$device['uptime'] = $days."d ".$hours."h ".$min."m ".$sec."s";

//this version fails if the uptime is < 24 hours or 1 day since it's shown
as 20:15
//$temp = preg_split("/:/",exec("uptime"), -1, PREG_SPLIT_NO_EMPTY);
//$device['loadavg'] = substr($temp[2],1);
preg_match("/load average: (.*)/",exec("uptime"), $temp);
$device['loadavg'] = $temp[1];

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux