Saturday, May 11, 2024

CommValut API Integration commonly used APIs along with their HTTP methods and endpoints DevOps SRE Automations

  1. Authentication

    • POST /login: Authenticate and obtain an AuthToken for subsequent API requests.
  2. Jobs

    • GET /Job: Retrieve a list of jobs and their statuses.
    • GET /Job/{jobId}: Retrieve details of a specific job.
    • POST /Job: Create a new job.
    • PUT /Job/{jobId}: Modify an existing job.
    • DELETE /Job/{jobId}: Delete a job.
  3. Backups

    • GET /Backup: Retrieve a list of backups and their statuses.
    • GET /Backup/{backupId}: Retrieve details of a specific backup.
    • POST /Backup: Create a new backup.
    • PUT /Backup/{backupId}: Modify an existing backup.
    • DELETE /Backup/{backupId}: Delete a backup.
  4. Clients

    • GET /Client: Retrieve a list of clients.
    • GET /Client/{clientId}: Retrieve details of a specific client.
    • POST /Client: Add a new client.
    • PUT /Client/{clientId}: Modify an existing client.
    • DELETE /Client/{clientId}: Delete a client.
    • ID

      GET <webservice>/Client/{clientId} HTTP/1.1
      
    • Name

      GET <webservice>/Client/byName(clientName='{clientName}') HTTP/1.1
  5. Agents

    • GET /Agent: Retrieve a list of agents.
    • GET /Agent/{agentId}: Retrieve details of a specific agent.
    • POST /Agent: Add a new agent.
    • PUT /Agent/{agentId}: Modify an existing agent.
    • DELETE /Agent/{agentId}: Delete an agent.
  6. Media

    • GET /Media: Retrieve a list of media (tapes, disks, etc.).
    • GET /Media/{mediaId}: Retrieve details of a specific media.
    • POST /Media: Add a new media.
    • PUT /Media/{mediaId}: Modify an existing media.
    • DELETE /Media/{mediaId}: Delete a media.
  7. Storage Pools

    • GET /StoragePool: Retrieve a list of storage pools.
    • GET /StoragePool/{storagePoolId}: Retrieve details of a specific storage pool.
    • POST /StoragePool: Add a new storage pool.
    • PUT /StoragePool/{storagePoolId}: Modify an existing storage pool.
    • DELETE /StoragePool/{storagePoolId}: Delete a storage pool.
  8. Alerts

    • GET /Alert: Retrieve a list of alerts.
    • GET /Alert/{alertId}: Retrieve details of a specific alert.
    • POST /Alert: Add a new alert.
    • PUT /Alert/{alertId}: Modify an existing alert.
    • DELETE /Alert/{alertId}: Delete an alert.
  9. Reports

    • GET /Report: Retrieve a list of available reports.
    • POST /Report: Generate a new report.
  10. Schedules

    • GET /Schedule: Retrieve a list of schedules.
    • GET /Schedule/{scheduleId}: Retrieve details of a specific schedule.
    • POST /Schedule: Add a new schedule.
    • PUT /Schedule/{scheduleId}: Modify an existing schedule.
    • DELETE /Schedule/{scheduleId}: Delete a schedule.
  11.  APIs that you can use to find cluster details:

    1. GET /commcell/entities/clients

      • This API retrieves information about all the clients registered with the CommCell environment, including cluster clients.
    2. GET /commcell/entities/clients/{clientId}

      • This API retrieves detailed information about a specific client, including cluster clients, based on the provided clientId.
    3. GET /commcell/entities/virtualservers

      • This API retrieves information about all the virtual servers configured in the CommCell environment, which can include cluster nodes.
    4. GET /commcell/entities/virtualservers/{virtualServerId}

      • This API retrieves detailed information about a specific virtual server, which can be a cluster node, based on the provided virtualServerId.
    5. GET /commcell/entities/mediaagents

      • This API retrieves information about all the media agents configured in the CommCell environment, which can include cluster nodes acting as media agents.
    6. GET /commcell/entities/mediaagents/{mediaAgentId}

      • This API retrieves detailed information about a specific media agent, which can be a cluster node, based on the provided mediaAgentId.
    7. GET /commcell/entities/commservers

      • This API retrieves information about all the CommServers configured in the CommCell environment, which can include cluster nodes acting as CommServers.
    8. GET /commcell/entities/commservers/{commServerId}

      • This API retrieves detailed information about a specific CommServer, which can be a cluster node, based on the provided commServerId.

Please note that the actual endpoint URLs and request/response formats may vary depending on the CommVault version and configuration. It's recommended to refer to the official CommVault documentation or API reference for the most up-to-date and accurate information.



  1. After the authentication request, we check the status code of the response. If it's 200 (OK), we proceed with the API requests. Otherwise, we print an error message with the status code.
  2. For the agent jobs API request, we check the status code of the response. If it's 200 (OK), we process the response data. Otherwise, we print an error message with the status code.
  3. For the backups API request, we check the status code of the response. If it's 200 (OK), we process the response data. Otherwise, we print an error message with the status code.

By checking the status codes, we can handle different error scenarios and provide more informative error messages if something goes wrong with the API requests.


=============================================

import requests
import json

# CommVault API endpoint and credentials
api_endpoint = "https://commvault.example.com/webconsole/api"
username = "your_username"
password = "your_password"

# Authentication
auth_url = f"{api_endpoint}/login"
auth_payload = {
    "username": username,
    "password": password
}
auth_response = requests.post(auth_url, json=auth_payload, verify=False)
auth_token = auth_response.headers.get("AuthToken")

# Set headers for subsequent API requests
headers = {
    "AuthToken": auth_token,
    "Content-Type": "application/json"
}

# Monitor agent jobs
agent_jobs_url = f"{api_endpoint}/Job"
agent_jobs_response = requests.get(agent_jobs_url, headers=headers, verify=False)
agent_jobs_data = agent_jobs_response.json()

for job in agent_jobs_data["jobSummariesValue"]:
    job_id = job["jobId"]
    job_status = job["jobStatus"]["status"]
    if job_status == "failed" or job_status == "stopped":
        print(f"Job {job_id} is {job_status}")

# Monitor backups
backups_url = f"{api_endpoint}/Backup"
backups_response = requests.get(backups_url, headers=headers, verify=False)
backups_data = backups_response.json()

for backup in backups_data["backupSummariesValue"]:
    backup_id = backup["backupId"]
    backup_status = backup["backupStatus"]["status"]
    if backup_status == "failed" or backup_status == "stopped":
        print(f"Backup {backup_id} is {backup_status}")

Here's how the code works:

  1. First, we define the CommVault API endpoint and provide our username and password for authentication.
  2. We send a POST request to the /login endpoint to obtain an authentication token.
  3. We set the AuthToken and Content-Type headers for subsequent API requests.
  4. To monitor agent jobs, we send a GET request to the /Job endpoint and iterate through the jobSummariesValue list in the response. If a job has a status of "failed" or "stopped", we print a message with the job ID and status.
  5. To monitor backups, we send a GET request to the /Backup endpoint and iterate through the backupSummariesValue list in the response. If a backup has a status of "failed" or "stopped", we print a message with the backup ID and status.

Note that you'll need to replace "your_username" and "your_password" with your actual CommVault credentials, and "https://commvault.example.com/webconsole/api" with the correct API endpoint for your CommVault instance.

Also, make sure you have the requests library installed (pip install requests) before running this code.

In Commvault, you can use the following APIs to check job statuses:

  1. Job Manager API: This API provides endpoints to retrieve information about jobs, including their statuses. You can use endpoints like /JobManager/Job to list all jobs or filter by specific criteria such as client, agent, or job status.

  2. Client Computer Group API: If you have client computer groups set up in Commvault, you can use this API to retrieve information about them, including the status of associated jobs.

  3. Alerts API: The Alerts API allows you to query for alerts generated by Commvault, which may include alerts related to job failures or other issues.

  4. Event Viewer API: This API provides access to events generated by Commvault operations, including job-related events that can indicate success or failure.

By leveraging these APIs, you can effectively monitor job statuses and identify any failed agents or other issues within your Commvault environment.

-----------------------------------------------------------------------------------------

Ensuring the health of a Commvault environment via API involves checking various aspects such as job statuses, storage usage, client connectivity, and more. Here are several ways to do this along with examples in Python:

  1. Check Job Statuses:
    • API Endpoint: /JobManager/Job
    • Objective: Ensure backup, restore, and other jobs are completing successfully.
    • Example Code:
import requests

url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/JobManager/Job"

headers = {
    "Content-Type": "application/json",
    "Authtoken": "Your_Auth_Token"
}

response = requests.get(url, headers=headers)
jobs = response.json().get("jobs")

for job in jobs:
    print("Job ID:", job.get("jobId"))
    print("Status:", job.get("status"))
    # Add more job details as needed


  1. Check Storage Usage:
    • API Endpoint: /MediaAgent/Media
    • Objective: Ensure storage utilization is within acceptable limits.
    • Example Code:
url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/MediaAgent/Media" response = requests.get(url, headers=headers) media = response.json().get("media") for m in media: print("Media ID:", m.get("mediaId")) print("Used Capacity:", m.get("usedCapacity")) # Add more media details as needed


  1. Check Client Connectivity:
    • API Endpoint: /Client
    • Objective: Ensure clients are connected and communicating properly.
    • Example Code:
url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/Client" response = requests.get(url, headers=headers) clients = response.json().get("clients") for client in clients: print("Client Name:", client.get("clientName")) print("Status:", client.get("clientStatus")) # Add more client details as needed


  1. Check Storage Policy Usage:
    • API Endpoint: /StoragePolicy
    • Objective: Ensure storage policies are being applied correctly.
    • Example Code:
url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/StoragePolicy" response = requests.get(url, headers=headers) storage_policies = response.json().get("storagePolicies") for sp in storage_policies: print("Storage Policy Name:", sp.get("storagePolicyName")) print("Status:", sp.get("status")) # Add more storage policy details as needed

------------------------------------------------------------------------

Below are examples of how you can use different Commvault APIs to check the health of the environment across various scenarios:
  1. Event Viewer API:
    • API Endpoint: /EventViewer/Events
    • Objective: Monitor system events and errors.
    • Example Code:
url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/EventViewer/Events" response = requests.get(url, headers=headers) events = response.json().get("events") for event in events: print("Event ID:", event.get("eventId")) print("Severity:", event.get("severity")) print("Description:", event.get("description")) # Add more event details as needed


  1. Alerts API:
    • API Endpoint: /Alerts
    • Objective: Check for any active alerts in the system.
    • Example Code:
url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/Alerts" response = requests.get(url, headers=headers) alerts = response.json().get("alerts") for alert in alerts: print("Alert ID:", alert.get("alertId")) print("Severity:", alert.get("severity")) print("Description:", alert.get("description")) # Add more alert details as needed

  1. Client Computer Group API:
    • API Endpoint: /ClientGroup
    • Objective: Ensure client computers are organized into groups properly.
    • Example Code:

url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/ClientGroup" response = requests.get(url, headers=headers) client_groups = response.json().get("clientGroups") for group in client_groups: print("Group Name:", group.get("groupName")) print("Number of Clients:", group.get("numberOfClients")) # Add more group details as needed


  1. Agent API:
    • API Endpoint: /Agent
    • Objective: Check the status and health of agents installed on client computers.
    • Example Code:
url = "https://your-commvault-server:81/SearchSvc/CVWebService.svc/Agent" response = requests.get(url, headers=headers) agents = response.json().get("agents") for agent in agents: print("Agent ID:", agent.get("agentId")) print("Status:", agent.get("status")) # Add more agent details as needed

  1. Backup Recovery API:
    • API Endpoint: /JobManager/Job
    • Objective: Monitor backup and recovery jobs for success and failure.
    • Example Code: (Already provided in previous response)

These examples demonstrate how you can use Python with the requests library to query different aspects of the Commvault environment via API and ensure its health across various scenarios. Adjust the endpoints and parameters based on your specific requirements and API documentation.

=======================================

import requests # Replace with your Commvault environment details commvault_url = "https://<your_commvault_server>/webservice/" username = "<your_username>" password = "<your_password>" # Client and VM details client_name = "<client_name>" vm_name = "<vm_name>" # Define the subclient details (replace with your desired values) subclient_name = "My Subclient" backup_set_name = "DefaultBackupSet" content_type = "VMName" # Can be "VMName" or "DATASTORE" for Datastore content def get_auth_token(): """ Retrieves an authentication token from Commvault. """ url = commvault_url + "login" auth = (username, password) headers = {"Content-Type": "application/json"} response = requests.post(url, auth=auth, headers=headers) response.raise_for_status() # Raise exception for non-200 status codes return response.json()["authenticationToken"] def add_subclient(): """ Adds a subclient to the specified client VM. """ auth_token = get_auth_token() headers = { "Authorization": f"AuthToken {auth_token}", "Content-Type": "application/xml" # Body format for add subclient } # Prepare the XML request body (replace with your specific content details) body = f""" <subclient> <appName>Virtual Server</appName> <subClientEntity> <subClientProperties> <clientName>{client_name}</clientName> <subClientName>{subclient_name}</subClientName> <backupsetName>{backup_set_name}</backupsetName> <instanceName>DefaultInstanceName</instanceName> <content> <vmContent> <type>{content_type}</type> <displayName>{vm_name}</displayName> </vmContent> </content> </subClientProperties> </subClientEntity> </subclient> """ url = commvault_url + "Subclient" response = requests.post(url, headers=headers, data=body) response.raise_for_status() print(f"Subclient '{subclient_name}' added successfully!") if __name__ == "__main__": add_subclient()

=============================================================

import requests import json # Commvault API credentials cv_username = "your_username" cv_password = "your_password" cv_domain = "your_domain" # e.g., "cv" # Commvault API base URL cv_base_url = "https://your_commvault_server/webconsole/api" # Authentication endpoint auth_endpoint = f"{cv_base_url}/login" # Authenticate and get the session ID auth_payload = { "domain": cv_domain, "login": cv_username, "password": cv_password } auth_response = requests.post(auth_endpoint, json=auth_payload, verify=False) session_id = auth_response.headers.get("Set-Cookie").split(";")[0] # Set headers for subsequent API requests headers = { "Cookie": session_id, "Content-Type": "application/json", "Accept": "application/json" } # Client VM details client_name = "your_client_vm_name" client_id = "your_client_vm_id" # You can get this from the Commvault console or API # Subclient details subclient_name = "your_subclient_name" subclient_type = "your_subclient_type" # e.g., "Virtual Server" backup_set_name = "your_backup_set_name" content_path = "your_content_path" # e.g., "/" # Endpoint to add a subclient add_subclient_endpoint = f"{cv_base_url}/Client/{client_id}/Subclient" # Payload for adding a subclient add_subclient_payload = { "subClientProperties": { "clientName": client_name, "subclientName": subclient_name, "subclientType": subclient_type, "backupSetName": backup_set_name, "contentPath": { "path": [ content_path ] } } } # Send the request to add the subclient add_subclient_response = requests.post(add_subclient_endpoint, headers=headers, json=add_subclient_payload, verify=False) # Check the response status code if add_subclient_response.status_code == 200: print("Subclient added successfully.") else: print(f"Failed to add subclient. Error: {add_subclient_response.text}")

===========================================================
import requests
import json

# Replace with your Commvault server details and credentials
commvault_base_url = 'http://your-commvault-server:port'
username = 'your_username'
password = 'your_password'
client_name = 'your_client_vm_name'
backupset_name = 'your_backupset_name'  # Typically 'defaultBackupSet'
subclient_name = 'your_subclient_name'
content_path = 'path_to_include_in_subclient'  # The path to include in the subclient

# Step 1: Authenticate and get the token
auth_url = f'{commvault_base_url}/webconsole/api/Login'
auth_payload = {
    'username': username,
    'password': password
}
response = requests.post(auth_url, json=auth_payload)
if response.status_code != 200:
    raise Exception(f"Authentication failed: {response.text}")

token = response.json()['token']

# Step 2: Get client ID
clients_url = f'{commvault_base_url}/webconsole/api/Client'
headers = {
    'Authtoken': token,
    'Accept': 'application/json'
}
response = requests.get(clients_url, headers=headers)
if response.status_code != 200:
    raise Exception(f"Failed to retrieve clients: {response.text}")

clients = response.json()['clients']
client_id = next((client['clientId'] for client in clients if client['clientName'] == client_name), None)
if not client_id:
    raise Exception(f"Client VM '{client_name}' not found")

# Step 3: Get the backupset ID
backupsets_url = f'{commvault_base_url}/webconsole/api/Client/{client_id}/Backupset'
response = requests.get(backupsets_url, headers=headers)
if response.status_code != 200:
    raise Exception(f"Failed to retrieve backupsets: {response.text}")

backupsets = response.json()['backupsets']
backupset_id = next((backupset['backupsetId'] for backupset in backupsets if backupset['backupsetName'] == backupset_name), None)
if not backupset_id:
    raise Exception(f"Backupset '{backupset_name}' not found for client '{client_name}'")

# Step 4: Add the subclient
subclients_url = f'{commvault_base_url}/webconsole/api/Backupset/{backupset_id}/Subclient'
subclient_payload = {
    'subClientProperties': {
        'subClientEntity': {
            'subclientName': subclient_name
        },
        'contentOperationType': 1,
        'content': [{
            'path': content_path
        }]
    }
}
response = requests.post(subclients_url, headers=headers, json=subclient_payload)
if response.status_code != 200:
    raise Exception(f"Failed to add subclient: {response.text}")

print(f"Subclient '{subclient_name}' added successfully to client '{client_name}'")

No comments:

Post a Comment

Ansible Tutorial for Beginners: Playbook Commands & Examples | Getting started with Ansible Questions & Answers

  Troubleshoot and Create Ansible Playbook Create Ansible Inventory for App Server Testing Configure Default SSH User for Ansible Copy Data ...