Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os
- import time
- import urllib.request
- import datetime
- import psycopg2
- # ------------------------------CONEXÃO------------------------------------
- try:
- conn = psycopg2.connect(host='localhost', database='mcc_penitencia', user='postgres', password='xwq6206')
- print('Conectado com o banco de dados!\n')
- except:
- print('Falha ao conectar com o BD\n')
- c = conn.cursor()
- global data_offline
- global status_offline
- data_offline = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y - %H:%M:%S'))
- # -----------------------------FUNÇÕES--------------------------------------
- def get_status():
- c.execute("SELECT * FROM status")
- sqls = c.fetchall()
- for row in sqls:
- print(row)
- # --------------------------------------------------------------------------
- def verifica_internet():
- while True:
- #time.sleep(21600) # 6 HORAS
- data = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y - %H:%M:%S'))
- data_log = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y'))
- global salvou_registro
- salvou_registro = False
- # -----------------------VERIFICA ULTIMO STATUS-------------------------
- c.execute("SELECT * FROM status")
- sqls = c.fetchall()
- for row in sqls:
- global status
- status = row[1]
- # ----------------------------------------------------------------------
- try:
- urllib.request.urlopen("http://google.com")
- except:
- print ("SEM INTERNET -> " + data_offline + '\n')
- status_atual = 'OFFLINE'
- status_offline = status_atual
- text_file = open("./LOG/Output.txt", "w")
- text_file.write(status_offline + '=' + data_offline)
- text_file.close()
- time.sleep(5)
- else:
- print ("INTERNET OK")
- status_atual = 'ONLINE'
- text_file = open("./LOG/log_status.txt", "w")
- text_file.write(data_log)
- print('LOG: ' + data + '\n')
- text_file.close()
- try:
- text_file = open("./LOG/Output.txt", "r")
- split = text_file.read()
- result = split.split('=') # result = ['OFFLINE', '21/02/2019 - 10:10:26']
- text_file.close()
- except:
- text_file = open("./LOG/Output.txt", "r")
- salvou_registro = text_file.read()
- text_file.close()
- if result[0] == 'OFFLINE' and salvou_registro == False:
- c.execute("INSERT INTO status (status, data) VALUES (%s, %s)", (result[0], result[1]))
- conn.commit()
- print('INTERNET VOLTOU, SALVANDO REGISTRO NO BD\n')
- text_file = open("./LOG/Output.txt", "w")
- text_file.write('INTERNET OK')
- text_file.close()
- if (status != status_atual):
- c.execute("INSERT INTO status (status, data) VALUES (%s, %s)", (status_atual, data))
- conn.commit()
- print('INTERNET OK -> ' + data + '\n')
- time.sleep(5)
- else:
- print('Status anterior: ONLINE\n')
- time.sleep(5)
- # ----------------------------------------------------------------------
- def compara_data():
- data_atual_dia = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d'))
- data_atual_mes = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%m'))
- data_atual_ano = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y'))
- text_file = open("./LOG/log_status.txt", "r")
- data_caiu = text_file.read().split('/')
- text_file.close()
- if (data_atual_dia > data_caiu[0]):
- if (data_atual_mes >= data_caiu[1]):
- if (data_atual_ano >= data_caiu[2]):
- print('data atual maior')
- else:
- print('servidor online')
- # ----------------------------------------------------------------------
- get_status()
- print('')
- compara_data()
- verifica_internet()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement