This is an old revision of the document!
Ondra Mandik
Zadání: Monitoring parametru procesoru pres protokol SNMP v GUI s
Schválení:
Reseni
Semestralni prace je postavena nad Linuxem (distrubuce UBUNTU) s nainstalovanymi balicky
- snmp
- snmpwalk
- snmpget
- gtk..
- python
- python-wxpython
prace je implementovana v jednom davkovem souboru pri interpreter python. Konfirgurace je v prvnich radcich kodu v konstantach FROM, VERSION, apod.
zdrojovy kod aplikace je zde:
<source lang=“python”> #!/usr/bin/python
import os, wx, re, threading, time import wx.lib.plot as plot
ID_ABOUT = 101 ID_EXIT = 110 ID_REFRESH = 112 TIMER_ID = 100
class SnmpGuiClient(wx.Frame): FROM = 'localhost'
COMMUNITY = 'public' VERSION = '2c'
DATA = [ ('.1.3.6.1.4.1.2021.11.9.0','red','Users CPU time'), ('.1.3.6.1.4.1.2021.11.11.0','blue','System CPU time'), ('.1.3.6.1.4.1.2021.11.10.0','green','Idle CPU time') ]
REFRESH_TIME = 400
DATA_QUEUE_LENGTH = 100 timer = None
plotter = None dataQueue = []
def __init__(self,parent,title): self.frame = wx.Frame.__init__(self,parent,wx.ID_ANY, title, size = (400,200))
#creating graph plotPanel = wx.Panel(self) plotPanel.SetBackgroundColour("white") vbox = wx.BoxSizer(wx.VERTICAL) plotPanel.SetSizer(vbox)
self.plotter = plot.PlotCanvas(plotPanel) vbox.Add(self.plotter, 1, wx.EXPAND | wx.ALL, 5) self.plotter.SetSize(size=(300,300)) self.plotter.SetShowScrollbars(True) self.plotter.SetEnableZoom(False) self.plotter.SetEnableGrid(False) self.plotter.SetXSpec(type='none') self.plotter.SetEnableLegend(True) # Creting status bar self.CreateStatusBar() # A StatusBar in the bottom of the window # Setting up the menu. filemenu= wx.Menu() filemenu.Append(ID_ABOUT, "&About"," Information about this program") filemenu.AppendSeparator() filemenu.Append(ID_EXIT,"E&xit"," Terminate the program") actionmenu = wx.Menu() actionmenu.Append(ID_REFRESH, "&Refresh", "Refresh graph") # Creating the menubars. menuBar = wx.MenuBar() menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar menuBar.Append(actionmenu,"&Actions") # Adding the "filemenu" to the MenuBar self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
# Start timer self.timer = wx.Timer(self, TIMER_ID) self.timer.Start(self.REFRESH_TIME)
wx.EVT_MENU(self, ID_ABOUT, self.OnAbout) wx.EVT_MENU(self, ID_EXIT, self.OnExit) wx.EVT_MENU(self, ID_REFRESH, self.OnRefresh) wx.EVT_TIMER(self, TIMER_ID, self.OnTimer) self.initQueue() self.refresh() self.Centre() self.Show(True) def OnAbout(self,e): d= wx.MessageDialog( self, "A sample editor \n in wxPython","About Sample Editor", wx.OK) d.ShowModal() d.Destroy() def OnExit(self,e): self.Close(True) def OnRefresh(self,e): self.refresh()
def OnTimer(self,e): self.refresh() self.timer.Start(self.REFRESH_TIME) def initQueue(self): for i in range(0,len(self.DATA)): self.dataQueue.insert(i,[]); for j in range(0,self.DATA_QUEUE_LENGTH): self.dataQueue[i].insert(j,0); def refresh(self): for i in range(0,len(self.DATA)): self.dataQueue[i].pop(0) self.dataQueue[i].append(self.snmpGetInt(self.DATA[i][0])) self.redrawPlot()
def snmpGetInt(self,oid): intRe = re.compile("^.*INTEGER: ([0-9]+)$") cmd = "snmpget -c %s -v %s -O s %s %s" % (self.COMMUNITY,self.VERSION,self.FROM,oid) pipe = os.popen(cmd) return int(intRe.match(pipe.read()).group(1))
def redrawPlot(self): lines = [] for i in range(0,len(self.DATA)): data = [] for j in range(0,self.DATA_QUEUE_LENGTH): data.append((j,self.dataQueue[i][j])); lines.insert(i,plot.PolyLine(data, colour=self.DATA[i][1], width=4,legend=self.DATA[i][2])) gc = plot.PlotGraphics(lines, ('CPU Usage %s' % self.FROM), "", "CPU Usage") self.plotter.Draw(gc, xAxis=(0,self.DATA_QUEUE_LENGTH), yAxis=(0,100))
#Run main loop if __name__ == '__main__': app = wx.PySimpleApp() frame = SnmpGuiClient(None,"SNMP GUI CLIENT") app.MainLoop()
</source>
Presentace
Presentaci muzete stahnout na grafymandio1.pdf
~~DISCUSSION~~