[SOLVED] Bash script to Monitor RHEL server - Help Needed!
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.