File: load_balancing.sh

package info (click to toggle)
smstools 3.1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,596 kB
  • ctags: 820
  • sloc: ansic: 14,175; sh: 1,173; php: 115; makefile: 44; awk: 17
file content (76 lines) | stat: -rwxr-xr-x 1,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# ---------------------------------------------------------------------------------------
# This example is for three modems, named GSM1, GSM2 and GSM3 using queues Q1, Q2 and Q3.
# In the global part of smsd.conf, enable message counters:
# stats = /var/spool/sms/stats
# Use zero value for interval if statistics files are not used:
# stats_interval = 0
#
# Enable checkhandler (this script):
# checkhandler = /usr/local/bin/load_balancing.sh
#
# Define queues and providers:
# [queues]
# Q1 = /var/spool/sms/Q1
# Q2 = /var/spool/sms/Q2
# Q3 = /var/spool/sms/Q3
# 
# [providers]
# Q1 = 0,1,2,3,4,5,6,7,8,9,s
# Q2 = 0,1,2,3,4,5,6,7,8,9,s
# Q3 = 0,1,2,3,4,5,6,7,8,9,s
#
# Add queue definition for each modem:
# [GSM1]
# queues = Q1
# etc...
# ---------------------------------------------------------------------------------------

STATSDIR=/var/spool/sms/stats

read_counter()
{
  RESULT=0
  FILE=$STATSDIR/$1.counter

  if [[ -e $FILE ]]
  then
    COUNTER=`formail -zx $1: < $FILE`
    if [ "$COUNTER" != "" ]; then
      RESULT=$COUNTER
    fi
  fi
  return $RESULT
}

# If there is Queue (or Provider) defined, load balancing is ignored:
QUEUE=`formail -zx Queue: < $1`
if [ "$QUEUE" = "" ]; then
  QUEUE=`formail -zx Provider: < $1`
  if [ "$QUEUE" = "" ]; then
    # Read current counters:
    read_counter GSM1
    COUNTER1=$?
    read_counter GSM2
    COUNTER2=$?
    read_counter GSM3
    COUNTER3=$?

    QUEUE=Q1
    COUNTER=$COUNTER1
    if [ $COUNTER2 -lt $COUNTER ]; then
      QUEUE=Q2
      COUNTER=$COUNTER2
    fi
    if [ $COUNTER3 -lt $COUNTER ]; then
      QUEUE=Q3
      COUNTER=$COUNTER3
    fi

    TMPFILE=`mktemp /tmp/smsd_XXXXXX`
    cp $1 $TMPFILE
    formail -f -I "Queue: $QUEUE" < $TMPFILE > $1
    unlink $TMPFILE
  fi
fi
exit 0