Tuesday, December 21, 2010

Asterisk 1.8.1.1 Fixes Google Voice Out

This release fixes the Google Voice out bug. So this would then supersede any patching needed to get Google Voice working on Asterisk 1.8

Get it from asterisk.org

Cheers!

Sunday, December 19, 2010

Nagios Bulk Host Creator (and other bulk operations, if you wish) straight into nagios format


I was at the office today and had to create more than 150 hosts and to create it one by one even with a text editor or WUIs, it can be very taxing. So being the lazy guy that i am, i created this script to generate as many hosts and as an addon, create hostgroup list so that you just need to copy and paste into the hostgroup definition.
This is to create the first of steps when defining Nagios hosts. It of course can be used to generate similar items if they are repeated.
Pre-requisite:
1) You need csvtool (apt-get install csvtool or yum)
So, in essence here are the steps
1) Create a CSV file (in excel or in text editor) with at least 3 fields, hostname, description and IP address. This is probably what your customer had given or your existing inventory.
Example:
MIS-M-3-ASW,MIS Admin Switch 3,192.168.199.103
Where, MIS-M-3-ASW is the hostname, MIS Admin Switch 3 is the description and of course 192.168.199.103 is that hosts IP address

2) Create a script as seen below #nano ngenerator.sh and make it executable #chmod +x ngenerator.sh

###START ###
#!/bin/bash
# sanjay@astiostech.com
#
hosttemplate=switches-host # nagios template to use
inputfile=master.csv # inputfile in csv format only
outputhost=hosts.txt # the output for hosts definition which you can copy paste into nagios
outputservices=services.txt # the output for services (all these hosts) definition which you can copy paste into nagios
columns="1-3" #define like 3-4 to read column 3-4 etc


#start check tool
testcsvtool=`which csvtool`
if [[ $? == "1" ]]; then
    clear
    echo "ERROR"
    echo "No csvtool software found, do a 'apt-get install csvtool' or 'yum install csvtool'"
    exit 1
fi   

#start script
listdata=`csvtool -t ',' col $columns $inputfile`
IFS=`echo -en "\n\b"`
echo "" > $outputhost

# to add all this in hosts.cfg
# modify accordingly
for i in $listdata; do
    #set these
    field1=`echo $i | cut -d "," -f 1`
    field2=`echo $i | cut -d "," -f 2`
    field3=`echo $i | cut -d "," -f 3`

    echo "" >> $outputhost
    echo "# $field1" >> $outputhost
    echo "#----------------------------------------------------------------------" >> $outputhost
    echo "" >> $outputhost
    echo "define host{" >> $outputhost
    echo "        use                        $hosttemplate" >> $outputhost
    echo "        host_name                $field1" >> $outputhost
    echo "        alias                   $field2" >> $outputhost
    echo "        display_name            $field2" >> $outputhost
    echo "        address                    $field3" >> $outputhost
    echo "}" >> $outputhost
    echo "#----------------------------------------------------------------------" >> $outputhost
    echo "" >> $outputhost

done
# to add all this in services.cfg
echo "" > $outputservices
for i in $listdata; do
field1=`echo $i | cut -d "," -f 1`
    echo -n "$field1," >> $outputservices
done
exit 0

###END###
3) Define settings like the hosttemplate name you already created, inputfile file name (the csv file), the outputhost file name (will be the file you use to copy and paste into Nagios, outputservices file where you can use this that lists all the hosts that were imported from your CSV file (you can use e.g. to add to hostgroup or a minimum ping for instance) and the columns in the csv. If you have more than 3 values in your CSV you need to adjust this value and also the amount of fields you wish to import (field1, field2, field3 is currently defined).
4) Run the script and it will take the input csv file and generate your output files hosts.txt and services.txt
5) Paste into your actual nagios host configuration directly, restart nagios daemon.
You can of course define a lot of other stuff if you wish. Just get a little creative. But this script should already help you a lot a little with the initial script creation.

Monday, December 13, 2010

Unified Communications?– Use Google and Asterisk!



Over the past two weekends, I've dwelled in some not-so-light weighted testing of the spanking new Asterisk 1. 8 PBX. While honestly, there’s not a world of improvements from its already awesome predecessors but heck, its technically only a minor version update from the powerhouses 1.4 and 1.6. While there are many tiny yet significant changes, the ones that I've had a chance to work on (as also seen in this blog) are;
1) Google Talk/Voice (and oh, jabber)
2) Calendaring
3) Festival Text To Speech Engine
See, in contrast these three alone, comprise of probably 90% of what those big boys like Microsoft, Cisco and Ayava would claim Unified Communications on their offerings and thus swoop your left leg out of your budget should you then want/need it. Fret not homies, read on..there’s hope with quality and just about nothing spent on software!
Google boasts it’s business apps which is free for 200 users, mostly useless, but some really cool and completely integrated! Like GMail, Google Calendaring, Google Chat, Google Talk and Google Voice!
The missing link here, is, corporatization of these services and not pocketed or isolations.
Office PBXes are probably the first “corporatization” of a communication medium, next to email (but even that’s sometimes are disparate). People don’t really care if you are from @somedomain vs @someotherdomain, as long as you have a domain, you are regarded a more “serious” fish in the sea.
Now, lets jive!. Imagine, if you have a corporate PBX that can integrate with Google Business Apps! Imagine these;
1) Being able to make calls to the rest of the millions of Google (Talk) users worldwide for completely free
2) Send and receive messages via Google, route them through your cellphone and let your phone read that message to you and vis-a-viz reply a person from your cellphone back!
3) Let your PBX read out your calendar events and write impromptu events when on the GO
4) Put yourself as “busy” on your Google status and all calls now go to your voicemail
5) When a call comes to you, let the PBX decide, how to reach you? Your desk phone, cellphone, skype, google-talk, send you an SMS or an instant message that you got a call, do what ever. You can be reached and the lowest cost is decided by the system!
6) Send Fax? Sure, but its only the most ancient way of communicating, hah! But yea go crazy, how do you wish it delivered? Read it to you over the phone? Sure. Print out straight to your printer/fax, easy!. Send it as an email, anytime!
7) With a Text-To-Speech engine and the capability to do just about any dial-plan, use your PBX to read not only calendars, but things like your email, your tasks, your RSS feeds…google docs? yea, that also…
8) With Google, you have a single UI when you sign in, to call/chat/email friends straight from your browser and integrates with your PBX or the other way around
I am tickled silly with these capabilities. It really lies in the mind what you wish to build. The rest, coding etc, get a guy Smile
I hope organizations will soon come to realize the power of Asterisk 1.8 with its partner in crime Google Apps! Happy building.
From the Astiostechtech Geek (engineering) Office,
Sanjay

Sunday, December 12, 2010

Asterisk 1.8 Calendaring with text to speech reading out your calendar info


The Asterisk Calendaring API aims to be a generic interface for integrating Asterisk with various calendaring technologies. The goal is to be able to support reading and writing of calendar events as well as allowing notification of pending events through the Asterisk dialplan.
There are three calendaring modules that ship with Asterisk that provide support for iCalendar, CalDAV, and Microsoft Exchange Server calendars. All three modules support event notification. Both CalDAV and Exchange support reading and writing calendars, while iCalendar is a read-only format.
NOTE: This is for Debians alike. Please modify accordingly for your own distros.
What we are attempting here to do is to read the calendar event on an and in this example/setup a Google Calendar using its iCal link and read the event to the user of his latest schedule.
The Google iCal is currently set to a public accessible link showing Malaysian holidays: (IN HTML FORMAT)
https://www.google.com/calendar/b/0/embed?src=en.malaysia%23holiday@group.v.calendar.google.com&ctz=Asia/Kuala_Lumpur&gsessionid=3UZQTRxAyF3erYCVGGIcMw
Part 1 – Getting calendar for ical to work on Asterisk
You need to get Asterisk 1.8.
Go to your preferred source directory, extract Asterisk source
1) wget http://sourceforge.net/projects/freeassociation/files/libical/libical-0.44/libical-0.44.tar.gz/download
2)  tar -zxvf libical-0.44.tar.gz
3 cd libical-0.44
4) ./configure
5) make && make install
6) apt-get install libneon27 # or use yum
7) apt-get install libneon27-dev # or use yum
8)  (Re)compile and install asterisk (not going to show how here), do a “make menuselect” to ensure that Calendar and Internet Calendar Icalendar is enabled/selected
9) nano /etc/asterisk/calendar.conf # add a sample, like below from google’s malaysia holidays ical file
[myholidays]
type = ical
url =
http://www.google.com/calendar/ical/en.malaysia%23holiday%40group.v.calendar.google.com/public/basic.ics
refresh = 15
timeframe = 1576800


Part 2 – Getting festival (the free text to speech engine), parts taken off a guide from here
1) apt-get install festival # install it with apt or yum
2) nano /etc/festival.scm # modify some configs on how to parse the audio files
3) Add the following inside this file (below), check to ensure the texts are properly copied, i had issues with that causing festival not to start and listen on TCP port 1314.
;; Enable access to localhost (needed by debian users)
(set! server_access_list '("localhost\\.localdomain" "localhost"))

;; set italian voice (comment the following 2 lines to use british_american)
;;; Command for Asterisk begin

(define (tts_textasterisk string mode)
"(tts_textasterisk STRING MODE)
Apply tts to STRING. This function is specifically designed for
use in server mode so a single function call may synthesize the string.
This function name may be added to the server safe functions."
(let ((wholeutt (utt.synth (eval (list 'Utterance 'Text string)))))
(utt.wave.resample wholeutt 8000)
(utt.wave.rescale wholeutt 5)
(utt.send.wave.client wholeutt)))

;;; Command for Asterisk end
4) Copy the init script (so it will auto run next time you reboot)
5) cp /usr/share/doc/festival/examples/festival.init /etc/init.d/festival
6) chmod +x /etc/init.d/festival
7)  update-rc.d festival defaults
8) nano /etc/default/festival # add the line below inside
9) RUN_FESTIVAL=yes
10) nano /etc/asterisk/festival.conf # configure asterisk to use festival, make sure that cache directory exists and permission are okay
[general]
host=localhost
port=1314
usecache=yes
cachedir=/var/cache/asterisk/festival/
festivalcommand=(tts_textasterisk "%s" 'file)(quit)\n

11)  /etc/init.d/festival start # start festival
12)  netstat -an |grep 1314 # it must exist with TCP listening on 0.0.0.0, otherwise, can try starting it manually to see any error # /usr/bin/festival --server --script /etc/festival.scm   this is also good for debugging if the text file is sent to the festival server from asterisk
12) Now, create some dialplans in /etc/asterisk/extensions.conf or anywhere related (like in freepbx would be /etc/asterisk/extension_custom.conf)
exten => gc*cal,1,Answer
exten => gc*cal,n,Set(i=${MATH(${EPOCH}+1576800,int)}) ; 1576800 is about half a year EPOCH
exten => gc*cal,n,NoOp(Math func addon is ${i})
exten => gc*cal,n,Set(id=${CALENDAR_QUERY(myholidays,${EPOCH},${i})})
exten => gc*cal,n,NoOp(Event NoOp ${CALENDAR_QUERY_RESULT(${id},summary)})
exten => gc*cal,n,Set(es=$[${CALENDAR_QUERY_RESULT(${id},summary)}])
exten => gc*cal,n,Set(es=$[${CALENDAR_QUERY_RESULT(${id},summary)}])
;
exten => gc*cal,n,Festival(Event type is)
exten => gc*cal,n,Festival(${es})
;
exten => gc*cal,n,Set(epochstart=${CALENDAR_QUERY_RESULT(${id},start)})
exten => gc*cal,n,set(startdate=${STRFTIME(${epochstart},,%A%t %e%t %B%t %G%t)})
exten => gc*cal,n,set(starttime=${STRFTIME(${epochstart},,%H%t %M%t)}) 
exten => gc*cal,n,Festival(start date)
exten => gc*cal,n,Festival(${startdate})
exten => gc*cal,n,Festival(start time)
exten => gc*cal,n,Festival(${starttime} hours)
;
exten => gc*cal,n,Set(epochend=${CALENDAR_QUERY_RESULT(${id},end)})
exten => gc*cal,n,set(enddate=${STRFTIME(${epochend},,%A%t %e%t %B%t %G%t)})
exten => gc*cal,n,set(endtime=${STRFTIME(${epochend},,%H%t %M%t)}) 
exten => gc*cal,n,Festival(end date)
exten => gc*cal,n,Festival(${enddate})
exten => gc*cal,n,Festival(end time)
exten => gc*cal,n,Festival(${endtime} hours)
;
exten => gc*cal,n,Hangup

13) Remember to restart asterisk at least once for all these to work, as we are loading the calendar module and dialplans in it.
14) You would need a phone that’s capable to dial letters (or change that dialplan to dial a number instead like exten => 1000
15) Dial from your phone gc*cal

Saturday, December 11, 2010

Google Voice on Asterisk 1.8 bug is now fixed!



When Asterisk 1.8 was released, it had a perfect integration with Google Voice @voice.google.com service. Sometime in the 4th week of November, this broke and Gtalk worked (calling other google accounts) but calling Google Voice (e.g calling to US numbers for Free), didn’t.
I just saw this fixed in Digium’s bug tracker here
Here’s how i got my current working Asterisk to apply the patch. I am sure there are more “polished” ways but this just worked for me
1) Go to your Asterisk source directory, mine is #cd /usr/src/asterisk-1.8.0
2) Go to the channels directory #cd channels
3) Backup the original chan_gtalk.c to somewhere # mv chan_gtalk.c /usr/src/somebackup
4) Get the patch # wget http://svnview.digium.com/svn/asterisk/trunk/channels/chan_gtalk.c?revision=297958&pathrev=297958
5) Rename it #mv chan_gtalk.c\?revision\=297958 chan_gtalk.c
6) Go back to asterisk source main directory #cd ..
7) # ./configure
8) make && make install
9) Stop and restart asterisk # asterisk –rx “core stop now”
And voila! Google Voice worked again, just dialed a friend in US for free <joy>, my dialplan had exten => _1. I dial straight from my Deskphone!
Perfect! now lets enjoy our weekend,
PS> If you want some (flexible) dialplans, here an article that can help you. Go to http://highsecurity.blogspot.com/2010/11/googlevoice-asterisk-18-with-freepbx.html

Tuesday, December 7, 2010

Asterisk Script for Recordings to Move or Copy -n- number of days old (an archiving tool if you may)


Here’s' a script that moves or copies certain files (in my case Asterisk recordings) of anything above a certain date (as defined in the CLI) to a destination you pick.
You must first define the sourcepath, sourcefilename, destination and an option to flag move or just copy.
Place it anywhere and execute ./archivefiles 10
-> which means move/copy files that are over 10 days old from date to a desired location.
You could of course run it in cron if you like on a regular basis. Say you run ./
Here’s the script. Copy from the start till the end.
1) nano /usr/bin/archivefiles # create it
2) paste start –> end of script below # copy the script
3) chmod +x /usr/bin/archivefiles # make it executable
NOTE: Make sure your date/time in server is consistent/correct.
###start###
#!/bin/bash
# sanjay@astiostech.com
# usage #archivefiles 10 (meaning archivefiles will find files older than 10 days to do a move or copy)
#
# DEFINE THESE
SOURCEPATH=/var/spool/asterisk/monitor/                                 # With trailing / (slash )
SOURCEFILENAME=*.wav                                                 # filetype
DEST=/usr/src/mytestdst/                                                 # With trailing / (slash )
MOVE=0                                                                # NOTE:0 - copy, 1 - move
#
#
#
# START THE SCRIPT
# CHECK IF OLDER THAN VARIABLE DEFINED
#
OLDERTHAN=$1
if [[ $OLDERTHAN = "" ]]; then
        echo "Define a period. Like 10 for 10 days"
    exit 1
else
        echo ""
fi
# CHECK IF VALUE ENTERED IS AN INTEGER
#
if [ $OLDERTHAN ]; then
    if [ ! $(echo "$OLDERTHAN" | grep -E "^[0-9]+$") ]; then
        echo $OLDERTHAN is not a valid integer.
        exit 1
    else
    if ! [ $OLDERTHAN -ge 0 ] || ! [ $OLDERTHAN -le 10000 ]; then
        echo $OLDERTHAN is an invalid value. Range is [1-10000]
        exit 1
    fi
    fi
fi
# CHECK IF SOURCE DIRECTORY EXIST
#
if [[ -d $SOURCEPATH ]]; then
        echo "OK - Source directory exits"
        cd $SOURCEPATH
else
        echo "Source Directory not found, check your settings... quitting"
        exit 1
fi
grabfiles=`ls -l  $SOURCEPATH$SOURCEFILENAME | head -n 1 | grep -c "\-r"`
if [[ $grabfiles != "0" ]]; then
        echo "OK - Source files exist"
else
        echo "Oops, no source files, check your settings...quitting"
        exit 1
fi
       
# CHECK IF DESTINATION EXISTS
#
if [ -d $DEST ]; then
    echo "OK - Destination directory exits"
else
    echo "Destination directory not found, check your settings... quitting"
    exit 1
fi
# START OUR JOB, DEFINE ACTIONS
#
if [[ $MOVE = "1"  ]];then
   
    find  $SOURCEPATH$SOURCEFILENAME -mtime +$OLDERTHAN -exec mv -f {} $DEST \;
else
    find  $SOURCEPATH$SOURCEFILENAME -mtime +$OLDERTHAN -exec cp -fpr {} $DEST \;
fi   
###end###