Search code examples
linuxbashshellawklinux-disk-free

With df -h how to create custom alert-conditions


With df -h, awk, tail, tr, how to create a custom "alert" column that should display one of the following indications for /mnt/hgfs :

  • Warning: If the occupancy rate is between 75% and 80%
  • Critical: If the occupancy rate is between 81% and 95%
  • Alarm: If the rate is greater than 96%

Solution

  • edit: It's staying up.

    df |awk -v threshold="75 81 96" -v message="Warning Critical Alarm" -v mnt="/mnt/hgfs" '
      BEGIN {n=split(threshold, T); split(message, M)}
      $NF == mnt {
        for(i=n; i>0; i--)
          if(int($5) > T[i]) {print M[i] ":", mnt, "usage:", $5; exit}
        print mnt, "usage normal"                                                                                                                                               
      }'
    
    1. Import your arbitrary values as space-separated strings.
    2. split lists into arrays; get the number of elements with n=split()
    3. On any line whose last column is "/mnt/hgfs", iterate backwards over the T(hreshold) array, comparing the fifth column (Use%) to each value. If it's greater, print a warning message using the same value for the M(essage) array, then exit before other comparisons are performed.

    Solution for new requirements:

    df |awk '
      NR == 1      {alert="Alert"}
      int($5) < 75 {alert=""}
      int($5) > 75 {alert="Warning"}
      int($5) > 81 {alert="Critical"}
      int($5) > 96 {alert="Alarm"}
                   {printf("%-10s%s\n", alert, $0)}'