Color encoding on the disk space script

Hi All,

Hope all are doing good!! Am glad that i have utilized some ideas and written a code to make the disk space result comes better and it was successfully running in the production. The next update from my manager was to make this code to come in a table format with color added.

1) The below code will generate a report for the files which have crossed above 70% and i will FTP the same to the main server ( totally 17 server report files will be FTP'd to the main server.

df -g | awk 'NR>1&&($4+0)>70' > report151.txt
if [ -s report151.txt ]
then
cat report151.txt
else
echo "No file systems found above 70% utilization on the server `hostname`">>report151.txt

2) The main server will send the mail by collating all the report.txt files

 
#!/bin/sh
echo>blank_line
echo "The list of File systems that reached threshold were given below" > mailbody.txt
echo "---------------------------------------------------------------------------------">>mailbody.txt
cat report151.txt blank_line report115.txt blank_line report141.txt blank_line report161.txt>>mailbody.txt
cat mailbody.txt|mailx -s "Disk space Report-$(date +'%A %B %d, %Y')" -r "DSReport@wellpoint.com" hari1189@gmail.com 
 

Now i need these report.txt's to come in the mailbody as table format with the below colors

70% - 80% - AMBER or YELLOW
80% - 100% - RED

Can some one help me out with the above script by modifying the encoding part of that mailbody. Thanks in advance. Hope i will find a better solution from my friends over here.

Thanks
Hari

If you want color in an email, you will need to do HTML.

It'd really help to see what your report looks like...

I gave this away some time ago...

At A Glance Coloured Real Time Bargraph Generator... | Unix Linux Forums | Shell Programming and Scripting

It is an ANALOGUE bargraph generator that has Green, Yellow and Red portions.

It is purely an at a glance readout inside a terminal window...

You might be able to adapt it for your needs...

That's colors in console, not colors in email. Sadly quite different.

1 Like

Hi Corona688...

My bad
(It will teach me to read the whole post properly before jumping in feet first.)

I made an assumption that because the final collation was a pure text file and not HTML that it was eventually going to be displayed inside a terminal...

Thanks for pointing it out...

Hi Corona688,

I'd really appreciate if you can help me out with the HTML encoding as am not aware of it , am unable to do it

df -g | awk 'NR>1&&($4+0)>70' |awk 'NR>1&&($4+0)<80'  > hari.html

Just wrote this single line :slight_smile:

output came like the below format (as normal)
/dev/hd9var 20.75 4.47 79% 22378 3% /var
/dev/itmlv 2.00 0.58 71% 8400 6% /opt/IBM/ITM
/dev/oradatalv 10.00 2.28 78% 17 1% /u02/oradata

I need your help to align this in a table format using HTML code and background of the Used% should be in RED color and foreground should be in black BOLD.

If it is possible to make the result generate like this :expressionless:

Kindly do the needful

---------- Post updated at 04:21 AM ---------- Previous update was at 04:11 AM ----------

df -g | awk 'NR>1&&($4+0)>70' |awk 'NR>1&&($4+0)<80'  > hari.html
export MAILTO="hari1189@gmail.com"
export CONTENT="/home/ac899/hari.html"
export SUBJECT="Disk Space Report"
(
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $CONTENT
) | /usr/sbin/sendmail $MAILTO

Output came like this :

dev/hd9var 20.75 4.47 79% 22378 3% /var /dev/itmlv 2.00 0.58 71% 8400 6% /opt/IBM/ITM /dev/oradatalv 10.00 2.28 78% 17 1% /u02/oradata 

I want this in a table format :'(. Am trying from my side too. But needed your help guys to make this successful

Hi.

If you wrapped some <pre> / </pre> tags either side of cat $CONTENT , at least it would be formatted nicer.

Sure Scott. But if you can help me out by making it comes as a table formatted as requested it would be really appreciatable. I will post the output of your suggestion in my next reply. Thanks a lot

It should't be that difficult, it's just a question of adding the right tags in the right places.

i.e.

df -g | awk '
  BEGIN { print "<table>" }
  NR > 1 {
    print "<tr>"
    for( i = 1; i <= NF; i++ )
      print "<td>" $i "</td>"
    print "</tr>"
  }
  END { print "</table>" }
