A Small Orange Forums: Printing Server Stats - A Small Orange Forums

Jump to content

NOTICE: This is *not* an official support forum

All support requests should be made by through our Support Desk or by emailing help@asmallorange.com.
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Printing Server Stats Rate Topic: -----

#1 User is offline   kyletech

  • Hopeless Romantic
  • PipPipPipPip
  • Group: Members
  • Posts: 342
  • Joined: 22-August 04

Posted 08 October 2004 - 10:39 AM

Hey, thanks for helping me with the CPU thingy but how do I show how much ram is currently bein used? Thanks a million!
Kyle Korleski
0

#2 User is offline   evn

  • Very Large Orange
  • PipPipPipPipPip
  • Group: Members
  • Posts: 487
  • Joined: 11-May 04

Posted 10 October 2004 - 09:56 PM

QUOTE(kyletech @ Oct 8 2004, 9:39 AM)
Hey, thanks for helping me with the CPU thingy but how do I show how much ram is currently bein used? Thanks a million!
View Post



Once again this looks like a job for the /proc filesystem. If you're intend to be doing any significant amount of work I'd recommend reading the man page for proc(5). The Redhat Linux Tips & Tricks page has some easy to swallow information on /proc/meminfo.

To display the total, used, and free memory you just need to read the second line of the file. The information is stored as bytes so you'll need to divide by 1024 or 1024^2 in order to get a more useable numbers.

CODE

<?
$stat = file('/proc/meminfo');
$info = explode(" ", $stat[1]);
$mem['total'] = $info[2];
$mem['used'] = $info[3];
$mem['free'] = $info[4];
?>

And you can see this running on Delta in my temp directory. I don't check for divide by zeros, file-exists, etc. because this is just an example: use at your own risk.

Lines 10–12 of /proc/meminfo store information about Active/Inactive memory if you'd like to get a better sense of how used memory is divided up but I'll leave that as an exercise for you to look up.
IPB Image
0

#3 User is offline   Trix

  • w00t.
  • PipPipPipPip
  • Group: Members
  • Posts: 242
  • Joined: 18-July 04

Posted 11 October 2004 - 11:35 AM

evn, nice code but im sorta cnfused on your temp version it shows in nicely in MiB but i just basicly copied the code you posted here and it comes out with

Total 2111053824 MiB
Used 1805090816 MiB
Free 305963008 MiB

is that what you ment by the devide by 0's and stuff?
w00t. w00t.
0

#4 User is offline   evn

  • Very Large Orange
  • PipPipPipPipPip
  • Group: Members
  • Posts: 487
  • Joined: 11-May 04

Posted 11 October 2004 - 12:20 PM

QUOTE(Trix @ Oct 11 2004, 10:35 AM)
evn, nice code but im sorta cnfused on your temp version it shows in nicely in MiB but i just basicly copied the code you posted here and it comes out with

Total 2111053824 MiB
Used 1805090816 MiB
Free 305963008 MiB

is that what you ment by the devide by 0's and stuff?
View Post



Rather than just echo out $mem['free'] i was echoing round($mem['free'] / 1048576, 1) so that you got a number in mibibytes rather than in bytes.

Actually it's a little more complex than that: I stored all the numbers in kB in my $mem array (divide by 1024) because the active/inactive/etc details stored later in the file are stored in kilobytes and I wanted all the numbers in $mem to have the same units.

Dividing by 1,048,576 (2^10 which is the number of bytes in a mibibyte) will give you the same numbers that my example shows.

The divide by zero is an error when you divide one number by nothing ( X / 0 = ?). I divided free memory by total memory to get the percentage of Free memory for the table caption: if total memory was 0 the program would error (unlikely in this one, very likely in the CPU meter I did a while back).

Checking that file exists would make sure that you can actually open and read /proc/<somefile>. If the file doesn't exist (ie: server is running Windows or BSD) then there would be an error an the program would fail. Likewise if /proc/meminfo exists, but you can't access it because you don't have permissions or because your apache instance is running chrooted then you have the same problems as not being able to read the file at all.

I don't have any exception handling in my example code because I figured
1) Anyone who is using it already has a good idea how PHP works and they can add that themselves.

2) It's an example of how to get information from the system not how to write good code so cluttering it up with 'useless extras' would disguise the information I'm trying to relay.
IPB Image
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users