Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pexpect
- import string
- import datetime
- import MySQLdb
- from pexpect import pxssh
- import time
- import datetime
- from threading import Timer
- import warnings
- import re
- import logging
- #set up the logger
- #set up object to access the database
- db = MySQLdb.connect("localhost","root","1844","CPU")
- cursor = db.cursor()
- startTime = time.time()
- #delay between
- refreshRate = 1
- class sshSession(object):
- _processDict = {}#{'process_name':column_index}
- def __init__(self,cardName,ip,username,password):
- self.cardName = cardName
- self.ip = ip
- self.username = username
- self.password = password
- self.handler = pxssh.pxssh() #create ssh handler
- #create a new table in the database
- sql = "DROP TABLE IF EXISTS "+self.cardName+";"
- #sql = """CREATE TABLE """+self.cardName+""" (time TIMESTAMP) IF NOT EXISTS """+self.cardName
- cursor.execute(sql)
- sql = "CREATE TABLE "+self.cardName+" (time FLOAT);"
- cursor.execute(sql)
- def login(self): #logs in using the ssh handler created in __init__
- self.handler.login(self.ip,self.username,self.password)
- def beginTop(self):
- command = 'top -d '+str(refreshRate)+' -i'
- self.handler.sendline(command)
- def refreshData(self): #read the output of top command
- 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+)"
- data_to_be_inserted = [0]*(len(self._processDict)+1) #will become the row inserted into the db
- data_to_be_inserted[0] = time.time()-startTime
- while(True):
- index = self.handler.expect([regex,pexpect.TIMEOUT],timeout = 0.1)
- if(index == 0):
- process = self.handler.match.group(12)
- invalid_characters = ['.','/','-','+',':']
- for character in invalid_characters:
- process = string.replace(process,character,'')
- CPU = self.handler.match.group(9)
- if process in self._processDict: #check if the process is already a column in the db
- column_index = self._processDict[process]
- data_to_be_inserted[column_index] = CPU
- else:
- column_index = len(self._processDict)+1
- self._processDict[process] = column_index
- data_to_be_inserted.append(CPU)
- #add a new column to the database with label of process
- #print(process)
- sql = "ALTER TABLE "+self.cardName+" ADD "+process+" FLOAT;"
- cursor.execute(sql)
- #put the data into the database
- if(index == 1): #no match, after timeout of 0.1
- #print("finished")
- break
- data_to_be_inserted = tuple(data_to_be_inserted)
- #print(data_to_be_inserted)
- sql = "INSERT INTO "+self.cardName+" VALUES "+str(data_to_be_inserted)
- #print(sql)
- cursor.execute(sql)
- def dataLoop(self): #loops back to itself every refreshRate seconds and refreshes data
- self.refreshData()
- Timer(refreshRate,self.dataLoop).start()
- #open and read the config file
- config = open('config.txt','r')
- lines = config.readlines()
- config.close()
- #concatenate array into one long string
- text = ''.join(lines)
- #search for all matches to pattern
- regex = '(\S+)\n\s+IP: (\S+)\n\s+USER: (\S+)\n\s+PASSWORD: (\S+)'
- regex = re.compile(regex)
- matches = re.findall(regex,text)
- #use matches to create spawn and login children (ssh objects) and add them to the list of children
- childList = []
- for match in matches:
- name,ip,user,password = match
- #print(name)
- temp = sshSession(name,ip,user,password)
- temp.login()
- childList.append(temp)
- #start initial top command on all cards
- for child in childList:
- child.beginTop()
- time.sleep(1)
- #read in the data from all cards again
- for child in childList:
- child.dataLoop() #will start timers in refreshData class that loop back to themselves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement