<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import time <EOL> import datetime <EOL> from salt . exceptions import SaltInvocationError , CommandExecutionError <EOL> import salt . ext . six as six <EOL> try : <EOL> import salt . utils . psutil_compat as psutil <EOL> HAS_PSUTIL = True <EOL> PSUTIL2 = getattr ( psutil , '<STR_LIT>' , ( ) ) >= ( <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> except ImportError : <EOL> HAS_PSUTIL = False <EOL> def __virtual__ ( ) : <EOL> if not HAS_PSUTIL : <EOL> return False , '<STR_LIT>' <EOL> if psutil . version_info >= ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> return True <EOL> return ( False , '<STR_LIT>' . format ( psutil . version_info ) ) <EOL> def _get_proc_cmdline ( proc ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return proc . cmdline ( ) if PSUTIL2 else proc . cmdline <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied ) : <EOL> return '<STR_LIT>' <EOL> def _get_proc_create_time ( proc ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return proc . create_time ( ) if PSUTIL2 else proc . create_time <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied ) : <EOL> return None <EOL> def _get_proc_name ( proc ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return proc . name ( ) if PSUTIL2 else proc . name <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied ) : <EOL> return [ ] <EOL> def _get_proc_status ( proc ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return proc . status ( ) if PSUTIL2 else proc . status <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied ) : <EOL> return None <EOL> def _get_proc_username ( proc ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return proc . username ( ) if PSUTIL2 else proc . username <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied , KeyError ) : <EOL> return None <EOL> def _get_proc_pid ( proc ) : <EOL> '''<STR_LIT>''' <EOL> return proc . pid <EOL> def top ( num_processes = <NUM_LIT:5> , interval = <NUM_LIT:3> ) : <EOL> '''<STR_LIT>''' <EOL> result = [ ] <EOL> start_usage = { } <EOL> for pid in psutil . pids ( ) : <EOL> try : <EOL> process = psutil . Process ( pid ) <EOL> user , system = process . cpu_times ( ) <EOL> except ValueError : <EOL> user , system , _ , _ = process . cpu_times ( ) <EOL> except psutil . NoSuchProcess : <EOL> continue <EOL> start_usage [ process ] = user + system <EOL> time . sleep ( interval ) <EOL> usage = set ( ) <EOL> for process , start in six . iteritems ( start_usage ) : <EOL> try : <EOL> user , system = process . cpu_times ( ) <EOL> except ValueError : <EOL> user , system , _ , _ = process . cpu_times ( ) <EOL> except psutil . NoSuchProcess : <EOL> continue <EOL> now = user + system <EOL> diff = now - start <EOL> usage . add ( ( diff , process ) ) <EOL> for idx , ( diff , process ) in enumerate ( reversed ( sorted ( usage ) ) ) : <EOL> if num_processes and idx >= num_processes : <EOL> break <EOL> if len ( _get_proc_cmdline ( process ) ) == <NUM_LIT:0> : <EOL> cmdline = _get_proc_name ( process ) <EOL> else : <EOL> cmdline = _get_proc_cmdline ( process ) <EOL> info = { '<STR_LIT>' : cmdline , <EOL> '<STR_LIT:user>' : _get_proc_username ( process ) , <EOL> '<STR_LIT:status>' : _get_proc_status ( process ) , <EOL> '<STR_LIT>' : _get_proc_pid ( process ) , <EOL> '<STR_LIT>' : _get_proc_create_time ( process ) , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> for key , value in six . iteritems ( process . cpu_times ( ) . _asdict ( ) ) : <EOL> info [ '<STR_LIT>' ] [ key ] = value <EOL> for key , value in six . iteritems ( process . memory_info ( ) . _asdict ( ) ) : <EOL> info [ '<STR_LIT>' ] [ key ] = value <EOL> result . append ( info ) <EOL> return result <EOL> def get_pid_list ( ) : <EOL> '''<STR_LIT>''' <EOL> return psutil . pids ( ) <EOL> def proc_info ( pid , attrs = None ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> proc = psutil . Process ( pid ) <EOL> return proc . as_dict ( attrs ) <EOL> except ( psutil . NoSuchProcess , psutil . AccessDenied , AttributeError ) as exc : <EOL> raise CommandExecutionError ( exc ) <EOL> def kill_pid ( pid , signal = <NUM_LIT:15> ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> psutil . Process ( pid ) . send_signal ( signal ) <EOL> return True <EOL> except psutil . NoSuchProcess : <EOL> return False <EOL> def pkill ( pattern , user = None , signal = <NUM_LIT:15> , full = False ) : <EOL> '''<STR_LIT>''' <EOL> killed = [ ] <EOL> for proc in psutil . process_iter ( ) : <EOL> name_match = pattern in '<STR_LIT:U+0020>' . join ( _get_proc_cmdline ( proc ) ) if full else pattern in _get_proc_name ( proc ) <EOL> user_match = True if user is None else user == _get_proc_username ( proc ) <EOL> if name_match and user_match : <EOL> try : <EOL> proc . send_signal ( signal ) <EOL> killed . append ( _get_proc_pid ( proc ) ) <EOL> except psutil . NoSuchProcess : <EOL> pass <EOL> if not killed : <EOL> return None <EOL> else : <EOL> return { '<STR_LIT>' : killed } <EOL> def pgrep ( pattern , user = None , full = False ) : <EOL> '''<STR_LIT>''' <EOL> procs = [ ] <EOL> for proc in psutil . process_iter ( ) : <EOL> name_match = pattern in '<STR_LIT:U+0020>' . join ( _get_proc_cmdline ( proc ) ) if full else pattern in _get_proc_name ( proc ) <EOL> user_match = True if user is None else user == _get_proc_username ( proc ) <EOL> if name_match and user_match : <EOL> procs . append ( _get_proc_pid ( proc ) ) <EOL> return procs or None <EOL> def cpu_percent ( interval = <NUM_LIT:0.1> , per_cpu = False ) : <EOL> '''<STR_LIT>''' <EOL> if per_cpu : <EOL> result = list ( psutil . cpu_percent ( interval , True ) ) <EOL> else : <EOL> result = psutil . cpu_percent ( interval ) <EOL> return result <EOL> def cpu_times ( per_cpu = False ) : <EOL> '''<STR_LIT>''' <EOL> if per_cpu : <EOL> result = [ dict ( times . _asdict ( ) ) for times in psutil . cpu_times ( True ) ] <EOL> else : <EOL> result = dict ( psutil . cpu_times ( per_cpu ) . _asdict ( ) ) <EOL> return result <EOL> def virtual_memory ( ) : <EOL> '''<STR_LIT>''' <EOL> if psutil . version_info < ( <NUM_LIT:0> , <NUM_LIT:6> , <NUM_LIT:0> ) : <EOL> msg = '<STR_LIT>' <EOL> raise CommandExecutionError ( msg ) <EOL> return dict ( psutil . virtual_memory ( ) . _asdict ( ) ) <EOL> def swap_memory ( ) : <EOL> '''<STR_LIT>''' <EOL> if psutil . version_info < ( <NUM_LIT:0> , <NUM_LIT:6> , <NUM_LIT:0> ) : <EOL> msg = '<STR_LIT>' <EOL> raise CommandExecutionError ( msg ) <EOL> return dict ( psutil . swap_memory ( ) . _asdict ( ) ) <EOL> def disk_partitions ( all = False ) : <EOL> '''<STR_LIT>''' <EOL> result = [ dict ( partition . _asdict ( ) ) for partition in <EOL> psutil . disk_partitions ( all ) ] <EOL> return result <EOL> def disk_usage ( path ) : <EOL> '''<STR_LIT>''' <EOL> return dict ( psutil . disk_usage ( path ) . _asdict ( ) ) <EOL> def disk_partition_usage ( all = False ) : <EOL> '''<STR_LIT>''' <EOL> result = disk_partitions ( all ) <EOL> for partition in result : <EOL> partition . update ( disk_usage ( partition [ '<STR_LIT>' ] ) ) <EOL> return result <EOL> def total_physical_memory ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return psutil . virtual_memory ( ) . total <EOL> except AttributeError : <EOL> return psutil . TOTAL_PHYMEM <EOL> def num_cpus ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return psutil . cpu_count ( ) <EOL> except AttributeError : <EOL> return psutil . NUM_CPUS <EOL> def boot_time ( time_format = None ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> b_time = int ( psutil . boot_time ( ) ) <EOL> except AttributeError : <EOL> b_time = int ( psutil . boot_time ( ) ) <EOL> if time_format : <EOL> b_time = datetime . datetime . fromtimestamp ( b_time ) <EOL> try : <EOL> return b_time . strftime ( time_format ) <EOL> except TypeError as exc : <EOL> raise SaltInvocationError ( '<STR_LIT>' . format ( exc ) ) <EOL> return b_time <EOL> def network_io_counters ( interface = None ) : <EOL> '''<STR_LIT>''' <EOL> if not interface : <EOL> return dict ( psutil . net_io_counters ( ) . _asdict ( ) ) <EOL> else : <EOL> stats = psutil . net_io_counters ( pernic = True ) <EOL> if interface in stats : <EOL> return dict ( stats [ interface ] . _asdict ( ) ) <EOL> else : <EOL> return False <EOL> def disk_io_counters ( device = None ) : <EOL> '''<STR_LIT>''' <EOL> if not device : <EOL> return dict ( psutil . disk_io_counters ( ) . _asdict ( ) ) <EOL> else : <EOL> stats = psutil . disk_io_counters ( perdisk = True ) <EOL> if device in stats : <EOL> return dict ( stats [ device ] . _asdict ( ) ) <EOL> else : <EOL> return False <EOL> def get_users ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> recs = psutil . users ( ) <EOL> return [ dict ( x . _asdict ( ) ) for x in recs ] <EOL> except AttributeError : <EOL> try : <EOL> import utmp <EOL> result = [ ] <EOL> while True : <EOL> rec = utmp . utmpaccess . getutent ( ) <EOL> if rec is None : <EOL> return result <EOL> elif rec [ <NUM_LIT:0> ] == <NUM_LIT:7> : <EOL> started = rec [ <NUM_LIT:8> ] <EOL> if isinstance ( started , tuple ) : <EOL> started = started [ <NUM_LIT:0> ] <EOL> result . append ( { '<STR_LIT:name>' : rec [ <NUM_LIT:4> ] , '<STR_LIT>' : rec [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : started , '<STR_LIT:host>' : rec [ <NUM_LIT:5> ] } ) <EOL> except ImportError : <EOL> return False </s>
