Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- import subprocess
- import re
- import time
- from datetime import datetime
- import threading
- import tkinter as tk
- from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
- from matplotlib.ticker import FuncFormatter, MaxNLocator
- valor_x = []
- valor_y = []
- valor_z = []
- host =''
- community =''
- oid_upload =''
- oid_download=''
- start = True
- def getValue(cmd, oid):
- # adiciona o oid a linha de comando
- cmd += " -o:" + oid
- # execucao da linha de comando "cmd" e resgate da saida
- output = subprocess.check_output(cmd, shell=True)
- # formatancao da saida para obter apenas o VALUE
- Value = str(output)
- ind = Value.find('Value')
- Value = Value[ind:]
- Value = int(re.sub('[^0-9]', '', Value))
- return Value
- def getValues():
- global valor_x
- global valor_y
- global valor_z
- global host
- global community
- global oid_upload
- global oid_download
- host = txtIp.get()
- community = txtCommunity.get()
- oid_upload = txtOIDin.get()
- oid_download = txtOIDout.get()
- cmd = "SnmpGet.exe -r:" + host + " -c:" + community
- while start == True:
- # chamando a funcao getValue para busca dos valores de cada oid
- value_up = getValue(cmd, oid_upload)
- value_down = getValue(cmd, oid_download)
- # definicao dos intervalo de tempo
- time_exec = 2
- time.sleep(time_exec)
- # calculo das taxas de upload e download em bps
- value_up = (getValue(cmd, oid_upload) - value_up) * \
- 8 / time_exec
- value_down = (getValue(cmd, oid_download) -
- value_down) * 8 / time_exec
- # registro do tempo para cada "Value"
- timestamp = datetime.now().strftime('%H:%M:%S')
- valor_x += [timestamp]
- valor_y += [value_up]
- valor_z += [value_down]
- # definicao das variaves necessarias para busca dos oids
- # host = "192.168.56.101"
- # community = "public"
- # oid_upload = ".1.3.6.1.2.1.31.1.1.1.6.2"
- # oid_download = ".1.3.6.1.2.1.31.1.1.1.10.2"
- # linha de comando parcial para busca dos valores
- # cmd = "SnmpGet.exe -r:" + host + " -c:" + community
- #ao clicar o botao chamar essa função
- def btn_stop():
- global start
- start = False
- def btn_click():
- global valor_x
- global valor_y
- global valor_z
- valor_x = []
- valor_y = []
- valor_z = []
- global start
- start = True
- t = threading.Thread(target=getValues)
- t.start()
- #configurações da tela
- root= tk.Tk()
- root.title('Titulo do Programa')
- root.geometry('800x600')
- #root.resizable(0,0)
- root.configure(bg='#fff')
- #componentes
- labelAlerta = tk.Label(root, text='', bg='#fff')
- labelAlerta.place(x=320, y=20)
- labelAlerta.config(font= ('none',12))
- labelIp = tk.Label(root, text='Endereço IP do agente', bg='#fff')
- labelIp.place(x=200, y=60)
- txtIp = tk.Entry(root, bg='#fff')
- txtIp.place(x=340, y=60, width='250', height='22')
- labelCommunity = tk.Label(root, text='Community', bg='#fff')
- labelCommunity.place(x=254, y=100)
- txtCommunity = tk.Entry(root, bg='#fff')
- txtCommunity.place(x=340, y=100, width='250', height='22')
- labelOIDin = tk.Label(root, text='OID Entrada', bg='#fff')
- labelOIDin.place(x=255, y=140)
- txtOIDin = tk.Entry(root, bg='#fff')
- txtOIDin.place(x=340, y=140, width='250', height='22')
- labelOIDout = tk.Label(root, text='OID saida', bg='#fff')
- labelOIDout.place(x=270, y=180)
- txtOIDout = tk.Entry(root, bg='#fff')
- txtOIDout.place(x=340, y=180, width='250', height='22')
- btnIniciar = tk.Button(root, text ='Iniciar', width='15', command=btn_click)
- btnIniciar.place(x=290, y=215)
- btnParar = tk.Button(root, text ='Parar', width='15', command=btn_stop)
- btnParar.place(x=420, y=215)
- fig = plt.figure(figsize=(7,3), dpi=120)
- ax = fig.add_subplot(1, 1, 1)
- def animate(i):
- global valor_x
- global valor_y
- global valor_z
- ax.clear()
- ax.plot(valor_x, valor_z, label='Download')
- ax.plot(valor_x, valor_y, label='Upload')
- ax.legend(loc='lower right')
- line2 = FigureCanvasTkAgg(fig, root)
- line2.get_tk_widget().pack(side=tk.BOTTOM)
- ani = animation.FuncAnimation(fig, animate, interval=1000)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement