def update_progess(self, i): #It is easier to ask for forgiveness, than permission! #~ try: #~ self.main_win.progress.pulse() #~ self.main_win.progress.set_text("idle_" + str(i)) #~ #print 'success' #~ except AttributeError: #~ #print "Method does not exist" #~ pass #~ if self.main_win.active: self.main_win.liststore[0][2] = str(round(random.random(), 2)) #. Open local database and do any needed one-time maintenance in constructor (singleton pattern). #~ if self.db == None: #~ self.db = Database() gui_tab = self.main_win.notebook.get_current_page( ) #-1 if the window is closed #. PARSE THE NEEDED THINGS FIRST pnd = netpatrol_parser.parse_procnetdev() # {'ppp0': {'rx':0, 'tx':0}} logging.debug("/proc/net/dev=========") for iface in pnd: #if iface!='lo': print iface, pnd[iface][0], pnd[iface][8] pass logging.debug("") #. PARSE /proc/<pid>/net/dev ppnd = netpatrol_parser.parse_procnetdev_pid( ) # {'ppp0': {2010: {'rx':0, 'tx':0}}} #print ppnd for iface in ppnd: #print "/proc/" + str(pid) + "/net/dev=========" for pid in ppnd[iface]: rx = int(ppnd[iface][pid]['rx']) tx = int(ppnd[iface][pid]['tx']) #if (iface!='lo' and (rx>0 or tx>0)): #print iface, pid, rx, tx #logging.info(str.format("{3}, {0} {1} {2}", iface, rx, tx, pid)) #print ppnd[pid], pid #~ pass #. PARSE /proc/<pid>/net/tcp #DO THIS ONLY IF GUI IS ACTIVE, AND WE NEED TO SHOW SOMETHING ppntcp = netpatrol_parser.parse_procnettcp_pid() #. START DOING SOME ACTUAL WORK if self.active_sessions == None: # START A NEW SESSION self.active_sessions = {} self.active_processes = {} self.active_session_id = int(time.time( )) #Generate a random new session_id which should be pretty long. self.db = Database(self.active_session_id) logging.info("New db created.") added = set(pnd.keys()).difference( self.active_sessions.keys()) #newly added iface in pnd removed = set(self.active_sessions.keys()).difference( pnd.keys()) #just removed iface from pnd #no change inters = set(self.active_sessions.keys()).intersection(pnd.keys()) if len(added) > 0: logging.info('ADDED') logging.info(added) if len(removed) > 0: logging.info('REMOVED') logging.info(removed) #~ if len(inters)>0: #~ logging.info('INTERSECTION') #~ logging.info(len(inters)) #. Check our self.active_sessions and compare with pnd. #. if new iface found in pnd, add it to active_sessions and give it a start_time. for iface in added: self.active_sessions[iface] = {} self.active_sessions[iface]['name'] = iface self.active_sessions[iface]['pnd'] = pnd[ iface] #REMEMBER: pnd attr only pertains to this iface. self.active_sessions[iface]['ppnd'] = ppnd[ iface] #REMEMBER: ppnd attr only pertains to this iface. self.active_sessions[iface]['start_time'] = time.time( ) #TODO: This is only for ref, actually set by db class self.active_sessions[iface]['end_time'] = 0 self.db.start_session(self.active_sessions[iface]) #. else if iface exists in active_sessions but not in pnd, it means session is ended, so update end_time and remove from db and then dict. for iface in removed: #active_sessions[iface]['data'] = pnd[iface] self.active_sessions[iface]['end_time'] = time.time() self.db.end_session(self.active_sessions.pop(iface)) self.active_sessions[iface]['pnd'] = pnd[iface] self.active_sessions[iface]['ppnd'] = ppnd[iface] #. For each existing iface in active_sessions, update its rx and tx values to db. last_updated = self.db.last_updated if last_updated == None or ( time.time() - last_updated) > 10: #update after interval of a few secs for iface in inters: #update db if needed self.db.update_session(self.active_sessions[iface]) # lets do it for processes too for pid in ppnd[iface]: self.db.update_session_p(self.active_sessions[iface], pid) #print self.active_sessions[iface]['ppnd'] logging.info("updated session") self.db.conn.commit() #. if main_win is active, update the gui too. if gui_tab == 0: #basic stats #print self.db.get_hist('1M') self.db.get_hist_p('1M') elif gui_tab == 1: #active processes pass return
def update_progess(self, i): #It is easier to ask for forgiveness, than permission! #~ try: #~ self.main_win.progress.pulse() #~ self.main_win.progress.set_text("idle_" + str(i)) #~ #print 'success' #~ except AttributeError: #~ #print "Method does not exist" #~ pass #~ if self.main_win.active: self.main_win.liststore[0][2] = str(round(random.random(), 2)) #. Open local database and do any needed one-time maintenance in constructor (singleton pattern). #~ if self.db == None: #~ self.db = Database() gui_tab = self.main_win.notebook.get_current_page() #-1 if the window is closed #. PARSE THE NEEDED THINGS FIRST pnd = netpatrol_parser.parse_procnetdev() # {'ppp0': {'rx':0, 'tx':0}} logging.debug("/proc/net/dev=========") for iface in pnd: #if iface!='lo': print iface, pnd[iface][0], pnd[iface][8] pass logging.debug( "") #. PARSE /proc/<pid>/net/dev ppnd = netpatrol_parser.parse_procnetdev_pid() # {'ppp0': {2010: {'rx':0, 'tx':0}}} #print ppnd for iface in ppnd: #print "/proc/" + str(pid) + "/net/dev=========" for pid in ppnd[iface]: rx = int(ppnd[iface][pid]['rx']) tx = int(ppnd[iface][pid]['tx']) #if (iface!='lo' and (rx>0 or tx>0)): #print iface, pid, rx, tx #logging.info(str.format("{3}, {0} {1} {2}", iface, rx, tx, pid)) #print ppnd[pid], pid #~ pass #. PARSE /proc/<pid>/net/tcp #DO THIS ONLY IF GUI IS ACTIVE, AND WE NEED TO SHOW SOMETHING ppntcp = netpatrol_parser.parse_procnettcp_pid() #. START DOING SOME ACTUAL WORK if self.active_sessions == None: # START A NEW SESSION self.active_sessions = {} self.active_processes = {} self.active_session_id = int(time.time()) #Generate a random new session_id which should be pretty long. self.db = Database(self.active_session_id) logging.info("New db created.") added = set(pnd.keys()).difference(self.active_sessions.keys()) #newly added iface in pnd removed = set(self.active_sessions.keys()).difference(pnd.keys()) #just removed iface from pnd #no change inters = set(self.active_sessions.keys()).intersection(pnd.keys()) if len(added)>0: logging.info('ADDED') logging.info(added) if len(removed)>0: logging.info('REMOVED') logging.info(removed) #~ if len(inters)>0: #~ logging.info('INTERSECTION') #~ logging.info(len(inters)) #. Check our self.active_sessions and compare with pnd. #. if new iface found in pnd, add it to active_sessions and give it a start_time. for iface in added: self.active_sessions[iface] = {} self.active_sessions[iface]['name'] = iface self.active_sessions[iface]['pnd'] = pnd[iface] #REMEMBER: pnd attr only pertains to this iface. self.active_sessions[iface]['ppnd'] = ppnd[iface] #REMEMBER: ppnd attr only pertains to this iface. self.active_sessions[iface]['start_time'] = time.time() #TODO: This is only for ref, actually set by db class self.active_sessions[iface]['end_time'] = 0 self.db.start_session(self.active_sessions[iface]) #. else if iface exists in active_sessions but not in pnd, it means session is ended, so update end_time and remove from db and then dict. for iface in removed: #active_sessions[iface]['data'] = pnd[iface] self.active_sessions[iface]['end_time'] = time.time() self.db.end_session( self.active_sessions.pop(iface)) self.active_sessions[iface]['pnd'] = pnd[iface] self.active_sessions[iface]['ppnd'] = ppnd[iface] #. For each existing iface in active_sessions, update its rx and tx values to db. last_updated = self.db.last_updated if last_updated==None or (time.time() - last_updated)>10: #update after interval of a few secs for iface in inters: #update db if needed self.db.update_session(self.active_sessions[iface]) # lets do it for processes too for pid in ppnd[iface]: self.db.update_session_p(self.active_sessions[iface], pid) #print self.active_sessions[iface]['ppnd'] logging.info("updated session") self.db.conn.commit() #. if main_win is active, update the gui too. if gui_tab==0: #basic stats #print self.db.get_hist('1M') self.db.get_hist_p('1M') elif gui_tab==1: #active processes pass return
class MyApp(Gtk.Application): def __init__(self, app_name): Gtk.Application.__init__(self) self.db = None self.active_session_id = 0 self.active_sessions = None self.active_processes = None self.name = app_name self.main_win = MyMainWin(self) self.main_win.set_position(Gtk.WindowPosition.CENTER) #self.main_win.connect("delete-event", redraw) #self.main_win.hide_on_delete #~ self.conf_win = MyConfigWin(self) #~ self.conf_win.set_position(Gtk.WindowPosition.CENTER) #self.conf_win.connect("delete-event", redraw) #self.main_win.hide_on_delete #self.about_win = MyAboutWin(self) #self.about_win.set_position(Gtk.WindowPosition.CENTER) self.indicator = MyIndicator(self) #handler=Handler() #builder.connect_signals(handler) #w_main.resize(600,300) #wmain.show() def show_about_dialog(self, arg1): global about_win if not 'about_win' in globals(): about_win = None if about_win != None: return #old dialog is still running builder = Gtk.Builder() builder.add_from_file('netpatrol.glade') about_win = builder.get_object('win_about') about_win.set_version(__version__) about_win.app = self response = about_win.run() #if response == Gtk.RESPONSE_DELETE_EVENT or response == Gtk.RESPONSE_CANCEL: if response == Gtk.ResponseType.OK or response == Gtk.ResponseType.CANCEL or response == Gtk.ResponseType.DELETE_EVENT: about_win.close() about_win = None logging.info('about dialog destroyed') else: logging.info(response) #self.wTree.get_widget("aboutdialog1").hide() def show_basic_stats(): pass ## Added for multi-threading def update_progess(self, i): #It is easier to ask for forgiveness, than permission! #~ try: #~ self.main_win.progress.pulse() #~ self.main_win.progress.set_text("idle_" + str(i)) #~ #print 'success' #~ except AttributeError: #~ #print "Method does not exist" #~ pass #~ if self.main_win.active: self.main_win.liststore[0][2] = str(round(random.random(), 2)) #. Open local database and do any needed one-time maintenance in constructor (singleton pattern). #~ if self.db == None: #~ self.db = Database() gui_tab = self.main_win.notebook.get_current_page( ) #-1 if the window is closed #. PARSE THE NEEDED THINGS FIRST pnd = netpatrol_parser.parse_procnetdev() # {'ppp0': {'rx':0, 'tx':0}} logging.debug("/proc/net/dev=========") for iface in pnd: #if iface!='lo': print iface, pnd[iface][0], pnd[iface][8] pass logging.debug("") #. PARSE /proc/<pid>/net/dev ppnd = netpatrol_parser.parse_procnetdev_pid( ) # {'ppp0': {2010: {'rx':0, 'tx':0}}} #print ppnd for iface in ppnd: #print "/proc/" + str(pid) + "/net/dev=========" for pid in ppnd[iface]: rx = int(ppnd[iface][pid]['rx']) tx = int(ppnd[iface][pid]['tx']) #if (iface!='lo' and (rx>0 or tx>0)): #print iface, pid, rx, tx #logging.info(str.format("{3}, {0} {1} {2}", iface, rx, tx, pid)) #print ppnd[pid], pid #~ pass #. PARSE /proc/<pid>/net/tcp #DO THIS ONLY IF GUI IS ACTIVE, AND WE NEED TO SHOW SOMETHING ppntcp = netpatrol_parser.parse_procnettcp_pid() #. START DOING SOME ACTUAL WORK if self.active_sessions == None: # START A NEW SESSION self.active_sessions = {} self.active_processes = {} self.active_session_id = int(time.time( )) #Generate a random new session_id which should be pretty long. self.db = Database(self.active_session_id) logging.info("New db created.") added = set(pnd.keys()).difference( self.active_sessions.keys()) #newly added iface in pnd removed = set(self.active_sessions.keys()).difference( pnd.keys()) #just removed iface from pnd #no change inters = set(self.active_sessions.keys()).intersection(pnd.keys()) if len(added) > 0: logging.info('ADDED') logging.info(added) if len(removed) > 0: logging.info('REMOVED') logging.info(removed) #~ if len(inters)>0: #~ logging.info('INTERSECTION') #~ logging.info(len(inters)) #. Check our self.active_sessions and compare with pnd. #. if new iface found in pnd, add it to active_sessions and give it a start_time. for iface in added: self.active_sessions[iface] = {} self.active_sessions[iface]['name'] = iface self.active_sessions[iface]['pnd'] = pnd[ iface] #REMEMBER: pnd attr only pertains to this iface. self.active_sessions[iface]['ppnd'] = ppnd[ iface] #REMEMBER: ppnd attr only pertains to this iface. self.active_sessions[iface]['start_time'] = time.time( ) #TODO: This is only for ref, actually set by db class self.active_sessions[iface]['end_time'] = 0 self.db.start_session(self.active_sessions[iface]) #. else if iface exists in active_sessions but not in pnd, it means session is ended, so update end_time and remove from db and then dict. for iface in removed: #active_sessions[iface]['data'] = pnd[iface] self.active_sessions[iface]['end_time'] = time.time() self.db.end_session(self.active_sessions.pop(iface)) self.active_sessions[iface]['pnd'] = pnd[iface] self.active_sessions[iface]['ppnd'] = ppnd[iface] #. For each existing iface in active_sessions, update its rx and tx values to db. last_updated = self.db.last_updated if last_updated == None or ( time.time() - last_updated) > 10: #update after interval of a few secs for iface in inters: #update db if needed self.db.update_session(self.active_sessions[iface]) # lets do it for processes too for pid in ppnd[iface]: self.db.update_session_p(self.active_sessions[iface], pid) #print self.active_sessions[iface]['ppnd'] logging.info("updated session") self.db.conn.commit() #. if main_win is active, update the gui too. if gui_tab == 0: #basic stats #print self.db.get_hist('1M') self.db.get_hist_p('1M') elif gui_tab == 1: #active processes pass return def daemon_thread(self): while True: #for i in range(1): #TODO: #. Spare thread. Make use of this thread in case it is needed. GLib.idle_add(self.update_progess, 0) time.sleep(0.2) def close_all_sessions(self): for iface in self.active_sessions.keys(): self.db.end_session(self.active_sessions[iface]) def run(self): thread = threading.Thread(target=self.daemon_thread) thread.daemon = True thread.start() ## GLADE experiment #~ builder=Gtk.Builder() #~ builder.add_from_file('nsgui.glade') #~ w_main=builder.get_object('win_main') #frmConfigure=builder.get_object('frmConfigure') #handler=Handler() #builder.connect_signals(handler) #~ w_main.resize(600,300) #~ w_main.show() #~ Gtk.main() #~ return self.main_win.cb_show(None, None) Gtk.main() #self.db.close() #TODO: Ensure that our thread is closed when it reaches here. self.close_all_sessions() logging.info("database closed.") logging.info("Have a Good Day!")
class MyApp(Gtk.Application): def __init__(self, app_name): Gtk.Application.__init__(self) self.db = None self.active_session_id = 0 self.active_sessions = None self.active_processes = None self.name = app_name self.main_win = MyMainWin(self) self.main_win.set_position(Gtk.WindowPosition.CENTER) #self.main_win.connect("delete-event", redraw) #self.main_win.hide_on_delete #~ self.conf_win = MyConfigWin(self) #~ self.conf_win.set_position(Gtk.WindowPosition.CENTER) #self.conf_win.connect("delete-event", redraw) #self.main_win.hide_on_delete #self.about_win = MyAboutWin(self) #self.about_win.set_position(Gtk.WindowPosition.CENTER) self.indicator = MyIndicator(self) #handler=Handler() #builder.connect_signals(handler) #w_main.resize(600,300) #wmain.show() def show_about_dialog(self, arg1): global about_win if not 'about_win' in globals(): about_win = None if about_win != None: return #old dialog is still running builder = Gtk.Builder() builder.add_from_file('netpatrol.glade') about_win = builder.get_object('win_about') about_win.set_version(__version__) about_win.app = self response = about_win.run() #if response == Gtk.RESPONSE_DELETE_EVENT or response == Gtk.RESPONSE_CANCEL: if response == Gtk.ResponseType.OK or response == Gtk.ResponseType.CANCEL or response == Gtk.ResponseType.DELETE_EVENT: about_win.close() about_win = None logging.info('about dialog destroyed') else: logging.info(response) #self.wTree.get_widget("aboutdialog1").hide() def show_basic_stats(): pass ## Added for multi-threading def update_progess(self, i): #It is easier to ask for forgiveness, than permission! #~ try: #~ self.main_win.progress.pulse() #~ self.main_win.progress.set_text("idle_" + str(i)) #~ #print 'success' #~ except AttributeError: #~ #print "Method does not exist" #~ pass #~ if self.main_win.active: self.main_win.liststore[0][2] = str(round(random.random(), 2)) #. Open local database and do any needed one-time maintenance in constructor (singleton pattern). #~ if self.db == None: #~ self.db = Database() gui_tab = self.main_win.notebook.get_current_page() #-1 if the window is closed #. PARSE THE NEEDED THINGS FIRST pnd = netpatrol_parser.parse_procnetdev() # {'ppp0': {'rx':0, 'tx':0}} logging.debug("/proc/net/dev=========") for iface in pnd: #if iface!='lo': print iface, pnd[iface][0], pnd[iface][8] pass logging.debug( "") #. PARSE /proc/<pid>/net/dev ppnd = netpatrol_parser.parse_procnetdev_pid() # {'ppp0': {2010: {'rx':0, 'tx':0}}} #print ppnd for iface in ppnd: #print "/proc/" + str(pid) + "/net/dev=========" for pid in ppnd[iface]: rx = int(ppnd[iface][pid]['rx']) tx = int(ppnd[iface][pid]['tx']) #if (iface!='lo' and (rx>0 or tx>0)): #print iface, pid, rx, tx #logging.info(str.format("{3}, {0} {1} {2}", iface, rx, tx, pid)) #print ppnd[pid], pid #~ pass #. PARSE /proc/<pid>/net/tcp #DO THIS ONLY IF GUI IS ACTIVE, AND WE NEED TO SHOW SOMETHING ppntcp = netpatrol_parser.parse_procnettcp_pid() #. START DOING SOME ACTUAL WORK if self.active_sessions == None: # START A NEW SESSION self.active_sessions = {} self.active_processes = {} self.active_session_id = int(time.time()) #Generate a random new session_id which should be pretty long. self.db = Database(self.active_session_id) logging.info("New db created.") added = set(pnd.keys()).difference(self.active_sessions.keys()) #newly added iface in pnd removed = set(self.active_sessions.keys()).difference(pnd.keys()) #just removed iface from pnd #no change inters = set(self.active_sessions.keys()).intersection(pnd.keys()) if len(added)>0: logging.info('ADDED') logging.info(added) if len(removed)>0: logging.info('REMOVED') logging.info(removed) #~ if len(inters)>0: #~ logging.info('INTERSECTION') #~ logging.info(len(inters)) #. Check our self.active_sessions and compare with pnd. #. if new iface found in pnd, add it to active_sessions and give it a start_time. for iface in added: self.active_sessions[iface] = {} self.active_sessions[iface]['name'] = iface self.active_sessions[iface]['pnd'] = pnd[iface] #REMEMBER: pnd attr only pertains to this iface. self.active_sessions[iface]['ppnd'] = ppnd[iface] #REMEMBER: ppnd attr only pertains to this iface. self.active_sessions[iface]['start_time'] = time.time() #TODO: This is only for ref, actually set by db class self.active_sessions[iface]['end_time'] = 0 self.db.start_session(self.active_sessions[iface]) #. else if iface exists in active_sessions but not in pnd, it means session is ended, so update end_time and remove from db and then dict. for iface in removed: #active_sessions[iface]['data'] = pnd[iface] self.active_sessions[iface]['end_time'] = time.time() self.db.end_session( self.active_sessions.pop(iface)) self.active_sessions[iface]['pnd'] = pnd[iface] self.active_sessions[iface]['ppnd'] = ppnd[iface] #. For each existing iface in active_sessions, update its rx and tx values to db. last_updated = self.db.last_updated if last_updated==None or (time.time() - last_updated)>10: #update after interval of a few secs for iface in inters: #update db if needed self.db.update_session(self.active_sessions[iface]) # lets do it for processes too for pid in ppnd[iface]: self.db.update_session_p(self.active_sessions[iface], pid) #print self.active_sessions[iface]['ppnd'] logging.info("updated session") self.db.conn.commit() #. if main_win is active, update the gui too. if gui_tab==0: #basic stats #print self.db.get_hist('1M') self.db.get_hist_p('1M') elif gui_tab==1: #active processes pass return def daemon_thread(self): while True: #for i in range(1): #TODO: #. Spare thread. Make use of this thread in case it is needed. GLib.idle_add(self.update_progess, 0) time.sleep(0.2) def close_all_sessions(self): for iface in self.active_sessions.keys(): self.db.end_session(self.active_sessions[iface]) def run(self): thread = threading.Thread(target=self.daemon_thread) thread.daemon = True thread.start() ## GLADE experiment #~ builder=Gtk.Builder() #~ builder.add_from_file('nsgui.glade') #~ w_main=builder.get_object('win_main') #frmConfigure=builder.get_object('frmConfigure') #handler=Handler() #builder.connect_signals(handler) #~ w_main.resize(600,300) #~ w_main.show() #~ Gtk.main() #~ return self.main_win.cb_show(None, None) Gtk.main() #self.db.close() #TODO: Ensure that our thread is closed when it reaches here. self.close_all_sessions() logging.info("database closed.") logging.info("Have a Good Day!")