|
| 1 | + |
| 2 | +# Using python SDK to create OCI Monitoring custom metric namespace: Services Limit monitoring example use case |
| 3 | + |
| 4 | +## 1. INTRODUCTION |
| 5 | + |
| 6 | +Describes how any user can create an OCI Monitoring ***custom metric namespace*** to being able to extend the default services metric namespaces. For that, we'll support on python SDK to create an script that can be run in an OCI VM (using instance principals authentication), or any other external system (using OCI IAM principals). To cover this educational example, we'll use as an example the creation of a custom metric namespace to monitor the OCI Services Limits usage. With this custom metric namespace, OCI alarms can be created and OCI Notification Service can be used to send the alarm information by different means to allow to create proactively a Service Limit Service Request to increase the limit before causing any disruption in the running services or services to be provisioned. |
| 7 | + |
| 8 | +## 2. SOLUTION |
| 9 | +We can see the overall architecture in the following logical diagram: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +The script will take care of gathering the OCI Services Limits information and post as a custom metric namespace. The metrics where to post the data will be: |
| 14 | + |
| 15 | +1. **used**: This is the amount of the Service Limit that it is being used |
| 16 | +2. **max_limit**: This is the amount of the Service Limit that we can use (and increase with a SR) |
| 17 | +3. **available**: This is the remaining amount of the Service Limit that we can use until get the max_limit |
| 18 | + |
| 19 | +Not all the Services Limits are equal as they depend of the scope they have (Global, Regional or Availability Domain). We'll introduce the characteristics of a Service Limit as **Dimensions** of the metric, so we can select the Service Limit, the limit name and the scope to filter the specific limit. Thus, we'll have as **Dimensions**: |
| 20 | + |
| 21 | +1. **service_name**: The name of the OCI Service that the Service Limit belongs to (e.g.: Compute) |
| 22 | +2. **limit_name**: The name of the Service Limit (e.g.: bm-standard2-52-count) |
| 23 | +3. **AD**: If the Service Limit has an AD availability, we can filter for the AD where we would like to filter the metric |
| 24 | + |
| 25 | +With this raw data, we could be able to build Alarm Definitions on the specific Services Limits that we would like to monitor, as usually customers do not use all the possible OCI services. |
| 26 | + |
| 27 | +Optionally, they could use any OCI Notification Service to being notified when the alarm fires receiving the notification message in any of the supported options. Some of them will enable the integration with 3rd party services. |
| 28 | + |
| 29 | +## 3. SCRIPT'S LOGIC |
| 30 | + |
| 31 | +Here we'll focus in reviewing the logic behind using the OCI Python SDK to get the objectives to monitor the Services Limits with OCI Monitoring. |
| 32 | + |
| 33 | +We use 3 different OCI services API calls to gather the needed information to create the custom metric namespace for the Services Limit monitoring, these are: |
| 34 | + |
| 35 | +1. [Identity and Access Management Service API](https://docs.oracle.com/en-us/iaas/api/#/en/identity/20160918/): To use the [ListAvailabilityDomains](https://docs.oracle.com/en-us/iaas/api/#/en/identity/20160918/AvailabilityDomain/ListAvailabilityDomains) API Call to get the list of the availability domains existing in the input tenancy. |
| 36 | +2. [Monitoring API](https://docs.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/): To use the [PostMetricData](https://docs.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/MetricData/PostMetricData), to post the metrics information in the existing (or non yet existing) custom service metric namespace. If it doesn't exist yet, it just creates it so we don't need to explicitly create it before. |
| 37 | +3. [Service Limits API](https://docs.oracle.com/en-us/iaas/api/#/en/limits/20181025/): To use the [ListLimitsDefinition](https://docs.oracle.com/en-us/iaas/api/#/en/limits/20181025/LimitDefinitionSummary/ListLimitDefinitions) to get the full list of Service Limits in a given compartment and the [GetResouceAvailability](https://docs.oracle.com/en-us/iaas/api/#/en/limits/20181025/ResourceAvailability/GetResourceAvailability), to gather the used and available limits depending in the scope (AD, regional, whole tenancy), of the known Service Limits from the previous list. |
| 38 | + |
| 39 | +Basically the **logic** is: |
| 40 | + |
| 41 | +```` |
| 42 | +Start |
| 43 | +Parse input arguments (compartment_ocid, region) |
| 44 | +Setup the OCI connection authentication (OCI IAM Config or Instance Principals) |
| 45 | +Initialise the clients for the different API calls (IAM, Monitoring, Service Limits) |
| 46 | +Gather the full list of Service Limits Definitions sorted by Service Limit name |
| 47 | +For the list of Service Limit names |
| 48 | + If the scope is Availability Domain |
| 49 | + For every AD |
| 50 | + Get the limits and usage for the Service Limit name within this AD |
| 51 | + Post in the custom metric namespace the metric with the dimension of the Service Limit, name and AD with the resources used, available and the service limit maximum |
| 52 | + For the non-AD Service Limit names (Global or Regional) |
| 53 | + Get the limits and usage for the Service Limit name |
| 54 | + Post in the custom metric namespace the metric with the dimension of the Service Limit and name with the resources used, available and the service limit maximum |
| 55 | +End |
| 56 | +```` |
| 57 | + |
| 58 | +## 4. REQUIREMENTS |
| 59 | + |
| 60 | +We have different requirements depending on the variant of this asset that we would use: |
| 61 | + |
| 62 | +1. **IAM User** |
| 63 | + * An existing OCI IAM user with an API signing key |
| 64 | + * An .oci/config profile for the tenancy, with the previous IAM user details |
| 65 | + * A policy granting the user to: |
| 66 | + * inspect resource-availability in tenancy |
| 67 | + * inspect limits in tenancy |
| 68 | + * use metrics in tenancy |
| 69 | + |
| 70 | +2. **Instance Principal** |
| 71 | + * An existing dynamic group identifying the VM where to run the script as member of the group |
| 72 | + * A policy granting the dynamic group to: |
| 73 | + * inspect resource-availability in tenancy |
| 74 | + * inspect limits in tenancy |
| 75 | + * use metrics in tenancy |
| 76 | + |
| 77 | +3. ***Common requirements*** |
| 78 | +The VM where to run the script must have installed python3 with the following required packages installed with pip: |
| 79 | + * **oci** |
| 80 | + |
| 81 | +## 5. INPUT |
| 82 | + |
| 83 | +The required input arguments are: |
| 84 | + |
| 85 | +* **compartment_ocid** with the OCID of your tenancy root compartment. |
| 86 | +* **region** where you want to get the Services Limits with regional scope and where to publish metrics |
| 87 | + |
| 88 | +## 6. OUTPUT |
| 89 | + |
| 90 | +Every time the script is run, it will feed a custom metric namespace called "**limits_metrics**" in the tenancy's root compartment with the information of the Services Limits usage. |
| 91 | + |
| 92 | +You can check the custom metric extension from the OCI Metrics Explorer, where you will be able also to create an alarms from an specific metric query. |
| 93 | + |
| 94 | +## 7. GETTING STARTED |
| 95 | + |
| 96 | +To execute the script: |
| 97 | + |
| 98 | +1. Ensure that the requirements are met with your desired variant (using IAM user or Instance Principals) |
| 99 | +2. Upload the script into your administration VM inside OCI (IAM user or Instance Principals), or outside OCI (IAM user only) |
| 100 | +3. To execute the script: |
| 101 | + * For the IAM User principals authentication method, execute: |
| 102 | + ```` |
| 103 | + $ python3 serviceLimitsMetricsIAM.py -c/--compartment_ocid <root's compartment OCID> -r/--region <target region> |
| 104 | + ```` |
| 105 | + * The script is available [here](./files/Scripts/postServiceLimitsMetricsIAM.py) |
| 106 | + * For the Instance principal authentication method, execute: |
| 107 | + ```` |
| 108 | + $ python3 serviceLimitsMetricsIP.py -c/--compartment_ocid <root's compartment OCID> -r/--region <target region> |
| 109 | + ```` |
| 110 | + * The script is available [here](./files/Scripts/postServiceLimitsMetricsIP.py) |
| 111 | +
|
| 112 | +## 8.KNOWN PROBLEMS |
| 113 | +
|
| 114 | +None at this point. |
| 115 | +
|
| 116 | +## 9.RELEASE NOTES |
| 117 | +
|
| 118 | +There 2 scripts with same code except for the OCI authentication. Both scripts will be maintained in parallel with same versions: |
| 119 | +
|
| 120 | +2023-07-25 (version 0.2). compartment_ocid & region are now input arguments. Fixed region wide limits publishing. |
| 121 | +2023-07-18 (version 0.1). Initial public release. |
| 122 | + |
| 123 | +# LICENSE |
| 124 | +
|
| 125 | +Copyright (c) 2023 Oracle and/or its affiliates. |
| 126 | +
|
| 127 | +Licensed under the Universal Permissive License (UPL), Version 1.0. |
| 128 | +
|
| 129 | +See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/folder-structure/LICENSE) for more details. |
0 commit comments