|
41 | 41 | # Version: 0.2
|
42 | 42 | ###
|
43 | 43 |
|
44 |
| -import oci,datetime,json,argparse |
| 44 | +import oci, datetime, json, argparse |
| 45 | + |
45 | 46 | from oci.config import from_file
|
46 | 47 | from pytz import timezone
|
47 | 48 |
|
48 | 49 | # Functions definition
|
49 | 50 |
|
50 | 51 | # postMetric: posts custom monitoring metric information for compartment, metric(used,available,max_limit) and with its dimensions (service, limit name and availability domain(if AD specific))
|
51 |
| -def postMetric(compartment_ocid, m_name, s_name,l_name, value, a_domain=None): |
| 52 | +def postMetric(compartment_ocid, m_name, s_name,l_name, value, monitoring_client, a_domain=None): |
| 53 | + # Get the timestamp for setup the monitoring metric post information |
| 54 | + times_stamp = datetime.datetime.now(timezone('UTC')) |
52 | 55 | if a_domain is None:
|
53 | 56 | post_metric_data_response = monitoring_client.post_metric_data(
|
54 | 57 | post_metric_data_details=oci.monitoring.models.PostMetricDataDetails(
|
@@ -93,7 +96,7 @@ def postMetric(compartment_ocid, m_name, s_name,l_name, value, a_domain=None):
|
93 | 96 | return post_metric_data_response
|
94 | 97 |
|
95 | 98 | # getServiceLimitsUsage: gets the existing limits for a service and limit name in a compartment and, if AD specific, for its AD
|
96 |
| -def getServiceLimitsUsage(s_name, l_name, compartment_ocid, a_domain=None): |
| 99 | +def getServiceLimitsUsage(s_name, l_name, compartment_ocid, limits_client, a_domain=None): |
97 | 100 | if a_domain is None:
|
98 | 101 | # We gather the service limit usage
|
99 | 102 | get_resource_availability_response = limits_client.get_resource_availability(service_name = s_name, limit_name = l_name, compartment_id = compartment_ocid)
|
@@ -189,49 +192,43 @@ def getServiceLimitsUsage(s_name, l_name, compartment_ocid, a_domain=None):
|
189 | 192 |
|
190 | 193 | a_domain = json.loads(str(AD))
|
191 | 194 |
|
192 |
| - limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid, a_domain["name"])) |
193 |
| - |
194 |
| - # Get the timestamp for setup the monitoring metric post information |
195 |
| - times_stamp = datetime.datetime.now(timezone('UTC')) |
| 195 | + limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid, limits_client, a_domain["name"])) |
196 | 196 |
|
197 | 197 | # Posting custom metrics to oci monitoring for each of the metrics (max, used, available)
|
198 | 198 |
|
199 | 199 | # Max limit
|
200 |
| - postMetricMax = postMetric(compartment_ocid = compartment_ocid, m_name = "max_limit", s_name = s_name, l_name = l_name, value = int(limit_usage["max_limit"]), a_domain = a_domain["name"]) |
| 200 | + postMetric(compartment_ocid, "max_limit", s_name, l_name, limit_usage["max_limit"], monitoring_client, a_domain["name"]) |
201 | 201 |
|
202 | 202 | # Used
|
203 |
| - postMetricUsed = postMetric(compartment_ocid = compartment_ocid, m_name = "used", s_name = s_name, l_name = l_name, value = limit_usage["used"], a_domain = a_domain["name"]) |
| 203 | + postMetric(compartment_ocid, "used", s_name, l_name, limit_usage["used"], monitoring_client, a_domain = a_domain["name"]) |
204 | 204 |
|
205 | 205 | # Available
|
206 |
| - postMetricAvail = postMetric(compartment_ocid = compartment_ocid, m_name = "available", s_name = s_name, l_name = l_name, value = limit_usage["available"], a_domain = a_domain["name"]) |
| 206 | + postMetric(compartment_ocid, "available", s_name, l_name, limit_usage["available"], monitoring_client, a_domain = a_domain["name"]) |
207 | 207 |
|
208 | 208 | else:
|
209 | 209 | # We are in GLOBAL or REGION case
|
210 | 210 |
|
211 |
| - limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid)) |
| 211 | + limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid, limits_client)) |
212 | 212 |
|
213 | 213 | max_limit = limit_usage["max_limit"]
|
214 | 214 | used = limit_usage["used"]
|
215 | 215 |
|
216 | 216 | if max_limit == "null" :
|
217 | 217 | continue
|
218 | 218 |
|
219 |
| - # Get the timestamp for setup the monitoring metric post information |
220 |
| - times_stamp = datetime.datetime.now(timezone('UTC')) |
221 |
| - |
222 | 219 | # Posting custom metrics to oci monitoring for each of the metrics (max, used, available)
|
223 | 220 |
|
224 | 221 | # Max limit
|
225 |
| - postMetricMax = postMetric(compartment_ocid = compartment_ocid, m_name = "max_limit", s_name = s_name, l_name = l_name, value = max_limit) |
| 222 | + postMetric(compartment_ocid, "max_limit", s_name, l_name, max_limit, monitoring_client) |
226 | 223 |
|
227 | 224 | if used is None :
|
228 | 225 | continue
|
229 | 226 |
|
230 | 227 | # Used
|
231 |
| - postMetricUsed = postMetric(compartment_ocid = compartment_ocid, m_name = "used", s_name = s_name, l_name = l_name, value = used) |
| 228 | + postMetric(compartment_ocid, "used", s_name, l_name, used, monitoring_client) |
232 | 229 |
|
233 | 230 | # Available
|
234 |
| - postMetricAvail = postMetric(compartment_ocid = compartment_ocid, m_name = "used", s_name = s_name, l_name = l_name, value = limit_usage["available"]) |
| 231 | + postMetric(compartment_ocid, "used", s_name, l_name, limit_usage["available"], monitoring_client) |
235 | 232 |
|
236 | 233 | # Finish:
|
237 | 234 | now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
|
0 commit comments