Monitor Linux process by user or name - linux

i need to get stats from my Centos 6.7 with Cpanel and send to my external monitor server. What I would like to get is an average cpu load per user or per process name in the last 3 minutes. After many research and test not found any praticable solutions apart bash run top with
top -d 180 -b -n 2 > /top.log
second iteration looks like...
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
38017 mysql 20 0 760m 265m 6324 S 1.4 14.2 244:27.08 mysqld
39501 nobody 20 0 1047m 93m 7068 S 0.1 5.0 0:06.80 httpd
54877 johnd 20 0 32728 3612 2364 S 0.0 0.2 0:00.09 imap
51530 johnd 20 0 353m 5372 1928 S 0.0 0.3 0:04.17 php-fpm
39500 nobody 20 0 1046m 79m 3656 S 0.0 4.3 0:02.57 httpd
7 root 20 0 0 0 0 S 0.0 0.0 27:47.61 events/0
39497 nobody 20 0 1046m 84m 7784 S 0.0 4.5 0:02.77 httpd
etc...
then grep (only on the second iteration output) with COMMAND or USER, sum and divide by 100 to get value like cpu-load
echo "$PRTGTOP" | grep johnd | awk '{ sum += $9; } END { print sum/100; }'
I should probably also try to count the process times etc ?, maybe there is a simpler way to achieve the same result, maybe with third-party software to generate stats?
Thanks.

