Advertisement
Rybalka

Untitled

Jul 26th, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Коды выхода
  4. # 0 Всё отлично
  5. # 1 Ошибка при отработки скрипта
  6. # 2 проблемы с данными в интернете
  7. # 4 проблемы с данными в телефонии
  8. # 6 проблема с интернетом и телефонией
  9. import argparse
  10. import sys
  11. from datetime import datetime, timedelta
  12. import subprocess
  13. import mysql.connector
  14. AGENT_IDS = ['03', '04', '06', '18', '20', '21', '22', '23']
  15.  
  16.  
  17.  
  18. def zabbix_send(code_error):
  19.     cmd = f'zabbix_sender -z "192.168.20.86" -s "backup.flexline.ru" -k chek_backup -o {code_error}'
  20.     process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
  21.     output, error = process.communicate()
  22.  
  23.  
  24. class DBConnection:
  25.     def __init__(self, host, user, password, database):
  26.         self.conn = mysql.connector.connect(
  27.             host=host,
  28.             user=user,
  29.             password=password,
  30.             database=database,
  31.             auth_plugin='auth_socket'
  32.         )
  33.  
  34.     def execute_query(self, query):
  35.         cursor = self.conn.cursor()
  36.         cursor.execute(query)
  37.         rows = cursor.fetchall()
  38.         cursor.close()
  39.         return rows
  40.  
  41.  
  42. def ethernet_check(date_range):
  43.     try:
  44.         # set up connection to database
  45.         db = DBConnection('127.0.0.1', 'root', '', 'billstat')
  46.         # set up date range
  47.         total_rows = []
  48.         # execute queries and print results
  49.         for i in AGENT_IDS:
  50.             for d in date_range:
  51.                 query = f"SELECT '{i} {d}', COUNT(*) FROM user0{i}{d}"
  52.                 result = db.execute_query(query)
  53.                 total_rows.append([result[0][0], result[0][1]])
  54.         for item in total_rows:
  55.             if item[1] and item[1] < 1:
  56.                 return True
  57.         if len(total_rows) != len(AGENT_IDS) * len(date_range):
  58.             return True
  59.     except Exception as e:
  60.         print(1)
  61.         # zabbix_send(1)
  62.         sys.exit(1)
  63.  
  64.  
  65. def tel_check(date_range):
  66.     try:
  67.         db = DBConnection('127.0.0.1', 'root', '', 'dbt20014')
  68.         total_rows = []
  69.         for d in date_range:
  70.             query = f"SELECT '{d}', COUNT(*) FROM tel001{d}"
  71.             result = db.execute_query(query)
  72.             total_rows.append([result[0][0], result[0][1]])
  73.         for item in total_rows:
  74.             if item[1] and item[1] < 1:
  75.                 return True
  76.         if len(total_rows) != len(date_range):
  77.             return True
  78.     except Exception as e:
  79.         print(1)
  80.         # zabbix_send(1)
  81.         sys.exit(1)
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     date_range = [
  86.         (datetime.now() - timedelta(days=d)).strftime('%Y%m%d')
  87.         for d in range(1, 6)
  88.     ]
  89.     res_eth = ethernet_check(date_range)
  90.     res_tel = tel_check(date_range)
  91.     if res_eth and not res_tel:
  92.         #zabbix_send(2)
  93.         print(2)
  94.     elif res_tel and not res_eth:
  95.         #zabbix_send(4)
  96.         print(4)
  97.     elif res_tel:
  98.         #zabbix_send(6)
  99.         print(6)
  100.     else:
  101.         #zabbix_send(0)
  102.         print(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement