samir82show

VPlex report script

Dec 29th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #!/opt/rh/python33/root/usr/bin/python3
  2.  
  3. import sys, os
  4. import re
  5. import subprocess
  6. import json
  7. import logging
  8. import openpyxl
  9. from openpyxl.styles import *
  10. from openpyxl.chart import BarChart, Reference
  11. from openpyxl.chart.marker import DataPoint
  12. #logging.disable (logging.CRITICAL)
  13. logging.basicConfig(filename='vplex_fetch.log', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
  14. logging.debug('Start of %s' % (sys.argv[0]))
  15.  
  16. USERNAME = 'UserName'
  17. PASSWORD = 'PassWord'
  18. VPLEX_IP = 'VPLEX_IP'
  19. EMAIL_FROM = '[email protected]'
  20. EMAIL_TO = '[email protected]'
  21.  
  22. def st_view (clusName):
  23. cmd = "curl -k -H \"Username:" + USERNAME + "\" -H \"Password:" + PASSWORD + "\" -s -g -d \'{\"args\":\"/clusters/" + clusName + "/exports/storage-views/\"}\' -X POST https://" + VPLEX_IP + "/vplex/ls"
  24. sv_list = []
  25. output = json.loads (str ((subprocess.check_output (cmd, shell=True)).decode ("utf-8")))
  26. l = (((output['response'])['custom-data'])).split ()
  27. for i in l:
  28. if i != '/clusters/' + clusName + '/exports/storage-views:':
  29. sv_list.append (i)
  30. return (sv_list)
  31.  
  32. def view_show (view_name, clusName):
  33. r = []
  34. cmd = "curl -k -H \"Username:" + USERNAME + "\" -H \"Password:" + PASSWORD + "\" -s -g -d \'{\"args\":\"-lf /clusters/" + clusName + "/exports/storage-views/" + view_name + "\"}\' -X POST https://" + VPLEX_IP + "/vplex/ls"
  35. output = json.loads (str ((subprocess.check_output (cmd, shell=True)).decode ("utf-8")))
  36. logging.debug('output: %s' % (output))
  37. l = (((output['response'])['custom-data'])).split ()
  38. for i in l:
  39. if (re.search (r'\(.*\)', i)):
  40. r.append (((re.sub ('\[|\]|\(|\)|[,]$', '', i))).split (','))
  41. return (r)
  42.  
  43. def st_calc (sv_dict, view_name):
  44. total = 0
  45. num = 0
  46. size = ''
  47. for i, j, l, m in sv_dict[view_name]:
  48. num = float (re.search (r'(\d*.\d*)(G|T|P)', m).group (1))
  49. size = re.search (r'(\d*.\d*)(G|T|P)', m).group (2)
  50. if size == 'G':
  51. num = num * 1
  52. elif size == 'T':
  53. num = num * 1024
  54. elif size == 'P':
  55. num = num * (1024 ** 2)
  56. total = total + num
  57. sv_dict [view_name].append (['Total', '', '', format (total, '.2f')])
  58. return sv_dict
  59.  
  60. def pop_excl (sv_dict, ClusName):
  61. wb = openpyxl.Workbook ()
  62. sh = wb.active
  63. count1 = 0
  64. count2 = 2
  65. alph = ['a', 'b', 'c', 'd']
  66. f = sh['a1']
  67. f.font = Font (bold=True)
  68. f = sh['b1']
  69. f.font = Font (bold=True)
  70. sh.title = 'HighLevel'
  71. sh['a1'] = 'StorageView'
  72. sh['b1'] = 'Size(G)'
  73. for i in sv_dict:
  74. sh[alph[count1] + str (count2)] = i
  75. count1 += 1
  76. sh[alph[count1] + str (count2)] = float (sv_dict[i][-1][-1])
  77. count2 += 1
  78. count1 = 0
  79. count2 = 2
  80. for i in sv_dict:
  81. sh = wb.create_sheet (i)
  82. sh = wb.get_sheet_by_name (i)
  83. f = sh['a1']
  84. f.font = Font (bold=True)
  85. f = sh['b1']
  86. f.font = Font (bold=True)
  87. f = sh['c1']
  88. f.font = Font (bold=True)
  89. f = sh['d1']
  90. f.font = Font (bold=True)
  91. sh['a1'] = 'LunID'
  92. sh['b1'] = 'Name'
  93. sh['c1'] = 'VPD'
  94. sh['d1'] = 'Size(G/T)'
  95. for j in range (len (sv_dict[i])):
  96. for k in range (4):
  97. sh[alph[count1] + str (count2)] = sv_dict[i][j][k]
  98. count1 += 1
  99. count2 += 1
  100. count1 = 0
  101. count2 = 2
  102.  
  103. logging.debug('Start of chart')
  104. l = len(sv_dict)
  105.  
  106. sh = wb.get_sheet_by_name ('HighLevel')
  107. logging.debug('sheets: %s' % (wb.get_sheet_names ()))
  108. logging.debug('sh: %s' % (sh.title))
  109. chart1 = BarChart()
  110. chart1.type = "col"
  111. chart1.style = 11
  112. chart1.title = "VPlex Capacity Report"
  113. chart1.y_axis.title = 'Size'
  114. chart1.x_axis.title = 'View Name'
  115. logging.debug('len of sv_dict: %d' % (l))
  116. data = Reference(sh, min_col=2, min_row=2, max_row=l + 1, max_col=2)
  117. cats = Reference(sh, min_col=1, min_row=2, max_row=l + 1)
  118. chart1.add_data(data, titles_from_data=False)
  119. chart1.set_categories(cats)
  120. chart1.top = 100
  121. chart1.left = 30
  122. chart1.width = 27
  123. chart1.height = 10
  124. chart1.shape = sh.add_chart(chart1, "D2")
  125.  
  126. wb.save (ClusName)
  127. return 0
  128.  
  129. def main ():
  130. c_tup = ('cluster-1', 'cluster-2')
  131. clus_dict = {}
  132. sv_dict1 = {}
  133. sv_dict2 = {}
  134. sv_list = []
  135. for i in st_view ('cluster-1'):
  136. logging.debug('cluster-1 start view name: %s' % (i))
  137. for j in (view_show (i, 'cluster-1')):
  138. sv_list.append (j)
  139. sv_dict1[i] = sv_list
  140. sv_dict1[i] = st_calc (sv_dict1, i)[i]
  141. sv_list = []
  142.  
  143. pop_excl (sv_dict1, 'VPlexReport_Cluster1.xlsx')
  144.  
  145. for i in st_view ('cluster-2'):
  146. logging.debug('cluster-2 start view name: %s' % (i))
  147. for j in (view_show (i, 'cluster-2')):
  148. sv_list.append (j)
  149. sv_dict2[i] = sv_list
  150. sv_dict2[i] = st_calc (sv_dict2, i)[i]
  151. sv_list = []
  152. pop_excl (sv_dict2, 'VPlexReport_Cluster2.xlsx')
  153.  
  154. os.system ('echo "VPlex Report" | /bin/mailx -s "VPlex Report" -r %s -a VPlexReport_Cluster1.xlsx -a VPlexReport_Cluster2.xlsx %s' % (EMAIL_FROM, EMAIL_TO))
  155. logging.debug('end of script')
  156.  
  157. return 0
  158. if __name__ == '__main__':
  159. main ()
Advertisement
Add Comment
Please, Sign In to add comment