PDA

View Full Version : Script for Memory calculation



oj43085
07-Feb-2013, 04:50
Hi,

Admins, forgive me if I am posting a scripting question here, I think this is the most relevant sub-category I found.

I would like to know if there is a way to script:

1)ssh to server IP

2)free -m > file

3) Perform this operation: [(used buffer/cached)/(memory used)+(memory available)]*100

4) dump output to $serverip_free.log

5) end ssh

6) repeat for next server

Any help would be greatly appreciated.

malcolmlewis
07-Feb-2013, 05:27
On Thu 07 Feb 2013 03:54:02 AM CST, oj43085 wrote:


Hi,

Admins, forgive me if I am posting a scripting question here, I think
this is the most relevant sub-category I found.

I would like to know if there is a way to script:

1)ssh to server IP

2)free -m > file

3) Perform this operation: [(used buffer/cached)/(memory used)+(memory
available)]*100

4) dump output to $serverip_free.log

5) end ssh

6) repeat for next server

Any help would be greatly appreciated.




Hi
Do you have to use ssh? The nagios webserver application and the nagios
plugin client and scripts will do this for use.

You could use expect or a perl script with Net::ssh;
http://search.cpan.org/~ivan/Net-SSH-0.09/SSH.pm

--
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 14:37, 3 users, load average: 0.01, 0.03, 0.05
CPU Intel® i5 CPU M520@2.40GHz | GPU Intel® Ironlake Mobile

jmozdzen
07-Feb-2013, 13:10
Hi oj43085,

yes, that is possible. You can run any scripted code on the remote server by giving that command list to the ssh client, something along the lines

for i in $hostlist ; do
ssh $i "my cmd1; mycmd2;..."
done

In order not to have to type in each remote's password, setting up an appropriate public key environment ("authorized_keys") would be really helpful. And of course you can decide which remote user to contact etc.

Just for completeness: In case you'd be starting remote jobs as background tasks, it's advisable to locally detach stdin ("ssh -n") and to properly redirect the output of the remote process at the remote machine. A "hanging" ssh session (your remote script has completed, but the session will not terminate) is a typical indicator for such a situation.

As you have not indicated your scripting skills, I assume your question was actually only concerning the "ssh" remote invocation part.

And like Malcolm already pointed out: If that's some regular task, in terms of continuous monitoring, there are far better tools to do this, Nagios being near or at the top of such a list.

Regards,
Jens