示例#1
0
    for o, a in opts:
        if o == '-f':
            foreground = True
            continue
        if o == '-P':
            pidfile = a.strip()
            continue
        if o == 'c':
            global_config['conffile'] = a.strip()
            continue

    sip_logger.write(' o reading config "%s"...' % \
      global_config['conffile'])

    f = open(global_config['conffile'])
    config = read_cluster_config(global_config, f.read())

    if not foreground:
        # Shut down the logger and reopen it again to make sure it's worker
        # thread won't be affected by the fork()
        sip_logger.shutdown()
        daemonize(logfile = logfile)
        file(pidfile, 'w').write(str(os.getpid()) + '\n')
        sip_logger = SipLogger('rtp_cluster')

    global_config['sip_logger'] = sip_logger

    sip_logger.write(' o initializing CLI...')

    cli = ClusterCLI(global_config)
示例#2
0
 def receive_command(self, clim, cmd):
     if cmd.startswith('ls'):
         parts = cmd.split(None, 1)
         if len(parts) == 1:
             idx = 0
             for rtp_cluster in self.rtp_clusters:
                 if idx > 0:
                     clim.send('\n')                
                 clim.send('Cluster: #%d\n' % idx)
                 clim.send('    name = %s\n' % rtp_cluster.name)
                 clim.send('    address = %s\n' % str(rtp_cluster.address))
                 clim.send('    active members = %d\n' % len(rtp_cluster.active))
                 clim.send('    inactive members = %d\n' % len(rtp_cluster.pending))
                 idx += 1
         else:
             rtp_cluster, idx = self.cluster_by_name(parts[1])
             if rtp_cluster == None:
                 clim.send('ERROR: %s: cluster not found\n' % parts[1])
                 return False
             clim.send('Active members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in rtp_cluster.active:
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % rtpp.address)
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                     clim.send('        active sessions = ')
                     if rtpp.active_sessions == None:
                         clim.send('UNKNOWN\n')
                     else:
                         clim.send('%d\n' % rtpp.active_sessions)
                 else:
                     clim.send('offline\n')
                 clim.send('        status = %s\n' % rtpp.status)
                 ridx += 1
             clim.send('\nInactive members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in rtp_cluster.pending:
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % rtpp.address)
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                 else:
                     clim.send('offline\n')
                 ridx += 1
             if ridx == 0:
                 clim.send('\n')
         clim.send('OK\n')
         return False
     if cmd.startswith('modify'):
         parts = cmd.split(None, 4)
         rtp_cluster, idx = self.cluster_by_name(parts[1])
         if rtp_cluster == None:
             clim.send('ERROR: %s: cluster not found\n' % parts[1])
             return False
         if parts[2] == 'add':
             kvs = parts[3].split(',')
             rtpp_config = dict([x.split('=') for x in kvs])
             rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
             if rtpp != None:
                 clim.send('ERROR: %s: RTPproxy already exists\n' % rtpp_config['name'])
                 return False
             if rtpp_config['protocol'] not in ('unix', 'udp'):
                 raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
             if rtpp_config['protocol']  == 'udp':
                 address = rtpp_config['address'].split(':', 1)
                 if len(address) == 1:
                     address.append(22222)
                 else:
                     address[1] = int(address[1])
             else:
                 address = rtpp_config['address']
             rtpp = Rtp_cluster_member(rtpp_config['name'], global_config, address)
             if rtpp_config.has_key('wan_address'):
                 rtpp.wan_address = rtpp_config['wan_address']
             rtpp.weight = int(rtpp_config['weight'])
             rtpp.capacity = int(rtpp_config['capacity'])
             rtp_cluster.add_member(rtpp)
             clim.send('OK\n')
             return False
         if parts[2] in ('remove', 'delete', 'pause', 'resume'):
             rtpp, idx = rtp_cluster.rtpp_by_name(parts[3])
             if rtpp == None:
                 clim.send('ERROR: %s: RTPproxy not found\n' % parts[3])
                 return False
             if parts[2] in ('remove', 'delete'):
                 rtp_cluster.bring_down(rtpp)
             elif parts[2] == 'pause':
                 rtpp.status = 'SUSPENDED'
             elif parts[2] == 'resume':
                 rtpp.status = 'ACTIVE'
             clim.send('OK\n')
             return False
     if cmd in ('h', 'help'):
             clim.send('Supported commands:\n' \
               '\tls [CLUSTER_NAME]\n' \
               '\tmodify CLUSTER_NAME [add|remove|delete|pause|resume] ARGS\n' \
               '\treload\n' \
               '\tquit\n')
             return False
     if cmd in ('q', 'quit', 'exit'):
         clim.close()
         return False
     if cmd.startswith('reload'):
         f = open(self.global_config['conffile'])
         config = read_cluster_config(self.global_config, f.read())
         new_rtp_clusters = []
         new_rtpps_count = 0
         for c in config:
             rtp_cluster, idx = self.cluster_by_name(c['name'])
             if rtp_cluster == None:
                 rtp_cluster = Rtp_cluster(global_config, c['name'], c['address'])
             new_rtpps = []
             for rtpp_config in c['rtpproxies']:
                 #Rtp_cluster_member('rtpproxy1', global_config, ('127.0.0.1', 22222))
                 if rtpp_config['protocol'] not in ('unix', 'udp'):
                     raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
                 if rtpp_config['protocol'] == 'udp':
                     address = rtpp_config['address'].split(':', 1)
                     if len(address) == 1:
                         address.append(22222)
                     else:
                         address[1] = int(address[1])
                 else:
                     address = rtpp_config['address']
                 rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
                 if rtpp == None:
                     rtpp = Rtp_cluster_member(rtpp_config['name'], global_config, address)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     if rtpp_config.has_key('wan_address'):
                         rtpp.wan_address = rtpp_config['wan_address']
                     rtp_cluster.add_member(rtpp)
                 else:
                     rtpp.reconnect(address)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     if rtpp_config.has_key('wan_address'):
                         rtpp.wan_address = rtpp_config['wan_address']
                     else:
                         rtpp.wan_address = None
                 new_rtpps.append(rtpp)
             new_rtpps_count += len(new_rtpps)
             for rtpp in [x for x in rtp_cluster.all_members() if x not in new_rtpps]:
                 rtp_cluster.bring_down(rtpp)
             new_rtp_clusters.append(rtp_cluster)
         for rtp_cluster in [x for x in self.rtp_clusters if x not in new_rtp_clusters]:
             rtp_cluster.shutdown()
         self.rtp_clusters = new_rtp_clusters
         clim.send('Loaded %d clusters and %d RTP proxies\n' % (len(self.rtp_clusters), new_rtpps_count))
         clim.send('OK\n')
         return False
     clim.send('ERROR: unknown command\n')
     return False
示例#3
0
 def receive_command(self, clim, cmd):
     if cmd.startswith('ls'):
         parts = cmd.split(None, 1)
         if len(parts) == 1:
             idx = 0
             for rtp_cluster in self.rtp_clusters:
                 nonline = len(rtp_cluster.active)
                 nsuspended = len([x for x in rtp_cluster.active if x.status == 'SUSPENDED'])
                 ndraining = len([x for x in rtp_cluster.active if x.status == 'DRAINING'])                    
                 if idx > 0:
                     clim.send('\n')                
                 clim.send('Cluster: #%d\n' % idx)
                 clim.send('    name = %s\n' % rtp_cluster.name)
                 clim.send('    address = %s\n' % format_address(rtp_cluster.address))
                 clim.send('    online members = %d (%d active, %d suspended, %d draining)\n' % \
                   (nonline, nonline - nsuspended - ndraining, nsuspended, \
                   ndraining))
                 clim.send('    offline members = %d\n' % len(rtp_cluster.pending))
                 idx += 1
         else:
             rtp_cluster, idx = self.cluster_by_name(parts[1])
             if rtp_cluster == None:
                 clim.send('ERROR: %s: cluster not found\n' % parts[1])
                 return False
             clim.send('Online members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in sorted(rtp_cluster.active, key=lambda x: x.name):
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % format_address(rtpp.address))
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 if rtpp.lan_address != None:
                     clim.send('        lan_address = %s\n' % rtpp.lan_address)
                 if rtpp.cmd_out_address != None:
                     clim.send('        cmd_out_address = %s\n' % rtpp.cmd_out_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                     clim.send('        active sessions = ')
                     if rtpp.active_sessions == None:
                         clim.send('UNKNOWN\n')
                     else:
                         clim.send('%d\n' % rtpp.active_sessions)
                     clim.send('        capacity utilization = %f%%\n' % (rtpp.get_caputil() * 100.0))
                     clim.send('        average rtpc delay = %f sec\n' % rtpp.get_rtpc_delay())
                 else:
                     clim.send('offline\n')
                 clim.send('        status = %s\n' % rtpp.status)
                 ridx += 1
             clim.send('\nOffline members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in rtp_cluster.pending:
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % format_address(rtpp.address))
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 if rtpp.lan_address != None:
                     clim.send('        lan_address = %s\n' % rtpp.lan_address)
                 if rtpp.cmd_out_address != None:
                     clim.send('        cmd_out_address = %s\n' % rtpp.cmd_out_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                 else:
                     clim.send('offline\n')
                 ridx += 1
             if ridx == 0:
                 clim.send('\n')
         clim.send('OK\n')
         return False
     if cmd.startswith('modify'):
         parts = cmd.split(None, 4)
         rtp_cluster, idx = self.cluster_by_name(parts[1])
         if rtp_cluster == None:
             clim.send('ERROR: %s: cluster not found\n' % parts[1])
             return False
         if parts[2] == 'add':
             kvs = parts[3].split(',')
             rtpp_config = dict([x.split('=') for x in kvs])
             rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
             if rtpp != None:
                 clim.send('ERROR: %s: RTPproxy already exists\n' % rtpp_config['name'])
                 return False
             if rtpp_config['protocol'] not in ('unix', 'udp', 'udp6'):
                 raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
             if rtpp_config['protocol'] in ('udp', 'udp6'):
                 address = rtpp_config['address'].rsplit(':', 1)
                 if len(address) == 1:
                     address.append(22222)
                 else:
                     address[1] = int(address[1])
                 address = tuple(address)
                 if rtpp_config['protocol'] == 'udp':
                     family = AF_INET
                 else:
                     family = AF_INET6
             else:
                 address = rtpp_config['address']
                 family = AF_UNIX
             if 'cmd_out_address' in rtpp_config:
                 bind_address = rtpp_config['cmd_out_address']
             else:
                 bind_address = None
             rtpp = Rtp_cluster_member(rtpp_config['name'], self.global_config, \
               address, bind_address, family = family)
             if 'wan_address' in rtpp_config:
                 rtpp.wan_address = rtpp_config['wan_address']
             if 'lan_address' in rtpp_config:
                 rtpp.lan_address = rtpp_config['lan_address']
             rtpp.weight = int(rtpp_config['weight'])
             rtpp.capacity = int(rtpp_config['capacity'])
             rtpp.status = rtpp_config['status']
             rtp_cluster.add_member(rtpp)
             clim.send('OK\n')
             return False
         if parts[2] in ('remove', 'delete', 'pause', 'resume'):
             rtpp, idx = rtp_cluster.rtpp_by_name(parts[3])
             if rtpp == None:
                 clim.send('ERROR: %s: RTPproxy not found\n' % parts[3])
                 return False
             if parts[2] in ('remove', 'delete'):
                 rtp_cluster.bring_down(rtpp)
             elif parts[2] == 'pause':
                 rtpp.status = 'SUSPENDED'
             elif parts[2] == 'resume':
                 rtpp.status = 'ACTIVE'
             clim.send('OK\n')
             return False
     if cmd in ('h', 'help'):
             clim.send('Supported commands:\n' \
               '\tls [CLUSTER_NAME]\n' \
               '\tmodify CLUSTER_NAME [add|remove|delete|pause|resume] ARGS\n' \
               '\treload\n' \
               '\tquit\n')
             return False
     if cmd in ('q', 'quit', 'exit'):
         clim.close()
         return False
     if cmd.startswith('reload'):
         f = open(self.global_config['conffile'])
         config = read_cluster_config(self.global_config, f.read())
         new_rtp_clusters = []
         new_rtpps_count = 0
         for c in config:
             rtp_cluster, idx = self.cluster_by_name(c['name'])
             if rtp_cluster == None:
                 rtp_cluster = Rtp_cluster(self.global_config, c['name'], c['address'], \
                   dnconfig = c.get('dnconfig', None))
             else:
                 rtp_cluster.update_dnrelay(c.get('dnconfig', None))
             rtp_cluster.capacity_limit_soft = c.get('capacity_limit_soft', True)
             new_rtpps = []
             for rtpp_config in c['rtpproxies']:
                 #Rtp_cluster_member('rtpproxy1', self.global_config, ('127.0.0.1', 22222))
                 if rtpp_config['protocol'] not in ('unix', 'udp', 'udp6'):
                     raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
                 if rtpp_config['protocol'] in ('udp', 'udp6'):
                     address = rtpp_config['address'].rsplit(':', 1)
                     if len(address) == 1:
                         address.append(22222)
                     else:
                         address[1] = int(address[1])
                     address = tuple(address)
                     if rtpp_config['protocol'] == 'udp':
                         family = AF_INET
                     else:
                         family = AF_INET6
                 else:
                     address = rtpp_config['address']
                     family = AF_UNIX
                 rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
                 if rtpp == None:
                     if 'cmd_out_address' in rtpp_config:
                         bind_address = rtpp_config['cmd_out_address']
                     else:
                         bind_address = None
                     rtpp = Rtp_cluster_member(rtpp_config['name'], self.global_config, \
                       address, bind_address, family = family)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     rtpp.status = rtpp_config['status']
                     if 'wan_address' in rtpp_config:
                         rtpp.wan_address = rtpp_config['wan_address']
                     if 'lan_address' in rtpp_config:
                         rtpp.lan_address = rtpp_config['lan_address']
                     rtpp.status = rtpp_config['status'].upper()
                     rtp_cluster.add_member(rtpp)
                 else:
                     if 'cmd_out_address' in rtpp_config:
                         rtpp.cmd_out_address = rtpp_config['cmd_out_address']
                     else:
                         rtpp.cmd_out_address = None
                     rtpp.reconnect(address)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     rtpp.status = rtpp_config['status']
                     if 'wan_address' in rtpp_config:
                         rtpp.wan_address = rtpp_config['wan_address']
                     else:
                         rtpp.wan_address = None
                     if 'lan_address' in rtpp_config:
                         rtpp.lan_address = rtpp_config['lan_address']
                     else:
                         rtpp.lan_address = None
                     rtpp.status = rtpp_config['status'].upper()
                 new_rtpps.append(rtpp)
             new_rtpps_count += len(new_rtpps)
             for rtpp in [x for x in rtp_cluster.all_members() if x not in new_rtpps]:
                 rtp_cluster.bring_down(rtpp)
             new_rtp_clusters.append(rtp_cluster)
         for rtp_cluster in [x for x in self.rtp_clusters if x not in new_rtp_clusters]:
             rtp_cluster.shutdown()
         self.rtp_clusters = new_rtp_clusters
         clim.send('Loaded %d clusters and %d RTP proxies\n' % (len(self.rtp_clusters), new_rtpps_count))
         clim.send('OK\n')
         return False
     ##if cmd.startswith('objstats'):
     ##    parts = cmd.split(None, 2)
     ##    if len(parts) > 1:
     ##        limit = int(parts[1])
     ##    else:
     ##        limit = 20
     ##    stats = sorted(typestats().items(), key=operator.itemgetter(1),
     ##      reverse=True)
     ##    stats = stats[:limit]
     ##    width = max(len(name) for name, count in stats)
     ##    for name, count in stats[:limit]:
     ##        clim.send('\t%s %d\n' % (name.ljust(width), count))
     ##    return False
     clim.send('ERROR: unknown command\n')
     return False
示例#4
0
            global_config['_rtpc_sockowner'] = (sown_uid, sown_gid)
            continue
        if o == '-d':
            dry_run = True
            foreground = True
            continue
        if o == '-D':
            debug_threads = True
            continue

    sip_logger.write(' o reading config "%s"...' % \
      global_config['conffile'])

    global_config['_sip_logger'] = sip_logger
    f = open(global_config['conffile'])
    config = read_cluster_config(global_config, f.read())

    if not foreground:
        # Shut down the logger and reopen it again to make sure it's worker
        # thread won't be affected by the fork()
        sip_logger.shutdown()
        daemonize(logfile=logfile)
        file(pidfile, 'w').write(str(os.getpid()) + '\n')
        sip_logger = SipLogger('rtp_cluster')
        global_config['_sip_logger'] = sip_logger
        LogSignal(sip_logger, signal.SIGUSR1, reopen, logfile)
    LogSignal(sip_logger, signal.SIGTERM, terminate)

    sip_logger.write(' o initializing CLI...')

    if not dry_run:
示例#5
0
 def receive_command(self, clim, cmd):
     if cmd.startswith('ls'):
         parts = cmd.split(None, 1)
         if len(parts) == 1:
             idx = 0
             for rtp_cluster in self.rtp_clusters:
                 if idx > 0:
                     clim.send('\n')                
                 clim.send('Cluster: #%d\n' % idx)
                 clim.send('    name = %s\n' % rtp_cluster.name)
                 clim.send('    address = %s\n' % str(rtp_cluster.address))
                 clim.send('    active members = %d\n' % len(rtp_cluster.active))
                 clim.send('    inactive members = %d\n' % len(rtp_cluster.pending))
                 idx += 1
         else:
             rtp_cluster, idx = self.cluster_by_name(parts[1])
             if rtp_cluster == None:
                 clim.send('ERROR: %s: cluster not found\n' % parts[1])
                 return False
             clim.send('Active members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in rtp_cluster.active:
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % rtpp.address)
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                     clim.send('        active sessions = ')
                     if rtpp.active_sessions == None:
                         clim.send('UNKNOWN\n')
                     else:
                         clim.send('%d\n' % rtpp.active_sessions)
                 else:
                     clim.send('offline\n')
                 clim.send('        status = %s\n' % rtpp.status)
                 ridx += 1
             clim.send('\nInactive members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in rtp_cluster.pending:
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % rtpp.address)
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                 else:
                     clim.send('offline\n')
                 ridx += 1
             if ridx == 0:
                 clim.send('\n')
         clim.send('OK\n')
         return False
     if cmd.startswith('modify'):
         parts = cmd.split(None, 4)
         rtp_cluster, idx = self.cluster_by_name(parts[1])
         if rtp_cluster == None:
             clim.send('ERROR: %s: cluster not found\n' % parts[1])
             return False
         if parts[2] == 'add':
             kvs = parts[3].split(',')
             rtpp_config = dict([x.split('=') for x in kvs])
             rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
             if rtpp != None:
                 clim.send('ERROR: %s: RTPproxy already exists\n' % rtpp_config['name'])
                 return False
             if rtpp_config['protocol'] not in ('unix', 'udp'):
                 raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
             if rtpp_config['protocol']  == 'udp':
                 address = rtpp_config['address'].split(':', 1)
                 if len(address) == 1:
                     address.append(22222)
                 else:
                     address[1] = int(address[1])
             else:
                 address = rtpp_config['address']
             rtpp = Rtp_cluster_member(rtpp_config['name'], global_config, address)
             if rtpp_config.has_key('wan_address'):
                 rtpp.wan_address = rtpp_config['wan_address']
             rtpp.weight = int(rtpp_config['weight'])
             rtpp.capacity = int(rtpp_config['capacity'])
             rtp_cluster.add_member(rtpp)
             clim.send('OK\n')
             return False
         if parts[2] in ('remove', 'delete', 'pause', 'resume'):
             rtpp, idx = rtp_cluster.rtpp_by_name(parts[3])
             if rtpp == None:
                 clim.send('ERROR: %s: RTPproxy not found\n' % parts[3])
                 return False
             if parts[2] in ('remove', 'delete'):
                 rtp_cluster.bring_down(rtpp)
             elif parts[2] == 'pause':
                 rtpp.status = 'SUSPENDED'
             elif parts[2] == 'resume':
                 rtpp.status = 'ACTIVE'
             clim.send('OK\n')
             return False
     if cmd in ('h', 'help'):
             clim.send('Supported commands:\n' \
               '\tls [CLUSTER_NAME]\n' \
               '\tmodify CLUSTER_NAME [add|remove|delete|pause|resume] ARGS\n' \
               '\treload\n' \
               '\tquit\n')
             return False
     if cmd in ('q', 'quit', 'exit'):
         clim.close()
         return False
     if cmd.startswith('reload'):
         f = open(self.global_config['conffile'])
         config = read_cluster_config(f.read())
         new_rtp_clusters = []
         new_rtpps_count = 0
         for c in config:
             rtp_cluster, idx = self.cluster_by_name(c['name'])
             if rtp_cluster == None:
                 rtp_cluster = Rtp_cluster(global_config, c['name'], c['address'])
             new_rtpps = []
             for rtpp_config in c['rtpproxies']:
                 #Rtp_cluster_member('rtpproxy1', global_config, ('127.0.0.1', 22222))
                 if rtpp_config['protocol'] not in ('unix', 'udp'):
                     raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
                 if rtpp_config['protocol'] == 'udp':
                     address = rtpp_config['address'].split(':', 1)
                     if len(address) == 1:
                         address.append(22222)
                     else:
                         address[1] = int(address[1])
                 else:
                     address = rtpp_config['address']
                 rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
                 if rtpp == None:
                     rtpp = Rtp_cluster_member(rtpp_config['name'], global_config, address)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     if rtpp_config.has_key('wan_address'):
                         rtpp.wan_address = rtpp_config['wan_address']
                     rtp_cluster.add_member(rtpp)
                 else:
                     rtpp.reconnect(address)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     if rtpp_config.has_key('wan_address'):
                         rtpp.wan_address = rtpp_config['wan_address']
                     else:
                         rtpp.wan_address = None
                 new_rtpps.append(rtpp)
             new_rtpps_count += len(new_rtpps)
             for rtpp in [x for x in rtp_cluster.all_members() if x not in new_rtpps]:
                 rtp_cluster.bring_down(rtpp)
             new_rtp_clusters.append(rtp_cluster)
         for rtp_cluster in [x for x in self.rtp_clusters if x not in new_rtp_clusters]:
             rtp_cluster.shutdown()
         self.rtp_clusters = new_rtp_clusters
         clim.send('Loaded %d clusters and %d RTP proxies\n' % (len(self.rtp_clusters), new_rtpps_count))
         clim.send('OK\n')
         return False
     clim.send('ERROR: unknown command\n')
     return False
示例#6
0
    logfile = '/var/log/rtp_cluster.log'
    global_config['conffile'] = '/usr/local/etc/rtp_cluster.xml'
    global_config['_sip_address'] = MyAddress()
    for o, a in opts:
        if o == '-f':
            foreground = True
            continue
        if o == '-P':
            pidfile = a.strip()
            continue
        if o == 'c':
            global_config['conffile'] = a.strip()
            continue

    f = open(global_config['conffile'])
    config = read_cluster_config(f.read())

    if not foreground:
        #print 'foobar'
        # Fork once
        if os.fork() != 0:
            os._exit(0)
        # Create new session
        os.setsid()
        if os.fork() != 0:
            os._exit(0)
        #os.chdir('/')
        fd = os.open('/dev/null', os.O_RDONLY)
        os.dup2(fd, sys.__stdin__.fileno())
        os.close(fd)
        fd = os.open(logfile, os.O_WRONLY | os.O_CREAT | os.O_APPEND)
示例#7
0
 def receive_command(self, clim, cmd):
     if cmd.startswith('ls'):
         parts = cmd.split(None, 1)
         if len(parts) == 1:
             idx = 0
             for rtp_cluster in self.rtp_clusters:
                 nonline = len(rtp_cluster.active)
                 nsuspended = len([x for x in rtp_cluster.active if x.status == 'SUSPENDED'])
                 ndraining = len([x for x in rtp_cluster.active if x.status == 'DRAINING'])                    
                 if idx > 0:
                     clim.send('\n')                
                 clim.send('Cluster: #%d\n' % idx)
                 clim.send('    name = %s\n' % rtp_cluster.name)
                 clim.send('    address = %s\n' % str(rtp_cluster.address))
                 clim.send('    online members = %d (%d active, %d suspended, %d draining)\n' % \
                   (nonline, nonline - nsuspended - ndraining, nsuspended, \
                   ndraining))
                 clim.send('    offline members = %d\n' % len(rtp_cluster.pending))
                 idx += 1
         else:
             rtp_cluster, idx = self.cluster_by_name(parts[1])
             if rtp_cluster == None:
                 clim.send('ERROR: %s: cluster not found\n' % parts[1])
                 return False
             clim.send('Online members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in sorted(rtp_cluster.active, key=lambda x: x.name):
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % str(rtpp.address))
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 if rtpp.lan_address != None:
                     clim.send('        lan_address = %s\n' % rtpp.lan_address)
                 if rtpp.cmd_out_address != None:
                     clim.send('        cmd_out_address = %s\n' % rtpp.cmd_out_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                     clim.send('        active sessions = ')
                     if rtpp.active_sessions == None:
                         clim.send('UNKNOWN\n')
                     else:
                         clim.send('%d\n' % rtpp.active_sessions)
                     clim.send('        capacity utilization = %f%%\n' % (rtpp.get_caputil() * 100.0))
                     clim.send('        average rtpc delay = %f sec\n' % rtpp.get_rtpc_delay())
                 else:
                     clim.send('offline\n')
                 clim.send('        status = %s\n' % rtpp.status)
                 ridx += 1
             clim.send('\nOffline members of the cluster #%d:\n' % idx)
             ridx = 0
             for rtpp in rtp_cluster.pending:
                 if ridx > 0:
                     clim.send('\n')
                 clim.send(    '    RTPproxy: #%d\n' % ridx)
                 clim.send(    '        name = %s\n' % rtpp.name)
                 clim.send(    '        address = %s\n' % str(rtpp.address))
                 if rtpp.wan_address != None:
                     clim.send('        wan_address = %s\n' % rtpp.wan_address)
                 if rtpp.lan_address != None:
                     clim.send('        lan_address = %s\n' % rtpp.lan_address)
                 if rtpp.cmd_out_address != None:
                     clim.send('        cmd_out_address = %s\n' % rtpp.cmd_out_address)
                 clim.send(    '        weight = %d\n' % rtpp.weight)
                 clim.send(    '        capacity = %d\n' % rtpp.capacity)
                 clim.send(    '        state = ')
                 if rtpp.online:
                     clim.send('online\n')
                 else:
                     clim.send('offline\n')
                 ridx += 1
             if ridx == 0:
                 clim.send('\n')
         clim.send('OK\n')
         return False
     if cmd.startswith('modify'):
         parts = cmd.split(None, 4)
         rtp_cluster, idx = self.cluster_by_name(parts[1])
         if rtp_cluster == None:
             clim.send('ERROR: %s: cluster not found\n' % parts[1])
             return False
         if parts[2] == 'add':
             kvs = parts[3].split(',')
             rtpp_config = dict([x.split('=') for x in kvs])
             rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
             if rtpp != None:
                 clim.send('ERROR: %s: RTPproxy already exists\n' % rtpp_config['name'])
                 return False
             if rtpp_config['protocol'] not in ('unix', 'udp', 'udp6'):
                 raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
             if rtpp_config['protocol'] in ('udp', 'udp6'):
                 address = rtpp_config['address'].rsplit(':', 1)
                 if len(address) == 1:
                     address.append(22222)
                 else:
                     address[1] = int(address[1])
                 address = tuple(address)
                 if rtpp_config['protocol'] == 'udp':
                     family = AF_INET
                 else:
                     family = AF_INET6
             else:
                 address = rtpp_config['address']
                 family = AF_UNIX
             if rtpp_config.has_key('cmd_out_address'):
                 bind_address = rtpp_config['cmd_out_address']
             else:
                 bind_address = None
             rtpp = Rtp_cluster_member(rtpp_config['name'], self.global_config, \
               address, bind_address, family = family)
             if rtpp_config.has_key('wan_address'):
                 rtpp.wan_address = rtpp_config['wan_address']
             if rtpp_config.has_key('lan_address'):
                 rtpp.lan_address = rtpp_config['lan_address']
             rtpp.weight = int(rtpp_config['weight'])
             rtpp.capacity = int(rtpp_config['capacity'])
             rtp_cluster.add_member(rtpp)
             clim.send('OK\n')
             return False
         if parts[2] in ('remove', 'delete', 'pause', 'resume'):
             rtpp, idx = rtp_cluster.rtpp_by_name(parts[3])
             if rtpp == None:
                 clim.send('ERROR: %s: RTPproxy not found\n' % parts[3])
                 return False
             if parts[2] in ('remove', 'delete'):
                 rtp_cluster.bring_down(rtpp)
             elif parts[2] == 'pause':
                 rtpp.status = 'SUSPENDED'
             elif parts[2] == 'resume':
                 rtpp.status = 'ACTIVE'
             clim.send('OK\n')
             return False
     if cmd in ('h', 'help'):
             clim.send('Supported commands:\n' \
               '\tls [CLUSTER_NAME]\n' \
               '\tmodify CLUSTER_NAME [add|remove|delete|pause|resume] ARGS\n' \
               '\treload\n' \
               '\tquit\n')
             return False
     if cmd in ('q', 'quit', 'exit'):
         clim.close()
         return False
     if cmd.startswith('reload'):
         f = open(self.global_config['conffile'])
         config = read_cluster_config(self.global_config, f.read())
         new_rtp_clusters = []
         new_rtpps_count = 0
         for c in config:
             rtp_cluster, idx = self.cluster_by_name(c['name'])
             if rtp_cluster == None:
                 rtp_cluster = Rtp_cluster(self.global_config, c['name'], c['address'], \
                   dnconfig = c.get('dnconfig', None))
             else:
                 rtp_cluster.update_dnrelay(c.get('dnconfig', None))
             rtp_cluster.capacity_limit_soft = c.get('capacity_limit_soft', True)
             new_rtpps = []
             for rtpp_config in c['rtpproxies']:
                 #Rtp_cluster_member('rtpproxy1', self.global_config, ('127.0.0.1', 22222))
                 if rtpp_config['protocol'] not in ('unix', 'udp', 'udp6'):
                     raise Exception('Unsupported RTPproxy protocol: "%s"' % rtpp_config['protocol'])
                 if rtpp_config['protocol'] in ('udp', 'udp6'):
                     address = rtpp_config['address'].rsplit(':', 1)
                     if len(address) == 1:
                         address.append(22222)
                     else:
                         address[1] = int(address[1])
                     address = tuple(address)
                     if rtpp_config['protocol'] == 'udp':
                         family = AF_INET
                     else:
                         family = AF_INET6
                 else:
                     address = rtpp_config['address']
                     family = AF_UNIX
                 rtpp, idx = rtp_cluster.rtpp_by_name(rtpp_config['name'])
                 if rtpp == None:
                     if rtpp_config.has_key('cmd_out_address'):
                         bind_address = rtpp_config['cmd_out_address']
                     else:
                         bind_address = None
                     rtpp = Rtp_cluster_member(rtpp_config['name'], self.global_config, \
                       address, bind_address, family = family)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     if rtpp_config.has_key('wan_address'):
                         rtpp.wan_address = rtpp_config['wan_address']
                     if rtpp_config.has_key('lan_address'):
                         rtpp.lan_address = rtpp_config['lan_address']
                     rtpp.status = rtpp_config['status'].upper()
                     rtp_cluster.add_member(rtpp)
                 else:
                     if rtpp_config.has_key('cmd_out_address'):
                         rtpp.cmd_out_address = rtpp_config['cmd_out_address']
                     else:
                         rtpp.cmd_out_address = None
                     rtpp.reconnect(address)
                     rtpp.weight = rtpp_config['weight']
                     rtpp.capacity = rtpp_config['capacity']
                     if rtpp_config.has_key('wan_address'):
                         rtpp.wan_address = rtpp_config['wan_address']
                     else:
                         rtpp.wan_address = None
                     if rtpp_config.has_key('lan_address'):
                         rtpp.lan_address = rtpp_config['lan_address']
                     else:
                         rtpp.lan_address = None
                     rtpp.status = rtpp_config['status'].upper()
                 new_rtpps.append(rtpp)
             new_rtpps_count += len(new_rtpps)
             for rtpp in [x for x in rtp_cluster.all_members() if x not in new_rtpps]:
                 rtp_cluster.bring_down(rtpp)
             new_rtp_clusters.append(rtp_cluster)
         for rtp_cluster in [x for x in self.rtp_clusters if x not in new_rtp_clusters]:
             rtp_cluster.shutdown()
         self.rtp_clusters = new_rtp_clusters
         clim.send('Loaded %d clusters and %d RTP proxies\n' % (len(self.rtp_clusters), new_rtpps_count))
         clim.send('OK\n')
         return False
     if cmd.startswith('objstats'):
         parts = cmd.split(None, 2)
         if len(parts) > 1:
             limit = int(parts[1])
         else:
             limit = 20
         stats = sorted(typestats().items(), key=operator.itemgetter(1),
           reverse=True)
         stats = stats[:limit]
         width = max(len(name) for name, count in stats)
         for name, count in stats[:limit]:
             clim.send('\t%s %d\n' % (name.ljust(width), count))
         return False
     clim.send('ERROR: unknown command\n')
     return False