Category Archives: awk

Email alert for low disk space v2

A while back I wrote a post about a simple low disk space script that will email you if a specific partition on your system has free space lower than a percentage you specified. Now I improved that script, adding some more features:

– excluding some partitions (be careful here not too exclude to much)

– sending email to many recipients

– check if emails were already sent, then it would spam the users with emails

The script is below:

#!/bin/sh 
EXCLUDE_PARTITIONS="partition1\|partition2" 
WARNING=85 
RECIPIENTS="user1@domain.com user2@domain.com" 
DF=( `df -h | grep % | tail -n +2 | awk '{print $(NF-2)" "$(NF-1)" "$(NF)}' | sed 's/%//g' | sed 's#/\(.*\)#/\1 slash-\1#g' ` ) 
for ((i=1;i<${#DF[@]};i=i+4)) 
do 
  if [[ ${DF[i]} -gt $WARNING ]] 
  then 
    if [ ! -f /tmp/disk-warning-for-${DF[i+2]} ] 
    then 
      touch /tmp/disk-warning-for-${DF[i+2]} 
      echo "" | mail -s "SH Alert: Critical: `hostname` - ${DF[i+1]} has only ${DF[i]}% (${DF[i-1]}) free space" $RECIPIENTS 
    fi 
  else 
    if [ -f /tmp/disk-warning-for-${DF[i+2]} ] 
    then 
      echo "" | mail -s "SH Alert: Clear: `hostname` - ${DF[i+1]} has only ${DF[i]}% (${DF[i-1]}) free space" $RECIPIENTS 
      rm /tmp/disk-warning-for-${DF[i+2]} 
    fi 
  fi 
done

As in the previous post, you need to make the script executable:

chmod +x /path/to/script

And add it to the cronjob using “cronjob -e”:

# Minute   Hour   Day of Month       Month          Day of Week        Command
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)
*/10 * * * * /path/to/script

Note: This was tested on Oracle Linux 5/6 and is working great. For other distro’s you might need to change the email syntax, the order of the df columns, etc.

Fast method for finding differences between two unsorted files

At work I had two files: one with 13 000 records (filea) and one with 300 000 records (fileb). Every record was on a new line in the file. I was looking for a method of finding which records from filea are not in fileb. Because of the huge number of records per file, the “grep -v -f filea fileb” was taking forever and also I would need the files to be sorted before executing the grep.

After a few searches on the world wide web, I found a very quick way of doing this with awk:

1
awk 'NR == FNR { A[$0]=1; next } !A[$0]' fileb filea

If you want to find out more about this fast and easy solution (no need to presort the files) I found a similar example with an explanation here.