Saturday 8 October 2016

Monitoring Memory with Swap


A low amount of free memory is an indication that the system has many thing to  do; this may lead to disk swapping. A high use of swap space make a system go slow because disk access is many times slower than access.

cat /usr/local/src/memory.sh
#!/bin/bash
C=$(/usr/bin/free -m| head -2 | tail -n +2 | awk {'print $4'})
C=$(/usr/bin/free -m| head -4 | tail -n +4 | awk {'print $3'})
E=$(date)
printf "%s %s %s %s %s %s\n" "Mem: " "$C" "Swap: " "$D" "At" "$E"

crontab -l
*/5 * * * * /usr/local/src/memory.sh >> /var/log/memory.data

tail -5 /var/log/memory.data
Mem: 76080 Swap: 5274188 At Sat Oct  8 12:44:48 PDT 2016
Mem: 72128 Swap: 5274264 At Sat Oct  8 12:45:01 PDT 2016
Mem: 67780 Swap: 5274428 At Sat Oct  8 12:50:01 PDT 2016
Mem: 68720 Swap: 5274492 At Sat Oct  8 12:55:01 PDT 2016
Mem: 68472 Swap: 5274944 At Sat Oct  8 13:00:01 PDT 2016

Monitoring the TCP ESTABLISHED Connections


The following script tcpConnect.sh uses nestat to record the number of connection as well as the date and the time of the measurement.

cat /usr/local/src/tcpConnet.sh
#!/bin/bash
C=$(/bin/netstat -nt | tail -n +3 | grep ESTABLISHED | wc -l)
D=$(date)
printf "%s %s %s\n" "$C" "||" "$D"

crontab -l
*/5 * * * * /usr/local/src/tcpConnet.sh >> /var/log/tcpConnet.data

tail -5 /var/log/tcpConnet.data
25 || Sat Oct  8 11:10:01 PDT 2016
26 || Sat Oct  8 11:15:01 PDT 2016
23 ||  Sat Oct  8 11:20:01 PDT 2016
23 || Sat Oct  8 11:25:01 PDT 2016
23 || Sat Oct  8 11:30:01 PDT 2016

Monitoring Load Average


The easiest way to monitor the load average is by using the output o the uptime command and a small awk script that will run as a cron job to store your data into a text file.

The awk script is the following :

cat /usr/local/src/uptime.sh
#!/bin/bash
uptime | awk  {'print $9 $10 $11'} | awk -F, {'print $1 " " $2 " " $3'}

sh /usr/local/src/uptime.sh
0.68 0.86 1.48

crontab -l
*/5 * * * * /usr/local/src/uptime.sh >> /var/log/uptime.data

head -5 /var/log/uptime.data
0.61 1.40 1.96
0.47 1.33 1.92
0.49 0.94 1.68
0.64 0.80 1.41
0.45 0.61 1.16

       
The Linux system we used has only one CPU, so any load average greater than 1.00 indicates a performance issue.

The first number shows the load average during the last minute, the second during the last five minutes and the third during the last 15 minute,. The three values can show you if the load is increasing, decreasing or steady.