Conditional Column Value

Hi Folks,

I'm trying tog ain further experience with shell programming and have set my a small goal of writing a little filesystem monitoring script. So far my output is as follows:

PACMYDB03
Filesystem                       Size    Used    Avail   Use%    Status
/usr/local/mysql/data            20G     220M    19G     2
/usr/local/mysql/binlogs         15G     52M     14G     1
/usr/local/mysql/backups         25G     44M     24G     1

I'm now trying to figure a way of doing a conditional comparison ( $5 -ge 1 ) on the 'use%' column and based on the result filling in the 'Status' column with either "OK" or "Space Required"

Here is a snippet of code:

# Get The Filesystem Stats
echo ""
hostname
echo $HEAD | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $1, $2, $3, $4, $5, $6)}'
echo $DATA > $TMP
echo $BINLOGS >> $TMP
echo $BACKUPS >> $TMP
cat $TMP | sed 's/%//g' | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $5, $1, $2, $3, $4, $6)}'
rm $TMP
exit 0

Any help is appreciated.

Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

In this special case:

  • Structure and contents of input data missing (e.g. the HEAD variable, and others).
  • where to insert the conditional comparison? BTW, why not use awk for the entire task, now that you use it anyhow?

Aside: thanks for using CODE tags.

OK, let me try again. I appreciate my code thus far mat be sloppy, forgive me, but as I previously stated, this is something I am learning and hoping to improve upon.

This is my code so far:

#!/bin/bash

# Fixed Variables
TMP=/tmp/tmpx
HEAD="Filesystem Size Used Avail Use% Status"
DATA=$(df -h | gawk '$5 ~ "/data"')
BINLOGS=$(df -h | gawk '$5 ~ "/binlogs"')
BACKUPS=$(df -h | gawk '$5 ~ "/backup*"')


# Get The Filesystem Stats
echo ""
hostname
echo $HEAD | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $1, $2, $3, $4, $5, $6)}'
echo $DATA > $TMP
echo $BINLOGS >> $TMP
echo $BACKUPS >> $TMP
cat $TMP | sed 's/%//g' | awk '{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $5, $1, $2, $3, $4, $6)}'
rm $TMP
exit 0

And it produces this output:

PACMYDB03
Filesystem                       Size    Used    Avail   Use%    Status
/usr/local/mysql/data            20G     220M    19G     2
/usr/local/mysql/binlogs         15G     52M     14G     1
/usr/local/mysql/backups         25G     44M     24G     1

I want to be able to add a value of "OK" or "Space Required" under the 'Status' colum of each line based on a result of a conditional test of column $4 (something along of the lines of {if ($4 -ge 1){$6="OK"} or {if ($4 -ge 2){$6="Space Required"}.

Am I making sense?

I'm using Red Hat 6

Many Thanks

The way you approach your problem is not quite evident to me. You may do this as an exercise in the frayed way shown, but how about a "monlithic" approach, using one single awk script only, in lieu of expensively running 11 external commands? Something like

df | awk 'NR == 1 || /\/(data|binlogs|backup)/ {printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $6, $2, $3, $4, $5, NR==1?"Status":($5+0 > 80)?"Space required":"OK" )}'

Thanks.

I managed to figure a way myself in the end. However your approach is much more elegant and this is clearly something i need to work on and understand more. Would you mind breaking down your code to me.

Appreciate your time.

awk '
NR == 1                                                                 # On first line
||                                                                      # OR
/\/(data|binlogs|backup)/                                               # lines with respective mount points
{printf("%-30s\t %s\t %s\t %s\t %s\t %s\t \n", $6, $2, $3, $4, $5,      # print fields 2 - 6, AND, for field 7:
NR==1                                                                   # conditional assignment operator: 1. line
?"Status"                                                               # TRUE branch: print header field
:                                                                       # FALSE branch: another ...
($5+0 > 80)                                                             # cond.assg.op.: if percent > 80%
?"Space required"                                                       # TRUE branch
:"OK"                                                                   # FALSE branch
)}'

Please post the solution you found for others to learn, and for optional discussion.