Linux system monitoring and statistics gathering

We will look at some Linux commands which will help us monitor system performance, gather statistics and view historical resource usage.

Monitoring connections with ss command
ss should be preinstalled for most modern Linux distributions

Show established TCP connection

ss -t 

Show established UDP connections

ss -u 

Show ports listening for TCP connections

ss -ltn

Show ports listening to UDP connection

ss -lun

Show ports listening on TCP and UDP

ss -ltun


Gathering OS statistics

For gathering statistoics we will use sysstat tool and we will need to install it first

sudo apt-get install sysstat
yum install sysstat

This will make few tools available like sar and mpstat
Lets gather some basic information for 20 seconds

vmstat 1 20

We can use vmstat to gather basic disk usage information

vmstat -d 1 20

Other useful tool for gathering disk usage information is iostat

iostat 1 20

You can narrow down monitoring to specific device. For example sda

ipstat -d -p sda 1 20

Historical resource usage
SAR or system activity report can look back in time and see how system was performing. For this tool must be enabled which uses cron job to gather stats. Also sysstat package must already be installed.
Edit /etc/default/sysstat

ENABLED="true"
service sysstat restart 

By default it will collect stats every 10 min and it uses cron job /etc/cron.d/sysstat. Time span can be changed.
View cpu stats

sar -u

View memmory stats

sar -r

IO stats

sar -b