script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions.

 lpar-command | awk '
/^Name/ { next; }
/^---/ { next; }
{
    state[$2] = state[$2] $1 " ";
    if( $2 == "A" )
        acount++;
    else
        others++;
}
END {
printf( "==== WPAR Status Check =====\n" );
printf( "==== %s ====\n", date );
printf( "**** WPAR state Active:\n\t%s\n", state["A"] );
printf( "\t%d WPAR state A-Active\n\n", acount );
printf( "**** WPAR state Defined/Transition/broken:\n\t%s %s %s\n", state["D"], state["T"], state["B"] );
printf( "\t%d WPARs are in D-Defined/T-Transition/B-Broken state\n", others );
}
' date="$(date)" |mail-command

try:

 
 lpar-command | awk '
/^Name/ { next; }
/^---/ { next; }
{
    state[$2] = state[$2] $1 " ";
    if( $2 == "A" )
        acount++;
    else
        others++;
}
END {
if (state["D"] ~ /./ || state["T"] ~ /./ || state["B"] ~ /./) {
  printf( "==== WPAR Status Check =====\n" );
  printf( "==== %s ====\n", date );
  printf( "**** WPAR state Active:\n\t%s\n", state["A"] );
  printf( "\t%d WPAR state A-Active\n\n", acount );
  printf( "**** WPAR state Defined/Transition/broken:\n\t%s %s %s\n", state["D"], state["T"], state["B"] );
  printf( "\t%d WPARs are in D-Defined/T-Transition/B-Broken state\n", others );
}
}
' date="$(date)" > temporary_log_file
[[ -s "temporary_log_file" ]] && cat temporary_log_file | mail-command

1 Like

Thanks rdrtx1, Is there possibility that log does not gets over-written, each time script runs.

Its over writting the log file, I want to keep all generated output till atleast 1 week.

Try: add to the top of script:

find ./ \( -name . -o -prune \) -type f -mtime -7 -name temporary_log_file -exec rm -f {} \;

then add change to line:

' date="$(date)" >> temporary_log_file
1 Like

works perfect..

Thanks