LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   shell script to get CPU, RAM usage report (https://www.linuxquestions.org/questions/linux-server-73/shell-script-to-get-cpu-ram-usage-report-4175600913/)

bala1989murugan 03-02-2017 08:36 AM

shell script to get CPU, RAM usage report
 
Dear Linux lovers,
I need to get CPU, RAM and Disk usage report.

I have executed below mentioned script from my test server.
It's logging in to other server. but its providing test server details. Unable to capture other servers details.

I have stored all servers hostname in test file

for bala in $(cat /tmp/test)
do
echo "###############$bala###################"
ssh $bala df -PhT | awk '{print $3,$4,$5,$7}'
echo "*****************************"
top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4,$5}'
echo "*************************"
egrep 'processor|core id' /proc/cpuinfo
echo "***************"
free -m
echo "****************************"
done >>/tmp/checklist3.txt


Kindly correct me .
Or else provide another bash script to get these details.


Thanks Team.

szboardstretcher 03-02-2017 08:39 AM

Want to add quotes around your command:

Code:

ssh $bala "df -PhT | awk '{print $3,$4,$5,$7}'"

Turbocapitalist 03-02-2017 08:46 AM

It will be easier to read when you wrap your script in [code] [/code] tags.

If you want all of those programs to run on the remote machine, you'll need to actually run them on the remote machine not the local host.

Code:

ssh $bala "df -PhT | awk '{print $3,$4,$5,$7}'; top -b -n1 | awk '/Cpu(s)/ {print $2 + $4,$5}'; echo '*************************'; egrep 'processor|core id' /proc/cpuinfo; echo '***************'; free -m; echo '****************************';"
done >>/tmp/checklist3.txt

See the manual page for ssh for a little more but, if nothing else, notice the quotes above.

bala1989murugan 03-02-2017 03:07 PM

It's getting error.

szboardstretcher 03-02-2017 03:17 PM

Show error.

Turbocapitalist 03-03-2017 02:28 AM

Quote:

Originally Posted by bala1989murugan (Post 5678213)
It's getting error.

Yes, it would help to show the error, but looking at the line in the previous post I wrote, I had not escaped the awk variables. The whole line is within double quotes and thus any dollar sign needs to be escaped:

\$2, \$3, \$4

etc.

It is important to note the quotes used and how they are or aren't nested. You'll need to read a bit about quoting as well. Items in single quotes are taken literally by the shell. Items within double quotes are interpreted by the shell, so items like variables must be escaped if you wish to prevent the shell from interpreting them.

ondoho 03-04-2017 05:59 AM

on a side note (it's what came to mind when i saw the thread title) - it's possible to use conky with CLI only.

MadeInGermany 03-04-2017 08:37 AM

Passing a script as argument bears trouble because of the local AND remote evaluation.
It is much easier to have a local checkhost.sh
Code:

#!/bin/bash
df -PhT | awk '{print $3,$4,$5,$7}'
echo "*****************************"
top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4,$5}'
echo "*************************"
egrep 'processor|core id' /proc/cpuinfo
echo "***************"
free -m
echo "****************************"

And a loop scrip checkallhosts.sh
Code:

#!/bin/sh
for bala in $(cat /tmp/test)
do
echo "###############$bala###################"
ssh -x "$bala" /bin/sh < checkhost.sh
done >>/tmp/checklist3.txt

The loop script opens the checkhost.sh as a stream and passes it via the ssh to the /bin/sh on the remote host.


All times are GMT -5. The time now is 01:36 PM.