Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import pymysql.cursors
  4. import threading
  5. import time
  6. import queue
  7. import multiprocessing
  8.  
  9. exitFlag = 0
  10.  
  11. mysqlHost = 'localhost'
  12. mysqlUser = 'root'
  13. mysqlPass = 'Zertodata1!'
  14. mysqlDb = 'zerto'
  15.  
  16.  
  17. class myThread (threading.Thread):
  18. def __init__(self, id, threadName, workQueue, vmList, mysqlHost, mysqlUser, mysqlPass, mysqlDb):
  19. threading.Thread.__init__(self)
  20. self.id = id
  21. self.threadName = threadName
  22. self.workQueue = workQueue
  23. self.vmlist = vmList
  24. self.mysqlHost = mysqlHost
  25. self.mysqlUser = mysqlUser
  26. self.mysqlPass = mysqlPass
  27. self.mysqlDb = mysqlDb
  28. def run(self):
  29. print ("Starting " + self.name)
  30. mysql_worker(self.id, self.threadName, self.workQueue, self.vmlist, self.mysqlHost, self.mysqlUser, self.mysqlPass, self.mysqlDb)
  31. print ("Exiting " + self.name)
  32.  
  33. def mysql_worker(id, threadName, workQueue, vmlist, servername, user, password, db):
  34. while not exitFlag:
  35. queueLock.acquire()
  36. if not workQueue.empty():
  37. print("{} working...".format(threadName))
  38. date = workQueue.get()
  39. queueLock.release()
  40. connection = pymysql.connect(host=mysqlHost,user=mysqlUser,password=mysqlPass,db=mysqlDb,charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
  41.  
  42. try:
  43. with connection.cursor() as cursor:
  44. # Get the list of monitored VMs
  45. for i in range(len(vmlist)):
  46. sql = "SELECT (SUM(`IOPSWriteAvg`)) as iops, (SUM(`KBWriteAvg`)) as KBWrite FROM `stats` WHERE datestamp = '{}' AND vm = '{}';".format(date, vmlist[i])
  47. cursor.execute(sql)
  48. stats = cursor.fetchone()
  49.  
  50. io = stats['iops']
  51. KB = stats['KBWrite']
  52.  
  53. if io is None:
  54. io = 0
  55. if KB is None:
  56. KB = 0
  57.  
  58. io = str(int(io))
  59. KB = str(int(KB))
  60.  
  61. vm = vmlist[i]
  62. datestr = str(date)
  63.  
  64. print('{} - {} - {} - {} iops - {} KBps'.format(threadName, datestr, vm, io, KB))
  65.  
  66. sql = "INSERT INTO dailystats (datestamp, vm, WriteIOps, WriteKBps) VALUES ('{0}', '{1}', '{2}', '{3}');".format(datestr, vm, io, KB)
  67. cursor.execute(sql)
  68. connection.commit()
  69.  
  70. finally:
  71. connection.close()
  72. else:
  73. queueLock.release()
  74. print("{} sleeping".format(threadName))
  75. time.sleep(1)
  76.  
  77. # main thread begins here
  78. numCores = multiprocessing.cpu_count()
  79. print("{} Cores".format(numCores))
  80.  
  81. threadList = []
  82. dateList = []
  83. vmList = []
  84. queueLock = threading.Lock()
  85. workQueue = queue.Queue()
  86. threads = []
  87. threadID = 1
  88.  
  89. #build thread list for with numcores threads
  90. for x in range(numCores):
  91. y = "thread{}".format(x)
  92. threadList.append(y)
  93.  
  94. connection = pymysql.connect(host=mysqlHost,user=mysqlUser,password=mysqlPass,db=mysqlDb,charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
  95.  
  96. try:
  97. with connection.cursor() as cursor:
  98. # Get the list of monitored VMs
  99. sql = "SELECT `name` FROM `vms` WHERE `monitor` = 'Y';"
  100. cursor.execute(sql)
  101. MonitoredVms = cursor.fetchall()
  102. print("VM list retrieved")
  103. for x in MonitoredVms:
  104. vmList.append(x['name'])
  105.  
  106. with connection.cursor() as cursor:
  107. # get the list of datestamps
  108. sql = "SELECT DISTINCT `datestamp` FROM `stats`;"
  109. cursor.execute(sql)
  110. datestamps = cursor.fetchall()
  111. print("Datestamps retrieved")
  112. for x in datestamps:
  113. dateList.append(x['datestamp'])
  114.  
  115. for tName in threadList:
  116. # Create new threads
  117. thread = myThread(threadID, tName, workQueue, vmList, mysqlHost, mysqlUser, mysqlPass, mysqlDb)
  118. thread.start()
  119. threads.append(thread)
  120. threadID += 1
  121.  
  122. queueCount = 0
  123. queueLock.acquire()
  124. while dateList:
  125. workQueue.put(dateList.pop())
  126. queueCount += 1
  127. queueLock.release()
  128. print("{} Items in queue".format(queueCount))
  129.  
  130. # Wait for queue to empty
  131. while not workQueue.empty():
  132. time.sleep(1)
  133. pass
  134.  
  135. # Notify threads it's time to exit
  136. exitFlag = 1
  137.  
  138. for t in threads:
  139. t.join()
  140.  
  141. print ("Exiting Main Thread")
  142.  
  143. finally:
  144. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement