Required formatted output on mail for disk utilization

Hi Experts,

Could you pls help with below query

I have written below script to capture disk utilization and then send the output on mail. But getting unformated output on mail

#!/bin/bash 
echo "Hi Sur,">dfoutput.csv
printf '\n' >>dfoutput.csv
echo "Please find BAYSQUAR2 filesystem details as below. ">>dfoutput.csv
printf '\n' >>dfoutput.csv
df -hP /Baysquar2/undo /Baysquar2/redo1 /Baysquar2/redo2 /Baysquar2/temp /Baysquar2/arch01 /Baysquar2/disk04 /Baysquar2/disk03 /Baysquar2/disk02 /Baysquar2/disk01 /Tat
icr/data04 /Taticr/data05 /Baysquar2/disk08 /Baysquar2/disk07 /Baysquar2/disk06 /Baysquar2/disk05 /Baysquar2/arch02_new /Baysquar2/disk09 /Baysquar2/disk10 /Baysquar2/
disk11 /Baysquar2/disk12 /Baysquar2/disk13|awk '{printf "%-5s|%-5s|%-5s|%-5s|%5s\n", $2,$3,$4,$5,$6}' >>dfoutput.csv
printf '\n' >>dfoutput.csv
echo "Regards">>dfoutput.csv
echo "Unix Team">>dfoutput.csv
cat dfoutput.csv|mailx -s "BAYSQUAR2 172.17.5.199(Tata-Icr2)" xyz@abc.com
 

Current output(irregularize)

Size |Used |Avail|Use% |Mounted
99G  |96G  |3.2G |97%  |/Baysquar2/undo
50G  |12G  |39G  |23%  |/Baysquar2/redo1 50G  |9.2G |41G  |19%  |/Baysquar2/redo2 99G  |93G  |6.2G |94%  |/Baysquar2/temp
99G  |5.2G |94G  |6%   |/Baysquar2/arch01
148G |147G |1.4G |100% |/Baysquar2/disk04 148G |147G |1.4G |100% |/Baysquar2/disk03 168G |167G |1.2G |100% |/Baysquar2/disk02 296G |296G |9.0M |100% |/Baysquar2/disk01 1.2T |867G |339G |72%  |/Taticr/data04
1008G|719G |290G |72%  |/Taticr/data05
788G |785G |2.5G |100% |/Baysquar2/disk08 540G |536G |3.7G |100% |/Baysquar2/disk07 493G |491G |1.5G |100% |/Baysquar2/disk06 542G |539G |2.7G |100% |/Baysquar2/disk05 247G |91G  |144G |39%  |/Baysquar2/arch02_new
1008G|957G |619M |100% |/Baysquar2/disk09 956G |1.7G |100% 

Below is expected output which i need to have

Size |Used |Avail|Use% |Mounted
99G  |96G  |3.2G |97%  |/Baysquar2/undo
50G  |12G  |39G  |23%  |/Baysquar2/redo1
50G  |9.2G |41G  |19%  |/Baysquar2/redo2
99G  |93G  |6.2G |94%  |/Baysquar2/temp
99G  |5.2G |94G  |6%   |/Baysquar2/arch01
148G |147G |1.4G |100% |/Baysquar2/disk04
148G |147G |1.4G |100% |/Baysquar2/disk03
168G |167G |1.2G |100% |/Baysquar2/disk02
296G |296G |9.0M |100% |/Baysquar2/disk01
1.2T |867G |339G |72%  |/Taticr/data04

Regards

Please show the contents of dfoutput.csv and a (partial) hexdump of it. I guess the problem might be on the receiving side. What's your mail client?

I think it is because your df output that you are reading into awk may be split over multiple lines if the logical volume name or mount-mount are too long.

Before I worry about that, there are a lot of file IO redirections here and a big long linst making it rather messy. Would something like this this be neater?

#!/bin/bash

Target_dirs="\
/Baysquar2/undo   \
/Baysquar2/redo1  \
/Baysquar2/redo2  \
/Baysquar2/temp   \
/Baysquar2/arch01 \
..... etc.
/Baysquar2/disk13 \
"                                                        # Clear list of directories defined, and easy to adjust

{
echo "Hello"
echo
echo "Size |Used |Avail|Use% |Mounted"                   # Manually set the headings
for dir in "$Target_dirs"
do
   df -h $dir
done | grep -v "Filesystem"                              # .... plus whatever formatting needed here
echo
echo "Bye"
} > output_file                                          #  All output within the braces written as a single IO, reducing clutter too

This looks much cleaner to me so you can concentrate on your formatting.

For the formatting loop, perhaps this might work:-

while read lv size used avail usep mounted
do
   if [ -z "$mounted" ]
   then                                                   # Line is split, so shuffle everything over
      mounted="$usep"                                     # Maybe an array could be used to smarten this
      usep="$avail"                                       # to a single step
      avail="$used"
      used="$size"
      size="$lv"
   fi
   printf "%-5s|%-5s|%-5s|%-5s|%-s" "$size" "$used" "$avail" "$usep" "$mounted"
done < <(for dir in "$Target_dirs"                        # .... whatever
do
   df -h "$dir"
done | grep -v "Filesystem")

Of course, you could use awk for this, but you need to know what input you are really getting for each 'line' that whatever tool will use.

Is the output you have shared scraped from the email or is that what ends up in the file?

Can you share the output for a simple df -h /Baysquar2/* /Taticr/* or the more explicit:

df -hP /Baysquar2/undo /Baysquar2/redo1 /Baysquar2/redo2 /Baysquar2/temp /Baysquar2/arch01 /Baysquar2/disk04 /Baysquar2/disk03 /Baysquar2/disk02 /Baysquar2/disk01 /Tat
icr/data04 /Taticr/data05 /Baysquar2/disk08 /Baysquar2/disk07 /Baysquar2/disk06 /Baysquar2/disk05 /Baysquar2/arch02_new /Baysquar2/disk09 /Baysquar2/disk10 /Baysquar2/
disk11 /Baysquar2/disk12 /Baysquar2/disk13

Can you tell me if my suggestions help/hinder/confuse?

Thanks, in advance,
Robin

Shouldn't df -P (or df -hP ) force one line per filesystem?

1 Like

I guess this is happening at the email client.

E.g. Microsoft Outlook by default removes extra line breaks in plain text format. You will be able to see a warning "We removed extra line breaks from this message"

You can turn off this settings or send this email in table (Content-Type: text/html) format.

1 Like