-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBulkImportIoTop.py
executable file
·64 lines (52 loc) · 1.58 KB
/
BulkImportIoTop.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
import pymssql
import csv
import os
import sys
def GetConnectionString():
file = open("./connectionstring.txt", "r")
s = file.read().replace("\n","")
d = dict(item.split(":") for item in s.split(","))
return d
# Connect to the database
connectionString = GetConnectionString()
conn = pymssql.connect(host=connectionString["host"], user=connectionString["user"],
password=connectionString["password"], database=connectionString["database"])
cursor = conn.cursor()
# Loop through the text file
fn = './ioTopData.txt'
if not os.path.exists(fn):
exit()
with open(fn) as f:
for row in f:
data = (row.strip().split("|"))
#print(data)
sql = "insert into iotop_data(" \
"hostname,timestamp,tid,priority," \
"process_user,disk_read,read_unit,disk_write," \
"write_unit,swapin,io,command)" \
"values(" \
"'{0}','{1}',{2},'{3}'," \
"'{4}',{5},'{6}',{7}," \
"'{8}',{9},{10},'{11}')".format( \
data[0],data[1],data[2], \
data[3],data[4],data[5], \
data[6],data[7],data[8], \
data[9],data[11],data[13])
sys.stdout.write('.')
sys.stdout.flush()
try:
cursor.execute(sql)
except:
print("ERROR")
print(sql)
exit()
pass
# Display inserted data
cursor.execute('select * from dbo.iotop_data')
row = cursor.fetchone()
while row:
#print(str(row[0]) + " " + str(row[1]) + " " + str(row[2]))
row = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()