Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import time
  4. import urllib.request
  5. import datetime
  6. import psycopg2
  7.  
  8. # ------------------------------CONEXÃO------------------------------------
  9.  
  10. try:
  11.     conn = psycopg2.connect(host='localhost', database='mcc_penitencia', user='postgres', password='xwq6206')
  12.     print('Conectado com o banco de dados!\n')
  13.  
  14. except:
  15.     print('Falha ao conectar com o BD\n')
  16.  
  17. c = conn.cursor()
  18.  
  19. global data_offline
  20. global status_offline
  21. data_offline = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y - %H:%M:%S'))
  22.  
  23.  
  24. # -----------------------------FUNÇÕES--------------------------------------
  25.  
  26. def get_status():
  27.     c.execute("SELECT * FROM status")
  28.     sqls = c.fetchall()
  29.  
  30.     for row in sqls:
  31.         print(row)
  32.  
  33. # --------------------------------------------------------------------------
  34.  
  35.  
  36. def verifica_internet():
  37.  
  38.     while True:
  39.         #time.sleep(21600)               # 6 HORAS
  40.  
  41.         data = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y - %H:%M:%S'))
  42.         data_log = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d/%m/%Y'))
  43.  
  44.         global salvou_registro
  45.         salvou_registro = False
  46.  
  47.         # -----------------------VERIFICA ULTIMO STATUS-------------------------
  48.  
  49.         c.execute("SELECT * FROM status")
  50.         sqls = c.fetchall()
  51.  
  52.         for row in sqls:
  53.             global status
  54.             status = row[1]
  55.  
  56.         # ----------------------------------------------------------------------
  57.  
  58.  
  59.  
  60.         try:
  61.             urllib.request.urlopen("http://google.com")
  62.         except:
  63.             print ("SEM INTERNET  -> " + data_offline + '\n')
  64.             status_atual = 'OFFLINE'
  65.  
  66.             status_offline = status_atual
  67.  
  68.             text_file = open("./LOG/Output.txt", "w")
  69.             text_file.write(status_offline + '=' + data_offline)
  70.             text_file.close()
  71.  
  72.             time.sleep(5)
  73.  
  74.  
  75.  
  76.         else:
  77.             print ("INTERNET OK")
  78.             status_atual = 'ONLINE'
  79.  
  80.  
  81.             text_file = open("./LOG/log_status.txt", "w")
  82.             text_file.write(data_log)
  83.             print('LOG: ' + data + '\n')
  84.             text_file.close()
  85.  
  86.  
  87.  
  88.             try:
  89.  
  90.                 text_file = open("./LOG/Output.txt", "r")
  91.                 split = text_file.read()
  92.                 result = split.split('=')          # result = ['OFFLINE', '21/02/2019 - 10:10:26']
  93.                 text_file.close()
  94.  
  95.             except:
  96.                 text_file = open("./LOG/Output.txt", "r")
  97.                 salvou_registro = text_file.read()
  98.                 text_file.close()
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.             if result[0] == 'OFFLINE' and salvou_registro == False:
  106.                 c.execute("INSERT INTO status (status, data) VALUES (%s, %s)", (result[0], result[1]))
  107.                 conn.commit()
  108.                 print('INTERNET VOLTOU, SALVANDO REGISTRO NO BD\n')
  109.  
  110.                 text_file = open("./LOG/Output.txt", "w")
  111.                 text_file.write('INTERNET OK')
  112.                 text_file.close()
  113.  
  114.             if (status != status_atual):
  115.  
  116.                 c.execute("INSERT INTO status (status, data) VALUES (%s, %s)", (status_atual, data))
  117.                 conn.commit()
  118.                 print('INTERNET OK  -> ' + data + '\n')
  119.                 time.sleep(5)
  120.  
  121.             else:
  122.                 print('Status anterior: ONLINE\n')
  123.                 time.sleep(5)
  124.  
  125.  
  126. # ----------------------------------------------------------------------
  127.  
  128.  
  129. def compara_data():
  130.     data_atual_dia = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%d'))
  131.     data_atual_mes = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%m'))
  132.     data_atual_ano = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y'))
  133.  
  134.     text_file = open("./LOG/log_status.txt", "r")
  135.     data_caiu = text_file.read().split('/')
  136.     text_file.close()
  137.  
  138.  
  139.     if (data_atual_dia > data_caiu[0]):
  140.         if (data_atual_mes >= data_caiu[1]):
  141.             if (data_atual_ano >= data_caiu[2]):
  142.                 print('data atual maior')
  143.  
  144.     else:
  145.         print('servidor online')
  146.  
  147. # ----------------------------------------------------------------------
  148.  
  149. get_status()
  150. print('')
  151. compara_data()
  152. verifica_internet()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement