LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2019, 12:27 PM   #1
r34per
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Rep: Reputation: Disabled
Bash script to Monitor RHEL server - Help Needed!


Hi everyone

I am a newbie to the Linux world so please bear with me...

I need to create a bash script to monitor a RHEL server, specifically to monitor the top 10 CPU process' (with usage and process information), the CPU load, the Memory usage, the Disk space and Network traffic monitoring. I then need to have this script running every second for 5 minutes per day and write the output to a log file in the /tmp directory in a format which is understandable by us regular humans, so something like a table with the date and time the reading was taken.

I have tried searching google and this forum, did not find anything that made much sense.

I have a very basic understanding of how to write scripts.

Does anyone have something like this that can help? or can someone point me in the right direction? Any help will be appreciated

Thanks

################
EDIT
################

Sorry should have posted more info - I have created this script so far:

Code:
#! /bin/bash

printf "Date and Time\t\tMemory\t\tDisk\t\tCPU\n"

end=$((SECONDS+180))

while [ $SECONDS -lt $end ]; do

MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')

DISK=$(df -h | awk '$NF=="/informix_backup"{printf "%s\t\t", $4}')

CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')

CPU2=$(ps auxf|sort -nr -k3 | head -5)

CPU3=$(ps -e -o pcpu,pid,user,args | sort -k1 -nr | head -5)

NOW=$(date +"%m-%d-%Y-%H:%M:%S")

echo "$NOW$MEMORY$DISK$CPU"

echo
printf "ate and Time\t\ttop 10 CPU usage processes with user\n"
echo
echo "$NOW$CPU2"

echo
printf "Date and Time\t\ttop 10 CPU consuming process\n"
echo
echo "$NOW$CPU3"

sleep 1

done
I have a few issues -

The output format comes in a messy blocks and cannot figure out how to make it a readable table
I need to incorporate some kind of network traffic monitoring command in there (so monitoring network traffic from and to a specific NIC)
And I need to log the output to a log file in the /tmp directory


Any help is appreciated

Last edited by r34per; 08-14-2019 at 10:42 AM.
 
Old 08-13-2019, 01:22 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
See what you can do with top...
Code:
man top
 
1 members found this post helpful.
Old 08-13-2019, 02:00 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,685

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by r34per View Post
Hi everyone
I am a newbie to the Linux world so please bear with me...

I need to create a bash script to monitor a RHEL server, specifically to monitor the top 10 CPU process' (with usage and process information), the CPU load, the Memory usage, the Disk space and Network traffic monitoring. I then need to have this script running every second for 5 minutes per day and write the output to a log file in the /tmp directory in a format which is understandable by us regular humans, so something like a table with the date and time the reading was taken.

I have tried searching google and this forum, did not find anything that made much sense. I have a very basic understanding of how to write scripts. Does anyone have something like this that can help? or can someone point me in the right direction? Any help will be appreciated Thanks
Really? There are many, MANY hits for "linux system monitoring scripts" (over 12 million)....which ones didn't make sense? First two hits have scripts with examples. You can easily modify them to write to whatever file/directory you want, in whatever format you'd like. The printf statement in bash can get you customized output, as can just using tabs or commas, for easy import into a spreadsheet. Sleep statements can get the one-second interval, a simple counter can tick up to the five-minute mark, and cron can run it whatever time of day you'd like.

So...what have you actually done/tried so far? Read the "Question Guidelines" link.
 
Old 08-13-2019, 06:52 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
that is defiantly a piece together project if you're going to google about it.


Quote:
need to create a bash script to monitor a RHEL server, specifically to monitor the top 10 CPU process' (with usage and process information), the CPU load, the Memory usage, the Disk space and Network traffic monitoring. I then need to have this script running every second for 5 minutes per day and write the output to a log file in the /tmp directory in a format which is understandable by us regular humans, so something like a table with the date and time the reading was taken.
1. How to monitor / get top 10 CPU process?
2. how to monitor / get memory usage?
3. how to get disk space?
4. how to check network traffic?
5. how to use job to run a script every 5 minutes?
6. how to output from a script into a file?

