Problem this snippet solves:
Synopsis: This code will iterate over a list of pools and populate a webpage listing down nodes only, per pool. You’ll need Python2.5, cgi, and a webserver to get this all done. Apart of the main code is a blacklist program which allows you to edit a list of pools you wouldn’t otherwise want to monitor.*
How to use this snippet:
Installation
- Copy contents of cgi-bin and www files to their respective places.
- Apache will need to be configured for CGI, with .py extensions enabled.
- The apache2 user, (www-data) will need read/write perms to black.list in the www directory.
- Both blacklist.py and nodeAlert.py will need to be configured. You’ll want to add username/password (readonly is plenty) and the IP of your f5 to each respective script.
- You’ll need to add your own bar.png and orangex.png
nodeAlert.cgi
Code :
#!/usr/bin/python2.5
import os, sys
from time import asctime
from socket import gethostbyaddr
import pycontrol.pyControl as pyControl
#################
# David C. Isom #
# RR Bowker LLC #
# August 2008 #
########################
# BIG IP CONFIG #
host = '123.4.56.789' #
username = 'readonly' #
password = 'readonly' #
########################
print "Content-Type: text/html\n"
print
print "<title>F5 Status</title>\n"
print "<meta http-equiv="refresh" content="60">"
print "<font size="6" color="white">F5 Node Status</font> -<font color="666666">",asctime(),"</font>"
print "<a href="https://community.f5.com/t5/blacklist.cgi" rel="nofollow noopener noreferrer"><font color="white">Blacklist</font></a>"
# these get cached on the box anyway(linux)
def nslook(ip):
try:
output = gethostbyaddr(ip)
return output[0]
except:
output = "Host_Name_Not_Found"
return output
def blackcheck(poolName):
blackFile = open("/var/www/black.list", "r")
while True:
line = blackFile.readline()[:-1]
if len(line) == 0:
break
if str(line) == str(poolName):
return True
blackFile.close()
# Create object and bitbucket the wsdl messages
sys.stdout = open(os.devnull, "w")
b = pyControl.BIGIP(
hostname = host,
username = username,
password = password,
wsdl_files = ['LocalLB.Pool', 'LocalLB.PoolMember']
)
sys.stdout = sys.__stdout__
# quick object recreation
p = b.LocalLB_Pool
m = b.LocalLB_PoolMember
# get work done
poolDown = 0
poolList = p.get_list()['return']
status = m.get_monitor_status(pool_names = poolList)['return']
combined = zip(poolList, status)
for x in combined:
# Check the blacklist
if blackcheck(str(x[0])) == True:
continue
incr = len(x[1])
while incr > 0:
currentStatus = x[1][incr - 1]['monitor_status']
if currentStatus == "MONITOR_STATUS_DOWN":
address = x[1][incr - 1]['member']['address']
hostName = nslook(address)
if poolDown == 0:
print "
| "+x[0]+" | |
| "+address+" "+hostName+" |
" print “”