[英]How to filtering F5 monitors with python script and bigsuds
我嘗試過濾監視器列表,以僅保留那些包含特定字符串的監視器。 我得到了我的功能列表。
import bigsuds
import re
import string
def get_monitors(obj):
try:
return obj.LocalLB.Monitor.get_template_list()
except Exception, e:
print e
try:
b = bigsuds.BIGIP(
hostname = "F5.serveur.com",
username = "user",
password = "password",
)
except Exception, e:
print e
monitors = get_monitors(b)
這是一個結果示例:
[{'template_type': 'TTYPE_DIAMETER', 'template_name': '/Common/diameter'}, {'template_type': 'TTYPE_DNS', 'template_name': '/Common/dns'}, {'template_type': 'TTYPE_HTTP', 'template_name': '/Common/namexxxxx'}]
我需要過濾此列表以僅保留包含“ Common \\ name”的項目
我嘗試例如:
moniteur_ok = list(filter(lambda x: 'Common\name' in x, monitors))
or
moniteur_ok = re.findall(r'(?<=\s)/Common/name\S+' ,monitors)
根據我的需求進行篩選的想法?
然后,我可以通過F5恢復一些信息。
monitor_property= b.LocalLB.Monitor.get_template_string_property(["\Common\namexxxxx"],["STYPE_SEND"],["STYPE_RECEIVE"],["STYPE_TIMEOUT_PACKETS"])
僅設置活動分區( set_active_partition方法)並僅獲取Common監視器會更容易。 這是一個循環遍歷所有分區並在每個分區中打印監視器的示例:
>>> partitions = b.Management.Partition.get_partition_list()
>>> for partition in partitions:
... print "\t{}".format(partition['partition_name'])
... b.Management.Partition.set_active_partition(active_partition=partition['partition_name'])
... mons = b.LocalLB.Monitor.get_template_list()
... for mon in mons:
... print "\t\t{}".format(mon['template_name'])
...
Common
/Common/none
/Common/diameter
/Common/external
/Common/firepass
/Common/ftp
/Common/gateway_icmp
/Common/http
/Common/http_head_f5
/Common/https
/Common/https_443
/Common/https_head_f5
/Common/icmp
/Common/imap
/Common/inband
/Common/ldap
/Common/module_score
/Common/mssql
/Common/mysql
/Common/nntp
/Common/oracle
/Common/pop3
/Common/postgresql
/Common/radius
/Common/radius_accounting
/Common/real_server
/Common/rpc
/Common/sasp
/Common/scripted
/Common/sip
/Common/smb
/Common/smtp
/Common/snmp_dca
/Common/snmp_dca_base
/Common/soap
/Common/tcp
/Common/tcp_echo
/Common/tcp_half_open
/Common/udp
/Common/dns
/Common/virtual_location
/Common/wap
/Common/wmi
/Common/mqtt
/Common/myhttp
bc
/bc/new_http_test
mc
/mc/new_http_test
part1
part2
感謝您的回答,我以您的回答為例。 我還創建了從監視器檢索數據的功能。 然后,我將構建一個json文件以使用Datables顯示數據,例如:
#!/usr/bin/python
import bigsuds
import re
import datetime
import json
# Fonction pour recuperer les strings d'un moniteur
def get_moniteur_type(obj, monitor):
try:
return obj.LocalLB.Monitor.get_template_type([monitor])[0]
except Exception, e:
print e
# Fonction pour recuperer les strings d'un moniteur
def get_stype_string(obj, monitor, stype):
try:
return obj.LocalLB.Monitor.get_template_string_property([monitor],[stype])[0]['value']
except Exception, e:
print e
# Fonction reverse sur la reponse attendue a un test applicatif, si true, on ne doit pas avoir la reponse indiquee dans la receive string
def get_reverse_mode(obj, monitor):
try:
return obj.LocalLB.Monitor.get_template_reverse_mode([monitor])[0]
except Exception, e:
print e
# Fonction pour recuperer l'intervalle et le timeout d'un moniteur
def get_itype_string(obj, monitor, itype):
try:
return obj.LocalLB.Monitor.get_template_integer_property([monitor],[itype])[0]['value']
except Exception, e:
print e
# Fonction pour recuperer le port utilise par le moniteur
def get_moniteur_port(obj, monitor):
try:
return obj.LocalLB.Monitor.get_template_destination([monitor])[0]['ipport']['port']
except Exception, e:
print e
# Information de connexion au F5
try:
b = bigsuds.BIGIP(
hostname = "server-f5.domaine",
username = "user",
password = "password",
)
except Exception, e:
print e
#On recupere tous les moniteurs present dans la partition Common, qui est l'emplacement des moniteurs crees par le reseau
b.Management.Partition.set_active_partition(active_partition='Common')
noms_long = b.LocalLB.Monitor.get_template_list()
#On boucle sur les noms des moniteurs en filtrant ceux crees pour dune
for nom in noms_long:
#On supprime \Common\ dans le nom long pour avoir le nom du moniteur
moniteur = format(nom['template_name'])[8:]
#On ne va recuperer que les moniteurs qui commencent par"startname"
if re.match(r'^startname', moniteur):
print "{ \"moniteur\": \"",moniteur,"\", \"type\": \"",get_moniteur_type(b, moniteur),"\", \"requette_http\": \"",get_stype_string(b, moniteur, "STYPE_SEND"),"\", \"reponse_attendue\": \"",get_stype_string(b, moniteur, "STYPE_RECEIVE"),"\", \"mode_reverse\": \"",get_reverse_mode(b, moniteur),"\", \"timeout\": \"",get_itype_string(b, moniteur, "ITYPE_TIMEOUT"),"\", \"intervale\": \"",get_itype_string(b, moniteur, "ITYPE_INTERVAL"),"\", \"port\": \"",get_moniteur_port(b, moniteur),"\", \"type\": \"",get_moniteur_type(b, moniteur),"\"},"
在此示例中,我感興趣的是恢復應用程序測試,發送的請求和預期的響應,但在全局范圍內,我可以在特定的端口或UDP測試上進行測試。 這都是關於擁有主要數據的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.