one step at a time, put it all in a wrapper called a script, with some testing until it works the way you want and you got yourself a finished project.

just for the heck of it
Code:
$ top -b -n 10 | awk 'FNR>6 {print $1" "$2" "$10" "$11" "$12}' > toppers ; cat toppers
PID USERNAME TIME WCPU COMMAND
45652 userx 0:47 3.71% mpv
67686 root 11:56 2.20% Xorg
77560 userx 8:26 1.17% firefox
78279 userx 4:27 0.49% gkrellm
66040 userx 0:44 0.39% qbittorrent
94845 userx 0:02 0.20% firefox
77717 userx 20:30 0.10% firefox
98081 userx 0:54 0.00% firefox
77744 userx 0:42 0.00% python3.6
11934 userx 0:37 0.00% firefox

Last edited by BW-userx; 08-13-2019 at 07:15 PM.
 
1 members found this post helpful.
Old 08-14-2019, 10:46 AM   #5
r34per
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Really? There are many, MANY hits for "linux system monitoring scripts" (over 12 million)....which ones didn't make sense? First two hits have scripts with examples. You can easily modify them to write to whatever file/directory you want, in whatever format you'd like. The printf statement in bash can get you customized output, as can just using tabs or commas, for easy import into a spreadsheet. Sleep statements can get the one-second interval, a simple counter can tick up to the five-minute mark, and cron can run it whatever time of day you'd like.

So...what have you actually done/tried so far? Read the "Question Guidelines" link.
Thanks for the response! I have edited the main post to show how far I have got. Also have listed the issues I have there.

I have searched Google but 99% of the time its asking me to install an external tool, which is something I cannot do. I can only work with what comes as part of the standard OS monitoring tools. There were a few pointers I was able to pick up which is where most of the above commands came from.
 
Old 08-14-2019, 10:48 AM   #6
r34per
LQ Newbie
 
Registered: Nov 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
that is defiantly a piece together project if you're going to google about it.



1. How to monitor / get top 10 CPU process?
2. how to monitor / get memory usage?
3. how to get disk space?
4. how to check network traffic?
5. how to use job to run a script every 5 minutes?
6. how to output from a script into a file?

one step at a time, put it all in a wrapper called a script, with some testing until it works the way you want and you got yourself a finished project.

just for the heck of it
Code:
$ top -b -n 10 | awk 'FNR>6 {print $1" "$2" "$10" "$11" "$12}' > toppers ; cat toppers
PID USERNAME TIME WCPU COMMAND
45652 userx 0:47 3.71% mpv
67686 root 11:56 2.20% Xorg
77560 userx 8:26 1.17% firefox
78279 userx 4:27 0.49% gkrellm
66040 userx 0:44 0.39% qbittorrent
94845 userx 0:02 0.20% firefox
77717 userx 20:30 0.10% firefox
98081 userx 0:54 0.00% firefox
77744 userx 0:42 0.00% python3.6
11934 userx 0:37 0.00% firefox
Thanks This was most helpful - I did a Google search and like you said tried to take it one step at a time. Got each individual command working then tried to tie it all together. Im still having issues though - I have updated the main post. Any pointers will be appreciated
 
Old 08-14-2019, 12:20 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you might want to work on your formatting
https://linuxconfig.org/bash-printf-...-with-examples
should give you some good examples ...
 
  


Reply

Tags
bash, cpu, rhel, script



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
Rhel 7.3 lsblk showing output rhel-pool00_tmeta rhel-pool00_tdata Pravingoud89 Red Hat 7 12-27-2018 10:52 AM
what is a main difference between rhel 6 and rhel 7 accourding to rhel exam bikesh Linux - Newbie 6 12-05-2014 12:06 AM
Upgrade to RHEL 5.3 from RHEL 4.4 only using the kernel rpm of RHEL 5.3 rhystech Red Hat 4 05-28-2012 10:01 PM
RHEL 3 ES vs RHEL 4 ES vs RHEL 5 ES sweetcorn Linux - Enterprise 6 11-03-2008 01:27 PM
[SOLVED] Shell script to monitor or watch the disk space.... small help needed vikas027 Programming 5 10-21-2007 10:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:49 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