Top
“top” is versatile monitoring tool, providing run time summary view of many stats (CPU/Memory/Swap/Process/Load average etc), so admins prefer it a lot while troubleshooting several issues. It is provided by procps-ng-3.3.10-23.el7.x86_64 package, so you can install it as that well, if you feel missing, usually it is shipped by default in Fedora variant *nix servers (Red Hat / CentOS).
What if you want to build your own top command, for some further customization or in case it is not available. Please read further for steps for that. Feel free to update your customization as per your choice in your environment.
Understanding top output
In order to create our top, we need to first understand its functionality, so that we are as closer as possible.
It’s divided in two parts. The first stanza displaces following information:
- System uptime, load average, logged in users
- Process summary
- CPU statistics
- Memory statistics
- Swap statistics
Next stanza deals with process details and their consumption
Building your top step By step:
For writing 1st line, we can use below CLI excerpt:
Further 2nd line need some work, some knowledge of “ps”,”awk” & “grep” command:
Now proceeding towards our CPU%, this can be easily achieved through sar command.
Now coming towards memory/swap stats, try your free command as below
After 1st stanza is completed, let’s move to second stanza for process information with this small excerpt
Since we have completed almost all steps, let’s proceed to compile all the steps and print top replica.
So running below code in all on shell will show some great “top like” output.
[root@tower ~]# clear ;echo "top - `uptime`"; echo "Tasks: `ps -ef | egrep -v 'UID|-ef'| wc -l` total, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^R | wc -l` running, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^S* | wc -l` sleeping, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep '^T|^t' | wc -l` stopped, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep ^Z | wc -l` zombie"; echo "%Cpu(s): `sar 1 1 | egrep -v 'user|Average|Linux|^$' | awk '{print $4 " us, " $6 " sy, " $5 " ni, " $9 " id, " $7 " wa, " $8 " st" }'`"; echo "Kib Mem : `free | grep -v total | head -1 | awk '{ print $2 " total, " $4 " free, " $3 " used, " $6 " buff/cache"}'`";echo "Kib Swap: `free | grep -v total | tail -1 | awk '{ print $2 " total, " $4 " free, " $3 " used. "}'` `free | grep -v total | head -1 | awk '{ print $7 " avail Mem"}'`"; echo ; ps -eo pid,user,pri,ni,vsz,rss,state,%cpu,%mem,cputime,comm | sort -rk8 | head -20
Icing the cake
Similar to a cake, no code is good unless you can present it good. Let’s write a small bash script and then try to execute the code from there.
[root@tower ~]# cat mytop.sh #!/bin/bash clear echo "********************** Welcome to my custom top iteration $1 **************************" echo echo "top - `uptime`"; echo "Tasks: `ps -ef | egrep -v 'UID|-ef'| wc -l` total, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^R | wc -l` running, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^S* | wc -l` sleeping, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep '^T|^t' | wc -l` stopped, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep ^Z | wc -l` zombie" echo "%Cpu(s): `sar 1 1 | egrep -v 'user|Average|Linux|^$' | awk '{print $4 " us, " $6 " sy, " $5 " ni, " $9 " id, " $7 " wa, " $8 " st" }'`" echo "Kib Mem : `free | grep -v total | head -1 | awk '{ print $2 " total, " $4 " free, " $3 " used, " $6 " buff/cache"}'`" echo "Kib Swap: `free | grep -v total | tail -1 | awk '{ print $2 " total, " $4 " free, " $3 " used. "}'` `free | grep -v total | head -1 | awk '{ print $7 " avail Mem"}'`" echo ps -eo pid,user,pri,ni,vsz,rss,state,%cpu,%mem,cputime,comm | sort -rk8 | head -20 echo echo "****************************************************************************************" sleep $2 [root@tower ~]#
Provide execute permission to script using chmod command as below
[root@tower ~]# chmod +x mytop.sh
Now using this script inside simple loop with 1st positional parameter as loop iteration and 2nd as sleep number
[root@tower ~]# for i in {1..100} ; do ./mytop.sh $i 4 ; done
So this top command will run 100 times (iterations) repeating after every 4 seconds.
So, finally all set, you have successfully created your custom “top” command using bash shell. It is easy to understand and test in your environment. Do read, share with your friends and let us know about your feedback. Happy Learning, stay tuned 🙂
– Admin
References:
man pages:
- top
- sar
- ps
- free
- awk
In this era of darkness, you are spreading light of knowledge. Keep it up, God bless you.
LikeLiked by 1 person
Very neat article post.Much thanks again. Great.
LikeLike
Looking forward to reading more. Great article.Thanks Again.
LikeLike
Major thanks for the blog article. Really Great.
LikeLike
Thanks a lot for the blog.Much thanks again. Really Great.
LikeLike
I really liked your article post.Much thanks again. Really Cool.
LikeLike
I value the blog.Really thank you! Much obliged.
LikeLike
Great, thanks for sharing this post.Really thank you! Awesome.
LikeLike
Awesome blog.Thanks Again. Fantastic.
LikeLike
Very informative post.Really looking forward to read more. Fantastic.
LikeLike
Interesting content. I enjoyed your post. Thank you. I enjoyed your post. Thank you. I enjoyed reading what you had to say.
LikeLike
hello!,I like your writing very much! share we communicate more about your post on AOL? I need a specialist on this area to solve my problem. Maybe that’s you! Looking forward to see you.
LikeLike
This actually answered my downside, thank you!
LikeLike
This is one awesome blog article.Really thank you! Great.
LikeLike
You must take part in a contest for the most effective blogs on the web. I’ll advocate this web site!
LikeLike
Hello there, I discovered your web site via Google whilst searching for a similar matter, your site got here up, it appears to be like great. I have bookmarked it in my google bookmarks.
LikeLike
I really enjoy the blog post.Thanks Again. Great.
LikeLike
There’s certainly a great deal to know about this subject. I really like all of the points you made.
LikeLike
Way cool! Some very valid points! I appreciate you writing this post and also the rest of the website is extremely good.
LikeLike
Everything is very open with a very clear clarification of the issues. It was definitely informative. Your site is very helpful. Thanks for sharing!
LikeLike