def _do_kill(process_id): os.system("kill %d" % process_id) time.sleep(0.5) if process_id in gtop.proclist(): time.sleep(5.0) if process_id in gtop.proclist(): os.system("kill -9 %d" % process_id)
def _get_net_stats(): ifs = {} nets = gtop.netlist() for net in nets: netload = gtop.netload(net) ifs[net] = [netload.bytes_in, netload.bytes_out] nets.insert(0, "Net") return ifs, nets
def _get_time_list(cpu): """ Returns a 4 element list containing the amount of time the CPU has spent performing the different types of work 0 user 1 nice 2 system 3 idle Values are in USER_HZ or Jiffies """ if cpu.number == -1: cpu_times = gtop.cpu() else: cpu_times = gtop.cpu().cpus[cpu.number] return [cpu_times.user, cpu_times.nice, cpu_times.sys, cpu_times.idle]
def action_performed(self, binding): if self.page and self.page.is_visible(): if binding.action == g15driver.PREVIOUS_SELECTION and self.use_vnstat is True: if self.loadpage == 'vnstat_daily': self.gconf_client.set_string( self.gconf_key + "/vnstat_view", "vnstat_monthly") else: self.gconf_client.set_string( self.gconf_key + "/vnstat_view", "vnstat_daily") return True elif binding.action == g15driver.NEXT_SELECTION: if self.networkdevice is not None: # get all network devices self.net_data = gtop.netlist() # set network device id +1, to get next device idx = self.net_data.index(self.networkdevice) + 1 # if next device id is not present, take first device if idx >= len(self.net_data): idx = 0 self.gconf_client.set_string( self.gconf_key + "/networkdevice", self.net_data[idx]) return True
def show_preferences(parent, driver, gconf_client, gconf_key): widget_tree = gtk.Builder() widget_tree.add_from_file( os.path.join(os.path.dirname(__file__), "trafficstats.ui")) dialog = widget_tree.get_object("TrafficStats") # Resets the value of the use_vnstat flag if vnstat is not installed vnstat_installed = g15os.is_program_in_path('vnstat') if not vnstat_installed: gconf_client.set_bool("%s/use_vnstat" % gconf_key, False) # Displays a warning message to the user if vnstat is not installed warning = widget_tree.get_object("NoVnstatMessage") warning.set_visible(not vnstat_installed) # Disables the vnstat checkbox if vnstat is not installed use_vnstat = widget_tree.get_object('UseVnstat') use_vnstat.set_sensitive(vnstat_installed) g15uigconf.configure_checkbox_from_gconf(gconf_client, gconf_key + "/use_vnstat", "UseVnstat", vnstat_installed, widget_tree) ndevice = widget_tree.get_object("NetDevice") for netdev in gtop.netlist(): ndevice.append([netdev]) g15uigconf.configure_combo_from_gconf(gconf_client, gconf_key + "/networkdevice", "NetworkDevice", "lo", widget_tree) g15uigconf.configure_adjustment_from_gconf(gconf_client, gconf_key + "/refresh_interval", "RefreshingScale", 10.0, widget_tree) dialog.set_transient_for(parent) dialog.run() dialog.hide()
def _get_mem_info(): return gtop.mem()
def activate(self): self._net_icon = g15icontools.get_icon_path( ["network-transmit-receive", "gnome-fs-network", "network-server"], self.screen.height) self._cpu_icon = g15icontools.get_icon_path(CPU_ICONS, self.screen.height) self._mem_icon = g15icontools.get_icon_path( ["media-memory", "media-flash"], self.screen.height) self._thumb_icon = g15cairo.load_surface_from_file(self._cpu_icon) self.variant = 0 self.graphs = {} self.last_time_list = None self.last_times_list = [] self.last_time = 0 # CPU self.selected_cpu = None self.cpu_no = 0 self.cpu_data = [] selected_cpu_name = self.gconf_client.get_string(self.gconf_key + "/cpu") cpus = gtop.cpu().cpus for i in range(-1, len(cpus)): cpu = CPU(i) self.cpu_data.append(cpu) if cpu.name == selected_cpu_name: self.selected_cpu = cpu if self.selected_cpu is None: self.selected_cpu = self.cpu_data[0] # Net self.selected_net = None _, self.net_list = self._get_net_stats() net_name = self.gconf_client.get_string(self.gconf_key + "/net") self.net_data = [] for idx, n in enumerate(self.net_list): net = Net(idx, n) self.net_data.append(net) if net.name == net_name: self.selected_net = net if self.selected_net is None and len(self.net_data) > 0: self.selected_net = self.net_data[0] # Memory self.max_total_mem = 0 self.total = 1.0 self.cached = 0 self.free = 0 self.used = 0 self.cached_history = [0] * GRAPH_SIZE self.used_history = [0] * GRAPH_SIZE g15plugin.G15RefreshingPlugin.activate(self) self._set_panel() self.watch(["show_cpu_on_panel", "theme"], self._config_changed) self.screen.key_handler.action_listeners.append(self) # Start refreshing self.do_refresh()
def _do_reload_menu(self): if not self.active: return this_items = {} if self._mode == "applications": if self.bamf_matcher is not None: for window in self.bamf_matcher.RunningApplications(): try: item = self._get_item_for_bamf_application(window) this_items[item.id] = item except Exception as e: logger.debug("Could not get info from BAMF", exc_info=e) pass else: from gi.repository import Wnck as wnck screen = wnck.Screen.get_default() for window in screen.get_windows(): pid = window.get_pid() if pid > 0: item = self._get_menu_item(pid) item.process_name = window.get_name() this_items[item.id] = item pixbuf = window.get_icon() if pixbuf: item.icon = g15cairo.pixbuf_to_surface(pixbuf) else: for process_id in gtop.proclist(): process_id = "%d" % process_id try: pid = int(process_id) proc_state = gtop.proc_state(pid) proc_args = gtop.proc_args(pid) if self._mode == "all" or (self._mode != "all" and proc_state.uid == os.getuid()): item = self._get_menu_item(pid) item.icon = None item.process_name = self._get_process_name( proc_args, proc_state.cmd) this_items[item.id] = item except Exception as e: logger.debug("Process may have disappeared", exc_info=e) # In case the process disappears pass # Remove any missing items for item in self.menu.get_children(): if item.id not in this_items: self.menu.remove_child(item) # Make sure selected still exists if self.menu.selected is not None and self.menu.get_child_by_id( self.menu.selected.id) is None: if len(self.menu.get_child_count()) > 0: self.menu.selected = self.menu.get_children()[0] else: self.menu.selected = None self.page.mark_dirty() self.screen.redraw(self.page)
def get_theme_properties(self): properties = {} def convert_bytes(bytes): bytes = float(bytes) if bytes >= 1099511627776: terabytes = bytes / 1099511627776 size = '%.2fT' % terabytes elif bytes >= 1073741824: gigabytes = bytes / 1073741824 size = '%.2fG' % gigabytes elif bytes >= 1048576: megabytes = bytes / 1048576 size = '%.2fM' % megabytes elif bytes >= 1024: kilobytes = bytes / 1024 size = '%.2fK' % kilobytes else: size = '%.2fb' % bytes return size # Split vnstat data into array def get_traffic_data(dataType, dataValue, vn): line = '' for item in vn.split("\n"): if "%s;%d;" % (dataType, dataValue) in item: line = item.strip().split(';') break return line # convert MiB and KiB into KB def cb(mib, kib): return (int(mib) * 1000000) + (int(kib) * 1000) """ Get the details to display and place them as properties which are passed to the theme """ if self.use_vnstat is False: bootup = datetime.datetime.fromtimestamp( int(gtop.uptime().boot_time)).strftime('%d.%m.%y %H:%M') sd = gtop.netload(self.networkdevice) properties["sdn"] = "DL: " + convert_bytes(sd.bytes_in) properties["sup"] = "UL: " + convert_bytes(sd.bytes_out) properties["des1"] = "Traffic since: " + bootup properties["title"] = self.networkdevice + " Traffic" else: vnstat, vn = g15os.get_command_output('vnstat -i ' + self.networkdevice + ' --dumpdb') if vnstat != 0: properties["message"] = "vnstat is not installed!" else: chErr = str(vn.find("Error")) if chErr != "-1": properties[ "message"] = "No stats for device " + self.networkdevice else: properties["title"] = self.networkdevice + " Traffic (U/D)" def get_data(kind, period): # get vnstat data as array, array content: 2 = unixtime, 4 = up MiB, 6 = up KiB, 3 = dn MiB, 5 = dn KiB line = get_traffic_data(kind, period, vn) if line[7] == '1': up = convert_bytes(cb(line[4], line[6])) dn = convert_bytes(cb(line[3], line[5])) des = int(line[2]) return [up, dn, des] else: return None if self.loadpage == 'vnstat_daily': k = "d" fmt = '%A' elif self.loadpage == 'vnstat_monthly': k = "m" fmt = '%B' for p in range(0, 3): data = get_data(k, p) if data is not None: properties["d"] = "/" properties["dup" + str(p + 1)] = data[0] properties["ddn" + str(p + 1)] = data[1] properties[ "des" + str(p + 1)] = datetime.datetime.fromtimestamp( data[2]).strftime(fmt) return properties