Shell script - Asterisk logs report

Dear all,
I start to build script(s) for few tasks, and I'll use log files to complete the following:

1) when ringnoanswer for a particular operator hits count 10 for waittime > 14000 send mail alert with summary of calls
2) per queue - exitwithtimout > 1 in any hour, then send mail alert.
3)  7am each morning mail report showing count for each operator that was  logged in 
the previous day (i.e. midnight to midnight from day before)   with total ringnowanswer > 14000 wait time.

So I have plan to use cron tasks, and bash scripts with queue_log grep for EXITEMPTY, EXITWITHTIMEOUT, RINGNOANSWER, operator name and queue number.
Well, that is just plan for now. Please if you have any ideas, could you please share.. ? May be there is easier way to do this.. I have zabbix already, and data needed is there in sql database. May be there is a way to pull the needed data from there?
thanks in advance
OS: CentOS
Asterisk 11.18.0

---------- Post updated at 12:56 PM ---------- Previous update was at 04:35 AM ----------

My first step is created user "select" for mysql and the following:

mysql -u select -D zabbix -e "select clock,value from history_log" > /tmp/text.log

The output is:

1458182176      1458182148|1458182143.44543|1111|NAME NAME|RINGNOANSWER|4000
1458183289      1458183261|1458183252.44657|1111|NAME NAME|RINGNOANSWER|9000
1458183410      1458183393|1458183388.44682|1111|NAME NAME|RINGNOANSWER|4000
1458183621      1458183596|1458183592.44700|1111|NAME NAME|RINGNOANSWER|4000

So I believe my next step should be to make unix time into readable, may be using:
awk '{ print strftime("%c", $0); }'

I have no idea how to grep only with waittime > 14000 (this is last value after RINGNOANSWER), and how to add them into new text file (may be using cron and update text file with new results instead to rewrite the file every time). After that to check if there is NAME > 10, and if true to send mail with output. This may be will lead to new problem - duplicate emails, so may be when NAME > 10 is true, on email to clear the text file.

Please .. someone.. give idea

strftime("%c",$1) should use column #1.

Could you please show us what your expected final output should be based on your sample?

For RINGNOANSWER:
current output (first column is zabbix alert time - not needed) :

1458182148      1458182148|1458182143.44543|1111|NAME NAME|RINGNOANSWER|4000

it should look like:

 Date time start|date time end|1111|NAME NAME|RINGNOANSWER| only > 14000

For EXITEMPTY current output is (fist column is zabbix time - not needed):

1457841440     1457841440|1457841419.11419|1111|NONE|EXITEMPTY|1|1|20
1457841440     1457841440|1457841419.11419|1111|NONE|EXITEMPTY|1|1|20

it should look like:

date time start| date time end|1111-name1|exitempty|1|1|20
date time start| date time end|1112-name2|exitempty|1|1|20

for EXITWITHTIMEOUT current output is (first column - zabbix time, not needed):

1457844081    1457844081|1457844020.11685|1111|NONE|EXITWITHTIMEOUT|1|1|60
1457844081    1457844081|1457844020.11685|1112|NONE|EXITWITHTIMEOUT|1|1|60

it should look like:

date time start| date time end|1111-name|exitwithtimeout|1|1|60
date time start| date time end|1112-name|exitwithtimeout|1|1|60

-----
what i did so far for exitwithtimeout:

  • pull data from zabbix mysql using user "select" (have only select rights, no password):
mysql -u select -D zabbix -e "select clock,value from history_log;" | 
     grep -v -e endpoint -e ADDMEMBER -e REMOVEMEMBER -e '|02' |  grep EXITWITHTIMEOUT > "/tmp/exitwithtimeout.txt"
  • rename queues:
    sed -i -- 's/|1111|/|1111-Name_1|/g; s/|1112|/|1112-Name_2|/g' /tmp/exitwithtimeout.txt

  • removed |NONE|:

sed -i -- 's/|NONE|/|/g' /tmp/exitwithtimeout.txt
  • removed first column:
cat /tmp/exitwithtimeout.txt | awk '{print $2,$3,$4,$5;}' > /tmp/exitwithtimeout_strp.txt
  • added date (do not know how to convert second column ..):
cat /tmp/exitwithtimeout_strp.txt | while read f1 f2; do echo $(date -d @$f1) $f2; done

-----

Thank you so much for your time helping me!

You need awk. -F and the OFS variable are helpful here
You can set the characters you want to be field separators:

awk -F '[ |]'  '{ awk script here }'

You can set the output field separator with OFS to the the pipe symbol:

awk -F '[ |]'  'BEGIN {OFS="|"} {rest of awk script here }'

You can print to different output files on the fly, plus you can eliminate the first field. And NONE records.
So:

awk -F '[ |]'  'BEGIN {OFS="|"} 
                   /NONE/ {next}   # skip the NONE record
                  {print $2,$3,$4,$5 > "$6"}' inputfile

The output files will be named: EXITEMPTY, RINGNOANSWER and so on.
I do not know what "1111-name" means - what is name in this context?

What I am trying to get you to do is to use one tool for a file transform - when you cram multiple commands together on single lines over and over again your code becomes un-maintainable and error prone. Trying tinkering with the awk code we have so far.

Thanks Jim. I'll try with AWK and will reply with results.

May be someone will ask why I'm not using logs directly from Asterisk. Well the pbx server is on different machine, and it's not possible to do so. At least for now. That's why I'm using zabbix mysql base.

@Jim - quotes like 1111 are for queues. Each queue have his own name, but sadly in logs I have only queue number, so I have to add name. In logs I have 1111, and I need to change it to 1111-name... like 1111-queue_support_team.. and so on.
I know it's not good the way I did it. I'm newbie with this things...., that's why I'm asking you guys... :slight_smile:

---------- Post updated at 02:12 PM ---------- Previous update was at 08:58 AM ----------

Thanks to Jim for pointing me to right direction.. now I have this for exitwithtimeout:

  1. mysql pull data
mysql -u select -D zabbix -e "select clock,value from history_log;" | grep -v -e endpoint -e ADDMEMBER -e REMOVEMEMBER -e '|02' |  grep EXITWITHTIMEOUT > "/tmp/exitwithtimeout.txt"

before text format:

1458314962      1458314937|1458314907.591|1111|NONE|EXITWITHTIMEOUT|1|1|30

first column is zabbix time, not needed. Second column is end time (why, don't ask, asterisk thing I think), third column is start time.

  1. text format:
cat /tmp/exitwithtimeout.txt | awk -F '[ |]'  'BEGIN {OFS="|"} { print strftime("%c|", $2) strftime("%c|", $1) $3,$5,$8; }' | sed 's/|1111|/|1111-Support_team_queue|/g' > exitwithtimeout.log

final result:

Fri 18 Mar 2016 11:28:27 AM EDT|Fri 18 Mar 2016 11:29:22 AM EDT|1111-Support_team_queue|EXITWITHTIMEOUT|30

Good thing - first step is done. I have data and formatted text, and I will do that for other two log files.

Now I have to think how to include only waittime > 14000 into final result, and how to track particular
queue for exitwithtimout > 1 in any hour. WAITTIME and EXITWITHTIMEOUT are part from different files - WAITTIME is from RINGNOANSWER, and queues are in all three,
but I need only from EXITWITHTIMEOUT.

WAITTIME is last one in this output (raw, before text format):

1458220872 1458220872|1458220852.47158|1111|John Wright|RINGNOANSWER|20000

Queue in EXITWITHTIMEOUT is 4th in log file (raw output before text formatting):

1458314937  1458314937|1458314907.591|1111|NONE|EXITWITHTIMEOUT|1|1|30 

---------- Post updated at 04:54 PM ---------- Previous update was at 02:12 PM ----------

Well.. the answer how to include only > 14000 maybe is :

awk -F '[|]' -v x=14000 '$6 > x' /tmp/ringnoanswer.txt

and included with things above:

cat /tmp/ringnoanswer.txt | awk -F '[|]'  'BEGIN {OFS="|" } {print strftime("%c|", $2) strftime("%c|", $1) $3,$4,$5,$6;'} | 
                    awk -F '[|]' -v x=14000 '$6 > x' | sed 's/|1111|/|1111-Support_team_queue|/g'  > ringnoanswer.log

These are my three scripts-like running every hour with cron.

RINGNOANSWER:

mysql -u select -D zabbix -e "select clock,value from history_log where itemid=25143;" |
        grep -v -e endpoint -e ADDMEMBER -e REMOVEMEMBER -e '|0' | 
        grep RINGNOANSWER > "/tmp/ringnoanswer.txt"

wait

cat /tmp/ringnoanswer.txt | awk -F '[|]'  'BEGIN {OFS="|" } {print strftime("%c|", $2) strftime("%c|", $1) $3,$4,$5,$6;'} |
awk -F '[|]' -v x=14000 '$6 > x' |       
        sed 's/|1111|/|1111-NOC_queue|/g;
        s/|1112|/|1112-Support_level_1|/g;
        s/|1113|/|1113-Support_level_2|/g' > /var/log/asterisk_agents_log/ringnoanswer.log

-------
Output:
Sun 20 Mar 2016 10:12:57 PM EDT|Sun 20 Mar 2016 10:13:21 PM EDT|1111-NOC_queue|John Inlall|RINGNOANSWER|20000
Sun 20 Mar 2016 10:40:38 PM EDT|Sun 20 Mar 2016 10:41:23 PM EDT|1111-NOC_queue|Ethan Milyon|RINGNOANSWER|20000
Mon 21 Mar 2016 07:45:18 AM EDT|Mon 21 Mar 2016 07:45:39 AM EDT|1111-NOC_queue|Peter Hill|RINGNOANSWER|20000
------
*waittime is the latest one (20000)

EXITWITHTIMEOUT:

mysql -u select -D zabbix -e "select clock,value from history_log where itemid=25152;" |
        grep -v -e endpoint -e ADDMEMBER -e REMOVEMEMBER -e '|02' | 
        grep EXITWITHTIMEOUT > "/tmp/exitwithtimeout.txt"

wait

cat /tmp/exitwithtimeout.txt | awk -F '[ |]'  'BEGIN {OFS="|"} { print strftime("%c|", $2) strftime("%c|", $1) $3,$5,$8; }' |
        sed 's/|1111|/|1111-NOC_queue|/g;
        s/|1112|/|1112-Support_level_1|/g;
        s/|1113|/|1113-Support_level_2|/g' > /var/log/asterisk_agents_log/exitwithtimeout.log

-----
Output:
Mon 21 Mar 2016 07:45:18 AM EDT|Mon 21 Mar 2016 07:46:08 AM EDT|1113-Support_level_2|EXITWITHTIMEOUT|30
Mon 21 Mar 2016 08:25:23 AM EDT|Mon 21 Mar 2016 08:26:11 AM EDT|1113-Support_level_2|EXITWITHTIMEOUT|30
-----

EXITEMPTY:

mysql -u select -D zabbix -e "select clock,value from history_log where itemid=25161;" |
        grep -v -e endpoint -e ADDMEMBER -e REMOVEMEMBER -e '|02' | 
        grep EXITEMPTY > "/tmp/exitempty.txt"

wait

cat /tmp/exitempty.txt | awk -F '[ |]'  'BEGIN {OFS="|"} { print strftime("%c|", $2) strftime("%c|", $1) $3,$5; }' |
        sed 's/|1111|/|1111-NOC_queue|/g;
        s/|1112|/|1112-Support_level_1|/g;
        s/|1113|/|1113-Support_level_2|/g' > /var/log/asterisk_agents_log/exitempty.log

-----
Output:
Wed 16 Mar 2016 07:53:17 PM EDT|Wed 16 Mar 2016 07:53:31 PM EDT|1112-Support_level_1|EXITEMPTY
Sat 19 Mar 2016 09:43:26 PM EDT|Sat 19 Mar 2016 09:44:38 PM EDT|1112-Support_level_1|EXITEMPTY
-----

Now I'm wondering how to improve them, and how to do the following:

  • when RINGNOANSWER for a particular operator hits count 10 for WAITTIME > 14000. alert with summary of calls
  • if EXITWITHTIMEOUT > 1 per each queue, in any hour - alert with mail including queues with this issue
  • at 7am each morning, send mail report showing count for each operator that was logged in the previous day (ie midnight to midnight from day before)
    with total RINGNOANSWER > 14000 "waittime"

And I am stuck..

print strftime("%c|", $2) strftime("%c|", $1)

is changed to

print strftime("%a %d %b %Y %H:%M:%S %Z|",$2) strftime("%a %d %b %Y %H:%M:%S %Z|",$1)

because when it's executed by cron, some mismatch date output happen

#!/bin/bash

#Subject - Mail body
echo "Subject: Everyday call report - $( date '+%d/%m/%Y' )" > /var/log/asterisk_agents_log/everyday.html
echo "FROM: Alert system <alerts@mail.com>" >> /var/log/asterisk_agents_log/everyday.html
echo "To: NOC - Management <alerts@mail.com>" >> /var/log/asterisk_agents_log/everyday.html
echo "Content-Type: text/html; charset=us-ascii" >> /var/log/asterisk_agents_log/everyday.html

echo "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">" >> /var/log/asterisk_agents_log/everyday.html
echo "<html>" >> /var/log/asterisk_agents_log/everyday.html
echo "<body>" >> /var/log/asterisk_agents_log/everyday.html

echo "<style>" >> /var/log/asterisk_agents_log/everyday.html 
echo "table, th," >> /var/log/asterisk_agents_log/everyday.html
echo "td {border: 1px solid black;border-collapse:collapse;}" >> /var/log/asterisk_agents_log/everyday.html
echo "th {border: 1px solid white; background-color: black;color: white;}," >> /var/log/asterisk_agents_log/everyday.html
echo "td {padding:5px;text-align: center;}</style>" >> /var/log/asterisk_agents_log/everyday.html

echo "<br>" >> /var/log/asterisk_agents_log/everyday.html
echo "RingNoAnswer log" >> /var/log/asterisk_agents_log/everyday.html
echo "<br><br>" >> /var/log/asterisk_agents_log/everyday.html

#Table 1 - START

echo "<table>" >> /var/log/asterisk_agents_log/everyday.html
echo "<tr>" >> /var/log/asterisk_agents_log/everyday.html
echo "<th>Start Date and Time</th>" >> /var/log/asterisk_agents_log/everyday.html 
echo "<th>End Date and Time</th> " >> /var/log/asterisk_agents_log/everyday.html
echo "<th>Queue</th>" >> /var/log/asterisk_agents_log/everyday.html
echo "<th>Operator</th>" >> /var/log/asterisk_agents_log/everyday.html
echo "<th>Issue</th>" >> /var/log/asterisk_agents_log/everyday.html
echo "<th>WaitTime</th>" >> /var/log/asterisk_agents_log/everyday.html
echo "</tr>" >> /var/log/asterisk_agents_log/everyday.html

cat /var/log/asterisk_agents_log/ringnoanswer.log |
        grep "$(date --date '-1 days' +'%d %b')" |
        awk  -F '[|]' '{print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"}' >> /var/log/asterisk_agents_log/everyday.html

echo "</table>" >> /var/log/asterisk_agents_log/everyday.html

#Table 1 - END

echo "<br>" >> /var/log/asterisk_agents_log/everyday.html
echo "ExitEmpty log" >> /var/log/asterisk_agents_log/everyday.html
echo "<br><br>" >> /var/log/asterisk_agents_log/everyday.html

#Table 2 - START

echo "<table>" >> /var/log/asterisk_agents_log/everyday.html
echo "<tr>" >> /var/log/asterisk_agents_log/everyday.html
echo "<th>Start Date and Time</th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "<th>End Date and Time</th> " >> /var/log/asterisk_agents_log/everyday.html            
echo "<th>Queue</th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "<th>Issue</th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "</tr>" >> /var/log/asterisk_agents_log/everyday.html

cat /var/log/asterisk_agents_log/exitempty.log |
        grep "$(date --date '-1 days' +'%d %b')" |
        awk  -F '[|]' '{print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"}' >> /var/log/asterisk_agents_log/everyday.html

echo "</table>" >> /var/log/asterisk_agents_log/everyday.html

#Table 2 - END

echo "<br>" >> /var/log/asterisk_agents_log/everyday.html
echo "ExitWithTimeout log" >> /var/log/asterisk_agents_log/everyday.html
echo "<br><br>" >> /var/log/asterisk_agents_log/everyday.html

#Table 3 - START

echo "<table>" >> /var/log/asterisk_agents_log/everyday.html
echo "<tr>" >> /var/log/asterisk_agents_log/everyday.html
echo "<th>Start Date and Time</th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "<th>End Date and Time</th> " >> /var/log/asterisk_agents_log/everyday.html            
echo "<th>Queue</th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "<th>Issue</th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "<th></th>" >> /var/log/asterisk_agents_log/everyday.html            
echo "</tr>" >> /var/log/asterisk_agents_log/everyday.html

cat /var/log/asterisk_agents_log/exitwithtimeout.log |
        grep "$(date --date '-1 days' +'%d %b')" |
        awk  -F '[|]' '{print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"}' >> /var/log/asterisk_agents_log/everyday.html

echo "</table>" >> /var/log/asterisk_agents_log/everyday.html

#Table 3 - END

echo "<br><br>" >> /var/log/asterisk_agents_log/everyday.html
echo "</body>" >> /var/log/asterisk_agents_log/everyday.html
echo "</html>" >> /var/log/asterisk_agents_log/everyday.html

sleep 3

/usr/lib/sendmail "mail@mail.com" < /var/log/asterisk_agents_log/everyday.html

echo "Alert sent"

this is script for html mail alerts for all yesterday events
I'd love if someone help me little to get this thing done.. PLEASE!

What help do you need?

You have a script. What is it doing wrong?

Hey Don, thank you for answer :slight_smile:

I'm trying to do the following two things. I'm stuck right now :frowning:

  • when RINGNOANSWER for a particular operator hits count 10 for WAITTIME > 14000. alert with summary of calls
  • if EXITWITHTIMEOUT > 1 per each queue, in any hour - alert with mail including queues with this issue

May be with "tail -F" for those three log files?!
Also if someone can help me to improve scripts.. I think they could be useful for other people, no only me.

Thank you in advance.. :slight_smile:

I don't have any of your data to work with and the awk available on my system doesn't include the strftime() function you're using, so the following is untested (and given the number of manual edits made, is likely to contain typos). But, I find this type of code easier to follow than yours and (since it gets rid of well over 50 openings and closings of the file /var/log/asterisk_agents_log/everyday.html , reduces the number of times that directory hierarchy has to be evaluated to just once, and gets rid of several unneeded invocations of cat and grep ) should run faster. I don't see any need for the file everyday.html , but I kept a tee in the script to save the data you were writing to that file if you need it for some other unrelated processing by some other script. If you don't need that file after this script completes, remove the line shown in red in the script below:

#!/bin/bash
cd /var/log/asterisk_agents_log || exit 1
{   # Subject - Mail body

    printf '%s\n' "Subject: Everyday call report - $( date '+%d/%m/%Y' )" \
	'FROM: Alert system <alerts@mail.com>' \
	'To: NOC - Management <alerts@mail.com>' \
	'Content-Type: text/html; charset=us-ascii' \
	'<!doctype html public "-//w3c//dtd html 4.0 transitional//en">' \
	'<html>' \
	'<body>' \
	'<style>' \
	'table, th,' \
	'td {border: 1px solid black;border-collapse:collapse;}' \
	'th {border: 1px solid white; background-color: black;color: white;},' \
	'td {padding:5px;text-align: center;}</style>' \
	'<br>' \
	'RingNoAnswer log' \
	'<br><br>'

    # Table 1 - START

    printf '%s\n' '<table>' \
	'<tr>' \
	'<th>Start Date and Time</th>' \
	'<th>End Date and Time</th> ' \
	'<th>Queue</th>' \
	'<th>Operator</th>' \
	'<th>Issue</th>' \
	'<th>WaitTime</th>' \
	'</tr>'

    awk -F '|' -v day_mon="$(date --date '-1 day' +'%d %b')" '
    $0 ~ day_mon {
	print "<tr>"
	for(i=1;i<=NF;i++)
		print "<td>" $i "</td>"
	print "</tr>"
    }' ringnoanswer.log

    echo '</table>'

    # Table 1 - END

    printf '%s\n' '<br>' \
	'ExitEmpty log' \
	'<br><br>'

    # Table 2 - START

    printf '%s\n' '<table>' \
	'<tr>' \
	'<th>Start Date and Time</th>' \
	'<th>End Date and Time</th>' \
	'<th>Queue</th>' \
	'<th>Issue</th>' \
	'</tr>'

    awk -F '|' -v day_mon="$(date --date '-1 day' +'%d %b')" '
    $0 ~ day_mon {
    	print "<tr>"
	for(i=1;i<=NF;i++)
		print "<td>" $i "</td>"
	print "</tr>"
    }' exitempty.log

    echo '</table>'

    # Table 2 - END

    printf '%s\n' '<br>' \
	'ExitWithTimeout log' \
	'<br><br>'

    # Table 3 - START

    printf '%s\n' '<table>' \
	'<tr>' \
	'<th>Start Date and Time</th>' \
	'<th>End Date and Time</th>' \
	'<th>Queue</th>' \
	'<th>Issue</th>' \
	'<th></th>' \
	'</tr>'

    awk -F '|' -v day_mon="$(date --date '-1 day' +'%d %b')" '
    $0 ~ day_mon {
	print "<tr>"
	for(i=1;i<=NF;i++)
		print "<td>" $i "</td>"
	print "</tr>"
    }' exitwithtimeout.log

    echo '</table>'

    # Table 3 - END

    printf '%s\n' '<br><br>' \
	'</body>' \
	'</html>'
} |
    tee everyday.html |
    /usr/lib/sendmail 'mail@mail.com'

echo 'Alert sent'

I haven't been able to follow all of your repeated updates to the awk scripts you use to create the exitempty.log , exitwithtimeout.log , and ringnoanswer.log files. And you haven't specified what format you want for the alerts you said you want to create in post #10 in this thread. Therefore, I didn't attempt to include code to address those issues in this post.

You mentioned using tail -F , but that doesn't seem appropriate. (Note that tail -F never terminates until you interrupt it. You could set a timer to kill it after you let it run for some period of time, but that seems error prone and complicated for any reason I would think you might want to use it.)

If you would like to show us your latest scripts to produce those three files, we can probably help you simplify them to get rid of unneeded invocations of cat and grep (as was done in the script above) and if you show us the alerts you want to produce from those files, we can probably help you build that into the same awk scripts.

I hope this helps...

Thanks Don for your help. Your code looks much better than mine, I will use it. Later will update..

I tried to cast ALL of this into one single awk script but was not too sure if I got the input file structures right, so YMMV. Why don't you constrain the selection with an adequate where clause on date and waittime in the first place?
However, with a file structure like

/tmp/exitempty.txt
/tmp/exitwithtimeout.txt
/tmp/ringnoanswer.txt
/tmp/starthtml.txt

, try this

awk -F '|' -v day_mon="$(date --date '-1 day' +'%s')" '

function DT(R)  {("date " R) | getline X
                 return X
                }

function STTBL()        {print "<table>" ORS "<tr>" ORS "<th>Start Date and Time</th>" ORS "<th>End Date and Time</th>" ORS "<th>Queue</th>"
                         if (RNA) print "<th>Operator</th>"
                         print "<th>Issue</th>" 
                         if (RNA) print "<th>WaitTime</th>"
                         if (XTO) print "<th></th>" 
                         print "</tr>"
                        }

FNR == NR       {if (NR == 1) print "Subject: Everyday call report - " DT("")
                 print
                 next 
                }
FNR == 1        {if (!FLW)  print "</table>"
                 FLW = 1
                 sub (/^.*\//, "", FILENAME)
                 RNA = (FILENAME ~ /^ri/)   
                 XTO = (FILENAME ~ /^ex.*with/)
                 EMP = (FILENAME ~ /^ex.*emp/) 
                 STTBL()
                }

$2 > day_mon &&
(EMP || XTO || 
$NF >= 14000)   {split ($1, T, "[ \t]*")
                 $1 = DT("-d@" T[2])
                 $2 = DT("-d@" $2)  
                 print "<tr>"
                 for (i=1; i<=NF; i++) print "<td>" $i "</td>"
                 print "</tr>"

                }
END             {print "</table>" ORS "<br><br>" ORS "</body>" ORS "</html>"
                }
' /tmp/st*.txt /tmp/[er]*.txt

and see how far you get...