File: //usr/lib/python2.7/site-packages/lap/thawte_credits.py
import socket
import mechanize
import types
import sys
import re
import time
import os
reload(sys)
sys.setdefaultencoding('UTF-8')
try:
from bs4 import BeautifulSoup
except ImportError, e:
raise(e)
socket.setdefaulttimeout(20)
def htmlParse(data, element, attr, attr_name):
soup = BeautifulSoup(data, "html.parser")
if (attr != ''):
result = soup.findAll(element, attrs={attr: attr_name})
else:
result = soup.findAll(element)
if not isinstance(result, types.NoneType):
return result
def perfData(stime):
diff = round(time.time() - float(stime), 2)
return 'exec_time=%.2f' % float(diff)
def getData(username, password, cache_file, stime):
try:
br = mechanize.Browser()
br.set_handle_robots(False)
br.open('https://products.thawte.com/geocenter/reseller/logon.do')
except Exception, e:
return [0,"OK: Will be checked next time (getData)"]
try:
for form in br.forms():
if form.attrs['name'] == 'logonForm':
br.form = form
control = form.find_control("userName")
control.value = username
control = form.find_control("password")
control.value = password
response = br.submit()
response = br.open('https://products.thawte.com/geocenter/SSL/RequestCertificates.do')
results = htmlParse(response.get_data(), 'tr', 'class', 'tableHeaderRow')
totalCredit = 0
for result in results:
if 'BULK' in result.text:
credit = float(result.findNext('tr').findNext('tr').findNext('td').findNext('b').findNext('b').findNext('b').text.strip().replace('$','').replace(' ','').strip().replace(',',''))
if credit > 52:
totalCredit += credit
result = "%s,%s" % (totalCredit, stime)
with open(cache_file, 'w') as f:
f.write(result)
return result
except Exception, e:
return "Error: %s" % repr(e)
def __run__(params):
try:
limit_price = float(params.get("limit"))
warning_price = float(params.get("warning"))
critical_price = float(params.get("critical"))
username = params.get("username")
password = params.get("password")
cache_time = int(params.get("cache_time", "3600"))
cache_file = params.get("cache_file", "/var/cache/locaweb-plugins/thawte_credits")
stime = time.time()
except Exception, e:
return [0, "OK: Will be checked next time (run)"]
if os.path.isfile(cache_file) and \
(time.time() - os.stat(cache_file).st_mtime) < cache_time and \
os.stat(cache_file).st_size > 0:
totalCredit, stime = open(cache_file).read().split(",")
else:
totalCredit = getData(username, password, cache_file, stime)
if not "Error" in totalCredit:
totalCredit = float(totalCredit.split(",")[0])
if totalCredit < limit_price:
if totalCredit < critical_price:
return [2, "CRITICAL: Total credit is $%0.2f" % totalCredit, perfData(stime)]
elif totalCredit < warning_price:
return [1, "WARNING: Total credit is $%0.2f" % totalCredit, perfData(stime)]
else:
return [0, "OK: Total credit is $%0.2f" % totalCredit, perfData(stime)]
else:
return [0, "OK: Total credit is $%0.2f" % totalCredit, perfData(stime)]
else:
try:
os.remove(cache_file)
return [0, "OK: Will be checked next time (cache removed)"]
except OSError:
return [0, "OK: Will be checked next time (OSError)"]