Shell script - Asterisk logs report


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script - Asterisk logs report
# 8  
Old 03-25-2016
Code:
#!/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!
# 9  
Old 03-25-2016
Quote:
Originally Posted by bigbrobg
... ... ...

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?
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 03-25-2016
Hey Don, thank you for answer Smilie

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

- 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.. Smilie
# 11  
Old 03-26-2016
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:
Code:
#!/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...

Last edited by Don Cragun; 03-26-2016 at 09:19 PM.. Reason: Fix typo: s/Table 1/# Table 1/
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 03-26-2016
Thanks Don for your help. Your code looks much better than mine, I will use it. Later will update..
# 13  
Old 03-26-2016
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
Code:
/tmp/exitempty.txt
/tmp/exitwithtimeout.txt
/tmp/ringnoanswer.txt
/tmp/starthtml.txt

, try this
Code:
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...

Last edited by RudiC; 03-26-2016 at 09:46 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Shell script for capturing FTP logs

I have a script #!/bin/bash HOST=ftp.example.com USER=ftpuser PASSWORD=P@ssw0rd ftp -inv $HOST <<EOF user $USER $PASSWORD cd /path/to/file mput *.html bye EOF the script executes sucessfully I need to capture the FTP logs to a logfile should contain FTP Login successful ... (1 Reply)
Discussion started by: rajeshas83
1 Replies

3. Shell Programming and Scripting

Help with Shell Script to View Logs

Hi I'm very new to unix shell scripting. Im also new here in this forum. I'm a SQL Server DBA but I'm slowly learning Oracle and Sybase DB. Our Oracle and Sybase are on Unix platforms. Im slowly learning Linux Admin and Shell Scripting to automate tasks. I'm writing a script to view DB error... (4 Replies)
Discussion started by: Ricky777
4 Replies

4. Shell Programming and Scripting

Managing logs in shell script

Hi, I need write a shell script which should be executed from the crontab every day. This shell script is running several other shell scripts , and each one of them is writing to its log file. Few of the the shell script are also connecting using ssh to some other users on remote machine , do... (1 Reply)
Discussion started by: Yoav
1 Replies

5. Shell Programming and Scripting

Help with extract application logs through shell script in performance testing

Hi Experts, I am new to shell.How to extract logs (Web,APP,Database) using shell in performance testing? Need for webserver logs,app server logs and d/b logs code. Thanks in advance Sree (3 Replies)
Discussion started by: sree vasu
3 Replies

6. Shell Programming and Scripting

Shell Script for GC Logs

Hi, I have a strange situation here, I want to archive gc.logs file, generated by a java application, the strange thing about gc.log file is is doesn't have any time/date stamp appended to it unlike other logs (catalina/access/error) and one more strange thing is when ever the application is... (6 Replies)
Discussion started by: Neeryan
6 Replies

7. Shell Programming and Scripting

Need to develop a script to create a report reading multiple server logs

I am currently trying to develop a script to connect to mulltiple servers, reading specifc data from log files on the servers and append the data from each file into a single tab delimited row. So, at the end I am planning to have a report with all the extracted data with each row per server. I am... (5 Replies)
Discussion started by: scriptingnewbie
5 Replies

8. Shell Programming and Scripting

help with a shell script that greps an error from the logs

Hello everyone. I wrote the following script but the second part is not excecuting. It is not sending the notification by email if the error occurs. the send mail is working so i think the errorr should be in the if statement LOGDIR=/logs/out LOG=`date "+%Y%m%d"`.LOG-FILE.out #the log file ... (11 Replies)
Discussion started by: adak2010
11 Replies

9. Shell Programming and Scripting

Use asterisk in shell script bash

Hello, I am trying to save in a file a single "*" but its not working... look what i am doing... FILE="/home/teste/a.txt" ...BEGIN... ASTERISK="*" echo "STRING $ASTERISK STRING" >> $FILE ...END... when i do it, the result is a list of all files of the current... (4 Replies)
Discussion started by: diogooute
4 Replies

10. Shell Programming and Scripting

Shell script to view logs of a server

Please share a shell script to collect logs of a server (like cpu utilization, memory etc) for a perticular time interval by giving date, time and server name as input. (1 Reply)
Discussion started by: abhishek27
1 Replies
Login or Register to Ask a Question