def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): ntmtools.dbg_msg("MTime.update_h - conn_state:{0}".format(conn_state)) if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): # offline -> online self.dialog_showed = False last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used(self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 else: # last_state is online self.this_slot_sec = ntmtools.timedelta2sec(self.now - self.connStartTime) if (conn_state == 0): # online -> offline pass self.last_state = conn_state self.update_main_gui() if self.logUpdate: print('Time: {0} of {1}\n'. format( self.used_sec + self.this_slot_sec, self.total_sec) ) self.check_limit()
def preferences_data_export_h_clicked(self, data = None): ntmtools.dbg_msg("ntmgui.preferences_data_export_h_clicked(self, data = None)") filename = self.entry_data_file.get_text() dbc_d = sqlite3.connect(filename) self.ntmo.create_tables(dbc_d) self.ntmo.copy_data(self.ntmo.db_conn, dbc_d)
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): ntmtools.dbg_msg("MTime.update_h - conn_state:{0}".format(conn_state)) if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): # offline -> online self.dialog_showed = False last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used( self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 else: # last_state is online self.this_slot_sec = ntmtools.timedelta2sec(self.now - self.connStartTime) if (conn_state == 0): # online -> offline pass self.last_state = conn_state self.update_main_gui() if self.logUpdate: print('Time: {0} of {1}\n'.format( self.used_sec + self.this_slot_sec, self.total_sec)) self.check_limit()
def update_status_and_icon(self, conn_state): device_info = self.ntmo.net_man.device_info dev_type_text = self.device_type_text(device_info.deviceType) net_name = dev_type_text if device_info.modem: ntmtools.dbg_msg("@@@ {0}".format(device_info.modem)) if device_info.modem_enabled: ntmtools.dbg_msg("@@@ {0}".format(device_info.modem_enabled)) net_name = device_info.modem_mnc_net_name #self.nsb_net_name.set_markup("<small>{0}</small>".format(net_name)) self.nsb_interface_name.set_markup("<small>{0}</small>".format( self.ntmo.interface)) self.nsb_interface_name.set_tooltip_markup("{0}".format(dev_type_text)) if conn_state == 0: #self.status_bar.push(self.status_bar.get_context_id("ntm"), "{0}: ".format(self.ntmo.interface) + _("Offline") + "; " + net_name) self.statusIcon.set_from_file(self.ntmo.trayicon_file) self.nsb_conn_state_icon.set_from_stock( gtk.STOCK_DISCONNECT, gtk.ICON_SIZE_SMALL_TOOLBAR) self.nsb_conn_state_icon.set_tooltip_text(_("Disconnect")) elif conn_state == 1: #self.status_bar.push(self.status_bar.get_context_id("ntm"), "{0}: ".format(self.ntmo.interface) + _("Online") + "; " + net_name) self.statusIcon.set_from_file(self.ntmo.trayicon_active_file) self.nsb_conn_state_icon.set_from_stock( gtk.STOCK_CONNECT, gtk.ICON_SIZE_SMALL_TOOLBAR) self.nsb_conn_state_icon.set_tooltip_text(_("Connect"))
def update_status_and_icon(self, conn_state): device_info = self.ntmo.net_man.device_info dev_type_text = self.device_type_text(device_info.deviceType) net_name = dev_type_text if device_info.modem: ntmtools.dbg_msg("@@@ {0}".format(device_info.modem)) if device_info.modem_enabled: ntmtools.dbg_msg("@@@ {0}".format(device_info.modem_enabled)) net_name = device_info.modem_mnc_net_name #self.nsb_net_name.set_markup("<small>{0}</small>".format(net_name)) self.nsb_interface_name.set_markup("<small>{0}</small>".format(self.ntmo.interface)) self.nsb_interface_name.set_tooltip_markup("{0}".format(dev_type_text)) if conn_state == 0: #self.status_bar.push(self.status_bar.get_context_id("ntm"), "{0}: ".format(self.ntmo.interface) + _("Offline") + "; " + net_name) self.statusIcon.set_from_file(self.ntmo.trayicon_file) self.nsb_conn_state_icon.set_from_stock(gtk.STOCK_DISCONNECT, gtk.ICON_SIZE_SMALL_TOOLBAR) self.nsb_conn_state_icon.set_tooltip_text(_("Disconnect")) elif conn_state == 1: #self.status_bar.push(self.status_bar.get_context_id("ntm"), "{0}: ".format(self.ntmo.interface) + _("Online") + "; " + net_name) self.statusIcon.set_from_file(self.ntmo.trayicon_active_file) self.nsb_conn_state_icon.set_from_stock(gtk.STOCK_CONNECT, gtk.ICON_SIZE_SMALL_TOOLBAR) self.nsb_conn_state_icon.set_tooltip_text(_("Connect"))
def load_sessions(db_conn, start_period, end_period, slot_length): c = db_conn.cursor() rows = c.execute("select * from session where start>=? AND start<=?", (start_period, end_period) ) slotsUsed = 0 len = 0 time = 0 for r in rows: try: tsStart = datetime.datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S") tsEnd = datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S") diff = tsEnd - tsStart len = diff.days*86400 + diff.seconds except: ntmtools.dbg_msg("Error in the session table. Row: {0} .\n".format(str(r))) len = 0 slotsUsed += int(len / slot_length) time = (len % slot_length) if time > 0: slotsUsed += 1 return (slotsUsed, time)
def nm_h_device_added(self, device_path): ntmtools.dbg_msg("NetMan.nm_h_device_added : {0}".format(device_path)) if (self.state == -1): dev_proxy = self.bus.get_object("org.freedesktop.NetworkManager", device_path) prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties") interface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface") if (interface == self.interface): self.device_path = device_path self.state = prop_iface.Get( "org.freedesktop.NetworkManager.Device", "State") self.bus.add_signal_receiver( self.nm_h_state_changed, dbus_interface="org.freedesktop.NetworkManager.Device", signal_name="StateChanged", path=self.device_path) self.online = (self.state in self.NTM_MAP_NM_DEVICE_STATE_ACTIVATED) if self.online: self.set_online() ntmtools.dbg_msg("END - NetMan.nm_h_device_added")
def load_sessions(db_conn, start_period, end_period, slot_length): c = db_conn.cursor() rows = c.execute("select * from session where start>=? AND start<=?", (start_period, end_period)) slotsUsed = 0 len = 0 time = 0 for r in rows: try: tsStart = datetime.datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S") tsEnd = datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S") diff = tsEnd - tsStart len = diff.days * 86400 + diff.seconds except: ntmtools.dbg_msg( "Error in the session table. Row: {0} .\n".format(str(r))) len = 0 slotsUsed += int(len / slot_length) time = (len % slot_length) if time > 0: slotsUsed += 1 return (slotsUsed, time)
def check_limit(self): ntmtools.dbg_msg("MTime.check_limit") if (not self.active): return if ( (self.used_sec + self.this_slot_sec) >= self.total_sec): if (self.last_state == 1) and (not self.disc_msgDialog): self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Time Limit Reached') + ', {0}. '.format(ntmtools.format_time(self.total_sec)) + _('Disconnection is done!!\nPeriod: {0} to {1}').format(self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() showDialog = True else: msg = _('Time Limit Reached') + ', {0}.\n'.format(ntmtools.format_time(self.total_sec)) + _('Period: {0} to {1}').format(self.first_day.isoformat(), last_day.isoformat()) showDialog = not self.dialog_showed if showDialog: dialog = gtk.Dialog( _("NTM - Message"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK) ) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.dialog_showed = True self.disc_msgDialog = False
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): if conn_state != self.last_conn_state: try: self.update_status_and_icon(conn_state) except: ntmtools.dbg_msg("ntmgui.update_h : Unexpected error: " + str(sys.exc_info()), 100) self.last_conn_state = conn_state
def set_offline(self): ntmtools.dbg_msg("NetMan.set_offline") if self.online: self.online = False self.offline_event() ntmtools.dbg_msg("END - NetMan.set_offline")
def set_online(self): ntmtools.dbg_msg("NetMan.set_online") if not self.online: self.online = True self.online_event() ntmtools.dbg_msg("END - NetMan.set_online")
def preferences_data_export_h_clicked(self, data=None): ntmtools.dbg_msg( "ntmgui.preferences_data_export_h_clicked(self, data = None)") filename = self.entry_data_file.get_text() dbc_d = sqlite3.connect(filename) self.ntmo.create_tables(dbc_d) self.ntmo.copy_data(self.ntmo.db_conn, dbc_d)
def traffic_arrow_h_button_press_event(self, widget, event, data=None): ntmtools.dbg_msg("traffic_arrow_h_button_press_event") if self.gui_traffic_arrow_state == 0: self.gui_traffic_arrow.set(gtk.ARROW_DOWN, gtk.SHADOW_IN) self.gui_traffic_arrow_state = 1 else: self.gui_traffic_arrow.set(gtk.ARROW_RIGHT, gtk.SHADOW_IN) self.gui_traffic_arrow_state = 0
def set_offline(self): ntmtools.dbg_msg("NTM.set_offline") self.online = False self.update_count(True) self.update_event_th(datetime.datetime.now(), self.session_start, self.update_interval, self.d_rb, self.d_tb, 0) ntmtools.dbg_msg("END - NTM.set_offline")
def get_report_from_db(self, first_day, last_day, all_days=False): ntmtools.dbg_msg("NTM.get_report_from_db") ctra = self.db_conn.cursor() if all_days: ctra.execute("select * from dailytraffic") else: last_day_t = last_day + datetime.timedelta(1) ctra.execute("select * from dailytraffic where (date>=?) AND (date<?)", (first_day, last_day_t)) t_count = 0 t_in = 0 t_out = 0 tot_min = tot_max = 0 tra_list = [] for r in ctra: t_in += r[1] t_out += r[2] tot = r[1] + r[2] if (t_count == 0): tot_min = tot tot_max = tot else: if tot > tot_max: tot_max = tot elif tot < tot_min: tot_min = tot t_count += 1 tra_list += [r] cses = self.db_conn.cursor() if all_days: cses.execute("select * from session") else: cses.execute("select * from session where (start>=?) AND (start<?)", (first_day, last_day_t)) s_count = 0 tot_time = 0 s_max = s_min = 0 ses_list = [] for r in cses: tsStart = datetime.datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S") tsEnd = datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S") diff = ntmtools.timedelta2sec(tsEnd - tsStart) tot_time += diff if (s_count == 0): s_min = diff s_max = diff else: if diff > s_max: s_max = diff elif diff < s_min: s_min = diff s_count += 1 ses_list += [r] ntmtools.dbg_msg("END - NTM.get_report_from_db") return (tra_list, ses_list, t_count, t_in, t_out, tot_max, tot_min, s_count, tot_time, s_max, s_min)
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): if conn_state != self.last_conn_state: try: self.update_status_and_icon(conn_state) except: ntmtools.dbg_msg( "ntmgui.update_h : Unexpected error: " + str(sys.exc_info()), 100) self.last_conn_state = conn_state
def nm_h_state_changed(self, new_state, old_state, reason): ntmtools.dbg_msg("NetMan.nm_h_state_changed") if self.mode == 0: if new_state in self.NTM_MAP_NM_DEVICE_STATE_ACTIVATED: self.set_online() else: self.set_offline() ntmtools.dbg_msg("END - NetMan.nm_h_state_changed")
def substitute_data(self, db_conn): ntmtools.dbg_msg("NTM.substitute_data") self.copy_data(db_conn, self.db_conn) self.mtraffic.reload_traffic() self.mtimeslot.reload_sessions() self.mtime.reload_time_used() self.ntmgui.update_report() ntmtools.dbg_msg("END - NTM.substitute_data")
def quit(self): ntmtools.dbg_msg("NTM.quit") self.pinger.stop() self.update_count(True) self.stop = True self.net_man.stop() sys.exit() ntmtools.dbg_msg("END - NTM.quit")
def nm_h_device_removed(self, object_path): ntmtools.dbg_msg("NetMan.nm_h_device_removed : {0}".format(object_path)) if (self.state != -1): if (self.device_path == object_path): self.state = -1 self.bus.remove_signal_receiver(self.nm_h_state_changed, dbus_interface="org.freedesktop.NetworkManager.Device", signal_name="StateChanged", path = self.device_path) self.set_offline() ntmtools.dbg_msg("END - NetMan.nm_h_device_removed")
def get_proc_net_dev(self, interface): ntmtools.dbg_msg("NTM.get_proc_net_dev : {0}".format(interface)) ret = self.get_proc_net_dev_traffic(interface) if (ret == None): interfaceProcNetDev = self.get_proc_net_dev_name(interface) if interfaceProcNetDev != None: ret = self.get_proc_net_dev_traffic(interfaceProcNetDev) ntmtools.dbg_msg("END - NTM.get_proc_net_dev -> {0}".format(ret)) return ret
def update_event_th(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): ntmtools.dbg_msg("NTM.update_event_th") self.update_event(timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state) #def run_update_event(): # self.update_event(timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state) #update_event_thread = threading.Thread(target=run_update_event) ntmtools.dbg_msg("END - NTM.update_event_th")
def update_timer_handler(self): ntmtools.dbg_msg("NetMan.update_timer_handler") if (self.mode == 1): if (self.get_ping_test()): self.set_online() else: self.set_offline() gobject.timeout_add(self.update_timer_interval * 1000, self.update_timer_handler) ntmtools.dbg_msg("END - NetMan.update_timer_handler")
def preferences_data_sel_file_h_clicked(self, data = None): ntmtools.dbg_msg("ntmgui.preferences_data_sel_file_h_clicked(self, data = None)") chooser = gtk.FileChooserDialog( title = None, action = gtk.FILE_CHOOSER_ACTION_OPEN, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK) ) filename = self.entry_data_file.get_text() chooser.set_filename(filename) res = chooser.run() if res == gtk.RESPONSE_OK: filename = chooser.get_filename() self.entry_data_file.set_text(filename) chooser.destroy()
def remove_all_data(self): ntmtools.dbg_msg("NTM.remove_all_data") self.db_conn.execute("delete from dailytraffic") self.db_conn.execute("delete from session") self.db_conn.commit() self.mtraffic.reload_traffic() self.mtimeslot.reload_sessions() self.mtime.reload_time_used() self.ntmgui.update_report() ntmtools.dbg_msg("END - NTM.remove_all_data")
def get_proc_net_dev_name(self, interface): ntmtools.dbg_msg("NTM.get_proc_net_dev_name : {0}".format(interface)) ret_iface = None for line in open('/var/log/messages','r'): if 'pppd' not in line: continue if 'Connect: ' not in line: continue if interface in line: pair = line.split("Connect: ")[1].split(" <--> ") if pair[1][5:]: ret_iface = pair[0] ntmtools.dbg_msg("END - NTM.get_proc_net_dev_name -> {0}".format(ret_iface)) return ret_iface
def set_interface(self, interface): ntmtools.dbg_msg("NetMan.setInterface - interface:{0}".format(interface)) (new_device_path, new_prop_iface) = nettools.get_obj_path(self.bus, interface) ntmtools.dbg_msg("NetMan.setInterface - device_path:{0}".format(new_device_path, new_prop_iface)) if (self.mode == 0): if (new_device_path == ""): self.set_offline() else: self.bus.remove_signal_receiver(self.nm_h_state_changed, dbus_interface="org.freedesktop.NetworkManager.Device", signal_name="StateChanged", path = self.device_path) self.device_path = new_device_path try: self.bus.add_signal_receiver(self.nm_h_state_changed, dbus_interface="org.freedesktop.NetworkManager.Device", signal_name="StateChanged", path = self.device_path) modems = self.get_modems() self.device_info = Device(self.bus, self.device_path, modems) except: ntmtools.dbg_msg("Unexpected error: " + str(sys.exc_info())) if (self.get_state()): self.set_online() else: self.set_offline() else: # ping None self.device_path = new_device_path self.interface = interface ntmtools.dbg_msg("END - NetMan.setInterface")
def update_preferences_gui(self): if self.gtkb == None: ntmtools.dbg_msg( "MTraffic.update_preferences_gui(*): Gui not builded.\n", 100) return self.gui_pref_active.set_active(self.active) self.gui_pref_limit.set_value(int(self.traffic_limit / 1024 / 1024)) self.gui_pref_auto_disconnect.set_active(self.auto_disconnect) self.gui_pref_period_length.set_active(self.period_length) self.gui_pref_days.set_value(self.custom_days) self.gui_pref_first_day.set_label(self.first_day.isoformat()) self.temp_gui_pref_first_day = self.first_day self.gui_pref_period_autoupdate.set_active(self.period_autoupdate)
def get_proc_net_dev_name(self, interface): ntmtools.dbg_msg("NTM.get_proc_net_dev_name : {0}".format(interface)) ret_iface = None for line in open('/var/log/messages', 'r'): if 'pppd' not in line: continue if 'Connect: ' not in line: continue if interface in line: pair = line.split("Connect: ")[1].split(" <--> ") if pair[1][5:]: ret_iface = pair[0] ntmtools.dbg_msg( "END - NTM.get_proc_net_dev_name -> {0}".format(ret_iface)) return ret_iface
def get_proc_net_dev_default(self): ntmtools.dbg_msg("NTM.get_proc_net_dev_default") if self.interfaceProcNetDev == None: self.interfaceProcNetDev = self.interface ret = self.get_proc_net_dev_traffic(self.interfaceProcNetDev) if (ret == None): self.interfaceProcNetDev = self.get_proc_net_dev_name(self.interface) if self.interfaceProcNetDev != None: ret = self.get_proc_net_dev_traffic(self.interfaceProcNetDev) ntmtools.dbg_msg("END - NTM.get_proc_net_dev_default -> {0}".format(ret)) return ret
def bwu_hEnter(self, widget, event, user_param1): ntmtools.dbg_msg("->bwu_hEnter") tlw = gtk.Widget.get_toplevel(self.gui_main_bwu) (tx, ty) = tlw.get_position() #(tx, ty) = tlw.get_origin() (x, y) = gtk.Widget.translate_coordinates(self.gui_main_bwu, tlw, 0, 0) self.nw.move(tx + x, ty + y) #(x, y, m) = tlw.get_pointer() #self.nw.move(x, y-1) #self.nw.show() return False
def preferences_data_sel_file_h_clicked(self, data=None): ntmtools.dbg_msg( "ntmgui.preferences_data_sel_file_h_clicked(self, data = None)") chooser = gtk.FileChooserDialog( title=None, action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) filename = self.entry_data_file.get_text() chooser.set_filename(filename) res = chooser.run() if res == gtk.RESPONSE_OK: filename = chooser.get_filename() self.entry_data_file.set_text(filename) chooser.destroy()
def nm_h_device_removed(self, object_path): ntmtools.dbg_msg( "NetMan.nm_h_device_removed : {0}".format(object_path)) if (self.state != -1): if (self.device_path == object_path): self.state = -1 self.bus.remove_signal_receiver( self.nm_h_state_changed, dbus_interface="org.freedesktop.NetworkManager.Device", signal_name="StateChanged", path=self.device_path) self.set_offline() ntmtools.dbg_msg("END - NetMan.nm_h_device_removed")
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): ntmtools.dbg_msg('mtraffic.update_h') #ntmtools.dbg_msg('update_h(self, {0}, {1}, {2}, {3})\n'.format(timestamp, last_rec_traffic, last_tra_traffic, conn_state)) if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): self.dialog_showed = False last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.load_traffic(self.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] else: self.rec_traffic += last_rec_traffic self.tra_traffic += last_tra_traffic if (conn_state == 1): self.last_rec_traffic = last_rec_traffic self.last_tra_traffic = last_tra_traffic else: self.last_rec_traffic = 0 self.last_tra_traffic = 0 self.last_state = conn_state self.last_update_interval = update_interval self.last_speed = (self.last_rec_traffic + self.last_tra_traffic) / self.last_update_interval self.update_main_gui() if self.logTraffic: speed = (last_rec_traffic + last_tra_traffic) / update_interval / 1024 total = (self.rec_traffic + self.tra_traffic) / 1024 if speed == 0: bar = '|' else: bar = '#' * (1 + int(speed)) print('{0}\t{1}\t{2}\t{3:.3f}\t\t'.format( total / 1024, self.last_rec_traffic / 1024, self.last_tra_traffic / 1024, speed) + bar) self.check_limit()
def set_preferences_from_gui(self): if self.gtkb == None: ntmtools.dbg_msg( "MTraffic.update_preferences_gui(*): Gui not builded.\n", 100) return active = self.gui_pref_active.get_active() traffic_limit = self.gui_pref_limit.get_value() * 1024 * 1024 auto_disconnect = self.gui_pref_auto_disconnect.get_active() period_length = self.gui_pref_period_length.get_active() custom_days = self.gui_pref_days.get_value() period_autoupdate = self.gui_pref_period_autoupdate.get_active() self.set_preferences(active, traffic_limit, auto_disconnect, period_length, custom_days, self.temp_gui_pref_first_day, period_autoupdate)
def update_db_daily_traffic(self, datetime, recbytes, trabytes): ntmtools.dbg_msg("NTM.update_db_daily_traffic") date = datetime.date() c = self.db_conn.cursor() c.execute("select * from dailytraffic where date=?", (date,) ) r = c.fetchone() if r != None: self.db_conn.execute("update dailytraffic set recbytes=?, trabytes=? where date=?", (r[1] + recbytes, r[2] + trabytes, r[0])) else: self.db_conn.execute("insert into dailytraffic values (?, ?, ?)", (date, recbytes, trabytes)) self.db_conn.commit() ntmtools.dbg_msg("END - NTM.update_db_daily_traffic")
def nm_h_device_added(self, device_path): ntmtools.dbg_msg("NetMan.nm_h_device_added : {0}".format(device_path)) if (self.state == -1): dev_proxy = self.bus.get_object("org.freedesktop.NetworkManager", device_path) prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties") interface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface") if (interface == self.interface): self.device_path = device_path self.state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State") self.bus.add_signal_receiver(self.nm_h_state_changed, dbus_interface="org.freedesktop.NetworkManager.Device", signal_name="StateChanged", path = self.device_path) self.online = (self.state in self.NTM_MAP_NM_DEVICE_STATE_ACTIVATED) if self.online: self.set_online() ntmtools.dbg_msg("END - NetMan.nm_h_device_added")
def menu_about_h_activate(self, widget): if self.aboutActive: return self.aboutActive = True about = gtk.AboutDialog() icon = about.render_icon(gtk.STOCK_ABOUT, gtk.ICON_SIZE_DIALOG) about.set_icon(icon) about.set_name("NTM") about.set_version(globaldef.VERSION) about.set_copyright("Copyright 2009-2010 - Luigi Tullio") about.set_license(globaldef.LICENSE) about.set_website("http://netramon.sourceforge.net") about.set_authors( ["Luigi Tullio <*****@*****.**> (http://luigit.altervista.org)"]) about.set_translator_credits( "Luigi Tullio <*****@*****.**> (english,italian)\n" + "Renato Peixe <*****@*****.**> (portuguese)\n" + "A. Ullrich <*****@*****.**> (german)\n" + "Marcin Dopieralski <*****@*****.**> (polish)\n" + "César Sánchez <*****@*****.**> (spanish)\n" + "Ainurko <*****@*****.**> (slovak)\n" + "Dimitris Styliara <*****@*****.**> (greek)\n") about.set_documenters([ "Tester: Luigi Tullio <*****@*****.**>", "Tester: johnnymax <*****@*****.**>", "Tester: Luigi Romagnosi (Hìr0) <*****@*****.**>" ]) about.set_artists([ "Luigi Tullio <*****@*****.**>", "Luigi Romagnosi (Hìr0) <*****@*****.**>", "kofm" ]) try: about.set_logo(gtk.gdk.pixbuf_new_from_file(globaldef.NTM_ICON)) except: ntmtools.dbg_msg("Err: ntmgui.menu_about_h_activate - set_logo", 100) about.set_comments("(" + globaldef.VERSION_NN + ")\n" + _("Monitors your internet traffic.")) about.run() about.destroy() self.aboutActive = False
def get_proc_net_dev_traffic(self, interface): ntmtools.dbg_msg("NTM.get_proc_net_dev_traffic : {0}".format(interface)) retVal = None for line in open('/proc/net/dev','r'): # print("line : {0}".format(line)) if ':' not in line: continue splitline = string.split(line, ':', 1) if string.strip(splitline[0]) != interface: continue x = splitline[1].split() rec_bytes = int(x[0]) tra_bytes = int(x[8]) retVal = (rec_bytes, tra_bytes) break ntmtools.dbg_msg("END - NTM.get_proc_net_dev_traffic -> {0}".format(retVal)) return retVal
def get_ping_test(self): ntmtools.dbg_msg("NetMan.get_ping_test") ret = subprocess.call("ping -q -w2 -c1 " + self.ping_test_url, shell=True, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT) retVal = None if ret == 0: retVal = True # Online else: retVal = False # Offline ntmtools.dbg_msg("END - NetMan.get_ping_test -> {0}".format(retVal)) return retVal
def menu_about_h_activate(self, widget): if self.aboutActive: return self.aboutActive = True about = gtk.AboutDialog() icon = about.render_icon(gtk.STOCK_ABOUT, gtk.ICON_SIZE_DIALOG) about.set_icon(icon) about.set_name("NTM") about.set_version(globaldef.VERSION) about.set_copyright("Copyright 2009-2010 - Luigi Tullio") about.set_license(globaldef.LICENSE) about.set_website("http://netramon.sourceforge.net") about.set_authors(["Luigi Tullio <*****@*****.**> (http://luigit.altervista.org)"]) about.set_translator_credits( "Luigi Tullio <*****@*****.**> (english,italian)\n" + "Renato Peixe <*****@*****.**> (portuguese)\n" + "A. Ullrich <*****@*****.**> (german)\n" + "Marcin Dopieralski <*****@*****.**> (polish)\n" + "César Sánchez <*****@*****.**> (spanish)\n" + "Ainurko <*****@*****.**> (slovak)\n" + "Dimitris Styliara <*****@*****.**> (greek)\n" ) about.set_documenters([ "Tester: Luigi Tullio <*****@*****.**>", "Tester: johnnymax <*****@*****.**>", "Tester: Luigi Romagnosi (Hìr0) <*****@*****.**>" ]) about.set_artists([ "Luigi Tullio <*****@*****.**>", "Luigi Romagnosi (Hìr0) <*****@*****.**>", "kofm" ]) try: about.set_logo(gtk.gdk.pixbuf_new_from_file(globaldef.NTM_ICON)) except: ntmtools.dbg_msg("Err: ntmgui.menu_about_h_activate - set_logo", 100) about.set_comments("(" + globaldef.VERSION_NN + ")\n" + _("Monitors your internet traffic.")) about.run() about.destroy() self.aboutActive = False
def get_proc_net_dev_default(self): ntmtools.dbg_msg("NTM.get_proc_net_dev_default") if self.interfaceProcNetDev == None: self.interfaceProcNetDev = self.interface ret = self.get_proc_net_dev_traffic(self.interfaceProcNetDev) if (ret == None): self.interfaceProcNetDev = self.get_proc_net_dev_name( self.interface) if self.interfaceProcNetDev != None: ret = self.get_proc_net_dev_traffic(self.interfaceProcNetDev) ntmtools.dbg_msg( "END - NTM.get_proc_net_dev_default -> {0}".format(ret)) return ret
def update_db_session(self, commit = True): ntmtools.dbg_msg("NTM.update_db_session") dtStart = self.session_start.replace(microsecond=0).isoformat(' ') dtEnd = self.last_update.replace(microsecond=0).isoformat(' ') c = self.db_conn.cursor() c.execute("select * from session where start=?", (dtStart,) ) r = c.fetchone() if r != None: self.db_conn.execute("update session set end=? where start=?", (dtEnd, dtStart)) else: self.db_conn.execute("insert into session values (?, ?)", (dtStart, dtEnd)) if commit: self.db_conn.commit() ntmtools.dbg_msg("END - NTM.update_db_session")
def check_limit(self): ntmtools.dbg_msg("MTime.check_limit") if (not self.active): return if ((self.used_sec + self.this_slot_sec) >= self.total_sec): if (self.last_state == 1) and (not self.disc_msgDialog): self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Time Limit Reached') + ', {0}. '.format( ntmtools.format_time(self.total_sec) ) + _( 'Disconnection is done!!\nPeriod: {0} to {1}').format( self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() showDialog = True else: msg = _('Time Limit Reached') + ', {0}.\n'.format( ntmtools.format_time( self.total_sec)) + _('Period: {0} to {1}').format( self.first_day.isoformat(), last_day.isoformat()) showDialog = not self.dialog_showed if showDialog: dialog = gtk.Dialog( _("NTM - Message"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK)) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.dialog_showed = True self.disc_msgDialog = False
def get_proc_net_dev_traffic(self, interface): ntmtools.dbg_msg( "NTM.get_proc_net_dev_traffic : {0}".format(interface)) retVal = None for line in open('/proc/net/dev', 'r'): # print("line : {0}".format(line)) if ':' not in line: continue splitline = string.split(line, ':', 1) if string.strip(splitline[0]) != interface: continue x = splitline[1].split() rec_bytes = int(x[0]) tra_bytes = int(x[8]) retVal = (rec_bytes, tra_bytes) break ntmtools.dbg_msg( "END - NTM.get_proc_net_dev_traffic -> {0}".format(retVal)) return retVal
def update_db_daily_traffic(self, datetime, recbytes, trabytes): ntmtools.dbg_msg("NTM.update_db_daily_traffic") date = datetime.date() c = self.db_conn.cursor() c.execute("select * from dailytraffic where date=?", (date, )) r = c.fetchone() if r != None: self.db_conn.execute( "update dailytraffic set recbytes=?, trabytes=? where date=?", (r[1] + recbytes, r[2] + trabytes, r[0])) else: self.db_conn.execute("insert into dailytraffic values (?, ?, ?)", (date, recbytes, trabytes)) self.db_conn.commit() ntmtools.dbg_msg("END - NTM.update_db_daily_traffic")
def load_time_used(db_conn, start_period, end_period): c = db_conn.cursor() rows = c.execute("select * from session where start>=? AND start<=?", (start_period, end_period) ) # start session allocation used_time = 0 for r in rows: try: tsStart = datetime.datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S") tsEnd = datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S") diff = tsEnd - tsStart sec_len = diff.days*86400 + diff.seconds except: ntmtools.dbg_msg("Error in the session table. Row: {0} .\n".format(str(r))) sec_len = 0 used_time += sec_len return used_time
def preferences_data_import_h_clicked(self, data = None): ntmtools.dbg_msg("ntmgui.preferences_data_import_h_clicked(self, data = None)") filename = self.entry_data_file.get_text() if os.path.exists(filename): dbc_s = sqlite3.connect(filename) self.ntmo.substitute_data(dbc_s) else: dialog = gtk.Dialog( "NTM - " + _("Import Data"), self.statusIconMenu.get_toplevel(), gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK) ) icon = self.mainWindow.render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(_("No such file") + ": {0}".format(filename)) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy()