0

I have been searching for a couple of days, and I know what needs to be done, but cannot figure out the exact code. I know it has to use preg_match in order for me to get exactly what I want, but I am having trouble with that.

I am trying to have a script show how much RAM is used on the system. I am using php exec. I use the command free -mt. Online, I did manage to find this code and I made a slight modification.

exec('free -mo', $out);
var_dump($out[1]);
list($mem, $used, $free, $shared, $buffers, $cached) = explode(" ", $out[1]);
echo "Memory: " .$used. "/" . $total;

What do I need to do if I just want to output the amount of RAM currently used?

The code ends of showing all RAM, but I need it to just show the total used amount of RAM. My output I get from that code is:

string(73) "Mem: 768 162 605 0 0 0" Memory: /

I would like it to just say the RAM being used, so, it should come out saying 162 during this exact moment, but it should be real data every time it is refreshed.

Jacob
  • 1
  • 1
  • What kind of output does `free -mo` give you? – Pekka May 08 '11 at 14:28
  • @Pekka - In PHP, I get: string(73) "Mem: 768 162 605 0 0 0" Memory: / from entering that. – Jacob May 08 '11 at 14:31
  • Looks okay. What result are you getting with the above code? – Pekka May 08 '11 at 14:35
  • @Pekka - Instead of getting those many details back, I am looking for one number to be outputted, the used amount of RAM. How would I do that? – Jacob May 08 '11 at 14:38
  • @Jacob the code you show should already do exactly that. `$used` should contain the amount of used memory. What happens instead if you run it? – Pekka May 08 '11 at 14:39
  • @Pekka - That is what I thought too. However, when I take away everything except $used everything still shows up. Any ideas? By the way, thanks for your help so far! – Jacob May 08 '11 at 14:46
  • No problem. Can you show what output you get at the moment exactly? – Pekka May 08 '11 at 14:47
  • @ Pekka When I keep the code the same and remove everything in the list part, except for $used I get the exact same output as if everything was in there. Isn't that odd? – Jacob May 08 '11 at 14:50
  • @ Pekka Actually it is a bit different, when it is just $used the output is: string(73) "Mem: 768 168 599 0 0 0" Memory: Mem:/ – Jacob May 08 '11 at 14:52

1 Answers1

3

The reason the above code is not working is because you're exploding on a space while the value contains many spaces in between the values you're trying to get. A better practice is to use a preg_match_all().

Try this instead:

exec('free -mo', $out);
preg_match_all('/\s+([0-9]+)/', $out[1], $matches);
list($total, $used, $free, $shared, $buffers, $cached) = $matches[1];
echo "Memory: " . $used . "/" . $total;
aminal
  • 105
  • 6
  • @ Mark Pitman - I do not know how to thank you enough! Do you offer development services? I would be interested in hiring you for a project. – Jacob May 08 '11 at 14:57
  • Not a problem. I do on occasion. Feel free to email me anytime. – aminal May 08 '11 at 14:59