· 9 years ago · Jun 13, 2017, 07:14 PM
1import pexpect
2import string
3import datetime
4import MySQLdb
5from pexpect import pxssh
6import time
7import datetime
8from threading import Timer
9import warnings
10import re
11import logging
12
13#set up the logger
14#set up object to access the database
15db = MySQLdb.connect("localhost","root","1844","CPU")
16cursor = db.cursor()
17startTime = time.time()
18
19#delay between
20refreshRate = 1
21
22class sshSession(object):
23 _processDict = {}#{'process_name':column_index}
24 def __init__(self,cardName,ip,username,password):
25 self.cardName = cardName
26 self.ip = ip
27 self.username = username
28 self.password = password
29 self.handler = pxssh.pxssh() #create ssh handler
30 #create a new table in the database
31 sql = "DROP TABLE IF EXISTS "+self.cardName+";"
32 #sql = """CREATE TABLE """+self.cardName+""" (time TIMESTAMP) IF NOT EXISTS """+self.cardName
33 cursor.execute(sql)
34 sql = "CREATE TABLE "+self.cardName+" (time FLOAT);"
35 cursor.execute(sql)
36
37 def login(self): #logs in using the ssh handler created in __init__
38 self.handler.login(self.ip,self.username,self.password)
39
40 def beginTop(self):
41 command = 'top -d '+str(refreshRate)+' -i'
42 self.handler.sendline(command)
43
44 def refreshData(self): #read the output of top command
45 regex = "(\d+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\D)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+:\d+.\d+)\s+(\S+)"
46 data_to_be_inserted = [0]*(len(self._processDict)+1) #will become the row inserted into the db
47 data_to_be_inserted[0] = time.time()-startTime
48
49 while(True):
50 index = self.handler.expect([regex,pexpect.TIMEOUT],timeout = 0.1)
51
52 if(index == 0):
53 process = self.handler.match.group(12)
54 invalid_characters = ['.','/','-','+',':']
55 for character in invalid_characters:
56 process = string.replace(process,character,'')
57 CPU = self.handler.match.group(9)
58
59 if process in self._processDict: #check if the process is already a column in the db
60 column_index = self._processDict[process]
61 data_to_be_inserted[column_index] = CPU
62 else:
63 column_index = len(self._processDict)+1
64 self._processDict[process] = column_index
65 data_to_be_inserted.append(CPU)
66 #add a new column to the database with label of process
67 #print(process)
68 sql = "ALTER TABLE "+self.cardName+" ADD "+process+" FLOAT;"
69 cursor.execute(sql)
70 #put the data into the database
71 if(index == 1): #no match, after timeout of 0.1
72 #print("finished")
73 break
74 data_to_be_inserted = tuple(data_to_be_inserted)
75 #print(data_to_be_inserted)
76 sql = "INSERT INTO "+self.cardName+" VALUES "+str(data_to_be_inserted)
77 #print(sql)
78 cursor.execute(sql)
79
80
81 def dataLoop(self): #loops back to itself every refreshRate seconds and refreshes data
82 self.refreshData()
83 Timer(refreshRate,self.dataLoop).start()
84
85#open and read the config file
86config = open('config.txt','r')
87lines = config.readlines()
88config.close()
89#concatenate array into one long string
90text = ''.join(lines)
91#search for all matches to pattern
92regex = '(\S+)\n\s+IP: (\S+)\n\s+USER: (\S+)\n\s+PASSWORD: (\S+)'
93regex = re.compile(regex)
94matches = re.findall(regex,text)
95
96#use matches to create spawn and login children (ssh objects) and add them to the list of children
97childList = []
98for match in matches:
99 name,ip,user,password = match
100 #print(name)
101 temp = sshSession(name,ip,user,password)
102 temp.login()
103 childList.append(temp)
104
105#start initial top command on all cards
106for child in childList:
107 child.beginTop()
108time.sleep(1)
109#read in the data from all cards again
110for child in childList:
111 child.dataLoop() #will start timers in refreshData class that loop back to themselves