Last active
March 14, 2017 12:57
-
-
Save wvoliveira/0c9e19d311efed3f804e2b94ceaca864 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python2.6 | |
| from pymysql import connect | |
| from datetime import datetime | |
| from os import path | |
| from tempfile import NamedTemporaryFile | |
| from subprocess import Popen, PIPE | |
| from urllib2 import urlopen | |
| from socket import gethostname | |
| from shutil import copy | |
| """ | |
| Script que coleta os links direto do banco e adiciona no proxy (Squid) | |
| """ | |
| __author__ = 'Wellington Oliveira' | |
| sql_user = '' | |
| sql_pass = '' | |
| sql_server = '' | |
| sql_db = '' | |
| arquivo_callback = '/data/squid/etc/liberados.txt' | |
| arquivo_log = '/var/log/squid_adicionar_links.log' | |
| telegram_chat_id = '' | |
| telegram_chave = '' | |
| squid_bin = 'squid' | |
| hostname = gethostname() | |
| def sql_abrir_conexao(): | |
| try: | |
| global conexao | |
| conexao = connect(host=sql_server, user=sql_user, password=sql_pass, db=sql_db) | |
| return conexao | |
| except Exception as erro: | |
| escrever_log(erro, 2) | |
| telegram_enviar_notificacao('{0}: {1}'.format(hostname, erro)) | |
| exit(2) | |
| def sql_coletar_urls(): | |
| sql_abrir_conexao() | |
| tabela = '' | |
| sql = "SELECT distinct(url) FROM {0} WHERE url LIKE 'http%';".format(tabela) | |
| with conexao.cursor() as c: | |
| c.execute(sql) | |
| conteudo = [] | |
| for line in c.fetchall(): | |
| line = line[0].split('\n')[0] | |
| conteudo.append(line.lstrip('https://').lstrip('http://')) | |
| return conteudo | |
| def arquivo_ler(arquivo): | |
| try: | |
| with open(arquivo, 'r') as f: | |
| conteudo = [] | |
| for line in f.readlines(): | |
| line = line.split('\n')[0] | |
| conteudo.append(line.lstrip('https://').lstrip('http://')) | |
| return conteudo | |
| except Exception as erro: | |
| escrever_log(erro, 2) | |
| telegram_enviar_notificacao('{0}: {1}'.format(hostname, erro)) | |
| exit(2) | |
| def escrever_log(mensagem, loglevel): | |
| data = str(datetime.now().strftime('%d/%m/%Y %H:%M:%S')) | |
| if not path.exists(arquivo_log): | |
| try: | |
| with open(arquivo_log, 'w') as f: | |
| f.close() | |
| except Exception: | |
| pass | |
| if loglevel == 0: | |
| loglevel_text = '[INFO]' | |
| elif loglevel == 1: | |
| loglevel_text = '[WARN]' | |
| else: | |
| loglevel_text = '[CRIT]' | |
| with open(arquivo_log, 'a') as f: | |
| f.write('{0} {1}: {2}\n'.format(data, loglevel_text, mensagem)) | |
| f.close() | |
| def dif_lista(lista1, lista2): | |
| iguais = False | |
| lista1 = set(lista1) | |
| resultado = [item for item in lista1 if item not in lista2] | |
| if len(resultado) == 0: | |
| resultado = [item for item in lista2 if item not in lista1] | |
| if len(resultado) == 0: | |
| iguais = True | |
| return iguais | |
| def squid_reconfigurar(): | |
| try: | |
| stout, stderr = Popen([squid_bin, '-k', 'reconfigure'], stdout=PIPE, stderr=PIPE).communicate() | |
| if len(stderr) == 0: | |
| return True, None | |
| else: | |
| return False, stderr | |
| except Exception as erro: | |
| return False, erro | |
| def telegram_enviar_notificacao(mensagem): | |
| try: | |
| html = urlopen("https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}".format( | |
| telegram_chave, telegram_chat_id, mensagem)) | |
| if html.getcode() == 200: | |
| escrever_log('Telegram - Notificacao enviada. Codigo: {0}'.format(html.getcode()), 0) | |
| else: | |
| escrever_log('Telegram - Notificacao falhou. Codigo: {0}'.format(html.getcode()), 1) | |
| except Exception: | |
| pass | |
| def main(): | |
| arquivo_temporario = NamedTemporaryFile() | |
| links_base = sql_coletar_urls() | |
| links_arquivo = arquivo_ler(arquivo_callback) | |
| with open(arquivo_temporario.name, 'w') as f: | |
| for links in links_base: | |
| f.write(links + '\n') | |
| links_arquivo_temporario = arquivo_ler(arquivo_temporario.name) | |
| diferenca = dif_lista(links_arquivo, links_arquivo_temporario) | |
| if not diferenca: | |
| try: | |
| copy(arquivo_temporario.name, arquivo_callback) | |
| except Exception as erro: | |
| mensagem = 'Nao consegui copiar o arquivo {0} para {1}. {2}'.format( | |
| arquivo_temporario.name, arquivo_callback, erro) | |
| escrever_log(mensagem, 2) | |
| telegram_enviar_notificacao('{0}: {1}'.format(hostname, mensagem)) | |
| squid = squid_reconfigurar() | |
| print(squid[0]) | |
| if squid[0]: | |
| escrever_log('Squid reiniciado para atualizacao do callback', 0) | |
| else: | |
| mensagem = 'Squid - Nao foi possivel reconfigurar. Verifique! {0}'.format(squid[1]) | |
| escrever_log(mensagem, 2) | |
| telegram_enviar_notificacao('{0}: {1}'.format(hostname, mensagem)) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment