[Up]常用資訊

[重點文章] 重點文章 [重點文章] 重點文章

2020年6月14日 星期日

[python] 指定特定幾個 pool 中 的 pool member 連線數

[python] 指定特定幾個 pool 中 的 pool member 連線數

 
#Step: 01 – 特定幾個 pool 中 的 pool member 連線數
特定幾個 pool 中 的 pool member 連線數


#!/usr/bin/python
# -*- coding: UTF-8 -*-
from f5.bigip import ManagementRoot
from f5.utils.responses.handlers import Stats
import datetime
import sys
import getopt
import getpass

#username = 'jenkins'
#password = 'jenkins'
password = getpass.getpass()
username = sys.argv[4]
#password = sys.argv[5]
host_ip = sys.argv[1]
fnames = sys.argv[2]
poolnames = sys.argv[3]
now = datetime.datetime.now()
mgmt = ManagementRoot(host_ip, username, password, token=True)
#my_pool = mgmt.tm.ltm.pools.pool.load(partition='Common', name=(poolnames))
#
pool_lists = [ 'Pool_ssh', 'pool_80_for_172.19.58.121', 'pool_80_for_172.19.58.121' ]

#for poolnamee in pool_lists:
#    my_pool = mgmt.tm.ltm.pools.pool.load(partition='Common', name=(poolnamee))
#    my_pool_mbrs = my_pool.members_s.get_collection()

#my_pool_mbrs = my_pool.members_s.get_collection()

Count = 0
#print ( "\n" )
#print ("\033[0;37;44m\tCurrent Run date and time : \033[0m")
#print (now.strftime("\033[0;37;45m\t%Y-%m-%d %H:%M:%S\t\t\033[0m"))
# 紀錄一下
for poolnamee in pool_lists:
    my_pool = mgmt.tm.ltm.pools.pool.load(partition='Common', name=poolnamee)
    my_pool_mbrs = my_pool.members_s.get_collection()

    print ( "\n" )
    print ("\033[0;37;44m\tCurrent Run date and time : \033[0m")
    print (now.strftime("\033[0;37;45m\t%Y-%m-%d %H:%M:%S\t\t\033[0m"))
    for pool_mbr in my_pool_mbrs:
        mbr_stats = Stats(pool_mbr.stats.load())
        dic_test = mbr_stats.stat.nodeName
        dic_test1 = dic_test['description']
        dic_test2 = dic_test1.replace('/Common/', '')
        dic_btest = mbr_stats.stat.status_availabilityState
        dic_btest1 = dic_btest['description']
        dic_ctest = mbr_stats.stat.serverside_curConns
        dic_ctest1 = dic_ctest['value']
        dic_dtest = mbr_stats.stat.status_enabledState
        dic_dtest1 = dic_dtest['description']
        Count = ((Count+1))
        #print ( "%s_%02d pool_member: [ %s ] 主機狀態 : %s  目前連線數 : \033[43m[ %s ]\033[0m" % (fnames, Count, dic_test2, dic_btest1,  dic_ctest1) )
        if (dic_btest1 == 'available' or dic_dtest1 != 'enabled' or dic_btest1 == 'offline'):
            if (dic_dtest1 == 'enabled' and dic_btest1 == 'available'):
                print("%s  pool_member: [ %s ] 主機狀態 : \033[0;37;42m[ %s ]\033[0m 目前連線數 : \033[43m[ %s ]\033[0m" %(fnames, dic_test2, dic_btest1, dic_ctest1))
            elif (dic_dtest1 != 'disabled' or dic_btest1 != 'offline'):
                print("%s  pool_member: [ %s ] 主機狀態 : \033[0;37;41m[ %s ]\033[0m 目前連線數 : \033[43m[ %s ]\033[0m" %(fnames, dic_test2, dic_dtest1, dic_ctest1))
            elif (dic_btest1 == 'offline'):
                print("%s  pool_member: [ %s ] 主機狀態 : \033[0;37;41m[ %s ]\033[0m 目前連線數 : \033[43m[ %s ]\033[0m" %(fnames, dic_test2, dic_btest1, dic_ctest1))


print ( "\n" )
# vim:set nu et ts=4 sw=4 cino=>4:


#
# python lspool-loop8.py    
# python lspool-loop8.py F5-Host Description pooo_01,pool_02,pool_03 username
#
#使用輸入方式 pool 清單來列出 清單
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from f5.bigip import ManagementRoot
from f5.utils.responses.handlers import Stats
import datetime
import sys
import getopt
import getpass

#username = 'jenkins'
#password = 'jenkins'
password = getpass.getpass()
username = sys.argv[4]
#password = sys.argv[5]
host_ip = sys.argv[1]
fnames = sys.argv[2]
#poolnames = sys.argv[3]
pool_lists = sys.argv[3].split(',')
now = datetime.datetime.now()
mgmt = ManagementRoot(host_ip, username, password, token=True)


Count = 0

for poolnamee in pool_lists:
    my_pool = mgmt.tm.ltm.pools.pool.load(partition='Common', name=poolnamee)
    my_pool_mbrs = my_pool.members_s.get_collection()

    print ( "\n" )
    print ("\033[0;37;44m\tCurrent Run date and time : \033[0m")
    print (now.strftime("\033[0;37;45m\t%Y-%m-%d %H:%M:%S\t\t\033[0m"))
    for pool_mbr in my_pool_mbrs:
        mbr_stats = Stats(pool_mbr.stats.load())
        dic_test = mbr_stats.stat.nodeName
        dic_test1 = dic_test['description']
        dic_test2 = dic_test1.replace('/Common/', '')
        dic_btest = mbr_stats.stat.status_availabilityState
        dic_btest1 = dic_btest['description']
        dic_ctest = mbr_stats.stat.serverside_curConns
        dic_ctest1 = dic_ctest['value']
        dic_dtest = mbr_stats.stat.status_enabledState
        dic_dtest1 = dic_dtest['description']
        Count = ((Count+1))
        #print ( "%s_%02d pool_member: [ %s ] 主機狀態 : %s  目前連線數 : \033[43m[ %s ]\033[0m" % (fnames, Count, dic_test2, dic_btest1,  dic_ctest1) )
        if (dic_btest1 == 'available' or dic_dtest1 != 'enabled' or dic_btest1 == 'offline'):
            if (dic_dtest1 == 'enabled' and dic_btest1 == 'available'):
                print("%s  pool_member: [ %s ] 主機狀態 : \033[0;37;42m[ %s ]\033[0m 目前連線數 : \033[43m[ %s ]\033[0m" %(fnames, dic_test2, dic_btest1, dic_ctest1))
            elif (dic_dtest1 != 'disabled' or dic_btest1 != 'offline'):
                print("%s  pool_member: [ %s ] 主機狀態 : \033[0;37;41m[ %s ]\033[0m 目前連線數 : \033[43m[ %s ]\033[0m" %(fnames, dic_test2, dic_dtest1, dic_ctest1))
            elif (dic_btest1 == 'offline'):
                print("%s  pool_member: [ %s ] 主機狀態 : \033[0;37;41m[ %s ]\033[0m 目前連線數 : \033[43m[ %s ]\033[0m" %(fnames, dic_test2, dic_btest1, dic_ctest1))


print ( "\n" )
# vim:set nu et ts=4 sw=4 cino=>4:


測試 telegraf 使用 snmp discover 主機
monitors:
 - type: telegraf/snmp
   discoveryRule: container_name =~ "snmp" && port == 161
   version: 2
   community: "public"
   fields:
     - name: "uptime"
       oid: ".1.3.6.1.2.1.1.3.0"
       

沒有留言:

張貼留言