Difference between revisions of "F5 python scripts"
Jump to navigation
Jump to search
(Created page with "# F5 Python Scripts ## requirements.txt ``` requests pydig fuzzywuzzy pandas tabulate office365 Office365-REST-Python-Client flake8 ``` ## setup virtual environment ``` sudo...") |
|||
Line 11: | Line 11: | ||
Office365-REST-Python-Client | Office365-REST-Python-Client | ||
flake8 | flake8 | ||
+ | f5-sdk | ||
+ | f5-icontrol-rest | ||
``` | ``` | ||
Latest revision as of 20:35, 15 August 2023
F5 Python Scripts
requirements.txt
requests pydig fuzzywuzzy pandas tabulate office365 Office365-REST-Python-Client flake8 f5-sdk f5-icontrol-rest
setup virtual environment
sudo apt install expect sqlite3 python3 -m venv .venv . .venv/bin/activate pip install -r requirements.txt
Get VS, Pools, Nodes & stats
#!/usr/bin/env python3 import os import sys import time import json from f5.utils.responses.handlers import Stats from IPython.display import display from tabulate import tabulate import pandas as pd import pprint pp = pprint.PrettyPrinter(width=1180) from f5.bigip import BigIP from f5.bigip import ManagementRoot import sqlite3 # cnx = sqlite3.connect(':memory:') cnx = sqlite3.connect('df_sqlite3.db') F5_HOST = os.getenv('F5_HOST') AD_USER = os.getenv('AD_USER') AD_PASS = os.environ.get('AD_PASS') LTM_API_URL_PREFIX = "https://{F5_HOST}/mgmt/tm/ltm/" mgmt = ManagementRoot(F5_HOST, AD_USER, AD_PASS) bigip = BigIP(F5_HOST, AD_USER, AD_PASS) def get_virtuals(action="disable"): columns = ['partition', 'name', 'state', 'clientside_totConns', 'clientside_bitsIn', 'clientside_bitsOut', 'type'] virtual_rows = [] virtuals_df = pd.DataFrame(columns=columns) unused_virtuals_df = pd.DataFrame(columns=columns) used_virtuals_df = pd.DataFrame(columns=columns) all_virtuals = mgmt.tm.ltm.virtuals.get_collection() for virtual in all_virtuals: row = {} # pp.pprint(vars(virtual)) virtual_stats = Stats(virtual.stats.load()) # pp.pprint(virtual_stats.stat) row['partition'] = virtual.partition row['name'] = virtual.name row['state'] = virtual_stats.stat.status_availabilityState['description'] row['clientside_totConns'] = virtual_stats.stat.clientside_totConns['value'] row['clientside_bitsIn'] = virtual_stats.stat.clientside_bitsIn['value'] row['clientside_bitsOut'] = virtual_stats.stat.clientside_bitsOut['value'] row['type'] = "virtualserver" virtual_rows.append(row) virtuals_df = pd.concat([virtuals_df, pd.DataFrame([row])], ignore_index=False) if row['clientside_totConns'] != 0: print(f"used: {row}") unused_virtuals_df = pd.concat([virtuals_df, pd.DataFrame([row])], ignore_index=False) elif row['clientside_totConns'] == 0: print(f"unused: {row}") used_virtuals_df = pd.concat([virtuals_df, pd.DataFrame([row])], ignore_index=False) return virtuals_df, unused_virtuals_df, used_virtuals_df def get_pools(get_members=False, delete=False): columns = ['partition', 'name', 'state', 'serverside_totConns', 'serverside_bitsIn', 'serverside_bitsOut', 'type'] pool_rows = [] member_rows = [] all_pools = mgmt.tm.ltm.pools.get_collection() pools_df = pd.DataFrame(columns=columns) members_df = pd.DataFrame(columns=columns) unused_pools_df = pd.DataFrame(columns=columns) unused_members_df = pd.DataFrame(columns=columns) used_pools_df = pd.DataFrame(columns=columns) used_members_df = pd.DataFrame(columns=columns) for pool in all_pools: row = {} pool_stats = Stats(pool.stats.load()) # pp.pprint(vars(pool_stats)) row['partition'] = pool.partition row['name'] = pool.name row['type'] = "pool" row['state'] = pool_stats.stat.status_availabilityState['description'] row['availableMemberCnt'] = pool_stats.stat.availableMemberCnt['value'] row['status_enabledState'] = pool_stats.stat.status_enabledState['description'] row['serverside_bitsIn'] = pool_stats.stat.serverside_bitsIn['value'] row['serverside_bitsOut'] = pool_stats.stat.serverside_bitsOut['value'] row['serverside_totConns'] = pool_stats.stat.serverside_totConns['value'] row['status_statusReason'] = pool_stats.stat.status_statusReason['description'] row['activeMemberCnt'] = pool_stats.stat.activeMemberCnt['value'] pool_rows.append(row) pools_df = pd.concat([pools_df, pd.DataFrame([row])], ignore_index=False) if row['serverside_totConns'] == 0: print(f"unused pool: {row}") unused_pools_df = pd.concat([pools_df, pd.DataFrame([row])], ignore_index=False) elif row['serverside_totConns'] != 0: print(f"used pool: {row}") used_pools_df = pd.concat([pools_df, pd.DataFrame([row])], ignore_index=False) if get_members == True: for member in pool.members_s.get_collection(): row = {} member_stats = Stats(member.stats.load()) # pp.pprint(vars(member_stats)); break row['partition'] = member.partition row['name'] = member.name row['type'] = "poolmember" row['pool'] = pool.name row['state'] = member_stats.stat.status_availabilityState['description'] row['serverside_bitsIn'] = member_stats.stat.serverside_bitsIn['value'] row['serverside_bitsOut'] = member_stats.stat.serverside_bitsOut['value'] row['serverside_totConns'] = member_stats.stat.serverside_totConns['value'] member_rows.append(row) members_df = pd.concat([members_df, pd.DataFrame([row])], ignore_index=False) if row['serverside_totConns'] == 0: print(f"disable member: {row}") unused_members_df = pd.concat([members_df, pd.DataFrame([row])], ignore_index=False) elif row['serverside_totConns'] != 0: used_members_df = pd.concat([members_df, pd.DataFrame([row])], ignore_index=False) return pools_df, members_df, unused_pools_df, unused_members_df, used_pools_df, used_members_df def print_unused(): print("To disable =====") for index, row in virtuals_df.iterrows(): if row['clientside_maxConns'] != 0: print(row['clientside_maxConns']) def main(): virtuals_df, unused_virtuals_df, used_virtuals_df = get_virtuals() pools_df, members_df, unused_pools_df, unused_members_df, used_pools_df, used_members_df = get_pools(get_members=True) with pd.ExcelWriter('F5.xlsx') as writer: virtuals_df.to_excel(writer, sheet_name='all_virtuals') pools_df.to_excel(writer, sheet_name='all_pools') members_df.to_excel(writer, sheet_name='all_members') unused_virtuals_df.to_excel(writer, sheet_name='unused_virtuals') unused_pools_df.to_excel(writer, sheet_name='unused_pools') unused_members_df.to_excel(writer, sheet_name='unused_members') used_virtuals_df.to_excel(writer, sheet_name='used_virtuals') used_pools_df.to_excel(writer, sheet_name='used_pools') used_members_df.to_excel(writer, sheet_name='used_members') virtuals_df.to_sql(name='virtuals', con=cnx) unused_virtuals_df.to_sql(name='unused_virtuals', con=cnx) used_virtuals_df.to_sql(name='used_virtuals', con=cnx) members_df.to_sql(name='members', con=cnx) unused_members_df.to_sql(name='unused_members', con=cnx) used_members_df.to_sql(name='used_members', con=cnx) pools_df.to_sql(name='pools', con=cnx) unused_pools_df.to_sql(name='unused_pools', con=cnx) used_pools_df.to_sql(name='used_pools', con=cnx) print("=====Complete=====") if __name__ == "__main__": main()