Forum Discussion

tima-fey_165922's avatar
tima-fey_165922
Icon for Nimbostratus rankNimbostratus
Jun 15, 2015

My irule results to full suspension of bigip ltm

My irule results to full suspension of bigip ltm, an it could be fixed by reboot of ltm there's a code of my irule (i wanna do lots of similar virtual servers and pools) and of course i'm not a programmer so my code isn't good sry for that) i suppose that when i connect to LTM i open connection, and then when i finish all stuff to do, I do not close the session (but i should do this) So am I right, and how can i do this? Thanks Tim

 

!/usr/bin/env python
    def create_pool(obj,pool, lbmethod, pl_mems, monitor):
pool = '/Common/%s' % pool
pmlist = []
MA = {}
MA['pool_name'] = pool
MR = {}
MR['type'] = 'MONITOR_RULE_TYPE_SINGLE'
MR['monitor_templates'] = monitor
quorum ignored
MR['quorum'] = 1
MA['monitor_rule'] = MR

for x in pl_mems.split(','):
    pm = {}
    y = x.split(':')
    pm['address'] = str(y[0])
    pm['port'] = int(y[1])
    pmlist.append(pm)
try:
    pllist = obj.LocalLB.Pool.get_list()
    if pool in pllist:
        obj.LocalLB.Pool.add_member_v2([pool], [pmlist])

    else:
         obj.LocalLB.Pool.create_v2([pool],[lbmethod],[pmlist])
         obj.LocalLB.Pool.set_action_on_service_down([pool],['SERVICE_DOWN_ACTION_RESELECT'])
    if monitor  != False:
        obj.LocalLB.Pool.set_monitor_association([MA])
   return obj.LocalLB.Pool.get_member_v2([pool])
except Exception, e:
    print e

    def load_config():
f_var = open('vs_maker.cfg', 'r')
profiles_ltm = []
for line in f_var:
    if line[0] != '':
        temp = {}
        params_var = line.split(' ')
        temp['host'] = params_var[0]
        temp['ip'] = params_var[1]
        temp['rd0_nat'] = params_var[2]
        temp['rd0_vlan'] = params_var[3]
        temp['rd1_nat'] = params_var[4]
        temp['rd1_vlan'] = params_var[5]
        profiles_ltm.append(temp)
return profiles_ltm


    def create_vs(obj, hostname, ip, port, pool, irule, vlans, ssl_profile, nat_pool, http_class):
definition = {}
resource = {}
profile = {}
profile2 = {}
profiles = []
vlanfilter = {}
vlanfilter['state'] = 'STATE_ENABLED'
vlanfilter['vlans'] = vlans
irule_priority_var = {}
irule_list_var = []
http_class_priority_var = {}
http_class_list_var = []


try:
    definition['name'] = hostname
    definition['address'] = ip
    definition['port'] = port
    definition['protocol'] = 'PROTOCOL_TCP'
    wildmask = '255.255.255.255'
    resource['type'] = 'RESOURCE_TYPE_POOL'
    resource['default_pool_name'] = pool 
    profile['profile_name'] = '/Common/http'
    profiles.append(profile)
    if ssl_profile != False:
        profile2['profile_name'] = '/Common/%s' % ssl_profile[0]
        profile2['profile_context'] = 'PROFILE_CONTEXT_TYPE_CLIENT'
        profiles.append(profile2)
    obj.LocalLB.VirtualServer.create([definition], [wildmask], [resource], [profiles] )
    print 'Set Irule........'
    if irule[0] != False:
        i_var = 0

        for z_VAR in irule:
            i_var = i_var + 1
            irule_priority_var['rule_name'] = z_VAR
            irule_priority_var['priority'] = i_var
            irule_list_var.append(irule_priority_var) 
        obj.LocalLB.VirtualServer.add_rule( [hostname] , [irule_list_var] )

    print 'set http_class.......'
    if http_class[0] != False:
        i_var = 0
        for w_VAR in http_class:
            i_var = i_var + 1
            http_class_priority_var['profile_name'] = w_VAR
            http_class_priority_var['priority'] = i_var
            http_class_list_var.append(http_class_priority_var) 
        obj.LocalLB.VirtualServer.add_httpclass_profile( [hostname] , [http_class_list_var] )
    print 'set vlans......'
    if vlanfilter['vlans'] != False:
        obj.LocalLB.VirtualServer.set_vlan([hostname],[vlanfilter] )
    print 'set nat pool ......'
    if nat_pool  != False:
        obj.LocalLB.VirtualServer.set_snat_pool([hostname],[nat_pool] )
    print 'set persistance .....'
    obj.LocalLB.VirtualServer.add_persistence_profile( virtual_servers = [hostname], profiles = [[ {'profile_name': '/Common/cookie', 'default_profile': 'true'} ]] )



except Exception, e:
    print e


    def split_vs(vs):
try:
    vs_pam_list=[]
    q = vs.split(',')
    for x in q:
        vs_pam = {}
        w = x.split(':')
        vs_pam['ip'] = str(w[0])
        vs_pam['port'] = int(w[1])
        vs_pam['hostname'] = '/Common/%s' % str(w[2])
        vs_pam_list.append(vs_pam)
    return vs_pam_list
except Exception, e:
    print e

    def split_params(params):
try:
    params_pam_list={}
    q = params.split(',')
    irule_list = q[0].split(':')
    i = 0
    temp1= []
    temp1.append(False)
    for zz in irule_list:
        irule_list[i] = '/Common/%s' % zz
        i = i + 1
    i = 0
    http_class_list = q[3].split(':')
    for xx in http_class_list:
        http_class_list[i] = '/Common/%s' % xx
        i = i + 1
    if q[0] == '':
        params_pam_list['irule'] = temp1
    else:
        params_pam_list['irule'] = irule_list
    if q[3] == '':
        params_pam_list['http_class'] = temp1
    else:
        params_pam_list['http_class'] =  http_class_list
    params_pam_list['nat_pool'] = nat
    params_pam_list['interface'] = vlan.split(':')
    if q[1] == '':
        params_pam_list['ssl'] = False
    else:
        params_pam_list['ssl'] = q[1].split(':')
    if q[2] == '':
        params_pam_list['monitor'] = False
    else:
        params_pam_list['monitor'] = q[2].split(':')
    return params_pam_list
except Exception, e:
    print e

    def check_vs(obj, vs, pool, irule, http_class):
try:
    global irule_exists
    global http_class_exists
    virtualservers = obj.LocalLB.VirtualServer.get_list()
    poollist = obj.LocalLB.Pool.get_list()
    irules_list = obj.LocalLB.Rule.get_list()
    for cx in vs:
        if cx['hostname'] in virtualservers:
            print 'vs already exists'
            sys.exit()
    if pool in poollist:
        print 'pool already exists'
        return 'false'
    for zz in irule:

        if zz != False:
            if zz in irules_list:
                irule_exists = True
            else:
                print 'irule doesnt exist'
                print irule
                print irules_list
                sys.exit()  
        else:
            irule_exists = False
    if http_class in locals():
    return True
except Exception, e:
    print e

    def main_task(a):
try:
    print '.........creating %s ... ' %a[2]
    global nat
    global vlan
    global ip
    print 'read config.....'
    config_from_file = load_config()
    for v in  config_from_file:
        if v['host'] == a[0]:
            ip = v['ip']
            if ip[-2] != '%':
                nat = v['rd0_nat']
                vlan = v['rd0_vlan']
            else:
                nat = v['rd1_nat']
                vlan = v['rd1_vlan']

    try:   
        print 'connect to f5.......'
        b = bigsuds.BIGIP(
        hostname = ip, 
        username = a[1], 
        password = upass,
        )
    except Exception, e:
        print e
        print 'somthing goes wrong with connection to f5'
        sys.exit()
    irule_exists = False
    http_class_exists = False
    vs = split_vs(a[2])
    print 'split params....'
    params = split_params(a[5])
    poolname_var = '/Common/%s' % a[3]
    print 'check configs...... wait a minute plz :)'
    check = check_vs(b, vs, poolname_var, params['irule'], params['http_class'] )
    if check == True:
        if poolname_var != '/Common/none':
            print 'create pool........'
            create_pool(b, poolname_var, 'LB_METHOD_LEAST_CONNECTION_NODE_ADDRESS', a[4], params['monitor'])
        else:
            poolname_var = ''

    for gx in vs:
        print 'create VS..........'
        create_vs(b, gx['hostname'], gx['ip'], gx['port'], poolname_var, params['irule'], params['interface'], params['ssl'], params['nat_pool'], params['http_class'])     

except Exception, e:

    print e
    print 'somthing goes wrong with connection to f5'
    sys.exit()


    if __name__ == "__main__":

import bigsuds
import getpass
import sys

 Directory location of bigsuds.py file
sys.path.append(r'/Library/Python/2.7/site-packages/')
ip = ' '
nat = ' '
vlan = ' '


print "\n Please enter your password below.\n"
upass = getpass.getpass()


if len(sys.argv) == 2:
    task_file_var = sys.argv[1]

    task_var = open(task_file_var, 'r')
    for line in task_var:
        if line[0] != '':
            main_task(line.split(' '))


elif len(sys.argv) < 5:
    print "\n\n\tUsage %s BIGIP_address username ip:port:hostname,ip:port:hostname poolname memberlis irule,ssl_profile,monitor,http_class" % sys.argv[0]
    sys.exit()
else:

    line = sys.argv[1:]
    main_task(line)