How to use total_running_processes method in autotest

Best Python code snippet using autotest_python

process_facts.py

Source:process_facts.py Github

copy

Full Screen

1#!/usr/bin/python2# Copyright: (c) 2019, Andrew J. Huffman <ahuffman@redhat.com>3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)4ANSIBLE_METADATA = {'metadata_version': '1.1',5 'status': ['preview'],6 'supported_by': 'community'}7DOCUMENTATION = '''8---9module: scan_processes10short_description: Collects currently running processes on a system11version_added: "2.8"12description:13 - "Collects the currently running processes on a system at the time the module is run."14 - "This module presents the currently running proceses as ansible_facts"15output_ps_stdout_lines:16 description:17 - Whether or not to output the collected standard out lines from the 'ps auxww' command18 default: False19 required: False20output_parsed_processes:21 description:22 - Whether or not to output parsed data from the 'ps auxww' command.23 default: True24 required: False25author:26 - Andrew J. Huffman (@ahuffman)27'''28EXAMPLES = '''29# Collect running processes and output parsed data30- name: "Collect current running processes"31 scan_processes:32# Collect only standard out lines from the ps auxww command33- name: "Collect process command output"34 scan_processes:35 output_ps_stdout_lines: True36 output_parsed_processes: False37# Collect both parsed process data and 'ps auxww' command standard out38- name: "Collect all process data"39 scan_processes:40 output_ps_stdout_lines: True41'''42RETURN = '''43running_processes:44 processes:45 - command: /usr/lib/systemd/systemd --switched-root --system --deserialize 3346 cpu_percentage: '0.0'47 memory_percentage: '0.0'48 pid: '1'49 resident_size: '5036'50 start: Jul0851 stat: Ss52 teletype: '?'53 time: '3:32'54 user: root55 ...56 ps_stdout_lines:57 - root 1 0.0 0.0 171628 5056 ? Ss Jul08 3:32 /usr/lib/systemd/systemd --switched-root --system --deserialize 3358 ...59 total_running_processes: 35960'''61from ansible.module_utils.basic import AnsibleModule62import os, re, subprocess63from os.path import isfile, isdir, join64def main():65 module_args = dict(66 output_ps_stdout_lines=dict(67 type='bool',68 default=False,69 required=False70 ),71 output_parsed_processes=dict(72 type='bool',73 default=True,74 required=False75 )76 )77 result = dict(78 changed=False,79 original_message='',80 message=''81 )82 module = AnsibleModule(83 argument_spec=module_args,84 supports_check_mode=True85 )86 params = module.params87 def get_processes():88 re_header = re.compile(r'^USER+.*')89 proc_stats = dict()90 procs = list()91 count = 092 running = subprocess.check_output(["ps auxww"], universal_newlines=True, shell=True)93 for l in running.split('\n'):94 if len(l) > 0 and re_header.search(l) is None:95 procs.append(l.replace('\n', '').replace('\t', ' '))96 count += 197 proc_stats['stdout'] = procs98 proc_stats['total_running_processes'] = count99 return proc_stats100 def parse_process_data(procs):101 re_ps = re.compile(r'^(?P<user>[\w\+\-\_\$\d]+)\s+(?P<pid>[0-9]+)\s+(?P<cpu>[0-9\.]+)\s+(?P<mem>[0-9\.]+)\s+(?P<vsz>[0-9]+)\s+(?P<rss>[0-9]+)\s+(?P<tty>[a-zA-Z0-9\?\/\-]+)\s+(?P<stat>[ADIRSTtWXZ\<NLsl\+]+)\s+(?P<start>[\w\:\d]+\s?[\d]{2})\s+(?P<time>[0-9\:]+)\s+(?P<command>.*)$')102 processes = list()103 for proc in procs:104 process = dict()105 if re_ps.search(proc):106 process['user'] = re_ps.search(proc).group('user')107 process['pid'] = re_ps.search(proc).group('pid')108 process['cpu_percentage'] = re_ps.search(proc).group('cpu')109 process['memory_percentage'] = re_ps.search(proc).group('mem')110 process['virtual_memory_size'] = re_ps.search(proc).group('vsz')111 process['resident_size'] = re_ps.search(proc).group('rss')112 process['teletype'] = re_ps.search(proc).group('tty')113 process['stat'] = re_ps.search(proc).group('stat')114 process['start'] = re_ps.search(proc).group('start')115 process['time'] = re_ps.search(proc).group('time')116 process['command'] = re_ps.search(proc).group('command')117 processes.append(process)118 return processes119 # Do work120 raw_procs = get_processes()121 if params['output_parsed_processes']:122 proc_data = parse_process_data(raw_procs['stdout'])123 # Build output124 processes = dict()125 if params['output_ps_stdout_lines']:126 processes['ps_stdout_lines'] = raw_procs['stdout']127 if params['output_parsed_processes']:128 processes['total_running_processes'] = raw_procs['total_running_processes']129 processes['processes'] = proc_data130 result = {'ansible_facts': {'running_processes': processes}}131 module.exit_json(**result)132if __name__ == '__main__':...

Full Screen

Full Screen

scan_processes.py

Source:scan_processes.py Github

copy

Full Screen

1#!/usr/bin/python2# Copyright: (c) 2019, Andrew J. Huffman <ahuffman@redhat.com>3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)4ANSIBLE_METADATA = {'metadata_version': '1.1',5 'status': ['preview'],6 'supported_by': 'community'}7DOCUMENTATION = '''8---9module: scan_processes10short_description: Collects currently running processes on a system11version_added: "2.8"12description:13 - "Collects the currently running processes on a system at the time the module is run."14 - "This module presents the currently running proceses as ansible_facts"15output_ps_stdout_lines:16 description:17 - Whether or not to output the collected standard out lines from the 'ps auxww' command18 default: False19 required: False20output_parsed_processes:21 description:22 - Whether or not to output parsed data from the 'ps auxww' command.23 default: True24 required: False25author:26 - Andrew J. Huffman (@ahuffman)27'''28EXAMPLES = '''29# Collect running processes and output parsed data30- name: "Collect current running processes"31 scan_processes:32# Collect only standard out lines from the ps auxww command33- name: "Collect process command output"34 scan_processes:35 output_ps_stdout_lines: True36 output_parsed_processes: False37# Collect both parsed process data and 'ps auxww' command standard out38- name: "Collect all process data"39 scan_processes:40 output_ps_stdout_lines: True41'''42RETURN = '''43running_processes:44 processes:45 - command: /usr/lib/systemd/systemd --switched-root --system --deserialize 3346 cpu_percentage: '0.0'47 memory_percentage: '0.0'48 pid: '1'49 resident_size: '5036'50 start: Jul0851 stat: Ss52 teletype: '?'53 time: '3:32'54 user: root55 ...56 ps_stdout_lines:57 - root 1 0.0 0.0 171628 5056 ? Ss Jul08 3:32 /usr/lib/systemd/systemd --switched-root --system --deserialize 3358 ...59 total_running_processes: 35960'''61from ansible.module_utils.basic import AnsibleModule62import os, re, subprocess63from os.path import isfile, isdir, join64def main():65 module_args = dict(66 output_ps_stdout_lines=dict(67 type='bool',68 default=False,69 required=False70 ),71 output_parsed_processes=dict(72 type='bool',73 default=True,74 required=False75 )76 )77 result = dict(78 changed=False,79 original_message='',80 message=''81 )82 module = AnsibleModule(83 argument_spec=module_args,84 supports_check_mode=True85 )86 params = module.params87 def get_processes():88 re_header = re.compile(r'^USER+.*')89 proc_stats = dict()90 procs = list()91 count = 092 running = subprocess.check_output(["ps auxww"], universal_newlines=True, shell=True)93 for l in running.split('\n'):94 if len(l) > 0 and re_header.search(l) is None:95 procs.append(l.replace('\n', '').replace('\t', ' '))96 count += 197 proc_stats['stdout'] = procs98 proc_stats['total_running_processes'] = count99 return proc_stats100 def parse_process_data(procs):101 re_ps = re.compile(r'^(?P<user>[\w\+\-\_\$]+)\s+(?P<pid>[0-9]+)\s+(?P<cpu>[0-9\.]+)\s+(?P<mem>[0-9\.]+)\s+(?P<vsz>[0-9]+)\s+(?P<rss>[0-9]+)\s+(?P<tty>[a-zA-Z0-9\?\/]+)\s+(?P<stat>[DIRSTtWXZ\<NLsl\+]+)\s+(?P<start>[A-Za-z0-9\:]+)\s+(?P<time>[0-9\:]+)\s+(?P<command>.*)$')102 processes = list()103 for proc in procs:104 process = dict()105 if re_ps.search(proc):106 process['user'] = re_ps.search(proc).group('user')107 process['pid'] = re_ps.search(proc).group('pid')108 process['cpu_percentage'] = re_ps.search(proc).group('cpu')109 process['memory_percentage'] = re_ps.search(proc).group('mem')110 process['virtual_memory_size'] = re_ps.search(proc).group('vsz')111 process['resident_size'] = re_ps.search(proc).group('rss')112 process['teletype'] = re_ps.search(proc).group('tty')113 process['stat'] = re_ps.search(proc).group('stat')114 process['start'] = re_ps.search(proc).group('start')115 process['time'] = re_ps.search(proc).group('time')116 process['command'] = re_ps.search(proc).group('command')117 processes.append(process)118 return processes119 # Do work120 raw_procs = get_processes()121 if params['output_parsed_processes']:122 proc_data = parse_process_data(raw_procs['stdout'])123 # Build output124 processes = dict()125 if params['output_ps_stdout_lines']:126 processes['ps_stdout_lines'] = raw_procs['stdout']127 if params['output_parsed_processes']:128 processes['total_running_processes'] = raw_procs['total_running_processes']129 processes['processes'] = proc_data130 result = {'ansible_facts': {'running_processes': processes}}131 module.exit_json(**result)132if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful