Script for Memory calculation


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

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

  2. dump output to $serverip_free.log

  3. end ssh

  4. repeat for next server

Any help would be greatly appreciated.

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;

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

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;…”

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.