-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiplogjobv4.py
144 lines (134 loc) · 4.63 KB
/
siplogjobv4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/home/GTSesMon/venv/bin/python
from netmiko import ConnectHandler
from datetime import datetime
import sys
import glob
import os, time
from time import gmtime, strftime
import smtplib
from email.mime.text import MIMEText
import jtextfsm as textfsm
import csv
from operator import itemgetter
import smbclient
import ftplib
import pyodbc
import os
def determine_parser(deviceTypeDB):
if deviceTypeDB=='SIP-VIDEO':
return "parserVideo.textfsm"
elif deviceTypeDB=='SIP-CALL':
return "parserSIP.textfsm"
elif deviceTypeDB=='PRI-CALL':
return "parserPRI.textfsm"
#upload file to //tekserv/Gargwacct/SIPGWMon
def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
#Connect to
def connect_db():
dsn = 'server'
user = 'user'
pwd = 'password'
database = 'DB'
host = 'host'
con_string = 'DSN=%s;UID=%s;PWD=%s;' % (dsn, user, pwd)
connection = pyodbc.connect(con_string)
return connection
def get_gateway():
connection = connect_db()
cursor = connection.cursor()
cursor.execute("SELECT gateway_id, device_type, ip_text FROM T_gateway_info")
results = cursor.fetchall()
cursor.close()
del cursor
connection.close()
return results
os.environ['TZ'] = 'Turkey'
time.tzset()
create_time = strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
gateway_info = get_gateway()
gt_counter = 0
for gateway in gateway_info:
isConnected = False
ip_info = gateway_info[gt_counter][2]
cisco_router = {
'device_type': 'cisco_ios',
'ip': ip_info,
'username': 'user',
'password': 'pass',
'secret': 'enable',
'verbose': False,
}
if gateway_info[gt_counter][1] == 'CALL':
logfile = open("/home/GTSesMon/PycharmProjects/SIPGatewayLog/siplog_1001302", "w")
elif gateway_info[gt_counter][1] == 'VIDEO':
logfile = open("/home/GTSesMon/PycharmProjects/SIPGatewayLog/siplog_14412", "w")
net_connect = ConnectHandler(**cisco_router)
prompt_text = net_connect.find_prompt()
print(prompt_text)
if prompt_text != '':
net_connect.enable()
if gateway_info[gt_counter][1]=='CALL':
output = net_connect.send_command('sh call history voice brief | i :')
elif gateway_info[gt_counter][1]=='VIDEO':
output = net_connect.send_command('sh call history video brief | i :')
logfile.write(output)
net_connect.disconnect()
logfile.close()
#CALL-SIP/CALL-PRI
#VIDEO-SIP/VIDEO-PRI
#IP RANGE
#OPEN TEMPLATE
if gateway_info[gt_counter][1] == 'CALL':
template = open("parserSIP.textfsm")
elif gateway_info[gt_counter][1] == 'VIDEO':
template = open("parserVideo.textfsm")
re_table = textfsm.TextFSM(template)
fsm_results = re_table.ParseText(output)
gateway_id_list = [[gateway_info[gt_counter][0]]] * len(fsm_results)
gateway_type_list = [[gateway_info[gt_counter][1]]] * len(fsm_results)
appended_fsm_results = [x+y+z for x,y,z in zip(fsm_results,gateway_id_list,gateway_type_list)]
outfilename = 'gwid'+str(gateway_info[gt_counter][0])+'date'+create_time+'.csv'
outfile = open(outfilename, "w+")
counter = 0
for row in appended_fsm_results:
col_counter = 0
for s in row:
outfile.write("%s;" % s)
col_counter += 1
outfile.write("\n")
counter += 1
print("Write %d records" % counter)
outfile.close()
template.close()
ftp = ftplib.FTP("ftp_ip_add")
ftp.login("ftp_user", "ftp_pass")
ftp.cwd("SIPGWMon")
ftpfile = open(outfilename, "rb")
ftp.storlines("STOR " +outfilename, ftpfile)
ftpfile.close()
ftp.quit()
else:
msg = MIMEText(ip_info+" DOWN!")
msg['Subject'] = 'SIP Gateway Monitoring Alert'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
s = smtplib.SMTP('smtpserv.fw.company.com.tr')
s.send_message(msg)
s.quit()
gt_counter += 1
now = time.time()
listing = glob.glob('/home/GTSesMon/PycharmProjects/SIPGatewayLog/*.csv')
logfileLOG = glob.glob('/home/GTSesMon/siplogjobv4.log')
for list in listing:
if os.path.isfile(list):
if os.stat(list).st_mtime < now - 1 * 60*60*(1/2):
os.remove(list)
for list in logfileLOG:
if os.path.isfile(list):
if os.stat(list).st_mtime < now - 1 * 60*60*(1/2):
os.remove(list)