LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-02-2017, 08:36 AM   #1
bala1989murugan
LQ Newbie
 
Registered: Sep 2016
Posts: 16

Rep: Reputation: Disabled
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.
 
Old 03-02-2017, 08:39 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Want to add quotes around your command:

Code:
ssh $bala "df -PhT | awk '{print $3,$4,$5,$7}'"
 
Old 03-02-2017, 08:46 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
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.
 
Old 03-02-2017, 03:07 PM   #4
bala1989murugan
LQ Newbie
 
Registered: Sep 2016
Posts: 16

Original Poster
Rep: Reputation: Disabled
It's getting error.
 
Old 03-02-2017, 03:17 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Show error.
 
Old 03-03-2017, 02:28 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Quote:
Originally Posted by bala1989murugan View Post
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.
 
Old 03-04-2017, 05:59 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
 
Old 03-04-2017, 08:37 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,779

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
  


Reply

Tags
shell script, shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for CPU usage, memory usage, disk partition space and service status reetesh.amity Linux - Server 6 10-12-2015 07:51 PM
Squid 3 near 100% cpu usage and high RAM usage piman Linux - Software 1 11-16-2013 02:20 AM
Optimize RAM and CPU usage cccc Debian 11 09-17-2010 01:12 AM
CPU and RAM usage rslinks Linux - Server 3 10-06-2008 01:39 PM
ram/cpu usage etc... program Ken~ Linux - Software 2 08-28-2007 05:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration