Created
November 6, 2024 04:37
-
-
Save adiralashiva8/1fdf845973ebfeae58ba956fd31146c2 to your computer and use it in GitHub Desktop.
ServiceNow get deployed server info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pysnow | |
from concurrent.futures import ThreadPoolExecutor, as_completed | |
import time | |
start = time.time() | |
print(f"Start time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start))}") | |
source_instance = 'service-now.com' | |
user_name = "abc" | |
user_password = "123" | |
fields = ['sys_id'] | |
rest_max_records = 1000 | |
client = pysnow.Client(host=source_instance, user=user_name, password=user_password) | |
client.parameters.exclude_reference_link = True | |
client.parameters.display_value = True | |
table_instance = client.resource(api_path='/table/cmdb_ci_server') | |
new_query = "install_status=13" | |
page_size = rest_max_records | |
offset = 0 | |
total_ticket_numbers = [] | |
# Fetch records in batches | |
counter = 0 | |
while True: | |
response = table_instance.get(query=new_query, fields=fields, limit=page_size, offset=offset) | |
records = response.all() | |
total_ticket_numbers.extend(records) | |
break | |
if len(records) < page_size: | |
break | |
else: | |
offset += page_size | |
# print(len(total_ticket_numbers)) | |
def process_ticket(ticket): | |
try: | |
table_instance = client.resource(api_path=f"""/table/cmdb_ci_server/{ticket['sys_id']}""") | |
ticket_response = table_instance.get() | |
record_info = ticket_response.all() | |
# print(record_info) | |
if not record_info: | |
return | |
record = record_info[0] | |
info = ( | |
f"{ticket['sys_id']} > " | |
f"{record['name']} > " | |
f"{record['u_businesses']} > " | |
f"{record['asset']} > " | |
f"{record['u_env_classification']} > " | |
f"{record['install_status']} > " | |
f"{record['location']} > " | |
f"{record['sys_created_by']} > " | |
f"{record['sys_created_on']} > " | |
f"{record['u_model_type']} > " | |
f"{record['model_id']} > " | |
f"{record['u_operating_system']} > " | |
f"{record['asset_tag']} > " | |
f"{record['serial_number']} > " | |
f"{record['ip_address']} > " | |
f"{record['u_business_application']} > " | |
f"{record['u_owned_by_group']} > " | |
f"{record['u_engineering_group']} > " | |
f"{record['sys_updated_by']} > " | |
f"{record['sys_updated_on']} " | |
) | |
print(info, flush=True) | |
except Exception as e: | |
print(f"Error processing ticket {ticket['sys_id']}: {e}") | |
# Use ThreadPoolExecutor for multithreading | |
with ThreadPoolExecutor(max_workers=10) as executor: | |
futures = [executor.submit(process_ticket, ticket) for ticket in total_ticket_numbers] | |
for future in as_completed(futures): | |
future.result() # This will raise any exception caught within the thread | |
done = time.time() | |
print(f"End time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(done))}") | |
elapsed = done - start | |
print(f"Elapsed time: {elapsed:.2f} seconds") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment