Advertisement
clecio-sf

Untitled

Nov 21st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. import subprocess
  5. import re
  6. import time
  7. from datetime import datetime
  8. import threading
  9. import tkinter as tk
  10. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  11. from matplotlib.ticker import FuncFormatter, MaxNLocator
  12.  
  13. valor_x = []
  14. valor_y = []
  15. valor_z = []
  16. host =''
  17. community =''
  18. oid_upload =''
  19. oid_download=''
  20. start = True
  21.  
  22. def getValue(cmd, oid):
  23. # adiciona o oid a linha de comando
  24. cmd += " -o:" + oid
  25.  
  26. # execucao da linha de comando "cmd" e resgate da saida
  27. output = subprocess.check_output(cmd, shell=True)
  28.  
  29. # formatancao da saida para obter apenas o VALUE
  30. Value = str(output)
  31. ind = Value.find('Value')
  32. Value = Value[ind:]
  33. Value = int(re.sub('[^0-9]', '', Value))
  34. return Value
  35.  
  36. def getValues():
  37.  
  38. global valor_x
  39. global valor_y
  40. global valor_z
  41. global host
  42. global community
  43. global oid_upload
  44. global oid_download
  45.  
  46. host = txtIp.get()
  47. community = txtCommunity.get()
  48. oid_upload = txtOIDin.get()
  49. oid_download = txtOIDout.get()
  50. cmd = "SnmpGet.exe -r:" + host + " -c:" + community
  51.  
  52.  
  53. while start == True:
  54. # chamando a funcao getValue para busca dos valores de cada oid
  55. value_up = getValue(cmd, oid_upload)
  56. value_down = getValue(cmd, oid_download)
  57.  
  58. # definicao dos intervalo de tempo
  59. time_exec = 2
  60. time.sleep(time_exec)
  61.  
  62. # calculo das taxas de upload e download em bps
  63. value_up = (getValue(cmd, oid_upload) - value_up) * \
  64. 8 / time_exec
  65. value_down = (getValue(cmd, oid_download) -
  66. value_down) * 8 / time_exec
  67.  
  68. # registro do tempo para cada "Value"
  69. timestamp = datetime.now().strftime('%H:%M:%S')
  70.  
  71. valor_x += [timestamp]
  72. valor_y += [value_up]
  73. valor_z += [value_down]
  74.  
  75.  
  76. # definicao das variaves necessarias para busca dos oids
  77.  
  78.  
  79.  
  80. # host = "192.168.56.101"
  81. # community = "public"
  82. # oid_upload = ".1.3.6.1.2.1.31.1.1.1.6.2"
  83. # oid_download = ".1.3.6.1.2.1.31.1.1.1.10.2"
  84.  
  85. # linha de comando parcial para busca dos valores
  86. # cmd = "SnmpGet.exe -r:" + host + " -c:" + community
  87.  
  88. #ao clicar o botao chamar essa função
  89. def btn_stop():
  90. global start
  91. start = False
  92.  
  93. def btn_click():
  94. global valor_x
  95. global valor_y
  96. global valor_z
  97. valor_x = []
  98. valor_y = []
  99. valor_z = []
  100.  
  101. global start
  102. start = True
  103. t = threading.Thread(target=getValues)
  104. t.start()
  105.  
  106. #configurações da tela
  107. root= tk.Tk()
  108. root.title('Titulo do Programa')
  109. root.geometry('800x600')
  110. #root.resizable(0,0)
  111. root.configure(bg='#fff')
  112.  
  113. #componentes
  114. labelAlerta = tk.Label(root, text='', bg='#fff')
  115. labelAlerta.place(x=320, y=20)
  116. labelAlerta.config(font= ('none',12))
  117.  
  118. labelIp = tk.Label(root, text='Endereço IP do agente', bg='#fff')
  119. labelIp.place(x=200, y=60)
  120. txtIp = tk.Entry(root, bg='#fff')
  121. txtIp.place(x=340, y=60, width='250', height='22')
  122.  
  123. labelCommunity = tk.Label(root, text='Community', bg='#fff')
  124. labelCommunity.place(x=254, y=100)
  125. txtCommunity = tk.Entry(root, bg='#fff')
  126. txtCommunity.place(x=340, y=100, width='250', height='22')
  127.  
  128. labelOIDin = tk.Label(root, text='OID Entrada', bg='#fff')
  129. labelOIDin.place(x=255, y=140)
  130. txtOIDin = tk.Entry(root, bg='#fff')
  131. txtOIDin.place(x=340, y=140, width='250', height='22')
  132.  
  133. labelOIDout = tk.Label(root, text='OID saida', bg='#fff')
  134. labelOIDout.place(x=270, y=180)
  135. txtOIDout = tk.Entry(root, bg='#fff')
  136. txtOIDout.place(x=340, y=180, width='250', height='22')
  137.  
  138. btnIniciar = tk.Button(root, text ='Iniciar', width='15', command=btn_click)
  139. btnIniciar.place(x=290, y=215)
  140.  
  141. btnParar = tk.Button(root, text ='Parar', width='15', command=btn_stop)
  142. btnParar.place(x=420, y=215)
  143.  
  144. fig = plt.figure(figsize=(7,3), dpi=120)
  145. ax = fig.add_subplot(1, 1, 1)
  146. def animate(i):
  147. global valor_x
  148. global valor_y
  149. global valor_z
  150. ax.clear()
  151. ax.plot(valor_x, valor_z, label='Download')
  152. ax.plot(valor_x, valor_y, label='Upload')
  153. ax.legend(loc='lower right')
  154. line2 = FigureCanvasTkAgg(fig, root)
  155. line2.get_tk_widget().pack(side=tk.BOTTOM)
  156. ani = animation.FuncAnimation(fig, animate, interval=1000)
  157. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement