Linux: getting the "load" values without using "top"

I would like to get some of the information provided by the “top”
command into my ruby program, in particular the “load” values. The only
way I can think of is to use popen3 to run “top -n 1” and parse the
output, but I would like to not have to call an external command and do
it using pure ruby.

On Tue, Apr 1, 2008 at 8:49 AM, James D. [email protected] wrote:

I would like to get some of the information provided by the “top”
command into my ruby program, in particular the “load” values. The only
way I can think of is to use popen3 to run “top -n 1” and parse the
output, but I would like to not have to call an external command and do
it using pure ruby.

look at the contents of /proc/loadavg

James D. wrote:

I would like to get some of the information provided by the “top”
command into my ruby program, in particular the “load” values.

Well, it’s in /proc/loadavg no ? Juste read this file.

Guy Decoux

Oh, thanks, I did not realize it was kept in a file.

I’m wanting this because this script currently blows up the server if
run when the load is too high. I’ll just have the script sleep for 5
minutes until the load is under a certain threshold.

pat eyler wrote:


Posted via http://www.ruby-forum.com/.

Yeah … that will work. But I’m curious why the OP’s application cares
about the load averages. My experience has been that there isn’t a lot
to be gained from it. “Real” load balancing is a bit more complicated,
if that’s what he’s trying to do.

Robert D. wrote:

Sorry for being negative, but what will happen if the load goes up
again while your script is running?

I’m pretty sure the server will blow up.

Maybe you just cannot run this script safely on that server.
Just curious what that script does, maybe you can run it on a nice
level that will allow the server to survive, seems
much a saver approach to me.

I don’t know what “a nice level” is, but I will look into it.

Thanks,
James

On Tue, Apr 1, 2008 at 5:08 PM, James D. [email protected] wrote:

Oh, thanks, I did not realize it was kept in a file.

I’m wanting this because this script currently blows up the server if
run when the load is too high. I’ll just have the script sleep for 5
minutes until the load is under a certain threshold.
Sorry for being negative, but what will happen if the load goes up
again while your script is running?
Maybe you just cannot run this script safely on that server.
Just curious what that script does, maybe you can run it on a nice
level that will allow the server to survive, seems
much a saver approach to me.
Cheers
Robert

Posted via http://www.ruby-forum.com/.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

2008/4/1, James D. [email protected]:

Oh, thanks, I did not realize it was kept in a file.

It’s not exactly a file. But it is mounted in a special file system
and can be read like a file or device.

I’m wanting this because this script currently blows up the server if
run when the load is too high. I’ll just have the script sleep for 5
minutes until the load is under a certain threshold.

That’s what process priorities are for. You can find out via “man
nice”. Maybe there’s even a Ruby library method that allows to lower
the priority of the current process.

Kind regards

robert

On Tue, Apr 1, 2008 at 5:20 PM, James D. [email protected] wrote:

level that will allow the server to survive, seems
much a saver approach to me.

I don’t know what “a nice level” is, but I will look into it.
It simply runs your program with less or more priority, but I guess
you can type “man nice” as you have
indicated above.
Good luck.
R.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Tue, Apr 01, 2008, James D. wrote:

I would like to get some of the information provided by the “top”
command into my ruby program, in particular the “load” values. The only
way I can think of is to use popen3 to run “top -n 1” and parse the
output, but I would like to not have to call an external command and do
it using pure ruby.

The uptime command will report it, if for whatever reason you don’t have
or can’t use the /proc option.

Ben

On Tue, Apr 1, 2008 at 8:49 AM, James D. [email protected] wrote:

I would like to get some of the information provided by the “top”
command into my ruby program, in particular the “load” values. The only
way I can think of is to use popen3 to run “top -n 1” and parse the
output, but I would like to not have to call an external command and do
it using pure ruby.

look at the contents of /proc/loadavg

Ruby Geek! :wink:

–Stafford (Very very newbie wanna-be Ruby Geek)

Robert D. wrote:

On Tue, Apr 1, 2008 at 5:08 PM, James D. [email protected] wrote:

Oh, thanks, I did not realize it was kept in a file.

I’m wanting this because this script currently blows up the server if
run when the load is too high. I’ll just have the script sleep for 5
minutes until the load is under a certain threshold.
Sorry for being negative, but what will happen if the load goes up
again while your script is running?
Maybe you just cannot run this script safely on that server.
Just curious what that script does, maybe you can run it on a nice
level that will allow the server to survive, seems
much a saver approach to me.
Cheers
Robert

Posted via http://www.ruby-forum.com/.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

A secure aproach can be SNMP*
You can use it from the localhost or somewhere else

http://snmplib.rubyforge.org/

  • What is SNMP?

SNMP is the Simple Network Management Protocol. This protocol provides
the capability to monitor and manage switches, routers, printers,
desktops, and other equipment in your network.