top gets its info from /proc/*/stat. Each numerical directory under /proc is a process number for a currently running process.
It may be easier for you to collect data directly from those directories. The data format is well defined and can be found in man proc under the subsection called "/proc/[pid]/stat".

You can try the pidstat tool (part of the sysstat package):
pidstat -C httpd -U johnd -h -u 180 1 | awk '{ sum += $7; } END { print sum/100;}'
This will return the percentage CPU usage of all processes matching the httpd command string and the johnd user over a 180-second interval.

ok, pidstat is better, thanks!, but if USER pid is run for only few seconds no cpu use is reported. i found best result with:
#run pidstat with 10 iterations for 18 times
pidstat -U -u 10 18 > /pidstat.log
then
#sum all cpu usage and divide by 18
cat /pidstat.log | grep -v Average | grep johnd | awk '{ sum += $8; } END { print sum/100/18;}' OFMT="%3.3f"
cat /pidstat.log | grep -v Average | grep httpd | awk '{ sum += $8; } END { print sum/100/18;}' OFMT="%3.3f"
with this i get best cpu usage stat per USER even if process is run only for few seconds but with high cpu usage

Related

How I can count how many suspended proceses there are in a shell script linux bash

I tried to write a shell script that shows and counts how many suspended processes there are.
But I succeeded only to show the suspended processes with:
#!/bin/bash
list_ps=`ps aux | awk '$8~/T/'`
echo "$list_ps"
I tried to count the suspended processes with:
nr=0
for i in $list_ps
do
nr=`expr $nr + 1`
done
Of course this didn't work because it counted every word there was even with the first row that had the USER PID STAT COMMAND.
Can you give me any suggestion on how I should do it?
Also here is the output for "ps aux | awk '$8~/T/" after I stopped some sleep processes.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
alexsan+ 6441 0.0 0.0 9008 736 pts/0 T 16:17 0:00 sleep 5000
alexsan+ 6511 0.0 0.0 9008 820 pts/0 T 16:18 0:00 sleep 5000
alexsan+ 7041 0.0 0.0 9008 760 pts/0 T 16:21 0:00 sleep 333
additional characters can be added to the state field (depending on the options you use), so this might be a safer approach:
ps aux | awk '$8~/T/'
to count how many processes you have with a header :
ps aux | awk '$8~/T/' | wc -l
to skip the header :
count=$(ps aux | awk '$8~/T/' | wc -l)
echo $((count -1))
one line version :
echo $(( $(ps aux | awk '$8~/T/' | wc -l)-1))
within single awk :
ps aux | awk 'NR>1 && $8~/T/' | wc -l
Using the option --no-header of the ps command, you will not need to substract 1 from the number of lines of output you get from wc -l, so you just have to issue:
ps --no-header aux | awk '$8 ~ /T/' | wc -l
And since the header will contain the 'T' of 'STAT' in field #8, it was necessary to filter this line out.
Within single awk could you please try following.
echo $(( $(ps aux | awk '$8~/T/{count++} END{print count-1}')))
Or to assign to a variable simply:
var=$(ps aux | awk '$8~/T/{count++} END{print count-1}')

overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu

I want to findout overall CPU usage and RAM usage in percentage, but i dint get success
$ command for cpu usage
4.85%
$ command for memory usage
15.15%
OR
$ command for cpu and mamory usage
cpu: 4.85%
mem: 15.15%
How can I achieve this?
You can use top and/or vmstat from the procps package.
Use vmstat -s to get the amount of RAM on your machine (optional), and
then use the output of top to calculate the memory usage percentages.
%Cpu(s): 3.8 us, 2.8 sy, 0.4 ni, 92.0 id, 1.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 24679620 total, 1705524 free, 7735748 used, 15238348 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 16161296 avail Mem
You can also do this for relatively short output:
watch '/usr/bin/top -b | head -4 | tail -2'
A shell pipe that calculates the current RAM usage periodically is
watch -n 5 "/usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf(\"used: %s total: %s => RAM Usage: %.1f%%\", \$F[7], \$F[3], 100*\$F[7]/\$F[3]) if /KiB Mem/'"
(CPU + Swap usages were filtered out here.)
This command prints every 5 seconds:
Every 5.0s: /usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf("u... wb3: Wed Nov 21 13:51:49 2018
used: 8349560 total: 24667856 => RAM Usage: 33.8%
Please use one of the following:
$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
Current Memory Utilization is : 14.6715
OR
$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
Current Memory Utilization is : 14.6703
CPU usage => top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100-$8 "%"}'
Memory usage => free -m | grep 'Mem:' | awk '{ print $3/$2*100 "%"}'
For the cpu usage% you can use:
top -b -n 1| grep Cpu | awk -F "," '{print $4}' | awk -F "id" '{print $1}' | awk -F "%" '{print $1}'
One liner solution to get RAM % in-use:
free -t | awk 'FNR == 2 {printf("%.0f%"), $3/$2*100}'
Example output: 24%
for more precision, you can change the integer N inside printf(%.<N>%) from the the previous command. For example to get 2 decimal places of precision you could do:
free -t | awk 'FNR == 2 {printf("%.2f%"), $3/$2*100}'
Example output: 24.57%

Get only command as output in ps aux AIX

With the below command:
# ps aux
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root 17760468 0.0 0.0 1180 1220 - A 18:15:00 0:00 iostat -Dsal 6
root 16777310 0.0 0.0 3172 408 - A 18:15:00 0:00 /usr/bin/perl
here i don't want iostst -Dsal 6 under COMMAND, instead it should show only iostat. I mean only command is needed not with its all options and parameters.
Reason for this query:
I am trying to use this below command on my AIX box -
ps aux | tr -s " " | head -1 | awk '{print $1,$2,$3,$4,$11}' ; ps aux | sort -rn +2 | tr -s " " | head -10 | awk '{print $1,$2,$3,$4,$NF}'
Since i have used NF in awk command, it will display only the last column of the line. In my case it will display "6" instead of iostst -Dsal 6 under COMMAND field.
Please help me here..!

How To Capture Unix 'Top' Command Output to a CSV file?

I am trying to get first 5 lines of top command through shell script & I need to write the output to a csv file ( I need to monitor the result in every 15 seconds ). Finally I need to plot a graph using the obtained datasheet.
I got the shell scrip to write the first 5 lines of top command to a txt file :
#!/bin/bash
echo "execution started.."
top -b -n 3 | sed -n '7,1p' >> out.txt
while [ true ]; do
sleep 15
echo "running.."
top -b -n 3 | sed -n '8, 12p' >> out.txt
done
Here is the out.txt file after few execution :
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3983 arun 20 0 1662480 309580 40936 S 26.3 6.4 13:36.00 gnome-shell
17907 arun 20 0 130020 1680 1172 R 10.5 0.0 0:00.03 top
2016 root 20 0 221792 51172 9636 S 5.3 1.1 4:40.97 Xorg
11917 arun 20 0 7004884 570312 22040 S 5.3 11.7 0:48.83 java
1 root 20 0 59732 7156 3992 S 0.0 0.1 0:02.71 systemd
3983 arun 20 0 1662480 309580 40936 S 36.8 6.4 13:37.23 gnome-shell
2016 root 20 0 221792 51172 9636 S 10.5 1.1 4:41.14 Xorg
2720 mongod 20 0 624364 33716 5200 R 5.3 0.7 1:44.36 mongod
17918 arun 20 0 130020 1676 1172 R 5.3 0.0 0:00.02 top
1 root 20 0 59732 7156 3992 S 0.0 0.1 0:02.71 systemd
3983 arun 20 0 1662480 309580 40936 S 25.0 6.4 13:38.60 gnome-shell
2720 mongod 20 0 624364 33672 5160 S 5.0 0.7 1:44.46 mongod
12081 arun 20 0 2687496 314248 21436 S 5.0 6.5 3:05.51 java
17922 arun 20 0 130020 1680 1172 R 5.0 0.0 0:00.02 top
1 root 20 0 59732 7156 3992 S 0.0 0.1 0:02.71 systemd
But I need the same data in csv format. I have tried to do this by giving output file name as out.csv ! But that didn't work. ( Because it was not in proper format, whole data came within first shell ! )
Can you please provide a solution to write the same output into a csv file ?
If you want to trim runs of whitespace and replace them with commas, try
top -b -n 3 | sed -n '8, 12{s/^ *//;s/ *$//;s/ */,/gp;};12q'
The sed script performs the following substitutions on lines 8 through 12:
Replace any leading space with nothing (otherwise you get an empty first column when the PID is right-aligned).
Replace any trailing spaces with nothing (similarly to avoid empty fields after the data).
Replace any remaining runs of adjacent spaces with a comma. Print this line.
Finally, on line 12, we are done, so we quit sed.
The shell does not pay any attention to the name of the file you are redirecting into and generally, file extensions on Unix are informal decorations, not file type specifiers like they are on some platforms.
You could do echo hello >outputfile.ps and the output would still be text, not PostScript (or a number of other possible interpretations of the .ps file extension). In any event, the echo command does not know that it is being redirected, because that is handled by the shell before the command runs, anyway. (Well, echo is a shell built-in, so in theory there could be some coordination in this case.)

Last pid process linux

I'm trying to get the last pid of a process (for example sleep). I do this:
# sleep 10 &
[1] 14115
But when I use this command to get the last pid:
# ps aux --sort +start_time | tail -n 4 | awk 'NR==1{print $2 " " $11}'
14125 sleep
The pid is not the same, i don't understand what I'm doing wrong, because if I do the same but without the last part of the command, I get the same pid:
# sleep 10 &
[1] 15853
# ps aux --sort +start_time | tail -n 4
root 15853 0.0 0.1 3364 520 pts/2 S 16:45 0:00 sleep 10
www-data 15871 0.0 0.0 1864 452 ? S 16:45 0:00 sleep 1
root 15872 0.0 0.2 4344 1168 pts/2 R+ 16:45 0:00 ps aux --sort +start_time
root 15873 0.0 0.1 3756 676 pts/2 S+ 16:45 0:00 tail -n 4
Thanks in advance
[Solved]
# ps aux --sort +start_time | tail -n 5 | awk 'NR==1{print $2 " " $11}'
another solution for a concrete process
# pgrep -n 'name_of_the_process'
The awk process is started, and occupies another line in the output. Try tail -n 5, but it's not reliable, as another process could be started in between, anyway. See psgrep.
You could get the pid of the last process with:
echo $!

Resources