4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jacob
  • 41
  • 1
  • 2
  • 2
    I guess you could try a `file_get_contents("/proc/cpuinfo")`, but I have no idea whether that would work... Chances are it wouldn't, but try it out. Why can't you use `exec`? – Pekka Sep 11 '11 at 13:35
  • http://stackoverflow.com/questions/4705759/how-to-get-cpu-usage-and-ram-usage-without-exec – Marek Sebera Sep 11 '11 at 13:37
  • @Pekka `exec()` has to be used with caution, but _only for user-generated input_. The OP might be worried about this, but for solely server side stuff, `exec()` is fine. – Bojangles Sep 11 '11 at 13:37
  • I think I probably could read using a code such as: $file = "/proc/loadavg"; $handle = fopen($file, "r"); however, how would I just read the processor part? – Jacob Sep 11 '11 at 13:38
  • @Pekka `exec` is *very* slow when there's a simple alternative. A simple benchmark suggests the difference is something like a factor of 50... – lonesomeday Sep 11 '11 at 13:41

5 Answers5

7

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];
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • Good start!! I get this output: `model name : Intel(R) Core(TM)2 Quad CPU Q8300 @ 2.50GHz` . How should I remove the "model name :" part? – Jacob Sep 11 '11 at 13:42
  • 1
    @Jacob Remove the first 13 characters: `$proc_details = substr($proc_details, 13);` – lonesomeday Sep 11 '11 at 13:44
1

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

Sid
  • 4,302
  • 3
  • 26
  • 27
0
$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);
0

Use file_get_contents() instead of the cat.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0
  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.

shiying yu
  • 93
  • 7