File: //usr/lib/python2.7/site-packages/lap/site_plesk_form.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)
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() - stime, 2)
return 'exec_time=%.2f' % float(diff)
def __run__(params):
try:
username = params.get("username")
password = params.get("password")
cache_time = int(params.get("cache_time", "3600"))
cache_file = params.get("cache_file", "/var/tmp/contratacao_plesk")
url = { 'locaweb': 'http://www.locaweb.com.br',
'checkout': 'http://www2.locaweb.com.br/contratar-revenda-plesk/autenticacao.html?plano=1&periodo=12&so=linux',
'checkout_check_user':'http://www2.locaweb.com.br/templates/locaweb-ficha-sem-dominio/checkSys.php?login_modal=%s' % username,
'checkout_login':'http://www2.locaweb.com.br/templates/locaweb-ficha-revenda/process.php?periodo=12&plano=1&so=linux&systems2_login=%s&cupom_emmkt=' % username}
stime = time.time()
except Exception, e:
return [2,"CRITICAL: %s" % repr(e)]
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:
return [0, open(cache_file).read(), 'exec_time=%.2f' % float(open(cache_file).read().split()[-2])]
else:
try:
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url['locaweb'])
except:
return [2, "CRITICAL: Cannot open the locaweb page", perfdata(stime)]
try:
br.follow_link(text_regex=r'Revenda de Hospedagem Plesk', nr=0)
if br.title().find('Hospedagem') == -1:
return [2, "CRITICAL: Problems on step 1", perfdata(stime)]
response = br.follow_link(text_regex=r'Planos', nr=0)
if br.title().find('Planos') == -1:
return [2, "CRITICAL: Problems on step 2", perfdata(stime)]
if 'modal manutencao' in response.get_data():
return [1, "WARNING: Maintenance", perfdata(stime)]
try:
br.open(url['checkout'])
except:
return [2, "CRITICAL: Cannot open the Checkout page", perfdata(stime)]
try:
for form in br.forms():
if form.attrs['id'] == 'form_existent':
br.form = form
control = form.find_control("login_modal")
control.value = username
br.open(url['checkout_check_user'])
if not 'Migrado' in br.response().get_data():
return [2, "CRITICAL: Error redirecting to login page", perfdata(stime)]
br.open(url['checkout_login'])
br.form = [ form for form in br.forms() if form ][0]
control = form.find_control("Login")
if control.value != username:
return [2, "CRITICAL: User is not %s" % username, perfdata(stime)]
br['Password'] = password
response = br.submit()
except :
return [2, "CRITICAL: Cannot complete the form data for Checkout Page", perfdata(stime)]
try:
result = htmlParse(response.get_data(), 'td', 'class', 'product')[0]
hospedagem = str(result).split('\n')[1].strip()
if hospedagem != 'Revenda Plesk I - Linux':
return [2, "CRITICAL: Revenda Plesk I - Linux is not on product list", perfdata(stime)]
for form in br.forms():
if form.attrs['id'] == 'order_review':
br.form = form
response = br.submit()
links = ['http://assets.locaweb.com.br/site/contratos/contrato-revenda-plesk-cpanel.pdf']
linksChecked = 0
for link in br.links():
for lnk in links:
if lnk == link.url:
linksChecked = linksChecked+1
if linksChecked < 1:
return [2, "CRITICAL: Could not find all links on payment page", perfdata(stime)]
data = htmlParse(br.response().get_data(), 'fieldset', 'class', 'contracts')[0]
r = re.compile(r'(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
ipaddress = r.search(data.cite.strong.text.strip()).group('ip')
if not ipaddress:
return [2, "CRITICAL: The IP Address information does not exist on payment page", perfdata(stime)]
try:
br.form = [ form for form in br.forms() if form ][0]
br.form['paymentMethod'] = ['current_boleto']
br.submit().read()
data = htmlParse(br.response().get_data(), 'strong', 'class', 'name')[0]
if not 'Funcional' in str(data):
return [2, "CRITICAL: The Monitoracao Funcional string does not exist on payment page", perfdata(stime)]
except Exception, e:
return [2, "CRITICAL: Cannot complete the payment", perfdata(stime)]
try:
response = br.follow_link(text_regex=r'Imprimir boleto', nr=0)
except:
return [2, "CRITICAL: Cannot open the boleto page", perfdata(stime)]
try:
result = htmlParse(response.get_data(), 'div', 'class', 'valor item')[0]
if not '538,92' in str(result):
return [2, "CRITICAL: Could not find 538,92 on boleto's Valor documento field", perfdata(stime)]
result = str(htmlParse(response.get_data(), 'img', '', '')[2])
if not 'barcode' in result:
return [2, "CRITICAL: Barcode does not exist on boleto page", perfdata(stime)]
except:
return [2, "CRITICAL: Cannot validate all fields from boleto page", perfdata(stime)]
except Exception, e:
return [2, "CRITICAL: Cannot complete the form data for Resume Page", perfdata(stime)]
except Exception, e:
return [2,"CRITICAL: %s" % repr(e), perfdata(stime)]
else:
try:
with open(cache_file, 'w') as f:
f.write('Last checked: %s, with %s seconds' % (time.ctime(), round((time.time() - stime), 2)))
return [0, open(cache_file).read(), 'exec_time=%.2f' % float(open(cache_file).read().split()[-2])]
except Exception, e:
return [2, "CRITICAL: Can't write cache file %s" % repr(e), perfdata(stime)]