def findMountInterfaces(self): ipaddrs = [] for line in open('/proc/mounts').readlines(): mountpoint = line.split()[1] if mountpoint.startswith('/fff/'): opts = line.split()[3].split(',') for opt in opts: if opt.startswith('clientaddr='): ipaddrs.append(opt.split('=')[1]) ipaddrs = list(set(ipaddrs)) ifs = [] #update list and reset counters only if interface is missing from the previous list if len(ipaddrs)>len(self.ifs): self.found_data_interfaces=True ifcdict = getnifs.get_network_interfaces() for ifc in ifcdict: name = ifc.name addresses = ifc.addresses if 2 in addresses and len(addresses[2]): if addresses[2][0] in ipaddrs: ifs.append(name) if self.log_ifconfig<2: self.logger.info('monitoring '+name) ifs = list(set(ifs)) self.ifs = ifs self.ifs_in=0 self.ifs_out=0 self.ifs_last = 0 self.getRatesMBs(silent=self.log_ifconfig<2) #initialize self.threadEventESBox.wait(0.1) self.log_ifconfig+=1
def running_with_ibm_network(self): for network in get_network_interfaces(): addresses = network.addresses.get(AF_INET) if not addresses: continue for addr in addresses: if addr.startswith('9.'): return True continue return False
def doIP(): print 'IP' network = '' nifs = getnifs.get_network_interfaces() for ni in nifs: if ni.name == 'lo': continue else: nif = ni.name[:1]+ni.name[-1:] network += '~'+nif+'#'+str(ni.addresses.get(socket.AF_INET)) print network ser.write(network)
def pineintfun(val): global pineint, pineapple, b1 pineint = val.widget.get() # Set pineapple to the ip address of the interface. interfaces = getnifs.get_network_interfaces() for i in interfaces: if i.name == pineint: pineapple = "172.16.42.42" try: if i.addresses[2] is not None: pineapple = i.addresses[2] except: pass b1.delete(0, END) b1.insert(0, pineapple)
def pineintfun(val): global pineint, pineapple, b1 pineint = val.widget.get() # Set pineapple to the ip address of the interface. interfaces = getnifs.get_network_interfaces() for i in interfaces: if i.name == pineint: pineapple="172.16.42.42" try: if i.addresses[2] is not None: pineapple = i.addresses[2] except: pass b1.delete(0, END) b1.insert(0, pineapple)
def main(): if os.geteuid() != 0: exit( "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting." ) global b1, b2, b3, c, resetIptables, root logo = PhotoImage(file="pineapple.png") desc = """ Wifi Pineapple Set up utility """ w1 = Label(root, image=logo).pack(side="left") w2 = Label(root, justify=LEFT, padx=10, text=desc).pack() w3 = Label(root, justify=LEFT, padx=10, text="""Choose the Pineapple Interface:""").pack() interfaces = getnifs.get_network_interfaces() interfacelist = [i.name for i in interfaces] int1select = ttk.Combobox(root, values=interfacelist) int1select.bind("<<ComboboxSelected>>", pineintfun) int1select.pack() w3 = Label(root, justify=LEFT, padx=10, text="""Choose the internet Interface:""").pack() int2select = ttk.Combobox(root, values=interfacelist) int2select.bind("<<ComboboxSelected>>", interintfun) int2select.pack() # Reset IPTables CheckBox resetIptables = IntVar() c = Checkbutton(root, text="Reset Firewall?", variable=resetIptables, onvalue=1, offvalue=0) c.pack() t1 = Label(root, justify=LEFT, padx=10, text="""IP of Pineapple Interface:""").pack() # IP Address boxes for the IP Address. b1 = Entry(root, width=16, textvariable=pineapple) b1.pack() b1.insert(0, pineapple) t3 = Label(root, justify=LEFT, padx=10, text="""NetMask:""").pack() # Netmask b3 = Entry(root, width=16, textvariable=mask) b3.pack() b3.insert(0, mask) t2 = Label(root, justify=LEFT, padx=10, text="""Default Gateway:""").pack() # Default Gateway b2 = Entry(root, width=16, textvariable=gw) b2.pack() b2.insert(0, gw) # Setup Networking button1 = Button(root, text='Set Up Networking', width=25, command=setupNetworking) button1.pack(side="left") # Exit control button2 = Button(root, text='Exit', width=25, command=root.destroy) button2.pack(side="left") root.mainloop()
import socket import getnifs nifs = getnifs.get_network_interfaces() for ni in nifs: if ni.name == "lo": continue else: print ni.name print ni.addresses.get(socket.AF_INET)
def main(): if os.geteuid() != 0: exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.") global b1,b2,b3,c,resetIptables,root logo = PhotoImage(file="pineapple.png") desc = """ Wifi Pineapple Set up utility """ w1 = Label(root,image=logo).pack(side="left") w2 = Label(root, justify=LEFT, padx=10, text=desc).pack() w3 = Label(root, justify=LEFT, padx=10, text="""Choose the Pineapple Interface:""").pack() interfaces = getnifs.get_network_interfaces() interfacelist = [i.name for i in interfaces] int1select = ttk.Combobox(root, values=interfacelist) int1select.bind("<<ComboboxSelected>>", pineintfun) int1select.pack() w3 = Label(root, justify=LEFT, padx=10, text="""Choose the internet Interface:""").pack() int2select = ttk.Combobox(root, values=interfacelist) int2select.bind("<<ComboboxSelected>>", interintfun) int2select.pack() # Reset IPTables CheckBox resetIptables = IntVar() c = Checkbutton(root, text="Reset Firewall?", variable=resetIptables, onvalue=1, offvalue=0) c.pack() t1 = Label(root, justify=LEFT, padx=10, text="""IP of Pineapple Interface:""").pack() # IP Address boxes for the IP Address. b1 = Entry(root, width=16, textvariable=pineapple) b1.pack() b1.insert(0, pineapple) t3 = Label(root, justify=LEFT, padx=10, text="""NetMask:""").pack() # Netmask b3 = Entry(root, width=16, textvariable=mask) b3.pack() b3.insert(0, mask) t2 = Label(root, justify=LEFT, padx=10, text="""Default Gateway:""").pack() # Default Gateway b2 = Entry(root, width=16, textvariable=gw) b2.pack() b2.insert(0, gw) # Setup Networking button1 = Button(root,text='Set Up Networking', width=25, command=setupNetworking) button1.pack(side="left") # Exit control button2 = Button(root,text='Exit', width=25, command=root.destroy) button2.pack(side="left") root.mainloop()
class ProtMonitorSummary(ProtMonitor): """ Provide some summary of the basic steps of the Scipion-Box: - Import movies - Align movies (global and/or local) - CTF estimation - Movie gain estimation. """ _label = 'monitor summary' _lastUpdateVersion = VERSION_1_1 nifs = getnifs.get_network_interfaces() nifsNameList = [nif.getName() for nif in nifs] def __init__(self, **kwargs): ProtMonitor.__init__(self, **kwargs) self.reportDir = '' self.reportPath = '' def _defineParams(self, form): ProtMonitor._defineParams(self, form) form.addSection('MovieGain Monitor') form.addParam('stddevValue', params.FloatParam, default=0.04, label="Raise Alarm if residual gain standard " "deviation >", help="Raise alarm if residual gain standard deviation " "is greater than given value") form.addParam('ratio1Value', params.FloatParam, default=1.15, label="Raise Alarm if the ratio between the 97.5 " "and 2.5 percentiles >", help="Raise alarm if the ratio between the 97.5 " "and 2.5 percentiles is greater than given value") form.addParam('ratio2Value', params.FloatParam, default=4.5, label="Raise Alarm if the ratio between the maximum " "gain value and the 97.5 percentile >", help="Raise alarm if the ratio between the maximum " "gain value and the 97.5 percentile is greater " "than given value") form.addSection('CTF Monitor') form.addParam('maxDefocus', params.FloatParam, default=40000, label="Raise Alarm if maximum defocus (A) >", help="Raise alarm if defocus is greater than given " "value") form.addParam('minDefocus', params.FloatParam, default=1000, label="Raise Alarm if minimum defocus (A) <", help="Raise alarm if defocus is smaller than given " "value") form.addParam('astigmatism', params.FloatParam, default=1000, label="Raise Alarm if astigmatism >", help="Raise alarm if astigmatism (defocusU-defocusV)is greater than given " "value") form.addParam('monitorTime', params.FloatParam, default=30000, label="Total Logging time (min)", help="Log during this interval") form.addSection('System Monitor') form.addParam('cpuAlert', params.FloatParam, default=101, label="Raise Alarm if CPU > XX%", help="Raise alarm if memory allocated is greater " "than given percentage") form.addParam('memAlert', params.FloatParam, default=101, label="Raise Alarm if Memory > XX%", help="Raise alarm if cpu allocated is greater " "than given percentage") form.addParam('swapAlert', params.FloatParam, default=101, label="Raise Alarm if Swap > XX%", help="Raise alarm if swap allocated is greater " "than given percentage") group = form.addGroup('GPU') group.addParam('doGpu', params.BooleanParam, default=False, label="Check GPU", help="Set to true if you want to monitor the GPU") group.addParam('gpusToUse', params.StringParam, default='0', label='Which GPUs to use:', condition='doGpu', help='Provide a list of GPUs ' '(e.g. "0 1 2 3"). Default is to monitor GPU 0 only') group = form.addGroup('NETWORK') group.addParam('doNetwork', params.BooleanParam, default=False, label="Check Network", help="Set to true if you want to monitor the Network") group.addParam('netInterfaces', params.EnumParam, choices=self.nifsNameList, default=1, # usually 0 is the loopback label="Interface", condition='doNetwork', help="Name of the network interface to be checked") group = form.addGroup('Disk') group.addParam('doDiskIO', params.BooleanParam, default=False, label="Check Disk IO", help="Set to true if you want to monitor the Disk " "Acces") form.addSection('Mail settings') ProtMonitor._sendMailParams(self, form) form.addSection('HTML Report') form.addParam('publishCmd', params.StringParam, default='', label="Publish command", help="Specify a command to publish the template. " "You can use the special token %(REPORT_FOLDER)s " "that will be replaced with the report folder. " "For example: \n" "rsync -avL %(REPORT_FOLDER)s " "scipion@webserver:public_html/") # --------------------------- INSERT steps functions --------------------- def _insertAllSteps(self): self._insertFunctionStep('monitorStep') # --------------------------- STEPS functions ---------------------------- def monitorStep(self): # create monitors movieGainMonitor = self.createMovieGainMonitor() ctfMonitor = self.createCtfMonitor() sysMonitor = self.createSystemMonitor() reportHtml = self.createHtmlReport(ctfMonitor, sysMonitor, movieGainMonitor) monitor = Monitor(workingDir=self.workingDir.get(), samplingInterval=self.samplingInterval.get(), monitorTime=self.monitorTime.get()) def initAll(): if ctfMonitor is not None: ctfMonitor.initLoop() if movieGainMonitor is not None: movieGainMonitor.initLoop() sysMonitor.initLoop() def stepAll(): finished = False try: if ctfMonitor is not None: # Call ctf monitor step ctfMonitor.step() if movieGainMonitor is not None: # Call movie gain step movieGainMonitor.step() # sysmonitor watches all input protocols so # when sysmonitor done all protocols done sysMonitorFinished = sysMonitor.step() htmlFinished = reportHtml.generate(finished) if sysMonitorFinished and htmlFinished: finished = True reportHtml.generate(finished) except Exception as ex: print("An error happened: %s" % ex) return finished monitor.initLoop = initAll monitor.step = stepAll monitor.loop() def createReportDir(self): self.reportDir = os.path.abspath(self._getExtraPath(self.getProject().getShortName())) self.reportPath = os.path.join(self.reportDir, 'index.html') # create report dir pwutils.makePath(self.reportDir) def _getAlignProtocol(self): for protPointer in self.inputProtocols: prot = protPointer.get() if isinstance(prot, ProtAlignMovies): return prot return None def _getCtfProtocol(self): for protPointer in self.inputProtocols: prot = protPointer.get() if isinstance(prot, ProtCTFMicrographs): return prot return None def _getMovieGainProtocol(self): from pyworkflow.em.packages.xmipp3 import XmippProtMovieGain for protPointer in self.inputProtocols: prot = protPointer.get() if prot.getClassName() == XmippProtMovieGain.__name__: return prot return None def createMovieGainMonitor(self): movieGainProt = self._getMovieGainProtocol() if movieGainProt is None: return None movieGainProt.setProject(self.getProject()) movieGainMonitor = MonitorMovieGain( movieGainProt, workingDir=self.workingDir.get(), samplingInterval=self.samplingInterval.get(), monitorTime=self.monitorTime.get(), email=self.createEmailNotifier(), stdout=True, stddevValue=self.stddevValue.get(), ratio1Value=self.ratio1Value.get(), ratio2Value=self.ratio2Value.get()) return movieGainMonitor def createCtfMonitor(self): ctfProt = self._getCtfProtocol() if ctfProt is None: return None ctfProt.setProject(self.getProject()) ctfMonitor = MonitorCTF(ctfProt, workingDir=self.workingDir.get(), samplingInterval=self.samplingInterval.get(), monitorTime=self.monitorTime.get(), email=self.createEmailNotifier(), stdout=True, minDefocus=self.minDefocus.get(), maxDefocus=self.maxDefocus.get(), astigmatism=self.astigmatism.get()) return ctfMonitor def createSystemMonitor(self): protocols = self.getInputProtocols() sysMon = MonitorSystem(protocols, workingDir=self.workingDir.get(), samplingInterval=self.samplingInterval.get(), monitorTime=self.monitorTime.get(), email=self.createEmailNotifier(), stdout=True, cpuAlert=self.cpuAlert.get(), memAlert=self.memAlert.get(), swapAlert=self.swapAlert.get(), doGpu=self.doGpu.get(), gpusToUse=self.gpusToUse.get(), doNetwork=self.doNetwork.get(), doDiskIO=self.doDiskIO.get(), nif=self.nifsNameList[ self.netInterfaces.get()]) return sysMon def getReportPath(self): return self.reportPath def createHtmlReport(self, ctfMonitor=None, sysMonitor=None, movieGainMonitor=None): ctfMonitor = ctfMonitor or self.createCtfMonitor() sysMonitor = sysMonitor or self.createSystemMonitor() movieGainMonitor = movieGainMonitor or self.createMovieGainMonitor() self.createReportDir() htmlReport = ReportHtml(self, ctfMonitor, sysMonitor, movieGainMonitor, self.publishCmd.get(), refreshSecs=self.samplingInterval.get()) htmlReport.setUp() return htmlReport
class ProtMonitorSystem(ProtMonitor): """ check CPU, mem and IO usage. """ _label = 'system_monitor' _lastUpdateVersion = VERSION_1_1 # get list with network interfaces nifs = getnifs.get_network_interfaces() nifsNameList = [nif.getName() for nif in nifs] def __init__(self, **kwargs): ProtMonitor.__init__(self, **kwargs) self.dataBase = 'log.sqlite' self.tableName = 'log' # --------------------------- DEFINE param functions --------------------- def _defineParams(self, form): ProtMonitor._defineParams(self, form) form.addParam('cpuAlert', params.FloatParam, default=101, label="Raise Alarm if CPU > XX%", help="Raise alarm if memory allocated is greater " "than given percentage") form.addParam('memAlert', params.FloatParam, default=101, label="Raise Alarm if Memory > XX%", help="Raise alarm if cpu allocated is greater " "than given percentage") form.addParam('swapAlert', params.FloatParam, default=101, label="Raise Alarm if Swap > XX%", help="Raise alarm if swap allocated is greater " "than given percentage") form.addParam('monitorTime', params.FloatParam, default=300, label="Total Logging time (min)", help="Log during this interval") ProtMonitor._sendMailParams(self, form) group = form.addGroup('GPU') group.addParam('doGpu', params.BooleanParam, default=False, label="Check GPU", help="Set to true if you want to monitor the GPU") group.addParam('gpusToUse', params.StringParam, default='0', label='Which GPUs to use:', condition='doGpu', help='Providing a list of which GPUs ' '(0,1,2,3, etc). Default is monitor GPU 0 only') group = form.addGroup('GPU') group.addParam('doGpu', params.BooleanParam, default=False, label="Check GPU", help="Set to true if you want to monitor the GPU") group.addParam('gpusToUse', params.StringParam, default='0', label='Which GPUs to use:', condition='doGpu', help='Providing a list of which GPUs ' '(0,1,2,3, etc). Default is monitor GPU 0 only') group = form.addGroup('NETWORK') group.addParam('doNetwork', params.BooleanParam, default=False, label="Check Network", help="Set to true if you want to monitor the Network") group.addParam( 'netInterfaces', params.EnumParam, choices=self.nifsNameList, default=1, # usually 0 is the loopback label="Interface", condition='doNetwork', help="Name of the network interface to be checked") group = form.addGroup('Disk') group.addParam('doDiskIO', params.BooleanParam, default=False, label="Check Disk IO", help="Set to true if you want to monitor the Disk " "Access") # --------------------------- STEPS functions ---------------------------- def monitorStep(self): self.createMonitor().loop() def createMonitor(self): protocols = [] for protPointer in self.inputProtocols: prot = protPointer.get() prot.setProject(self.getProject()) protocols.append(prot) sysMon = MonitorSystem(protocols, workingDir=self.workingDir.get(), samplingInterval=self.samplingInterval.get(), monitorTime=self.monitorTime.get(), email=self.createEmailNotifier(), stdout=True, cpuAlert=self.cpuAlert.get(), memAlert=self.memAlert.get(), swapAlert=self.swapAlert.get(), doGpu=self.doGpu.get(), doNetwork=self.doNetwork.get(), doDiskIO=self.doDiskIO.get(), nif=self.nifsNameList[self.netInterfaces.get()], gpusToUse=self.gpusToUse.get()) return sysMon # --------------------------- INFO functions ----------------------------- def _validate(self): # TODO if less than 20 sec complain return [] # no errors def _summary(self): summary = [] summary.append("GPU running Processes:") initGPU() try: gpusToUse = [int(n) for n in (self.gpusToUse.get()).split()] for i in gpusToUse: handle = nvmlDeviceGetHandleByIndex(i) cps = nvmlDeviceGetComputeRunningProcesses(handle) for ps in cps: # p_tags['pid'] = ps.pid msg = " %d) " % i + psutil.Process(ps.pid).name() msg += " (mem =%.2f MB)" % (float(ps.usedGpuMemory) / 1048576.) summary.append(msg) except NVMLError as err: summary.append(str(err)) return summary def _methods(self): return []
#! /usr/bin/python import socket import getnifs nifs = getnifs.get_network_interfaces() for ni in nifs: print(ni.name)
def interactive_web_port(self): """Return the port to use for interactive web.""" return self._parser.getint("airpnp", "interactive_web_port") def interface_ip(self): """Return the IP address of the interface to use for listening services and outbound connections.""" import socket return self._ni.addresses[socket.AF_INET] def interface_name(self): """Return the name of the interface to use for listening services and outbound connections.""" return self._ni.name def interface_index(self): """Return the index of the interface to use for listening services and outbound connections.""" return self._ni.index try: config except NameError: import getnifs import upnp outip = upnp.get_outip(upnp.UpnpBase.SSDP_ADDR) # IP address config = Config(getnifs.get_network_interfaces(), outip)