Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Коды выхода
- # 0 Всё отлично
- # 1 Ошибка при отработки скрипта
- # 2 проблемы с данными в интернете
- # 4 проблемы с данными в телефонии
- # 6 проблема с интернетом и телефонией
- import argparse
- import sys
- from datetime import datetime, timedelta
- import subprocess
- import mysql.connector
- AGENT_IDS = ['03', '04', '06', '18', '20', '21', '22', '23']
- def zabbix_send(code_error):
- cmd = f'zabbix_sender -z "192.168.20.86" -s "backup.flexline.ru" -k chek_backup -o {code_error}'
- process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
- output, error = process.communicate()
- class DBConnection:
- def __init__(self, host, user, password, database):
- self.conn = mysql.connector.connect(
- host=host,
- user=user,
- password=password,
- database=database,
- auth_plugin='auth_socket'
- )
- def execute_query(self, query):
- cursor = self.conn.cursor()
- cursor.execute(query)
- rows = cursor.fetchall()
- cursor.close()
- return rows
- def ethernet_check(date_range):
- try:
- # set up connection to database
- db = DBConnection('127.0.0.1', 'root', '', 'billstat')
- # set up date range
- total_rows = []
- # execute queries and print results
- for i in AGENT_IDS:
- for d in date_range:
- query = f"SELECT '{i} {d}', COUNT(*) FROM user0{i}{d}"
- result = db.execute_query(query)
- total_rows.append([result[0][0], result[0][1]])
- for item in total_rows:
- if item[1] and item[1] < 1:
- return True
- if len(total_rows) != len(AGENT_IDS) * len(date_range):
- return True
- except Exception as e:
- print(1)
- # zabbix_send(1)
- sys.exit(1)
- def tel_check(date_range):
- try:
- db = DBConnection('127.0.0.1', 'root', '', 'dbt20014')
- total_rows = []
- for d in date_range:
- query = f"SELECT '{d}', COUNT(*) FROM tel001{d}"
- result = db.execute_query(query)
- total_rows.append([result[0][0], result[0][1]])
- for item in total_rows:
- if item[1] and item[1] < 1:
- return True
- if len(total_rows) != len(date_range):
- return True
- except Exception as e:
- print(1)
- # zabbix_send(1)
- sys.exit(1)
- if __name__ == '__main__':
- date_range = [
- (datetime.now() - timedelta(days=d)).strftime('%Y%m%d')
- for d in range(1, 6)
- ]
- res_eth = ethernet_check(date_range)
- res_tel = tel_check(date_range)
- if res_eth and not res_tel:
- #zabbix_send(2)
- print(2)
- elif res_tel and not res_eth:
- #zabbix_send(4)
- print(4)
- elif res_tel:
- #zabbix_send(6)
- print(6)
- else:
- #zabbix_send(0)
- print(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement