from netmiko import ConnectHandler
from colorama import init, Fore, Back, Style
import threading
import time
import socket
import binascii
import zlib
from tkinter import *
import json
import os
init(autoreset=True)
success_message = "#"
fail_message = "!"
access_fail_message = "?"
tafics_failure = 0
tafics_failure_count = 3 # TAFICS hatası sayma
cpu_max = 20 # maximum CPU oranı
with open("../../../Desktop/pyhton_projeleri/pycurl/IP_LIST_Router.txt", "r") as Router_Count:
Router_Count_Info = Router_Count.read()
Router_Count_Info_List = Router_Count_Info.splitlines()
print("Total Number of Router: {count}, Router IP: {IP}".format(count=len(Router_Count_Info_List), IP= Router_Count_Info_List))
numberOfRouterDevice = 2 # YSHA Router Toplam Sayısı
with open("../../../Desktop/pyhton_projeleri/pycurl/IP_LIST_Switch.txt", "r") as Switch_Count:
Switch_Count_Info = Switch_Count.read()
Switch_Count_Info_List = Switch_Count_Info.splitlines()
print("Total Number of Switch: {count}, Switch IP: {IP}".format(count = len(Switch_Count_Info_List), IP = Switch_Count_Info_List))
numberOfSwitchDevice = 1 # YSHA Switch Toplam Sayısı
numberOFRouterDataToSend = 64 # YSHA Router başına ölçüm yapılacak bilgi sayısı
numberOFSwitchDataToSend = 64 # YSHA Switch başına ölçüm yapılacak bilgi sayısı
# Gönderilecek veri için dizin oluşturulması
a, b = numberOFRouterDataToSend, numberOfRouterDevice
data = [["0" for x in range(a)] for y in range(b)]
data1 = ["0"] * (a * b)
# Interface hata durum değişkenleri
router_1_gigabitetnernet10_interface_problem = 1
router_1_gigabitetnernet20_interface_problem = 1
router_2_gigabitetnernet10_interface_problem = 1
router_2_gigabitetnernet20_interface_problem = 1
bit_number = 0
check_first_time = 0
a = 0
if os.path.exists("../../../Desktop/pyhton_projeleri/pycurl/b_len.txt"):
os.remove("../../../Desktop/pyhton_projeleri/pycurl/b_len.txt")
if os.path.exists("../../../Desktop/pyhton_projeleri/pycurl/NKY_log.txt"):
os.remove("../../../Desktop/pyhton_projeleri/pycurl/NKY_log.txt")
# %%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router Setting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router IP Setting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
with open('../../../Desktop/pyhton_projeleri/pycurl/IP_LIST_Router.txt') as IP_file_router:
IP_lines_router = IP_file_router.read().splitlines()
router = [""] * len(IP_lines_router)
IP_lines_router_index = 0
for IP in IP_lines_router:
router[IP_lines_router_index] = {
'device_type': 'cisco_ios',
'ip': IP,
'username': 'cisco',
'password': 'cisco',
'secret': 'cisco'
}
IP_lines_router_index += 1
# %%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router ID Setting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
with open('../../../Desktop/pyhton_projeleri/pycurl/ID_LIST_Router.txt') as ID_file_router:
ID_lines_router = ID_file_router.read().splitlines()
router_id = [""] * len(ID_lines_router)
ID_lines_router_index = 0
for x in range(len(ID_lines_router)):
router_id[x] = ID_lines_router[x]
# %%%%%%%%%%%%%%%%%%%%%%%%% Data Send Socket Set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ip = "5.5.5.5"
port = 11111
except Exception as e:
logging("UDP Socket Fail", 1) # "0" is INFO, "1" is WARNING"
def logging(log_text, WCA):
time_info = time.asctime(time.localtime(time.time()))
if WCA == 0: #INFO
WCA_text = "INFO "
if WCA == 1: #FAIL
WCA_text = "FAILURE"
if WCA == 2: #WARNING
WCA_text = "WARNING"
with open("../../../Desktop/pyhton_projeleri/pycurl/NKY_log.txt", "a+") as file_log:
file_log.write(time_info + " -> " + WCA_text + " -> " + log_text + "\n")
print(log_text)
return
def load_default(eq_index_for_router):
global data
data[eq_index_for_router] = ["&"] * len(data[eq_index_for_router]) # "&" means not used bytes
def text_arrange(text_get, len_value):
l = len(text_get)
if l < len_value:
l_new = ["0"] * (len_value - l)
l_mid = ''.join(l_new)
text_get = ''.join(text_get)
l_last = l_mid + text_get
else:
l_last = text_get
return l_last
def data_prepare(prepare_eq, prepare_data):
global data
global bit_number
for luk in router_id:
if prepare_data == luk:
bit_number = 0
for fuh in prepare_data:
data[prepare_eq][bit_number] = fuh
bit_number += 1
def header_prepare(msg_type_var, eq_len):
# ayın ilk gününden geçen milisaniye değerini bulmak
tm = str(time.localtime())
tm = tm.split(",")
tt = []
for tm_ind in tm:
tt.append(tm_ind.strip(" "))
tt_day = str(tt[2])
tt_hour = str(tt[3])
tt_min = str(tt[4])
tt_sec = str(tt[5])
m_day = int(tt_day[8:len(tt_day):])
m_hour = int(tt_hour[8:len(tt_hour):])
m_min = int(tt_min[7:len(tt_min):])
m_sec = int(tt_sec[7:len(tt_sec):])
m_msec = (m_day * 24 * 60 * 60 * 1000) + (m_hour * 60 * 60 * 1000) + (m_min * 60 * 1000) + (m_sec * 1000)
################################################################
# Mesaj Zarfı ##################################################
################################################################
IDD = binascii.unhexlify("322e3030303030303030") # Veri boyutu 10 byte. Varsayılan olarak atanmıştır (2.00000000)
Msg_Instance = m_msec.to_bytes(4, byteorder='big') # ayın ilk gününden itibaren geçen milisaniye
# Message tipleri aşağıdaki gibidir;
# 2001: YSHA Router Durum Bilgisi
# 3001: YSHA Switch Durum Bilgisi
Msg_Type = msg_type_var.to_bytes(4, byteorder='big') # Mesaj tipi
Msg_Len = (numberOFRouterDataToSend * eq_len).to_bytes(4, byteorder='big') # Mesaj tipi
Stream_ID_constant = 4294967295 # Varsayılan olarak atanmıştır (0xFFFFFFFF)
Stream_ID = Stream_ID_constant.to_bytes(4, byteorder='big')
Package_Sequence_constant = 4294967295 # Varsayılan olarak atanmıştır (0xFFFFFFFF)
Package_Sequence = Package_Sequence_constant.to_bytes(4, byteorder='big')
Msg_header = IDD + Msg_Instance + Msg_Type + Msg_Len + Stream_ID + Package_Sequence
yuh = bytes(Msg_header)
return yuh
def dataSend():
global th
global data
global data1
global ip
global port
global yuh
yuh_new = header_prepare(2001, len(router_id))
yuh_new_switch = header_prepare(3001, len(switch_id))
yz = 0
for y in range(len(data)):
for z in range(len(data[0])):
data1[yz] = data[y][z]
yz += 1
b = bytearray()
for ji in data1:
data1_join = ''.join(data1)
for kkll in data1_join:
b.extend(kkll.encode(encoding='utf-8'))
b_new = yuh_new + b
with open("../../../Desktop/pyhton_projeleri/pycurl/b_len.txt", "a+") as b_len_file:
b_len_file.write("\n" + str(len(data1_join)) + " ")
b_len_file.write(str(data1_join))
#CRC-32 calculation
b_new_crc = format(zlib.crc32(b_new), 'x')
b_new_crc_int = int(b_new_crc, 16)
b_new_crc_bytes = b_new_crc_int.to_bytes(4, byteorder='big') # 4 bytes
b_new += b_new_crc_bytes
s.sendto(b_new, (ip, port))
th.cancel()
th = threading.Timer(3, dataSend)
th.start()
th = threading.Timer(1, dataSend)
th.start()
while TRUE:
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eq_index = 0 # Router-1
try:
# %%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 Connection Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if connection_router_1.is_alive() == True:
logging("YSHA Router 1 SSH Connection OK", 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, router_id[eq_index])
data_prepare(eq_index, success_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 Internal Check (Power, Temperature) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
env = connection_router_1.send_command('show env', use_textfsm=True)
# print(json.dumps(env, indent=2))
env_str = (str(env).strip()).splitlines()
for rear in range(len(env_str)):
env_str[rear] = env_str[rear].strip()
num_of_critical_alarm = env_str[0][26:len(env_str[0])].strip()
num_of_major_alarm = env_str[1][26:len(env_str[1])].strip()
num_of_minor_alarm = env_str[2][26:len(env_str[2])].strip()
fan0_RPM = env_str[6][23:30].strip()
fan1_RPM = env_str[7][23:30].strip()
fan2_RPM = env_str[8][23:30].strip()
fan3_RPM = env_str[9][23:30].strip()
power = env_str[10][23:30].strip()
temp_inlet = env_str[11][23:30].strip()
temp_interval = env_str[12][23:30].strip()
temp_outlet = env_str[13][23:30].strip()
temp_cpu = env_str[14][23:30].strip()
V_12 = env_str[15][23:30].strip()
V_5 = env_str[16][23:30].strip()
V_33 = env_str[17][23:30].strip()
V_30 = env_str[18][23:30].strip()
V_25 = env_str[19][23:30].strip()
V_135 = env_str[20][23:30].strip()
V_18 = env_str[21][23:30].strip()
V_12 = env_str[22][23:30].strip()
VNN_CPU = env_str[23][23:30].strip()
V_11 = env_str[24][23:30].strip()
V_10 = env_str[25][23:30].strip()
V_18_CPU = env_str[26][23:30].strip()
DDR = env_str[27][23:30].strip()
V_33_STBY = env_str[28][23:30].strip()
V_15 = env_str[29][23:30].strip()
V_10_CPU = env_str[30][23:30].strip()
VCC_CPU = env_str[31][23:30].strip()
V_075 = env_str[32][23:30].strip()
V_12 = env_str[33][23:30].strip()
PWR = env_str[34][23:30].strip()
result = re.findall(r"Fail", str(env))
fail = 0
for ali in result:
if ali == "Fail":
fail += 1
if fail == 0:
logging("YSHA Router 1 Internal Check PASS", 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, success_message)
else:
logging("YSHA Router 1 Internal FAIL (temperature or power)", 1) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, fail_message)
except Exception as e:
# print(e)
logging("YSHA Router 1 Internal Check can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 CPU Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
cpu_router_1 = connection_router_1.send_command('show processes cpu', use_textfsm=True)
# print(json.dumps(cpu_router_1, indent=2))
if int(cpu_router_1[0]['cpu_1_min']) >= cpu_max:
logging('YSHA Router 1 High CPU', 1) # "0" is INFO, "1" is WARNING"
cpu_router_1_problem = 1
data_prepare(eq_index, fail_message)
else:
logging('YSHA Router 1 Low CPU', 0) # "0" is INFO, "1" is WARNING"
cpu_router_1_problem = 0
data_prepare(eq_index, success_message)
except Exception as e:
logging("YSHA Router 1 CPU Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 Send Date & Time %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
time_router_1 = connection_router_1.send_command('show clock', use_textfsm=True)
# print(json.dumps(time_router_1, indent=2))
# print(json.dumps(time_router_1, indent=2))
time_to_bytes = str(time_router_1[0]['time'])
time_to_bytes = time_to_bytes.split(":")
for pill in range(len(time_to_bytes)):
time_to_bytes[pill] = float(time_to_bytes[pill])
for pill in range(len(time_to_bytes)):
time_to_bytes[pill] = int(time_to_bytes[pill])
for pill in range(len(time_to_bytes)):
time_to_bytes[pill] = str(time_to_bytes[pill])
day_to_bytes = str(time_router_1[0]['day'])
logging(year_to_bytes = str(time_router_1[0]['year']))
if time_router_1[0]['month'] == "Jan": month = "01"
if time_router_1[0]['month'] == "Feb": month = "02"
if time_router_1[0]['month'] == "Mar": month = "03"
if time_router_1[0]['month'] == "Apr": month = "04"
if time_router_1[0]['month'] == "May": month = "05"
if time_router_1[0]['month'] == "Jun": month = "06"
if time_router_1[0]['month'] == "Jul": month = "07"
if time_router_1[0]['month'] == "Aug": month = "08"
if time_router_1[0]['month'] == "Sep": month = "09"
if time_router_1[0]['month'] == "Oct": month = "10"
if time_router_1[0]['month'] == "Nov": month = "11"
if time_router_1[0]['month'] == "Dec": month = "12"
time_to_bytes[0] = text_arrange(time_to_bytes[0], 2) # arrange to be 2 digit
time_to_bytes[1] = text_arrange(time_to_bytes[1], 2) # arrange to be 2 digit
time_to_bytes[2] = text_arrange(time_to_bytes[2], 2) # arrange to be 2 digit
day_to_bytes = text_arrange(day_to_bytes, 2) # arrange to be 2 digit
year_to_bytes = text_arrange(year_to_bytes, 4) # arrange to be 4 digit
data_prepare(eq_index, time_to_bytes[0])
data_prepare(eq_index, time_to_bytes[1])
data_prepare(eq_index, time_to_bytes[2])
data_prepare(eq_index, month)
data_prepare(eq_index, day_to_bytes)
data_prepare(eq_index, year_to_bytes)
except Exception as e:
logging("YSHA Router 1 Date & Time can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, "??")
data_prepare(eq_index, "??")
data_prepare(eq_index, "??")
data_prepare(eq_index, "??")
data_prepare(eq_index, "??")
data_prepare(eq_index, "??")
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 Send Version %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
version_router_1 = connection_router_1.send_command('show version', use_textfsm=True)
# print(json.dumps(version_router_1, indent=2))
#print(Fore.GREEN + Back.BLACK + 'YSHA Router 1 Version: ' + str(version_router_1[0]['version']))
ver_str = text_arrange(str(version_router_1[0]['version']), 20) # arrange to be 20 digit
data_prepare(eq_index, ver_str)
except Exception as e:
logging("YSHA Router 1 Version can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
ver_str = "?" * 20
data_prepare(eq_index, ver_str)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 Interface Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
router_1_interface = connection_router_1.send_command('show ip interface brief', use_textfsm=True)
# print(json.dumps(router_1_interface, indent=2))
i = 0
for i in range(len(router_1_interface)):
if ((router_1_interface[i]['intf'] == 'GigabitEthernet0/0') and (
router_1_interface[i]['status'] == 'up')):
router_1_gigabitetnernet10_interface_problem = 0
else:
...
i = 0
for i in range(len(router_1_interface)):
if ((router_1_interface[i]['intf'] == 'GigabitEthernet1/0') and (
router_1_interface[i]['status'] == 'up')):
router_1_gigabitetnernet20_interface_problem = 0
else:
...
if router_1_gigabitetnernet10_interface_problem == 0 and router_1_gigabitetnernet20_interface_problem == 0:
router_1_interface_problem = 1
logging("YSHA Router 1 Interface Check PASS", 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, success_message)
else:
router_1_interface_problem = 0
logging("YSHA Router 1 Interface FAIL", 1) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, fail_message)
except Exception as e:
logging("YSHA Router 1 Interface Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 1 Standby Status %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
standby_router_1 = connection_router_1.send_command('show standby', use_textfsm=True)
# print(json.dumps(standby_router_1, indent=2))
if standby_router_1[0]['state'] == 'Active':
logging('YSHA Router 1 is Active', 0) # "0" is INFO, "1" is WARNING"
standby_router_problem = 0
active_router = 1
data_prepare(eq_index, success_message)
elif standby_router_1[0]['state'] == 'Standby':
logging('YSHA Router 1 is Standby', 2) # "0" is INFO, "1" is WARNING"
standby_router_problem = 0
data_prepare(eq_index, "£")
else:
logging('Redundancy FAIL', 1) # "0" is INFO, "1" is WARNING"
standby_router_problem = 1
active_router = 0
data_prepare(eq_index, fail_message)
except Exception as e:
logging("YSHA Router 1 Standby Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
else:
dummy_var_to_jump_exception_1 = dummy_var_to_jump_exception_2 # dummy fail mesaage to jump exception
except Exception as e:
logging("YSHA Router 1 SSH Connection can't be established", 2) # "0" is INFO, "1" is WARNING"
load_default(eq_index)
data_prepare(eq_index, router_id[eq_index])
data_prepare(eq_index, fail_message)
logging("New connection is trying to establish for " + "Router " + str(eq_index + 1) + "...", 0) # "0" is INFO, "1" is WARNING"
try:
connection_router_1 = ConnectHandler(**router[eq_index])
text = "New connection " + "(Router " + str(eq_index + 1) + ") " + "SUCCEED..."
logging(text, 0) # "0" is INFO, "1" is WARNING"
except:
text = "New connection " + "(Router " + str(eq_index + 1) + ") " + "FAILED..."
logging(text, 1) # "0" is INFO, "1" is WARNING"
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eq_index = 1 # Router-2
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 Connection Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
if connection_router_2.is_alive() == True:
logging('YSHA Router 2 SSH Connection OK', 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, router_id[eq_index])
data_prepare(eq_index, success_message)
try:
env = connection_router_2.send_command('show env', use_textfsm=True)
# print(json.dumps(env, indent=2))
# print(str(env))
env_str = (str(env).strip()).splitlines()
for rear in range(len(env_str)):
env_str[rear] = env_str[rear].strip()
# print(rear, ":", env_str[rear])
num_of_critical_alarm = env_str[0][26:len(env_str[0])].strip()
num_of_major_alarm = env_str[1][26:len(env_str[1])].strip()
num_of_minor_alarm = env_str[2][26:len(env_str[2])].strip()
fan0_RPM = env_str[6][23:30].strip()
fan1_RPM = env_str[7][23:30].strip()
fan2_RPM = env_str[8][23:30].strip()
fan3_RPM = env_str[9][23:30].strip()
power = env_str[10][23:30].strip()
temp_inlet = env_str[11][23:30].strip()
temp_interval = env_str[12][23:30].strip()
temp_outlet = env_str[13][23:30].strip()
temp_cpu = env_str[14][23:30].strip()
V_12 = env_str[15][23:30].strip()
V_5 = env_str[16][23:30].strip()
V_33 = env_str[17][23:30].strip()
V_30 = env_str[18][23:30].strip()
V_25 = env_str[19][23:30].strip()
V_135 = env_str[20][23:30].strip()
V_18 = env_str[21][23:30].strip()
V_12 = env_str[22][23:30].strip()
VNN_CPU = env_str[23][23:30].strip()
V_11 = env_str[24][23:30].strip()
V_10 = env_str[25][23:30].strip()
V_18_CPU = env_str[26][23:30].strip()
DDR = env_str[27][23:30].strip()
V_33_STBY = env_str[28][23:30].strip()
V_15 = env_str[29][23:30].strip()
V_10_CPU = env_str[30][23:30].strip()
VCC_CPU = env_str[31][23:30].strip()
V_075 = env_str[32][23:30].strip()
V_12 = env_str[33][23:30].strip()
PWR = env_str[34][23:30].strip()
result = re.findall(r"Fail", str(env))
fail = 0
for ali in result:
if ali == "Fail":
fail += 1
if fail == 0:
logging("YSHA Router 2 Internal Check PASS", 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, success_message)
else:
logging("YSHA Router 2 Internal FAIL (temperature or power", 1) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, fail_message)
except Exception as e:
logging("YSHA Router 2 Internal Check can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 CPU Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
cpu_router_2 = connection_router_2.send_command('show processes cpu', use_textfsm=True)
# print(json.dumps(cpu, indent=2))
if int(cpu_router_2[0]['cpu_1_min']) >= cpu_max:
logging('YSHA Router 2 High CPU', 1) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, fail_message)
else:
logging('YSHA Router 2 Low CPU', 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, success_message)
except Exception as e:
logging("YSHA Router 2 CPU Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 Send Date & Time %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
time_router_2 = connection_router_2.send_command('show clock', use_textfsm=True)
# print(json.dumps(time_router_1, indent=2))
time_to_bytes = str(time_router_2[0]['time'])
time_to_bytes = time_to_bytes.split(":")
for pill in range(len(time_to_bytes)):
time_to_bytes[pill] = float(time_to_bytes[pill])
for pill in range(len(time_to_bytes)):
time_to_bytes[pill] = int(time_to_bytes[pill])
for pill in range(len(time_to_bytes)):
time_to_bytes[pill] = str(time_to_bytes[pill])
if time_router_2[0]['month'] == "Jan": month = "01"
if time_router_2[0]['month'] == "Feb": month = "02"
if time_router_2[0]['month'] == "Mar": month = "03"
if time_router_2[0]['month'] == "Apr": month = "04"
if time_router_2[0]['month'] == "May": month = "05"
if time_router_2[0]['month'] == "Jun": month = "06"
if time_router_2[0]['month'] == "Jul": month = "07"
if time_router_2[0]['month'] == "Aug": month = "08"
if time_router_2[0]['month'] == "Sep": month = "09"
if time_router_2[0]['month'] == "Oct": month = "10"
if time_router_2[0]['month'] == "Nov": month = "11"
if time_router_2[0]['month'] == "Dec": month = "12"
time_to_bytes[0] = text_arrange(time_to_bytes[0], 2) # arrange to be 2 digit
time_to_bytes[1] = text_arrange(time_to_bytes[1], 2) # arrange to be 2 digit
time_to_bytes[2] = text_arrange(time_to_bytes[2], 2) # arrange to be 2 digit
data_prepare(eq_index, time_to_bytes[0])
data_prepare(eq_index, time_to_bytes[1])
data_prepare(eq_index, time_to_bytes[2])
data_prepare(eq_index, month)
data_prepare(eq_index, str(time_router_2[0]['day']))
data_prepare(eq_index, str(time_router_2[0]['year']))
except Exception as e:
logging("YSHA Router 1 Date & Time can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
data_prepare(eq_index, access_fail_message)
data_prepare(eq_index, access_fail_message)
data_prepare(eq_index, access_fail_message)
data_prepare(eq_index, access_fail_message)
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 Send Version %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
version_router_2 = connection_router_2.send_command('show version', use_textfsm=True)
# print(json.dumps(version_router_1, indent=2))
#print(Fore.GREEN + Back.BLACK + 'YSHA Router 2 Version: ' + str(version_router_2[0]['version']))
ver_str = text_arrange(str(version_router_2[0]['version']), 20) # arrange to be 20 digit
data_prepare(eq_index, ver_str)
except Exception as e:
logging("YSHA Router 2 Version can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 Interface Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
router_2_interface = connection_router_2.send_command('show ip interface brief', use_textfsm=True)
# print(json.dumps(router_2_interface, indent=2))
i = 0
for i in range(len(router_2_interface)):
if ((router_2_interface[i]['intf'] == 'GigabitEthernet0/0') and (
router_2_interface[i]['status'] == 'up')):
router_2_gigabitetnernet10_interface_problem = 0
else:
...
i = 0
for i in range(len(router_2_interface)):
if ((router_2_interface[i]['intf'] == 'GigabitEthernet1/0') and (
router_2_interface[i]['status'] == 'up')):
router_2_gigabitetnernet20_interface_problem = 0
else:
...
if router_2_gigabitetnernet10_interface_problem == 0 and router_2_gigabitetnernet20_interface_problem == 0:
router_2_interface_problem = 1
logging("YSHA Router 2 Interface Check PASS", 0) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, success_message)
else:
router_2_interface_problem = 0
logging("YSHA Router 2 Interface FAIL", 1) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, fail_message)
except Exception as e:
logging("YSHA Router 2 Interface Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, "k")
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Router 2 Standby Status %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
standby_router_2 = connection_router_2.send_command('show standby', use_textfsm=True)
if standby_router_2[0]['state'] == 'Active':
logging('YSHA Router 2 is Active', 0) # "0" is INFO, "1" is WARNING"
standby_router_problem = 0
active_router = 2
data_prepare(eq_index, success_message)
elif standby_router_2[0]['state'] == 'Standby':
logging('YSHA Router 2 is Standby', 2) # "0" is INFO, "1" is WARNING"
standby_router_problem = 0
data_prepare(eq_index, "*")
else:
logging('YSHA Router Redundancy FAIL', 1) # "0" is INFO, "1" is WARNING"
standby_router_problem = 1
active_router = 0
data_prepare(eq_index, fail_message)
except Exception as e:
logging("YSHA Router 2 Standby Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, active_router_connection)
else:
dummy_var_to_jump_exception_1 = dummy_var_to_jump_exception_2 # dummy fail mesaage to jump exception
except Exception as e:
logging("YSHA Router 2 SSH Connection can't be established", 1) # "0" is INFO, "1" is WARNING"
load_default(eq_index)
data_prepare(eq_index, router_id[eq_index])
data_prepare(eq_index, fail_message)
logging("New connection is trying to establish for " + "Router " + str(eq_index + 1) + "...", 0) # "0" is INFO, "1" is WARNING"
try:
connection_router_2 = ConnectHandler(**router[eq_index])
text = "New connection " + "(Router " + str(eq_index + 1) + ") " + "SUCCEED..."
logging(text, 0) # "0" is INFO, "1" is WARNING"
except:
text = "New connection " + "(Router " + str(eq_index + 1) + ") " + "FAILED..."
logging(text, 1) # "0" is INFO, "1" is WARNING"
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Switch CPU Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
with open('../../../Desktop/pyhton_projeleri/pycurl/IP_LIST_Switch.txt') as IP_file_switch:
IP_Switch = IP_file_switch.read().splitlines()
switch = {
'device_type': 'cisco_ios_telnet',
'ip': IP_Switch[0],
'username': 'cisco',
'password': 'cisco',
'secret': 'cisco',
'port': 23
}
connection_switch = ConnectHandler(**switch)
try:
cpu_switch_1 = connection_switch.send_command('show processes cpu', use_textfsm=True)
# print(json.dumps(cpu_switch_1, indent=2))
if int(cpu_switch_1_1[0]['cpu_1_min']) >= cpu_max:
logging('YSHA Switch 1 High CPU', 1) # "0" is INFO, "1" is WARNING"
cpu_switch_problem = 1
data_prepare(eq_index, fail_message)
else:
logging('YSHA Switch 1 Low CPU', 0) # "0" is INFO, "1" is WARNING"
cpu_switch_problem = 0
data_prepare(eq_index, success_message)
except Exception as e:
logging("YSHA Switch 1 CPU Status can't be reachable", 2) # "0" is INFO, "1" is WARNING"
data_prepare(eq_index, access_fail_message)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Switch STANDBY Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
cpu_switch = connection_switch.send_command('show switch detail', use_textfsm=True)
print(json.dumps(cpu_switch, indent=2))
if cpu_switch[0]['state'] == 'Ready' and cpu_switch[1]['state'] == 'Ready':
print('YSHA Switch 1 and Switch 2 OK')
cpu_switch_problem = 1
else:
print('YSHA Switch FAULT')
cpu_switch_problem = 0
except Exception as e:
print(e)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Switch INTERFACE Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
cpu_switch = connection_switch.send_command('show logging')
result = re.findall(r"down", str(cpu_switch))
if result[0] == 'down':
print(' YSHA Switch PORT KONTROL ET')
connection_switch.send_command('clear logging\n')
cpu_switch_problem = 1
else:
print('YSHA Switch 1 and Switch 2 PORT OK')
cpu_switch_problem = 0
except Exception as e:
print(e)
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% YSHA Switch KRITIK HATA KONTROLU %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
kritik_hata_switch = connection_switch.send_command('show logging')
result = re.findall(r"-1-", str(kritik_hata_switch))
if result == '-1-':
print(' YSHA Switch KRITIK HATA')
connection_switch.send_command('clear logging\n')
kritik_hata_switch_problem = 1
else:
print('YSHA Switch 1 and Switch 2 KRITIK HATA YOK')
kritik_hata_switch_problem = 0
except Exception as e:
print(e)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TAFICS Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try:
if active_router == 1:
active_router_connection = connection_router_1
aR = router_id[0]
elif active_router == 2:
active_router_connection = connection_router_2
aR = router_id[1]
else:
...
#print(json.dumps(interfaces, indent=2))
taffics_connection = active_router_connection.send_command('ping 2.2.2.10', use_textfsm=True)
result = re.findall(r"Success rate is 100 percent", str(taffics_connection))
if result == ['Success rate is 100 percent']:
logging('TAFICS Status: OK', 0) # "0" is INFO, "1" is WARNING"
tafics_failure = 0
else:
logging('TAFICS Status: Not OK!!!', 1) # "0" is INFO, "1" is WARNING"
tafics_failure += 1
if tafics_failure > tafics_failure_count:
tafics_failure = 1
if tafics_failure == 0:
check_first_time = 0
command = active_router_connection.send_command('show ip interface brief', use_textfsm=True)
i = 0
for i in range(len(command)):
if (command[i]['intf'] == 'Tunnel0') and (
command[i]['status'] == 'down' or
command[i]['status'] == 'administratively down'):
logging('Tunnel configuration starting (no shutdown)...', 0) # "0" is INFO, "1" is WARNING"
output = active_router_connection.send_config_set(['xinterface xtunnel 0',
'ip address 3.3.3.1 255.255.255.0',
'no shut',
'exit'])
else:
...
i = 0
for i in range(len(command)):
if command[i]['intf'] == 'Loopback0':
logging('Loopback 0 configuration starting (shutdown)...', 0) # "0" is INFO, "1" is WARNING"
output = active_router_connection.send_config_set(['xno interface loopback 0',
'exit'])
else:
...
if tafics_failure == tafics_failure_count:
command = active_router_connection.send_command('show ip interface brief', use_textfsm=True)
#print(command)
i = 0
for i in range(len(command)):
if (command[i]['intf'] == 'Tunnel0') and (
command[i]['status'] == 'up'):
logging('Tunnel configuration starting (shutdown)...', 0) # "0" is INFO, "1" is WARNING"
output = active_router_connection.send_config_set(['interface xtunnel 0',
'ip address 3.3.3.1 255.255.255.0',
'shut',
'exit'])
else:
...
if check_first_time == 0:
logging('Loopback 0 configuration starting (no shutdown)...', 0) # "0" is INFO, "1" is WARNING"
output = active_router_connection.send_config_set(['interface xloop 0',
'ip address 10.10.10.10 255.255.255.0',
'exit'])
check_first_time = 1
except Exception as e:
logging("###coding error### (Exception)", 2)