segunda-feira, dezembro 20, 2010

Howto: Asterisk PBX Holidays on your dial plan

Admin World | Howto: Asterisk PBX Greek National Holidays on your dial plan: "Howto: Asterisk PBX Greek National Holidays on your dial plan
Posted by admin on 09/04/2010 Leave a comment (2) Go to comments

Asterisk Logo

Well For some time now i work with asterisk servers and i try to learn as much as i can with a lot of reading and help from other IT friends.

This article is going to show how to tell asterisk which days of the year are holidays for Greece. It’s quite simple as principle but none of the greek IT community posted something similar to help the newbies, at least what i saw through Google. The most compicated part is where you must define the moving calendar holidays like easter and Clear Monday (??) :-) .

This scripting can also work with Other contries if you alter the dates on the astdb’s dates…

This example imports moving holidays to 2020 and standard calendar holidays for end of time….!

First of all create a file called what-ever-you-like.sh and imput the following lines:
#!/bin/sh
asterisk -rx 'database deltree holidays'
asterisk -rx 'database put holidays 20100215 1'
asterisk -rx 'database put holidays 20100402 1'
asterisk -rx 'database put holidays 20100405 1'
asterisk -rx 'database put holidays 20100524 1'
asterisk -rx 'database put holidays 20110307 1'
asterisk -rx 'database put holidays 20110422 1'
asterisk -rx 'database put holidays 20110425 1'
asterisk -rx 'database put holidays 20110613 1'
asterisk -rx 'database put holidays 20120227 1'
asterisk -rx 'database put holidays 20120413 1'
asterisk -rx 'database put holidays 20120416 1'
asterisk -rx 'database put holidays 20120604 1'
asterisk -rx 'database put holidays 20130318 1'
asterisk -rx 'database put holidays 20130503 1'
asterisk -rx 'database put holidays 20130506 1'
asterisk -rx 'database put holidays 20130624 1'
asterisk -rx 'database put holidays 20140303 1'
asterisk -rx 'database put holidays 20140418 1'
asterisk -rx 'database put holidays 20140421 1'
asterisk -rx 'database put holidays 20140609 1'
asterisk -rx 'database put holidays 20150223 1'
asterisk -rx 'database put holidays 20150410 1'
asterisk -rx 'database put holidays 20150413 1'
asterisk -rx 'database put holidays 20150601 1'
asterisk -rx 'database put holidays 20160314 1'
asterisk -rx 'database put holidays 20160429 1'
asterisk -rx 'database put holidays 20160502 1'
asterisk -rx 'database put holidays 20160620 1'
asterisk -rx 'database put holidays 20170227 1'
asterisk -rx 'database put holidays 20170414 1'
asterisk -rx 'database put holidays 20170417 1'
asterisk -rx 'database put holidays 20170605 1'
asterisk -rx 'database put holidays 20180219 1'
asterisk -rx 'database put holidays 20180406 1'
asterisk -rx 'database put holidays 20180409 1'
asterisk -rx 'database put holidays 20180528 1'
asterisk -rx 'database put holidays 20190311 1'
asterisk -rx 'database put holidays 20190426 1'
asterisk -rx 'database put holidays 20190429 1'
asterisk -rx 'database put holidays 20190617 1'
asterisk -rx 'database put holidays 20200302 1'
asterisk -rx 'database put holidays 20200417 1'
asterisk -rx 'database put holidays 20200420 1'
asterisk -rx 'database put holidays 20200608 1'

As you can see the first line removes all entries of the table holidays and then adds the holidays you want in the form %Y%m%d.

Now the line that calls astdb and checks if there is any time record matching the current time ( This is for 1.4) is:
exten => s,n,Set(HOLIDAYS=${DB_EXISTS(holidays/${STRFTIME(${EPOCH},GMT+2,%Y%m%d)})})
This line must be placed in a context that will run this command before starting doing the routing of the call. This command sets the ${HOLIDAYS} variable to 1 if the current date matches the records in the database, and to 0 if not.

Now there are several ways to route your call depending on the ${HOLIDAYS} variable value but i will show the most common and easy for me.

GotoIf Method:
exten => s,n,GotoIf($[${HOLIDAYS}>0]?,,)
This line says that if ${HOLIDAYS}>0 ie 1, then go to the context, context-name,priority and continiue from there. You could also to this:
exten => s,n,GotoIf($[${HOLIDAYS}>0]?,,: ,,)
which says that if it’s true to this else to that.

Now For a complete holidays definition i have set the following rules in the begining of the context that incoming calls are routed.
[context-for-incoming-calls]
exten => s,1,answer
exten => s,n,Set(HOLIDAYS=${DB_EXISTS(holidays/${STRFTIME(${EPOCH},GMT+2,%Y%m%d)})})
; Holidays
exten => s,n,GotoIfTime(*|*|1|jan? s,n,GotoIfTime(*|*|6|jan?context-ext-closed,closed,1)
exten => s,n,GotoIfTime(*|*|25|mar?context-ext-closed,closed,1)
exten => s,n,GotoIfTime(*|*|1|may?context-ext-closed,closed,1)
exten => s,n,GotoIfTime(*|*|15|aug?context-ext-closed,closed,1)
exten => s,n,GotoIfTime(*|*|28|oct?context-ext-closed,closed,1)
exten => s,n,GotoIf($[${HOLIDAYS}>0]?context-ext-closed,closed,1)
exten => s,n,GotoIfTime(*|*|25-26|dec?context-ext-closed,closed,1)
; Working Days
exten => s,n,GotoIfTime(09:00-17:00|mon-fri|*|*?context-ext-open,open,1)
exten => s,n,Goto(biznet-ext-closed,closed,1)
I think is quite straight forward if you look the dates on the definitions. These rules redirect the call to a context-ext-closed if the dates match, which handles the call from there.

I hope this article is usefull for somebody, and i am here to answer any question.

Enjoy!

- Sent using Google Toolbar"

sábado, dezembro 04, 2010

Routing for multiple uplinks/providers

what to do if you have more than one internet provider, two routers and one linux box?
4.2. Routing for multiple uplinks/providers

A common configuration is the following, in which there are two providers that connect a local network (or even a single machine) to the big Internet.

________
+------------+ /
| | |
+-------------+ Provider 1 +-------
__ | | | /
___/ \_ +------+-------+ +------------+ |
_/ \__ | if1 | /
/ \ | | |
| Local network -----+ Linux router | | Internet
\_ __/ | | |
\__ __/ | if2 | \
\___/ +------+-------+ +------------+ |
| | | \
+-------------+ Provider 2 +-------
| | |
+------------+ \________

There are usually two questions given this setup.
4.2.1. Split access

The first is how to route answers to packets coming in over a particular provider, say Provider 1, back out again over that same provider.

Let us first set some symbolical names. Let $IF1 be the name of the first interface (if1 in the picture above) and $IF2 the name of the second interface. Then let $IP1 be the IP address associated with $IF1 and $IP2 the IP address associated with $IF2. Next, let $P1 be the IP address of the gateway at Provider 1, and $P2 the IP address of the gateway at provider 2. Finally, let $P1_NET be the IP network $P1 is in, and $P2_NET the IP network $P2 is in.

One creates two additional routing tables, say T1 and T2. These are added in /etc/iproute2/rt_tables. Like this:

1 T1
2 T2



Then you set up routing in these tables as follows:

ip route add $P1_NET dev $IF1 src $IP1 table T1
ip route add default via $P1 table T1
ip route add $P2_NET dev $IF2 src $IP2 table T2
ip route add default via $P2 table T2


Nothing spectacular, just build a route to the gateway and build a default route via that gateway, as you would do in the case of a single upstream provider, but put the routes in a separate table per provider. Note that the network route suffices, as it tells you how to find any host in that network, which includes the gateway, as specified above.

Next you set up the main routing table. It is a good idea to route things to the direct neighbour through the interface connected to that neighbour. Note the `src' arguments, they make sure the right outgoing IP address is chosen.

ip route add $P1_NET dev $IF1 src $IP1
ip route add $P2_NET dev $IF2 src $IP2


Then, your preference for default route:

ip route add default via $P1


Next, you set up the routing rules. These actually choose what routing table to route with. You want to make sure that you route out a given interface if you already have the corresponding source address:

ip rule add from $IP1 table T1
ip rule add from $IP2 table T2


This set of commands makes sure all answers to traffic coming in on a particular interface get answered from that interface.

Now, this is just the very basic setup. It will work for all processes running on the router itself, and for the local network, if it is masqueraded. If it is not, then you either have IP space from both providers or you are going to want to masquerade to one of the two providers. In both cases you will want to add rules selecting which provider to route out from based on the IP address of the machine in the local network.
4.2.2. Load balancing

The second question is how to balance traffic going out over the two providers. This is actually not hard if you already have set up split access as above.

Instead of choosing one of the two providers as your default route, you now set up the default route to be a multipath route. In the default kernel this will balance routes over the two providers. It is done as follows (once more building on the example in the section on split-access):

ip route add default scope global nexthop via $P1 dev $IF1 weight 1 \
nexthop via $P2 dev $IF2 weight 1


This will balance the routes over both providers. The weight parameters can be tweaked to favor one provider over the other.

Note that balancing will not be perfect, as it is route based, and routes are cached. This means that routes to often-used sites will always be over the same provider.

Furthermore, if you really want to do this, you probably also want to look at Julian Anastasov's patches at http://www.linuxvirtualserver.org/~julian/#routes , Julian's route patch page. They will make things nicer to work with.


Script will look like this
vi route-add-eth0.sh
#START
#/bin/sh
IF1=eth0
IP1=192.168.10.2
P1=192.168.10.254
P1_NET=192.168.10.0


ip route add $P1_NET dev $IF1 src $IP1 table T1
ip route add default via $P1 table T1

ip route add $P1_NET dev $IF1 src $IP1

ip route add default via $P1

ip rule add from $IP1 table T1
#END