File: //usr/lib/python2.7/site-packages/lap/site_cloud_pro_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)
result = soup.findAll(element, attrs={attr: attr_name})
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_cloud_server_pro")
url = { 'locaweb': 'http://www.locaweb.com.br',
'options':'http://www.locaweb.com.br/produtos/cloud-server/ficha.html?plan=2&periodo=1',
'checkout':'http://www2.locaweb.com.br/contratar-cloud-pro/autenticacao.html?memory=2&period=1&codos=4',
'checkout_login':'http://www2.locaweb.com.br/templates/locaweb-ficha-cloud-pro/revenda.php?systems2.login=%s&login=%s&period=1&memory=2&codos=4&login=%s&system1.login=system2' % (username,username,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'Cloud Server', nr=1)
if br.title().find('Cloud') == -1:
return [2, "CRITICAL: Problems on step 1", perfdata(stime)]
for link in br.links():
if not isinstance(link.text,types.NoneType):
if 'contrate' in link.text.lower():
response = br.follow_link(link)
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)]
result = htmlParse(response.get_data(), 'span', 'class', 'plan-title-inner')[1].text.strip()
if result != 'Cloud Server 2GB':
return [2, "CRITICAL: Cloud Server 2GB does not exist", perfdata(stime)]
try:
response = br.open(url['options'])
except:
return [2, "CRITICAL: Cannot open the Escolha seu Cloud Server Pro page", 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_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].text.strip()
if not 'Cloud Server Pro' in result:
return [2, "CRITICAL: Cloud Server Pro is not on product list", perfdata(stime)]
result = htmlParse(response.get_data(), 'td', 'class', 'value last')[1].text.strip()
if not '149,00' in result:
return [2, "CRITICAL: Price on product list is not 149,00", perfdata(stime)]
for form in br.forms():
if form.attrs['id'] == 'order_review':
br.form = form
response = br.submit()
result = str(htmlParse(response.get_data(), 'strong', 'class', 'name')[0]).split(' ')[1]
if not 'Boleto' in result:
return [2, "CRITICAL: Payment page doesn't contain string boleto bancario", perfdata(stime)]
result = htmlParse(response.get_data(), 'td', 'class', 'value last')[0].text.strip()
if not '149,00' in result:
return [2, "CRITICAL: Price on payment page is not 149,00", perfdata(stime)]
links = ['http://assets.locaweb.com.br/site/contratos/contrato-cloud-servidor-dedicado.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:
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 '149,00' in str(result):
return [2, "CRITICAL: Could not find 149,00 on boleto's Valor documento field", perfdata(stime)]
result = htmlParse(response.get_data(), 'div', 'id', 'colunadireita')[0]
result = str(result).split('Valor do documento')[1].split('div')[0]
if not '149,00' in result:
return [2, "CRITICAL: Could not find 149,00 on boleto's column's Valor documento field", perfdata(stime)]
result = htmlParse(br.response().get_data(), 'div', 'id', 'sacado')[0].text.strip().encode('utf-8')
r = re.compile(r'(.*) - (?P<postal_code>\d{5}-\d{3}) - (.*)')
postal_code = [ r.search(line).group('postal_code') for line in result.split('\n') if not isinstance(r.search(line), types.NoneType) ][0]
if postal_code != '05707-000':
return [2, "CRITICAL: CEP 05707-000 does not exist on boleto page", 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)]