File: //usr/lib/python2.7/site-packages/lap/site_emkt_form.py
import socket
import mechanize
import types
import sys
import re
import time
import os
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_emailmarketing")
url = { 'locaweb': 'http://www.locaweb.com.br',
'contratacao': 'http://www.locaweb.com.br/templates/lw2013/includes/client.php?url=http://contratacao-emailmarketing.locaweb.com.br/?plano=1__AMP__periodo=6',
'checkout': 'http://contratacao-emailmarketing.sys20.locaweb.com.br/?plano=b1&periodo=6&systems2.login=%s' % 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'Email Marketing', nr=0)
if br.title().find('Marketing') == -1:
return [2, "CRITICAL: Problems on step 1", perfdata(stime)]
response = br.follow_link(text_regex=r'Novo Email Marketing Locaweb', nr=0)
if br.title().find('Planos') == -1:
return [2, "CRITICAL: Problems on step 2", perfdata(stime)]
result = htmlParse(response.get_data(), 'span', 'class', 'plan-title-inner')[0].text.strip()
if result != 'Email Marketing I':
return [2, "CRITICAL: Email Marketing I does not exist", perfdata(stime)]
result = htmlParse(response.get_data(), 'select', 'class', 'plan-select select-form')[0]
if result.find('option', attrs={'value': '6'}).text.strip() != 'Semestral -5%':
return [2, "CRITICAL: Semestral option does not exist", perfdata(stime)]
try:
br.open(url['contratacao'])
except:
return [2, "CRITICAL: Cannot open the Contratacao Email Marketing page", perfdata(stime)]
try:
br.open(url['checkout'])
except:
return [2, "CRITICAL: Cannot open the Checkout page", perfdata(stime)]
try:
br.form = [ form for form in br.forms() if form ][0]
br["Password"] = password
br.submit().read()
product = htmlParse(br.response().get_data(), 'td', 'class', 'product')[0].text.strip()
periodicity = htmlParse(br.response().get_data(), 'td', 'class', 'periodicity')[0].text.strip()
if product != 'Email Marketing I':
return [2, "CRITICAL: Product name is not Email Marketing I", perfdata(stime)]
elif periodicity != '6 meses':
return [2, "CRITICAL: Periodicity is not 6 months", perfdata(stime)]
except:
return [2, "CRITICAL: Cannot login on Checkout page", perfdata(stime)]
try:
br.form = [ form for form in br.forms() if form ][0]
br.submit().read()
except:
return [2, "CRITICAL: Cannot complete the form data for Checkout Page", perfdata(stime)]
try:
payment_method = htmlParse(br.response().get_data(), 'strong', 'class', 'name')[0].text.strip()
contract = htmlParse(br.response().get_data(), 'a', 'title', 'Email Marketing')[0].text.strip()
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 payment_method.startswith('Boleto'):
return [2, "CRITICAL: The boleto string on payment page does not exist", perfdata(stime)]
elif contract != 'Email Marketing':
return [2, "CRITICAL: The Email Marketing string does not exist on payment page", perfdata(stime)]
elif not ipaddress:
return [2, "CRITICAL: The IP Address information does not exist on payment page", perfdata(stime)]
except:
return [2, "CRITICAL: Cannot get the payment method, contract or ip address from payment page", perfdata(stime)]
try:
br.form = [ form for form in br.forms() if form ][0]
br.submit().read()
except:
return [2, "CRITICAL: Cannot complete the payment", perfdata(stime)]
try:
br.follow_link(text_regex=r'Imprimir boleto', nr=0)
except:
return [2, "CRITICAL: Cannot open the boleto page", perfdata(stime)]
try:
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)]
except:
return [2, "CRITICAL: Cannot get the postal code on boleto 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)]