'

If you don't want an HTML table, but just the data tabulated, use the <pre> tags, and then format the line using printf.

df -g | awk '
  BEGIN { print "<pre>" }
  NR > 1 { 
   printf( "%-20s %8s %8s ... (as you like)\n", "$1", "$2", "$3", ... )
  }
  END { print "</pre>" }
'

This might get you started:

printf "<html>\n<body>\n"
df -g | awk '
  BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}
  NR>1{
    print "<tr>"
    for( i = 1; i <= NF; i++ ) {
      printf "%s", "<td"
      if (i==4&&$i+0>80) printf " bgcolor=#FF3366"
      else if (i==4&&$i+0>70) printf " bgcolor=#FF6633"
      print ">" $i "</td>"
    }
    print "</tr>"
  }
  END { print "</table>" }
' 
printf "</body>\n</html>\n"

Hi Scott and Chubler ,

When i executed the script the result came like

table>
<tr>
<td>/dev/hd4</td>
<td>0.75</td>
<td>0.56</td>
<td>26%</td>
<td>9702</td>
<td>7%</td>
<td>/</td>
</tr>
<tr>
<td>/dev/hd2</td>
<td>5.50</td>
<td>0.34</td>
<td>94%</td>
<td>94242</td>
<td>42%</td>
<td>/usr</td>

This is good but when i gave the resulted thing in the Ultraedit notepad to check the HTML table it displayed an error message.

I really appreciate your help did so far and still a small distance to finish off our lap.

You made me to result the script in a shape at what i expected, but there is some minor error which am unable to figure it out.

Doubts in my mind as am new to this HTML stuff -
1)Chubler_XL -> your script is what i expected and it showed an error when i tried to open it in a HTML document. Doubt is

at which place on that script i have to redirect the output to a text format as i need to FTP (17 server files) to a main one and have to bring it to the email body.

if you help me out to redirect this script to a text or word document , i can open it using CAT command and bring in the mailbody as final report.

Help would be helpful for me as well as others too. Thanks a lot Scott/Cubler_x to make the result gets completed 85%. Still 15% only , please help me

Displayed what error message?

printf "<html>\n<body>\n"
df -g | awk 'NR>1&&($4+0)>70' | awk '
  BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}
  NR>1{
    print "<tr>"
    for( i = 1; i <= NF; i++ ) {
      printf "%s", "<td"
      if (i==4&&$i+0>80) printf " bgcolor=#FF3366"
      else if (i==4&&$i+0>70) printf " bgcolor=#FF6633"
      print ">" $i "</td>"
    }
    print "</tr>"
  }
  END { print "</table>" }
' 
printf "</body>\n</html>\n" > hari.html
cat hari.html | mailx - s " Disk space report  " hari1189@gmail.com
 

The result came in the email like

<html>
<body>
<table border="1" cellpadding="4" style="border-collapse: collapse">
<tr>
<td>/dev/hd9var</td>
<td>20.75</td>
<td>4.42</td>
<td bgcolor=#FF6633>79%</td>
<td>22399</td>
<td>3%</td>
<td>/var</td>
</tr>
<tr>
<td>/dev/hd1</td>
<td>1.50</td>
<td>0.30</td>
<td bgcolor=#FF3366>81%</td>
<td>7252</td>
<td>10%</td>
<td>/home</td>
</tr>
<tr>
<td>/dev/itmlv</td>
<td>2.00</td>
<td>0.58</td>
<td bgcolor=#FF6633>71%</td>
<td>8400</td>
<td>6%</td>
<td>/opt/IBM/ITM</td>
</tr>
<tr>
<td>/dev/productlv</td>
<td>25.00</td>
<td>1.39</td>
<td bgcolor=#FF3366>95%</td>
<td>118645</td>
<td>24%</td>
<td>/u01/app/oracle/product</td>
</tr>
<tr>
<td>/dev/oradatalv</td>
<td>10.00</td>
<td>2.28</td>
<td bgcolor=#FF6633>78%</td>
<td>17</td>
<td>1%</td>
<td>/u02/oradata</td>
</tr>
<tr>
<td>/dev/fsmlv</td>
<td>0.38</td>
<td>0.00</td>
<td bgcolor=#FF3366>100%</td>
<td>454</td>
<td>28%</td>
<td>/opt/FileNet/SysMon</td>
</tr>
</table>
</body>
</html>

Now the happy news is it came so perfectly when i open the HTML code by giving it in a text document outside of my AIX box and execute it by saving it in .html format. When i gave like that the result of that HTML is as attached.

We have to redirect the result to a text or word document and we have to FTP it . Once we redirect it to a document i will do the rest in all the 17 systems and collate it in a single document and mail it . Thanks in advance

Hi.

This is mostly a supplement to the post from wisecracker, and a little about tables, and conversions.

Let us suppose that you have a TAB-separated file:

$ cat data2
/dev/hd9var	20.75	4.47	79%	22378	3%	/var
/dev/itmlv	2.00	0.58	71%	8400	6%	/opt/IBM/ITM
/dev/oradatalv	10.00	2.28	88%	17	1%	/u02/oradata

then one can use an astoundingly short perl code to place this into an ASCII table like so:

$ cat f1
+----------------+-------+------+-----+-------+----+--------------+
| /dev/hd9var    | 20.75 | 4.47 | 79% | 22378 | 3% | /var         |
| /dev/itmlv     |  2.00 | 0.58 | 71% |  8400 | 6% | /opt/IBM/ITM |
| /dev/oradatalv | 10.00 | 2.28 | 88% |    17 | 1% | /u02/oradata |
'----------------+-------+------+-----+-------+----+--------------'

If you arrange for colors to be added for teminal codes, you can translate that, in turn, to bbcode:

$ cat f2
+----------------+-------+------+-----+-------+----+--------------+
| /dev/hd9var    | 20.75 | 4.47 | 79% | 22378 | 3% | /var         |
| /dev/itmlv     |  2.00 | 0.58 | 71% |  8400 | 6% | /opt/IBM/ITM |
| /dev/oradatalv | 10.00 | 2.28 | 88% |    17 | 1% | /u02/oradata |
'----------------+-------+------+-----+-------+----+--------------'

And you can also translate into HTML as noted in the attachment.

The table-creation is done with this:

#!/usr/bin/env perl

# @(#) p1	Demonstrate basic text table.

use Text::ASCIITable;

$t = Text::ASCIITable->new();
# $t->setCols( "AtomicNumber", "Symbol", "Name" );
$t->setCols( "c1", "c2", "c3", "c4", "c5", "c6", "c7" );
$t->setOptions({ hide_HeadRow => 1, hide_FirstLine => 1 });


while (<>) {
  @a = split;
  $t->addRow(@a);
}
print $t;

exit(0);

The perl module ASCIITable was available in repositories for Debian, Ubuntu, Fedora, and CentOS, as well as cpan.

And the conversions from terminal codes are done with:

NAME
       Ansifilter - ANSI escape code processor and converter

SYNOPSIS
       ansifilter [d:i:F:o:s:e:fhptvHRT] [-i input file] [-o output file]
       [--text] [--html] [--latex] [--tex] [--rtf] [input files]

DESCRIPTION
       Ansifilter is a small utility to handle text files containing ANSI
       terminal escape codes. The command sequences may be stripped or be
       interpreted to generate formatted output (HTML, LaTeX, TeX, RTF).

-- found at Andr� Simon - Startseite -- a c++ code, nicely packaged with a Makefile for easy use. It compiled without trouble for me.

The upshot is that the HTML is probably not as nice as a hand-crafted table, but it seems like it's a lot easier, and the result is just a report anyway. If not for the desire to add color, the ASCII table could be easily sent as part of the email body.

( As an aside -- it looks like bbcode conventions do not allow background color settings. )

Best wishes ... cheers, drl

Try something like this

(
printf "To: hari1189@gmail.com\n"
printf "Subject:Disk Space Report\n"
printf "Content-Type: text/html\n"
printf "Content-Disposition: inline\n\n"
printf "<html>\n<body>\n"
df -g | awk 'NR>1&&($4+0)>70' | awk '
  BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}
  NR>1{
    print "<tr>"
    for( i = 1; i <= NF; i++ ) {
      printf "%s", "<td"
      if (i==4&&$i+0>80) printf " bgcolor=#FF3366"
      else if (i==4&&$i+0>70) printf " bgcolor=#FF6633"
      print ">" $i "</td>"
    }
    print "</tr>"
  }
  END { print "</table>" }'
printf "</body>\n</html>\n" ) | sendmail -t

Hi Chubler X:L,

This will work for one server , but am planning to redirect the result to a .HTML format for all 17 servers which am planning to collate in a single email body and bring it as a resulting output. your code works for single server were i got the email . Thanks for that :slight_smile:

Could you please help me out to redirect the below code to a .HTML format were i can use the CAT command to open it up in a email body by giving the below command line.

cat <servername>.html |mailx -s " Disk space result" hari1189@gmail.com

So that i can collect all <servername.html> in a single email body and send it to the users.

The happy news is we have reached 80% of what we expected. Jus a small redirection of .HTML format will help us to finish this thread . Thanks all in advance.

Cheers
Hari

printf "<html>\n<body>\n" df -g | awk 'NR>1&&($4+0)>70' | awk '   BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}   NR>1{     print "<tr>"     for( i = 1; i <= NF; i++ ) {       printf "%s", "<td"       if (i==4&&$i+0>80) printf " bgcolor=#FF3366"       else if (i==4&&$i+0>70) printf " bgcolor=#FF6633"       print ">" $i "</td>"     }     print "</tr>"   }   END { print "</table>" }' printf "</body>\n</html>\n"

Firstly collect the required HTML for your tables on each server like this:

df -g | awk 'NR>1&&($4+0)>70' | awk '
  BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}
  NR>1{
    print "<tr>"
    for( i = 1; i <= NF; i++ ) {
      printf "%s", "<td"
      if (i==4&&$i+0>80) printf " bgcolor=#FF3366"
      else if (i==4&&$i+0>70) printf " bgcolor=#FF6633"
      print ">" $i "</td>"
    }
    print "</tr>"
  }
  END { print "</table>" }' > server1.html

The join them up for your email, with a empty paragraph between each like something like this:

(
printf "To: hari1189@gmail.com\n"
printf "Subject:Disk Space Report\n"
printf "Content-Type: text/html\n"
printf "Content-Disposition: inline\n\n"
printf "<html>\n<body>\n"
cat server1.html
printf "<p></p>\n"
cat server2.html
printf "<p></p>\n"
cat server3.html
...
cat serverN.html
printf "<p></p>\n"
printf "</body>\n</html>\n" ) | sendmail -t
1 Like

:b: THANKS A LOT !!! CHUBLER XL ..

YOUR CODE WORKED CHARMING !!

99% over now. I got the result but i jus want to add the server names in the table

df -g | awk 'NR>1&&($4+0)>70' | awk '   BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}   NR>1{
print "<tr>"
print "<td" printf "bgcolor=#gray" 
print ">" echo `hostname` "</td>"
print "</tr>"
 print "<tr>"     for( i = 1; i <= NF; i++ ) {       printf "%s", "<td"       if (i==4&&$i+0>80) printf " bgcolor=#FF3366"       else if (i==4&&$i+0>70) printf " bgcolor=#FF6633"       print ">" $i "</td>"     }     print "</tr>"   }   END { print "</table>" }' > server1.html

To bring the servername in the table , am i right with the RED colored area of the code. SO that i can split the tables with servernames and it will be easier.

Your help is highly commendable and thanks for the 99% completion . Thanks to all!!!

Cheers
Hari

Best bet is to pass hostname in as an awk variable and prepend it to the line.
Note that used% field is now #5, as hostname pushes all the fields over by 1:

df -g | awk -H=$(hostname) '
  BEGIN {print "<table border=\"1\" cellpadding=\"4\" style=\"border-collapse: collapse\">"}
  NR>1{
    $0= H " " $0
    print "<tr>"
    for( i = 1; i <= NF; i++ ) {
      printf "%s", "<td"
      if (i==5&&$i+0>80) printf " bgcolor=#FF3366"
      else if (i==5&&$i+0>70) printf " bgcolor=#FF6633"
      print ">" $i "</td>"
    }
    print "</tr>"
  }
  END { print "</table>" }' > server1.html

Am getting error message :

awk: Not a recognized flag: H
Usage: awk [-u] [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]

:frowning: