Jacob
Jacob

Reputation: 41

PHP Script - Get Server Processor

I have done a couple hours of research and have found a solution, however I would prefer a solution without using the exec command.

Currently I have this code:

exec ("cat /proc/cpuinfo", $details);
$string= $details[4];

I basically would like it to pull the first processor's type, like that code does, but without using the system or exec command.

I would prefer to read directly from /proc/cpuinfo.

Upvotes: 4

Views: 9105

Answers (5)

Sid
Sid

Reputation: 4512

Just put the code below:

<pre>
<strong>Uptime:</strong>
<?php system("uptime"); ?>
<br />
<strong>System Information:</strong>
<?php system("uname -a"); ?>
<br />
<strong>Memory Usage (MB):</strong>
<?php system("free -m"); ?>
<br />
<strong>Disk Usage:</strong>
<?php system("df -h"); ?>
<br />
<strong>CPU Information:</strong>
<?php system("cat /proc/cpuinfo | grep \"model name\\|processor\""); ?>
</pre>

And you will get something like : enter image description here

Upvotes: 1

&#246;mer &#246;zer
&#246;mer &#246;zer

Reputation: 1

$cpuget = file('/proc/cpuinfo');
$cpu["name"]    =   str_replace("           ", " ",substr(str_replace("model name   : ", "", $cpuget[4]),0,-1));
$cpu["cores"]   =   substr(str_replace("cpu cores   : ", "", $cpuget[12]),0,-1);
$cpu["cache"]   =   substr(str_replace("cache size  : ", "", $cpuget[8]),0,-1);

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60017

Use file_get_contents() instead of the cat.

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237995

All you're doing is getting information from a file. This can be done with native PHP functions. The simplest way of doing this (and the most similar to your current solution) is with file:

$file = file('/proc/cpuinfo');
$proc_details = $file[4];

Upvotes: 7

shiying yu
shiying yu

Reputation: 103

  1. That kind of operation is usually not welcome by server administrators because of security reason,so they might disable some functions like exec/system/popen, etc.
  2. using /proc/cpuinfo is not suitable for all operating systems, FreeBSD (for example) won't mount procfs by default.

RECOMMENDATION: use file_get_contents() function is good, but you'd check php_open_basedir restriction and file existence.

Upvotes: 0

Related Questions