|
| 1 | +--- |
| 2 | +title: "OpsRamp OpsQL API: Fulfilling wishes for IT Ops management" |
| 3 | +date: 2025-03-10T21:32:15.912Z |
| 4 | +featuredBlog: true |
| 5 | +author: BalaSubramanian Vetrivel |
| 6 | +authorimage: /img/balasubramanian-photo-2.png |
| 7 | +thumbnailimage: https://www.google.com/imgres?q=opsramp&imgurl=https%3A%2F%2Fwww.opsramp.com%2Fwp-content%2Fthemes%2Ffas-base%2Ffreemium%2Fassets%2Fbrand%2FOpsRamp_Logo_Gray_Blue_RGB.svg&imgrefurl=https%3A%2F%2Fwww.opsramp.com%2F&docid=zXQkTLrgWW4LtM&tbnid=LjasiVSx9A0jAM&vet=12ahUKEwjVvYfK2ICMAxV89DgGHQ9-OQoQM3oECB0QAA..i&w=800&h=239&hcb=2&ved=2ahUKEwjVvYfK2ICMAxV89DgGHQ9-OQoQM3oECB0QAA |
| 8 | +disable: false |
| 9 | +tags: |
| 10 | + - OpsRamp, OpsQL, OpsRamp API |
| 11 | +--- |
| 12 | +<style> |
| 13 | +li { |
| 14 | + font-size: 15px; |
| 15 | + line-height: 20px; |
| 16 | + max-width: none; |
| 17 | +} |
| 18 | +</style> |
| 19 | + |
| 20 | +In today's data centers, IT Ops managers face mounting challenges in managing and retrieving data efficiently. They've introduced so many network, storage, and computing devices into their environment that they have become unmanageable. At times performance issues and critical services going down keep happening and they're left with headaches, higher costs, and poor performance. If only they had a Kalpavriksha, a magical wish-fulfilling tree that could answer their needs for quickly searching for any resource from their complex, wide data centre to make all their dreams come true. |
| 21 | + |
| 22 | +With OpsRamp, a comprehensive IT operations management platform, they may very well get their wish. With its OpsRamp Query Language (OpsQL) and its powerful API, users can perform complex searches within the OpsRamp platform. It allows users to retrieve specific data based on various attributes and conditions. To quickly access and manipulate data in order to maintain optimal system performance and resolve issues, OpsQL is a Kalpavriksha that truly grants wishes. In this blog post, I will explain the basics of OpsQL and how to use it. |
| 23 | + |
| 24 | +## What is OpsQL? |
| 25 | +OpsQL is a flexible and powerful query language to search objects within the OpsRamp platform. It allows users to retrieve specific data based on various attributes and conditions. OpsQL is essential for IT administrators to quickly access and manipulate data, in order to maintain optimal system performance and resolve issues. IT Ops Managers, users can run OpsQL in intuitive OpsRamp UI or using OpsQL API. |
| 26 | + |
| 27 | +## Basic syntax and structure |
| 28 | +Let's say you want to make a query. The general syntax for an OpsQL query is very straightforward: |
| 29 | + |
| 30 | +```PlainText |
| 31 | + <attribute> <operator> | <coperator> "<value>" [[<operator> [<attribute> | <coperator> "<value>"[)]] ... ] |
| 32 | +``` |
| 33 | + |
| 34 | +You can use logical operators `AND` and `OR` to refine your search further. |
| 35 | + |
| 36 | +## Operators |
| 37 | +Operator is the key of the query. It relates the attribute to the value. |
| 38 | +OpsQL supports a variety of operators to create precise queries. |
| 39 | + 1. **Equality operators** |
| 40 | + `=`, `!=` |
| 41 | + 2. **String operators** |
| 42 | + `CONTAINS`, `STARTS WITH`, `LIKE` |
| 43 | + 3. **Logical operators** |
| 44 | +`AND`, `OR` |
| 45 | + |
| 46 | +For example, to find all resources with an agent installed and of type "Windows," you would use: |
| 47 | + |
| 48 | +```PlainText |
| 49 | +agentInstalled = "true" AND type = "Windows" |
| 50 | +``` |
| 51 | + |
| 52 | +## Attributes |
| 53 | +Attributes are different types of information available on an object. For instance, a resource might have attributes like `make`, `ipAddress`, and `agentInstalled`, while an alert might have attributes like `priority`, `currentState`, and `createdTime`. |
| 54 | + |
| 55 | +For more details, you can refer to the [OpsRamp Query Language Reference Documentation.](https://docs.opsramp.com/platform-features/feature-guides/query-language-reference/query-language-ref/) |
| 56 | + |
| 57 | +## More examples |
| 58 | +Here are some OpsQL examples to search resources on the OpsRamp platform. |
| 59 | + |
| 60 | +### Search for resources that were discovered by an AWS integration |
| 61 | +```PlainText |
| 62 | +installedAppName = aws |
| 63 | +``` |
| 64 | +### Search for resources that I tagged in AWS with AWS tag OwnerName :SivaBalaSubramanian |
| 65 | +```PlainText |
| 66 | +installedAppName = "aws" AND tags.name = "OwnerName" and tags.value = "SivaBalaSubramanian" |
| 67 | +``` |
| 68 | +### Search for resources with alerts that have been open for the last 2 hours |
| 69 | +```PlainText |
| 70 | +createdTime > "-7200sec" |
| 71 | +createdTime > "-120min" |
| 72 | +``` |
| 73 | +### Search for open alerts that are critical |
| 74 | +```PlainText |
| 75 | +currentState = "CRITICAL" AND status = "OPEN" |
| 76 | +``` |
| 77 | +### Search for open and critical alerts that have an open incident |
| 78 | +```PlainText |
| 79 | +currentState = "CRITICAL" AND status = "OPEN" AND incidentId IS NOT NULL |
| 80 | +``` |
| 81 | +### Search for alerts that have been Open for longer than 2 hours |
| 82 | +```PlainText |
| 83 | +createdTime < "-7200sec" |
| 84 | +``` |
| 85 | +### Search for Open and critical alerts on resources tagged with AWS tag “BU: bu-123” |
| 86 | +```PlainText |
| 87 | +currentState = "CRITICAL" AND status = "OPEN" AND resource.tags.name = "Team" AND resource.tags.value = "Opsqa" |
| 88 | +``` |
| 89 | + |
| 90 | +## What is OpsQL API? |
| 91 | +The OpsQL API is a powerful interface that allows users to execute OpsQL queries programmatically. This API provides the flexibility to filter, search data within the OpsRamp platform, making it an indispensable tool for IT administrators and developers. |
| 92 | +IT Administrators can invoke OpsQL API using tools such as Postman, cURL, Python. |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +#### Key features |
| 97 | +1. **Comprehensive querying**: The OpsQL API supports a wide range of query operations, allowing users to filter data based on various attributes and conditions. |
| 98 | +2. **Flexibility**: Users can create complex queries using logical operators and a variety of comparison operators. |
| 99 | +3. **Integration**: The API can be integrated into custom applications, scripts, and workflows, enhancing automation and efficiency. |
| 100 | + |
| 101 | + |
| 102 | +#### Basic syntax and structure |
| 103 | +The general structure of an OpsQL API request involves specifying the tenant ID and the query payload. |
| 104 | + |
| 105 | +Here’s a basic example: |
| 106 | + |
| 107 | +``` |
| 108 | +POST /opsql/api/v3/tenants/{tenantId}/queries |
| 109 | +``` |
| 110 | + |
| 111 | +The request body typically includes: |
| 112 | +* `objectType`: The type of object to query (e.g., resource, alert, ticket). |
| 113 | +* `fields`: The fields to retrieve. |
| 114 | +* `filterCriteria`: The criteria to filter the objects. |
| 115 | + |
| 116 | +## Common use cases and code samples |
| 117 | +### Filtering critical alerts |
| 118 | +There are times that you would want to filter only critical alerts. This is how that would be done: |
| 119 | + |
| 120 | + |
| 121 | +```python |
| 122 | +import requests |
| 123 | +import json |
| 124 | +accessToken='valid access token' |
| 125 | + |
| 126 | + |
| 127 | +url = "https://server/opsql/api/v3/tenants/client_id/queries" |
| 128 | + |
| 129 | +payload = json.dumps({ |
| 130 | + "objectType": "alert", |
| 131 | + "fields": [ |
| 132 | + "id", |
| 133 | + "clientId", |
| 134 | + "component", |
| 135 | + "currentState" |
| 136 | + ], |
| 137 | + "filterCriteria": "currentState=critical" |
| 138 | +}) |
| 139 | +headers = { |
| 140 | + 'Accept': 'application/json', |
| 141 | + 'Content-Type': 'application/json', |
| 142 | + 'Authorization': accessToken |
| 143 | +} |
| 144 | + |
| 145 | +response = requests.request("POST", url, headers = headers, data = payload) |
| 146 | +``` |
| 147 | +### Saving OpsQL response as CSV file |
| 148 | +There are times that you would want to save an OpsQL response as a CSV file, perhaps for further analysis. This is how that would be done: |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +```python |
| 153 | +def invoke_opsql() -> None: |
| 154 | + response = requests.request("POST", url, headers = headers, data = payload) |
| 155 | + timestr = time.strftime("%Y%m%d-%H%M%S") |
| 156 | + json_file_name = "siva_aws_resources-" + timestr + ".json" |
| 157 | + csv_file_name = "siva_aws_resources-" + timestr + ".csv" |
| 158 | + |
| 159 | + with open(json_file_name, "wb") as file: |
| 160 | + file.write(response.content) |
| 161 | + |
| 162 | + json_to_csv(json_file_name, csv_file_name) |
| 163 | + |
| 164 | +def json_to_csv(resources_json, file_csv) -> None : |
| 165 | + with open(resources_json) as json_file: |
| 166 | + data = json.load(json_file) |
| 167 | + |
| 168 | + opsql_response = data['results'] |
| 169 | + data_file = open(file_csv, 'w') |
| 170 | + csv_writer = csv.writer(data_file) |
| 171 | + count = 0 |
| 172 | + |
| 173 | + for opsql_row in opsql_response: |
| 174 | + if count == 0: |
| 175 | + header = opsql_row.keys() |
| 176 | + csv_writer.writerow(header) |
| 177 | + count += 1 |
| 178 | + |
| 179 | + csv_writer.writerow(opsql_row.values()) |
| 180 | + |
| 181 | + data_file.close() |
| 182 | + |
| 183 | +``` |
| 184 | +## Conclusion |
| 185 | +The OpsQL API is a powerful tool that enhances the capabilities of the OpsRamp platform, providing users with the flexibility to perform complex queries and manage data efficiently. By leveraging the OpsQL API, IT administrators and developers can streamline their operations, improve data management, and enhance overall productivity. Thus OpsQL grants the wishes of IT Ops Managers with a single pane of glass and unified searching capability across their wide, complex real estate. |
| 186 | +For more details, you can refer to the [OpsQL API Documentation](https://develop.opsramp.com/v3/api/opsql/tenantid-queries/) |
0 commit comments