Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import pexpect
  2. import string
  3. import datetime
  4. import MySQLdb
  5. from pexpect import pxssh
  6. import time
  7. import datetime
  8. from threading import Timer
  9. import warnings
  10. import re
  11. import logging
  12.  
  13. #set up the logger
  14. #set up object to access the database
  15. db = MySQLdb.connect("localhost","root","1844","CPU")
  16. cursor = db.cursor()
  17. startTime = time.time()
  18.  
  19. #delay between
  20. refreshRate = 1
  21.  
  22. class 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
  86. config = open('config.txt','r')
  87. lines = config.readlines()
  88. config.close()
  89. #concatenate array into one long string
  90. text = ''.join(lines)
  91. #search for all matches to pattern
  92. regex = '(\S+)\n\s+IP: (\S+)\n\s+USER: (\S+)\n\s+PASSWORD: (\S+)'
  93. regex = re.compile(regex)
  94. matches = re.findall(regex,text)
  95.  
  96. #use matches to create spawn and login children (ssh objects) and add them to the list of children
  97. childList = []
  98. for 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
  106. for child in childList:
  107. child.beginTop()
  108. time.sleep(1)
  109. #read in the data from all cards again
  110. for child in childList:
  111. child.dataLoop() #will start timers in refreshData class that loop back to themselves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement