def _run(): """Initializes the state for the script based on command line input.""" arg_parser = argparse.ArgumentParser() args = _setup_args(arg_parser) if args.logfile: c42api.set_log_file(args.logfile) if args.output or args.logfile: c42api.set_log_level(logging.DEBUG) else: c42api.set_log_level(logging.ERROR) server = argutil.server_from_args(args) is_json = args.format == 'json' params = { 'active': 'true', 'incBackupUsage': args.backup, 'incHistory': args.backup } with c42api.common.smart_open(args.output, overwrite=True) as out: if is_json: c42api.write_json( out, c42api.fetch_computers(server, params, insert_schema_version=True)) else: c42api.write_csv(out, c42api.fetch_computers( server, params, insert_schema_version=True), args.header, shallow=True)
def test_fetch_all_devicess(osname1, osname2): """Test to make sure the computers fetch behaves as expected""" body1 = {'data': {'computers': [{'computerId': 1, 'osName': osname1}]}} body2 = {'data': {'computers': [{'computerId': 2, 'osName': osname2}]}} httpretty.register_uri(httpretty.GET, 'http://test.com:7777/api/Computer', responses=[ httpretty.Response(body=json.dumps(body1), status=200), httpretty.Response(body=json.dumps(body2), status=200), httpretty.Response(body='{}', status=200), ]) computer_id = 1 # pylint: disable=protected-access params = {'active': 'true', 'incBackupUsage': False, 'incHistory': False} for computer in c42api.fetch_computers(basic_server(), params, insert_schema_version=True): assert 'computerId' in computer assert 'osName' in computer assert 'schema_version' in computer assert computer['schema_version'] == c42api.computers.SCHEMA_VERSION assert computer['computerId'] == computer_id if computer_id == 1: assert computer['osName'] == osname1 if computer_id == 2: assert computer['osName'] == osname2 computer_id += 1 assert computer_id == 3
def _run(): """ The script's body. Creates/Updates Splunk computer lookup table. """ app_home = common.app_home() lookups_dir = os.path.join(app_home, "lookups") old_computer_lookup_table = os.path.join(lookups_dir, "computer_lookup.csv") tmp_computer_lookup_table = os.path.join(lookups_dir, "computer_lookup_tmp.csv") if not os.path.exists(lookups_dir): os.makedirs(lookups_dir) server, _ = common.setup() # write computer lookup table params = {'active': 'true', 'incBackupUsage': False, 'incHistory': False} computer_results = c42api.fetch_computers(server, params, insert_schema_version=True) splunk_lookup_table.write_lookup_table(old_computer_lookup_table, tmp_computer_lookup_table, computer_results, COMPUTER_UID_KEY, COMPUTER_KEYS_TO_IGNORE, TIME_KEY)
def test_fetch_no_devices(): """Test behavior of computers fetch with no results""" httpretty.register_uri(httpretty.GET, 'http://test.com:7777/api/Computer', responses=[ httpretty.Response(body='{}', status=200), ]) # pylint: disable=protected-access for computer in c42api.fetch_computers(basic_server()): assert computer is None assert False
def _run(): """Initializes the state for the script based on command line input.""" arg_parser = argparse.ArgumentParser() args = _setup_args(arg_parser) if args.logfile: c42api.set_log_file(args.logfile) if args.output or args.logfile: c42api.set_log_level(logging.DEBUG) else: c42api.set_log_level(logging.ERROR) server = argutil.server_from_args(args) is_json = args.format == 'json' params = {'active': 'true', 'incBackupUsage': args.backup, 'incHistory': args.backup} with c42api.common.smart_open(args.output, overwrite=True) as out: if is_json: c42api.write_json(out, c42api.fetch_computers(server, params, insert_schema_version=True)) else: c42api.write_csv(out, c42api.fetch_computers(server, params, insert_schema_version=True), args.header, shallow=True)
def _run(): """ The script's body. Prints out backup usage data to stdout, which will be captured by Splunk. """ server, _ = common.setup() params = {'active': 'true', 'incBackupUsage': True, 'incHistory': True} results = c42api.fetch_computers(server, params, insert_schema_version=True) timestamp = datetime.datetime.now().isoformat() for result in results: guid = result['guid'] schema_version = result['schema_version'] backup_usage_array = result['backupUsage'] for backup_usage in backup_usage_array: backup_usage['guid'] = guid backup_usage['timestamp'] = timestamp backup_usage['schema_version'] = schema_version c42api.write_json_splunk(sys.stdout, backup_usage_array)