-3

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%
  • 3
    Welcome to Stackoverflow. What have you tried so far? Why do you mention awk, tail, tr? Are they required? – cronoik May 19 '19 at 15:58
  • Thanks i want to use one those command i tried creating a column df - h | awk v observation =´´$observation) ’´ – John Rudolph May 19 '19 at 16:03
  • 2
    Welcome to Stack Overflow! Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question](http://meta.stackoverflow.com/q/284236)? – kvantour May 19 '19 at 16:26
  • I suggest to start with `df -hP` and not `df -h`. – Cyrus May 19 '19 at 16:48
  • I want 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 – John Rudolph May 19 '19 at 17:45

1 Answers1

-1

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)}'
vintnes
  • 2,014
  • 7
  • 16
  • I want to add this to a 7th column named ‘’ Observation ‘’ – John Rudolph May 19 '19 at 19:08
  • @JohnRudolph I will work with you on additional requirements if you accept this answer to your original requirements. – vintnes May 19 '19 at 19:33
  • to create and add a custom column titled "Alert" shouldn’t it begin with df - h | awk -v alert ="($alert)" ? – John Rudolph May 19 '19 at 19:37
  • @JohnRudolph I would add a line: `{printf("%-10s%s\n", foobar, $0)}` to always print a padded string to the left of the original output. Then you could simply change the value of foobar based on Use%. – vintnes May 19 '19 at 20:18
  • @JohnRudolph I added a simple filter that checks the value of $5 repeatedly – vintnes May 19 '19 at 20:32