Advertisement
Guest User

Untitled

a guest
Sep 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. #FileGetter
  2. import ssh
  3. import re
  4. #Function to delete specified regix matches in a string
  5. def replace(value,reg):
  6. return re.sub(reg, r'', value)
  7. server = ssh.Connection(host='10.30.160.81', username='report_viewer_smtbusa', password='4S9xSfVFvzsyC1CXHWpR')
  8. relations=open(".\Relationships",'r')
  9. for relation in relations:
  10. relation=replace(relation,r'\n')
  11. temp=relation.split(":")
  12. creatorName=temp[0]
  13. folderName=temp[1]
  14. command='find /store/reporting/reports/{0}/reports* | sort -n -r | grep -P ".+{1}.+\.csv"| head -1'.format(creatorName, folderName)
  15. result = server.execute(command)[0].decode("utf-8")
  16. result=replace(result,r'\n')
  17. print(temp[2])
  18. server.get(result,f"./Reports/"+temp[2]+".csv")
  19.  
  20. #LogGenerator
  21. from docx import Document
  22. from docx.shared import Inches
  23. import matplotlib.pyplot as plt
  24. from LogGrapghGen import newest,barCreate, FolderFiles,replace
  25. import os
  26. from pathlib import Path
  27. import re
  28. import datetime
  29.  
  30. def BarGrapghCreator(filepath,description, document,heading=1):
  31. rows=[]
  32. label=0
  33. value=10
  34. fd=open(filepath,'r')
  35. for row in fd:
  36. row=row[:-1]
  37. rowArray=[]
  38. if row[0] == '"':
  39. rowArray=row.split('","')
  40. rowArray[0]=rowArray[0][1:]
  41. rowArray[-1]=rowArray[-1][:-1]
  42. else:
  43. rowArray=row.split(',')
  44. rows.append(rowArray)
  45. if len(rows)<2:
  46. return False
  47. p = Path(filepath)
  48. imagePath,catagory,large=barCreate(rows,p.name[:-4],label,value)
  49. document.add_heading(catagory, level=heading)
  50. if large:
  51. document.add_picture(imagePath, width=Inches(7), height=Inches(5))
  52. else:
  53. document.add_picture(imagePath, width=Inches(7), height=Inches(3.5))
  54. document.add_paragraph(description)
  55. return True
  56.  
  57. def TableCreator(filepath,description, document,heading=1):
  58. rows=[]
  59. fd=open(filepath,'r')
  60. for row in fd:
  61. row=row[:-1]
  62. rowArray=[]
  63. if row[0] == '"':
  64. rowArray=row.split('","')
  65. rowArray[0]=rowArray[0][1:]
  66. rowArray[-1]=rowArray[-1][:-1]
  67. else:
  68. rowArray=row.split(',')
  69. rows.append(rowArray)
  70. p = Path(filepath)
  71. if len(rows)<2:
  72. return False
  73. document.add_heading(p.name[:-4], level=heading)
  74. table = document.add_table(1, len(rows[0]))
  75. table.style = 'GridTable4-Accent1'
  76. heading_cells = table.rows[0].cells
  77. for index in range(len(rows[0])):
  78. heading_cells[index].text=rows[0][index]
  79. for arrayrow in rows[1:]:
  80. cells = table.add_row().cells
  81. for index in range(len(arrayrow)):
  82. cells[index].text=arrayrow[index]
  83. document.add_paragraph(description)
  84. return True
  85.  
  86. def USBTableCreator(filepath,description, document,heading=1):
  87. rows=[]
  88. names=[]
  89. fd=open(filepath,'r', encoding="utf8")
  90. rows.append(["Username", "PC Name", "Time"])
  91. for row in fd:
  92. row=row[:-1]
  93. rowArray=[]
  94. rowArray=row.split(',')
  95. if "USB Device" in rowArray[15]:
  96. if rowArray[21] in names:
  97. continue
  98. rows.append([rowArray[21],rowArray[4],rowArray[11]])
  99. names.append(rowArray[21])
  100. p = Path(filepath)
  101. if len(rows)<2:
  102. return False
  103. document.add_heading(p.name[:-4], level=heading)
  104. table = document.add_table(1, len(rows[0]))
  105. table.style = 'GridTable4-Accent1'
  106. heading_cells = table.rows[0].cells
  107. for index in range(len(rows[0])):
  108. heading_cells[index].text=rows[0][index]
  109. for arrayrow in rows[1:]:
  110. cells = table.add_row().cells
  111. for index in range(len(arrayrow)):
  112. cells[index].text=arrayrow[index]
  113. document.add_paragraph(description)
  114. return True
  115.  
  116. def main():
  117. document = Document('LOG-MANAGEMENT-REPORT-DAILY-TEMPLETE.docx')
  118. #document.add_heading('Siem Report', 0)
  119. current_section = document.sections[-1]
  120. current_section.left_margin = Inches(1)
  121. current_section.right_margin = Inches(1)
  122. #p = document.add_paragraph('')
  123. folderPath=".\Reports"
  124. folderpath = os.path.abspath(folderPath)
  125. categoriesFile=open(".\Categories",'r')
  126. for cat in categoriesFile:
  127. tabs=len(re.findall(r'\t', cat))
  128. cat=replace(cat,r'\t')
  129. cat=replace(cat,r'\n')
  130. export=False
  131. for filepath in filter(lambda x: cat in x, FolderFiles(folderpath)):
  132. print(cat)
  133. export=True
  134. if filepath == "":
  135. continue
  136. description=""
  137. if "Top 10" in cat:
  138. if not BarGrapghCreator(filepath,description,document,tabs+2):
  139. print("Invalid Input for "+filepath+" will skip genenration of graph")
  140. continue
  141. elif "USB" in cat:
  142. if not USBTableCreator(filepath,description,document,tabs+2):
  143. print("There is no data in "+filepath+" will skip genenration of table")
  144. continue
  145. else:
  146. if not TableCreator(filepath,description,document,tabs+2):
  147. print("There is no data in "+filepath+" will skip genenration of table")
  148. continue
  149. if not export and tabs==0:
  150. document.add_heading(cat, level=1+1)
  151. filename='Siem Report '+datetime.datetime.today().strftime('%Y-%m-%d')+'.docx'
  152. document.save(filename)
  153. print(filename)
  154.  
  155.  
  156. if __name__ == '__main__':
  157. main()
  158.  
  159. #LogGraphGen
  160. import os
  161. import matplotlib.pyplot as plt; plt.rcdefaults()
  162. import numpy as np
  163. import matplotlib.pyplot as plt
  164. from itertools import cycle, islice
  165. import re
  166. import operator
  167.  
  168.  
  169. def replace(value,reg):
  170. return re.sub(reg, r'', value)
  171.  
  172. def newest(path):
  173. files = os.listdir(path)
  174. paths = []
  175. for basename in files:
  176. if basename.endswith(".csv"):
  177. paths = [os.path.join(path, basename)]
  178. if len(paths) == 0:
  179. return ""
  180. else:
  181. return max(paths, key=os.path.getmtime)
  182.  
  183. def FolderFiles(path):
  184. files = os.listdir(path)
  185. paths = []
  186. for basename in files:
  187. if basename.endswith(".csv"):
  188. paths.append(os.path.join(path, basename))
  189. if len(paths) == 0:
  190. return ""
  191. else:
  192. return paths
  193.  
  194. def label_bar(ax, bars, text_format, is_inside=True, **kwargs):
  195. """
  196. Attach a text label to each bar displaying its y value
  197. """
  198. max_y_value = max(bar.get_height() for bar in bars)
  199. if is_inside:
  200. distance = max_y_value * 0.05
  201. else:
  202. distance = max_y_value * 0.01
  203. for bar in bars:
  204. text = text_format.format(bar.get_height())
  205. text_x = bar.get_x() + bar.get_width() / 2
  206. if is_inside:
  207. text_y = bar.get_height() - distance
  208. else:
  209. text_y = bar.get_height() + distance
  210. ax.text(text_x, text_y, text, ha='center', va='bottom', **kwargs)
  211.  
  212. def barCreate(rows, category, labels, data):
  213. x_labels=[]
  214. values=[]
  215. x_dic = {}
  216. big=False
  217. for index in range(1,len(rows)):
  218. x_label=rows[index][labels]
  219. if len(x_label)>30:
  220. big=True
  221. value=rows[index][data].split(",")
  222. valuenum="".join(value)
  223. if x_label in x_dic.keys():
  224. x_dic[rows[index][labels]]=x_dic[rows[index][labels]] + int(valuenum)
  225. else:
  226. x_dic[rows[index][labels]]=int(valuenum)
  227. sortedDic=sorted(x_dic.items(), key=operator.itemgetter(1),reverse=True)
  228. for item in sortedDic:
  229. x_labels.append(item[0])
  230. values.append(item[1])
  231. objects = tuple(x_labels)
  232. y_pos = np.arange(len(objects))
  233. if big:
  234. plt.figure(figsize=(15, 11))
  235. else:
  236. plt.figure(figsize=(11, 5.5))
  237. my_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(y_pos))]
  238. cmap = plt.cm.hsv
  239. bars=plt.bar(y_pos, values, align='center', alpha=0.5, color=cmap(np.linspace(0, 1, len(objects)+1)), label=objects)
  240. plt.xticks(y_pos, objects, rotation='vertical')
  241. try:
  242. plt.tight_layout()
  243. except:
  244. pass
  245. plt.ylabel(rows[0][data])
  246. plt.title(category)
  247. value_format = "{}"
  248. label_bar(plt, bars, value_format, is_inside=False, fontweight="bold")
  249. filepath = ".\images\\"+category+".png"
  250. if big:
  251. if "URL" in category:
  252. pass
  253. else:
  254. plt.legend(bars, objects)
  255. plt.savefig(filepath,dpi=300, format='png', bbox_inches='tight')
  256. #plt.show()
  257. plt.close()
  258. return filepath,category,big
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement