def get_idle_pids(): piddata = {} pidlist = [] for proc in psutil.process_iter(): try: # Get process name & pid from process object. process = {} process['name'] = proc.name() process['id'] = proc.pid userName = proc.username() process['username'] = userName #/if userName != 'mart': continue process['memory_percent'] = proc.memory_percent(memtype="rss") process['memory_info'] = ((proc.memory_info()).rss / 1e9) process['status'] = proc.status() process['foundtime'] = datetime.now().timestamp() if (process['status'] != 'running' and process['memory_info'] > 2): if config.sleeping_pids.get(str(process['id'])) != None: process['foundtime']=config.sleeping_pids[str(process['id'])]['foundtime'] piddata[str(process['id'])]=process pidlist.append(process['id']) except ( psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass #proctop.sort(key=lambda ar: -ar['memory_info']) config.change('sleeping_pids',piddata) config.store_config() return [piddata,pidlist]
def parse_message(self, msg: dict = {}): if msg['text'].find('/subscribe') > -1: config.change( 'subscribers', list( set(config.subscribers if config.subscribers else {}) | { msg['chat']['id'], })) self.send_message(msg['chat']['id'], 'You have been subscribed') if msg['text'].find('/unsubscribe') > -1: config.change( 'subscribers', list( set(config.subscribers if config.subscribers else {}) - {msg['chat']['id']})) self.send_message(msg['chat']['id'], 'You have been unsubscribed') if msg['text'].find('/info') > -1: message = '' for drive in config.drives: drv = psutil.disk_usage(drive['path']) message += "Filesystem {} \r\nTotal: {} GiB\r\n".format( drive['path'], round(drv.total / (2**30), 1)) message += "Used: {} GiB\r\n".format( round(drv.used / (2**30), 1)) message += "Free: {} GiB\r\n\r\n".format( round(drv.free / (2**30), 1)) if os.path.isfile(drive['report_file']): message += open(drive['report_file'], 'r').read() message += "\r\n\r\n" usertop, userlist = sm.get_top_ram_users() message += "Memory Usage:\r\n" for user in usertop: memory_usage = round(userlist[user]['memory'], 2) message += "{} {}%\r\n".format(user, memory_usage) #geting sleeping bustards piddata, pidlist = sm.get_idle_pids() pidlist.sort(key=lambda ar: -(piddata.get(str(ar)))['memory_info']) message += "\r\nTOP IDLE PIDs:\r\n" os.system('clear') for key in pidlist: process = piddata[str(key)] datefrom = datetime.fromtimestamp(process['foundtime']) seconds = (datetime.now() - datefrom).seconds if (seconds < 60 * 60 * 2): continue runing_time = "{}h {}m".format(int(seconds / 3600), int((seconds / 60) % 60)) message += "user: {}\r\npid: {}\r\npname: {}\r\nRAM: {}G\r\nIDLE: {}\r\n\r\n".format( process['username'], process['id'], process['name'], round(process['memory_info'], 2), runing_time) #print(process['username'], ' ', # process['id'], ':', process['name'], # ':', process['status'], ':', # round(process['memory_info'], 2), # 'GB', datefrom, ':', runing_time) if len(message) > 0: self.send_message(msg['chat']['id'], message)
def get_updates(self): response = self.do_tel_query('getUpdates', {'offset': int(config.lastupdate_id) + 1}) if type(response) == dict: if 'ok' in response: if 'result' in response: if len(response['result']) > 0: for resp in response['result']: config.change('lastupdate_id', resp['update_id']) print(resp) self.parse_message(resp['message']) return response
def parse_message(self, msg: dict = {}): if msg['text'].find('/subscribe') > -1: config.change( 'subscribers', list( set(config.subscribers if config.subscribers else {}) | { msg['chat']['id'], })) self.send_message(msg['chat']['id'], 'You have been subscribed') if msg['text'].find('/unsubscribe') > -1: config.change( 'subscribers', list( set(config.subscribers if config.subscribers else {}) - {msg['chat']['id']})) self.send_message(msg['chat']['id'], 'You have been unsubscribed')