Last active
December 1, 2017 07:51
-
-
Save jcPOLO/e4ec08a2ad133b419d87e7e9152af474 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import re | |
import sys | |
L7_LOADBALANCE_POLICY = 'policy-map type' | |
STICKY_GROUP = 'sticky ip-netmask' | |
CLASS_MAP = 'class-map' | |
SERVER_FARM = 'serverfarm host' | |
POLICY_MAP = 'policy-map' | |
def get_config_part(name, part, running_config='ace_running.cfg'): | |
lines = [] | |
with open(running_config) as f: | |
for line in f: | |
if name in line and part in line: | |
lines.append(line) | |
break | |
for line in f: | |
if part in line: | |
break | |
lines.append(line) | |
return "".join(lines) | |
def get_config(): | |
selection = '' | |
selection = input('want to show its run config? (y/n)[n]') | |
if selection == 'y': | |
return True | |
return False | |
def print_balanceo(balanceo): | |
print('{0:30}{1:30}'.format( | |
balanceo['name'], balanceo['status']) | |
) | |
print('') | |
print('{0:30}{1:30}{2:30}{3:30}'.format( | |
'name', 'hit count', 'state', 'vip') | |
) | |
print('{0:30}{1:30}{2:30}{3:30}'.format( | |
'-----------', '-----------', '-----------', '-----------') | |
) | |
for c in balanceo['class']: | |
print('{0:30}{1:30}{2:30}{3:30}'.format( | |
c['name'], | |
c['hit_count'], | |
c['state'], | |
c['vip'])) | |
print("------------------------------------------------------") | |
def get_menu_selection(balanceos): | |
selection = input() | |
if int(selection) < 1 or int(selection) > len(balanceos): | |
return False | |
else: | |
for balanceo in balanceos: | |
if balanceo['id'] == int(selection): | |
return balanceo | |
def print_menu(balanceos): | |
for balanceo in balanceos: | |
for balanceo_vip in balanceo['class']: | |
print('{0:2} - {1:40} {2:30} {3:30}'.format( | |
balanceo['id'], balanceo['name'], balanceo_vip['state'], balanceo_vip['vip']) | |
) | |
# ////////////////////////////////////////////////////////// | |
# Parsea el output del comando sh service-policy det de los ACE | |
# ////////////////////////////////////////////////////////// | |
service_policy_rexp = re.compile(r'Policy-map : (.*?\w+)') | |
pm_status_rexp = re.compile(r'Status : (.*?\w+)') | |
interface_rexp = re.compile(r'Interface: (.*?\w+)') | |
class_name_rexp = re.compile(r' class: (.*?\w+)') | |
l7_policy_rexp = re.compile(r' L7 loadbalance policy: (.*?\w+)') | |
state_rexp = re.compile(r' VIP .tate: (.*?\w+)') | |
curr_conns_rexp = re.compile(r' curr conns : (.*?\w+)') | |
hit_count_rexp = re.compile(r', hit count : (.*?\w+)') | |
dropped_cons_rexp = re.compile(r' dropped conns : (.*?\w+)') | |
vip_rexp = re.compile(r'^.*?\b(tcp|172.|188.|195.)\b.*$') | |
sticky_group_rexp = re.compile(r' sticky group: (.*?\w+)') | |
server_farm_rexp = re.compile(r' primary serverfarm: (.*?\w+)') | |
class_obj = { | |
'id': 0, | |
'name': '', | |
'l7': '', | |
'state': '', | |
'vip': '', | |
'curr_conns': 0, | |
'hit_count': 0, | |
'dropped_cons': 0, | |
'sticky_group': '', | |
'server_farm': '' | |
} | |
policy_map = { | |
'name': '', | |
'status': '', | |
'interface': '', | |
'class': [] | |
} | |
balanceos = [] | |
file_name = sys.argv[1] | |
with open('ace.txt') as f: | |
n = 0 | |
output = f.readlines() | |
for c in output: | |
if policy_map['name'] and service_policy_rexp.findall(c): | |
balanceos.append(policy_map.copy()) | |
policy_map = { | |
'name': '', | |
'status': '', | |
'interface': '', | |
'class': [] | |
} | |
if service_policy_rexp.findall(c): | |
service_policy = service_policy_rexp.findall(c) | |
policy_map['name'] = "".join(service_policy) | |
n = n + 1 | |
policy_map['id'] = n | |
if pm_status_rexp.findall(c): | |
pm_status = pm_status_rexp.findall(c) | |
policy_map['status'] = "".join(pm_status) | |
if interface_rexp.findall(c): | |
interface = interface_rexp.findall(c) | |
policy_map['interface'] = "".join(interface) | |
if class_name_rexp.findall(c): | |
class_name = class_name_rexp.findall(c) | |
class_obj['name'] = "".join(class_name) | |
if l7_policy_rexp.findall(c): | |
l7_policy = l7_policy_rexp.findall(c) | |
class_obj['l7'] = "".join(l7_policy) | |
if state_rexp.findall(c): | |
state = state_rexp.findall(c) | |
class_obj['state'] = "".join(state) | |
if c.find('195.') != -1 or c.find('172.') != -1 or \ | |
c.find('188.') != -1: | |
class_obj['vip'] = c.strip() | |
if curr_conns_rexp.findall(c): | |
curr_conns = curr_conns_rexp.findall(c) | |
class_obj['curr_conns'] = "".join(curr_conns) | |
if hit_count_rexp.findall(c): | |
hit_count = hit_count_rexp.findall(c) | |
class_obj['hit_count'] = "".join(hit_count) | |
if dropped_cons_rexp.findall(c) and class_obj['name']: | |
dropped_cons = dropped_cons_rexp.findall(c) | |
class_obj['dropped_cons'] = "".join(dropped_cons) | |
policy_map['class'].append(class_obj.copy()) | |
class_obj = { | |
'name': '', | |
'l7': '', | |
'state': '', | |
'vip': '', | |
'curr_conns': 0, | |
'hit_count': 0, | |
'dropped_cons': 0 | |
} | |
balanceos.append(policy_map.copy()) | |
print_menu(balanceos) | |
balanceo = get_menu_selection(balanceos) | |
print_balanceo(balanceo) | |
if get_config(): | |
print(get_config_part(balanceo['name'], POLICY_MAP)) | |
print(get_config_part(balanceo['class'][0]['l7'], L7_LOADBALANCE_POLICY)) | |
print(get_config_part('StickyFarm_POSTGIS_PRE_80', STICKY_GROUP)) | |
print(get_config_part(balanceo['class'][0]['name'], CLASS_MAP)) | |
print(get_config_part('Farm_POSTGIS_PRE_80', SERVER_FARM)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment