Need a script to filter high cpu utilization process value in log file
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.
Need a script to filter high cpu utilization process value in log file
We have created a script to find top 5 cpu usage process details and we stored that details in particular log file .we have added that script into Cron and scheduled every 5 mins . After one week it has lot of details .its take very long time to check above 50% process details in that particular file
Example log file lines :
www-data 2263 9.8 1.5 1069596 128300 ? S 09:37 0:02 php-fpm: pool www
www-data 2282 3.2 0.9 985808 74116 ? S 09:37 0:00 php-fpm: pool www
Bolded value is CPU usage :
We need a script to filter above 60% usage process and that script need to check every lines in that log file .Please help it will save our lot of time. thanks in advance
awk can do that kind of comparison. If white space is the delimiter between data fields then you don't have to set it with -F or by changing the FS variable. The individual fields are available as $1, $2, $3, and so on. In this case it looks like you want to compare the third field, so here's something to get you started:
We have created a script to find top 5 cpu usage process details and we stored that details in particular log file .we have added that script into Cron and scheduled every 5 mins . After one week it has lot of details .its take very long time to check above 50% process details in that particular file
Example log file lines :
www-data 2263 9.8 1.5 1069596 128300 ? S 09:37 0:02 php-fpm: pool www
www-data 2282 3.2 0.9 985808 74116 ? S 09:37 0:00 php-fpm: pool www
Bolded value is CPU usage :
We need a script to filter above 60% usage process and that script need to check every lines in that log file .Please help it will save our lot of time. thanks in advance
You've written a script, and now you wish someone to write a script for you to do something you haven't yet attempted on your own.
Please make an attempt.
I fully agree with Turbocapitalist's suggestion about awk and what it can do for you. I feel you should put forth an attempt. The few reasons are (1) that you should versus ask others to do for you, and (2) you best can assess the flow of your requirements as you generate output and determine how you wish to filter it and how you wish to format this output.
Once you have something, please then share a clip of sample data as well as your updated script and people can then evaluate and help you to optimize what you have started with.
Thank you so much its working as expected @Turbocapitalist
that log file have date and time for above , i forgot to update , we have to print that details also
Example :
Sat Jul 15 09:38:10 IST 2017
www-data 2263 9.8 1.5 1069596 128300 ? S 09:37 0:02 php-fpm: pool www
www-data 2282 3.2 0.9 985808 74116 ? S 09:37 0:00 php-fpm: pool www
@rtmistler ->Thank you for your suggestions ,we have tried to create a script but we can't . we are new to linux scripting .we will follow in future requests. Thanks
The scripting is the same for nearly all the recent operating systems, even OS X, not just GNU/Linux. So you will find it useful. However, nothing from Windows is relevant, it is the odd one out.
Quote:
Originally Posted by sakusri16
that log file have date and time for above , i forgot to update , we have to print that details also
Ok. We can guide you through that and help you find a way to get awk to print what you need. The first questions is, where are you getting that date + time from?
Thank you so much its working as expected @Turbocapitalist
Excellent progress. Glad to hear that this got you further.
Quote:
Originally Posted by sakusri16
@rtmistler ->Thank you for your suggestions ,we have tried to create a script but we can't . we are new to linux scripting .we will follow in future requests. Thanks
This is exactly why we ask to see code from posters and ask them to make their attempts. For us to understand your expertise with things.
If it is bash scripting you are doing, there are many references to help you get started, continue at your own pace, and also work though problems with scripts. Here are some references to consider:
Thank you so much its working as expected @Turbocapitalist that log file have date and time for above , i forgot to update , we have to print that details also
Example :
Sat Jul 15 09:38:10 IST 2017
www-data 2263 9.8 1.5 1069596 128300 ? S 09:37 0:02 php-fpm: pool www
www-data 2282 3.2 0.9 985808 74116 ? S 09:37 0:00 php-fpm: pool www
@rtmistler ->Thank you for your suggestions ,we have tried to create a script but we can't . we are new to linux scripting .we will follow in future requests. Thanks
Very confused here; from your first post, you said:
Quote:
Originally Posted by sakusri16
We have created a script....
...that indicates that you HAVE created a script. But you can't create a second script? rtmistler pointed you towards the bash scripting tutorials, which are very detailed and include examples. And since you've created a script before, you should have some experience doing so. You need to at least show your own efforts, and show us the first script you wrote, and what attempts you've made at the second.
We are always going to be happy to help, but you need to show your own efforts and tell us where you're stuck first.
One pattern that might work would be any line that is 29 characters long and starting with a letter. How far can you get adding that to your exising awk script?
Last edited by Turbocapitalist; 08-09-2017 at 12:51 PM.
Hi
Sat Jul 15 09:49:26 IST 2017
www-data 2773 6.0 1.8 1083404 149224 ? S 09:45 0:14 php-fpm: pool www
www-data 2697 5.1 1.9 1618592 162940 ? S 09:44 0:16 php-fpm: pool www
www-data 2623 4.6 1.6 1613324 130784 ? S 09:43 0:18 php-fpm: pool www
www-data 2866 4.3 1.8 1545412 153800 ? S 09:46 0:08 php-fpm: pool www
Sat Jul 15 12:17:34 IST 2017
www-data 8984 6.5 1.3 1009988 109804 ? S 12:17 0:02 php-fpm: pool www
www-data 9020 4.7 1.1 983736 94324 ? S 12:17 0:00 php-fpm: pool www
www-data 9019 3.3 0.8 912588 66956 ? S 12:17 0:00 php-fpm: pool www
www-data 9021 1.3 0.8 912484 65808 ? S 12:17 0:00 php-fpm: pool www
I m not familar with regular expressions pattern
Again, can you post what YOU have done/tried/written to even attempt to do this?? We are happy to help, but you've not posted anything so far that shows any effort on your part. Again, can you post the first script you wrote that generates this log file?? Because just appending the date as you mentioned seems very inefficient, if it's part of what you want reported on. You could probably output the date first, followed by the rest of the data, leaving you one line for each record, each with a date like
Code:
Sat Jul 15 12:17:34 IST 2017 www-data 8984 6.5 1.3 1009988 109804 ? S 12:17 0:02 php-fpm: pool www
...which would be far easier to parse/work with.
So again: post your script and show us your efforts.
Remember that ps does not process any input so sending it input via a pipe will be of no use.
If you want to prepend a date onto each line, you'd have to use sed or awk. There are several ways to do either. If things remain simple then perhaps command subsitution is one option:
Code:
ps aux | sort -k 3,3nr | head -n 5 | sed -e "s/^/$(date +'%F %T %Z ')/"
sed is using the substitution command there s/// It looks for the beginnig of the line ^ and replaces it with the result of $(...) In effect that is prepending the output. For sed see:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.