Get All Logs of a Number that was Dialed

Hello everyone! I know it’s been a long time since I’ve posted, but today you’re in for a treat.

Every asterisk or FreePBX administrator that has had to troubleshoot by pouring through pages and pages of logs will love this utility.

This is actually the first piece of my Asterisk Logger’s toolkit. This is still unavailable to the public, but it’s a multi-tool kit that, in addition to being able to see the complete logs of any phone call by number, or other keyword, there is a tool that highlights the logs, and another tool that breaks the logfile down into a visual representation so that you can see what percent of your logs are normal vs other conditions, and you can them ‘zoom-in’ to those areas to view the logs.

Contact me if you are interested in these tools.

Anyway, back to the original post, getting all the logs from a phone number: This is a 1-line bash script that gets scans through the ‘full’ log for and channels that match the keyword (a phone number), and then it grep’s the logs for that channel, to give you a continuous log of every event that happened on a particular channel from start to finish, without any other logs in the middle clouding your view.

Here is the Magic!

Here is the script, just name it rr-get-logs-by-channel.sh, don’t forget to chmod +x rr-get-logs-by-channel.sh so you can run it, and maybe create a sym link to /usr/bin/ so you can run it anywhere.

#!/bin/bash
grep $1 /var/log/asterisk/full | grep -o "C-[0-9a-f]\+" | uniq | xargs -I{} grep "\[{}\]" /var/log/asterisk/full
#the code above is one line (starting with grep $1), I know it wraps on some screens.

Change /var/log/asterisk/full to the name of your primary (Verbose) logging file.

Hopefully you found this useful, if you did, please +1! We are a small site trying to rise through the ranks!

Now, for you know-it-alls, let’s break down the script:
#!/bin/bash – this is the ‘magic line’ that tells unix systems to run this file through the /bin/bash interpreter.

We’re going to break the next line down into segments for easy explanation:
grep $1 /var/log/asterisk/full |
This gets every line in the log that has the provided phone number. ($1 is the first argument passed to the script)

grep -o “C-[0-9a-f]\+” | uniq |
This line extracts the channel names from the matching lines, and sends it through uniq which limits the results to one of each channel.

xargs -I{} grep “\[{}\]” /var/log/asterisk/full
This last bit executes a single search against the log for each of the channel names passed from the first part of the command. They are all piped through stdout sequentially, so you can run:
$ ./rr-get-logs-by-channel.sh 7145555555 | less
and see the calls one after another.

This works well on phone numbers, accountcodes, extensions (preface by SIP/, IAX/, or otherwise depending on your setup for best results) less well on extensions without a technology and queue numbers, but it can still be useful in those ‘vague’ situations because it breaks the call apart, so you can skip past irrelevant ones without fear of missing some vital information.

Tagged with: , , , , , ,
Posted in Asterisk

Leave a Reply

Your email address will not be published. Required fields are marked *

*