• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • Know how much an individual process or system-wide consume CPU or memory.

    As a sysadmin, you often have to deal with an incident where the application is slow or unresponsive due to high CPU/memory/network utilization. If the server host just one process, then it’s easy to find out when the process consumes all the resources. However, imagine a shared server where multiple services are running, and you need to find which one is eating all the resources.

    There are many monitoring software which does this out of the box. But if you don’t have one or looking for a command-based solution, then here you go. They are all FREE!

    top

    You may want to start by looking into top or htop result to see the processes overview.

    As you can see below, it gives an excellent idea about what all processes are utilizing. If you look at the first one, which is MySQL is taking 11.9% of CPU and 2.5% of CPU.

    top - 11:57:33 up 0 min,  1 user,  load average: 3.69, 0.96, 0.32
    Tasks: 165 total,   2 running, 113 sleeping,   0 stopped,   0 zombie
    %Cpu(s): 21.0 us,  5.5 sy,  0.0 ni, 70.5 id,  1.7 wa,  0.0 hi,  1.3 si,  0.0 st
    KiB Mem :  7637308 total,  5802888 free,   849512 used,   984908 buff/cache
    KiB Swap:        0 total,        0 free,        0 used.  6495648 avail Mem 
    
      PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                        
     1986 netdata   20   0 1738856 191560  22948 S  11.6  2.5   0:02.30 mysqld                                                                                                         
     3021 www-data  20   0  255288  78420  55484 S   6.6  1.0   0:01.55 php-fpm                                                                                                        
     3138 www-data  20   0  253096  79780  59228 S   6.6  1.0   0:00.92 php-fpm                                                                                                        
     3153 www-data  20   0  255116  79088  56472 S   5.0  1.0   0:00.70 php-fpm                                                                                                        
     3037 www-data  20   0  257200  81088  56216 S   4.3  1.1   0:01.50 php-fpm                                                                                                        
     3048 www-data  20   0  257088  78740  55380 S   4.3  1.0   0:01.46 php-fpm                                                                                                        
     3054 www-data  20   0  254160  72168  52108 S   3.7  0.9   0:01.32 php-fpm                                                                                                        
     3135 www-data  20   0  255084  75912  54836 S   3.7  1.0   0:00.91 php-fpm                                                                                                        
     3051 www-data  20   0  254096  73804  51964 S   3.0  1.0   0:01.38 php-fpm                                                                                                        
     2962 www-data  20   0   45280   7284   3488 R   2.0  0.1   0:00.22 openresty                                                                                                      
     1062 netdata   20   0  338748  76144   6720 S   1.0  1.0   0:01.31 netdata                                                                                                        
     1702 netdata   20   0   21852   4232   2352 S   1.0  0.1   0:00.34 apps.plugin                                                                                                    
     1729 netdata   20   0   18636   3280   2764 S   0.7  0.0   0:00.05 bash                                                                                                           
     1980 netdata   20   0   62008  12896   5796 S   0.7  0.2   0:00.14 redis-server                                                                                                   
       11 root      20   0       0      0      0 I   0.3  0.0   0:00.14 rcu_sched                                                                                                      
     1007 root      20   0 1347424  74524  38872 S   0.3  1.0   0:00.92 dockerd                                                                                                        
     1857 root      20   0   10600   5564   4276 S   0.3  0.1   0:00.03 containerd-shim                                                                                                
     2045 root      20   0    9948   6028   5016 S   0.3  0.1   0:00.14 forego                                                                                                         
     2934 root      20   0   13616   8760   5928 S   0.3  0.1   0:00.07 docker-gen                                                                                                     
     2966 systemd+  20   0   25784   7924   2340 S   0.3  0.1   0:00.06 nginx

    The top is installed on almost all Linux distribution.

    Once you identify the suspect, then you may want to focus on that process instead of everything like you saw above. You can still use top command but with some argument.

    Let’s say you know the process id (PID); you can use the below command.

    top -p $PID

    Below an example of top -p 3102

    top - 11:59:56 up 3 min,  1 user,  load average: 0.72, 0.70, 0.31
    Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  7.1 us,  2.9 sy,  0.0 ni, 89.1 id,  0.3 wa,  0.0 hi,  0.7 si,  0.0 st
    KiB Mem :  7637308 total,  5802024 free,   783672 used,  1051612 buff/cache
    KiB Swap:        0 total,        0 free,        0 used.  6555636 avail Mem 
    
      PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                        
     3102 www-data  20   0  329500  82376  60640 S   0.0  1.1   0:03.35 php-fpm

    You may also use grep with top. Below an example of checking Docker utilization.

    [email protected]:~# top | grep docker
     1007 root      20   0 1347424  74524  38872 S   0.3  1.0   0:01.38 dockerd                                                                                                        
     2934 root      20   0   14676   9652   5928 S   0.3  0.1   0:00.54 docker-gen                                                                                                     
     1007 root      20   0 1347424  74524  38872 S   0.3  1.0   0:01.39 dockerd                                                                                                        
     1007 root      20   0 1347424  74524  38872 S   1.0  1.0   0:01.42 dockerd                                                                                                        
     2934 root      20   0   14740   9652   5928 S   0.3  0.1   0:00.55 docker-gen                                                                                                     
     2934 root      20   0   14740   9652   5928 S   0.3  0.1   0:00.56 docker-gen

    htop

    Similar to the top but with more information. As you can, it got the command column, which is handy to identify the process path. And also it is colorful.

    htop may not be installed by default, but you can always do it as below.

    Install htop on Ubuntu

    apt-get install htop

    Install htop on CentOS/RHEL 8.x

    dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
    dnf update
    dnf install htop

    glances

    As the name says, you get a system utilization view on a single screen. Running processes are sorted by their CPU utilization.

    You can install glances on CentOS 8 using DNF as below.

    dnf install glances

    for CentOS7, you can use YUM

    yum install glances

    atop

    A similar to the above listed but with a brilliant feature to record the output in a file so you can view them later. Imagine there is a pattern of having an issue at a specific time window. You can schedule to write the output in a file through crontab or other, and later you can playback.

    To record the output in a file:

    atop -w filename

    and, to playback:

    atop -r filename

    It supports multiple arguments like interval, samples, etc. and I would strongly recommend taking a look at the man page.

    If you are just interested in real-time troubleshooting, then just execute atop and you should see like below.

    You can install atop as below.

    dnf install atop

    ps

    Let’s check ps command now.

    You can use the ps command with PID to print their CPU and memory utilization.

    ps -p $PID -o %cpu,%mem

    The output should look like this.

    [email protected]:~# ps -p 1048 -o %cpu,%mem
    %CPU %MEM
     0.2  3.0
    [email protected]:~#

    nmon

    Interactive command-line monitoring tool for CPU, memory, disks, network, NFS, and virtual memory utilization. To view the top process (by utilization), you can execute nmon and press t button.

    You can install nmon as below.

    dnf install nmon

    Monit

    Monit is a web-based and command-line open-source solution to monitor server resources, daemons, files, directory, file systems, etc.

    Monit also got a cool widget.

    Its light-weight monitoring software. But, there is more to explore here.

    Monitorix

    A lightweight open-source utility to monitor the Linux server. Monitorix got in-built HTTP so you can check the utilization and other stuff on the web. Some of the other usage reports include:

    • Kernal/temperature
    • Filesystem and I/O
    • Network traffic
    • Apache/Mail/FTP/Nginx/MySQL/Varnish/Memcached/

    Monitorix also offers alert configuration so you can get notified when things are not right. It will be a good choice when you are managing cloud-based servers and looking for a proactive monitoring solution.

    Netdata

    Netdata is a real-time performance monitoring for system resources, applications, web servers, databases, DNS, mail, hardware sensors, and a lot more. It is open-source and getting started is easy. All the data is collected, stored, and streamed for you to visualize interactively. Data is collected every second, so you never miss anything.

    Loved by many industry leaders.

    So what you are waiting for, try and take control of your Linux servers.

    Conclusion

    I hope the above tools help you to visualize the server utilization in real-time so you can take necessary action. If you just started as a system administrator and looking to get hands-on training, then check out this Udemy course.