Juni
16
APC USV – Kapazität mit Python auslesen
Das nachfolgende Programm meldet sich auf der APC USV an und liest die aktuelle Kapazität aus:
#!/usr/bin/env python # -*- coding: utf-8 -*- # ############################################################## # # # Python prototype which logs on to a APC USV and extracts # # the actual capacity of the USV # # # # original source code by Kloss Jan-Philip # # # # Python Version 2.7 # # # ############################################################## #imports import requests import re import sys import subprocess from optparse import OptionParser #login credentials values = {'login_username': 'apc', 'login_password': 'apc'} #subprogram def getPage(ip): url='http://'+ip+'/Forms/login1' s=requests.session() s.get(url) r=s.post(url, data=values) sid_url= r.url searchstring='NMC/(.*)/home.htm' sid = re.search(searchstring, sid_url).groups(2) url2='http://'+ip+'/NMC/'+sid[0]+'/upstat.htm' t = s.get(url2) page=t.content return page #main program def main(): usage = "usage: %prog [options]" parser = OptionParser(usage) parser.add_option("-i", "--ip", action="store", dest="ip", help="IP of host to check capacity of USV.") (options, args) = parser.parse_args() if options.ip is None: parser.print_help() try: ip = options.ip page=getPage(ip) for line in page.split('</tr>'): #Aktuelle Batteriekapazität if 'Capacity' in line: match = re.search('<td colspan="3">(.*?) %</td>',line) if match: capacity = match.group(1) print "capacity:"+str(capacity) except Exception, e: print e if __name__ == '__main__': main()
Das Programm bzw. daraus hervorgehende Plugin für Cacti befindet sich unter Downloads.