Skip to content

Commit 692c92d

Browse files
committed
Updated and simplified the scripts code
1 parent 06fc8e4 commit 692c92d

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

manageability-and-operations/observability-and-manageability/oci-monitoring/custom-metrics/custom-metric-python-SDK-services-limit-monitoring/files/Scripts/postServiceLimitsMetricsIAM.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@
4141
# Version: 0.2
4242
###
4343

44-
import oci,datetime,json,argparse
44+
import oci, datetime, json, argparse
45+
4546
from oci.config import from_file
4647
from pytz import timezone
4748

4849
# Functions definition
4950

5051
# 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'))
5255
if a_domain is None:
5356
post_metric_data_response = monitoring_client.post_metric_data(
5457
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):
9396
return post_metric_data_response
9497

9598
# 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):
97100
if a_domain is None:
98101
# We gather the service limit usage
99102
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):
189192

190193
a_domain = json.loads(str(AD))
191194

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"]))
196196

197197
# Posting custom metrics to oci monitoring for each of the metrics (max, used, available)
198198

199199
# 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"])
201201

202202
# 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"])
204204

205205
# 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"])
207207

208208
else:
209209
# We are in GLOBAL or REGION case
210210

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))
212212

213213
max_limit = limit_usage["max_limit"]
214214
used = limit_usage["used"]
215215

216216
if max_limit == "null" :
217217
continue
218218

219-
# Get the timestamp for setup the monitoring metric post information
220-
times_stamp = datetime.datetime.now(timezone('UTC'))
221-
222219
# Posting custom metrics to oci monitoring for each of the metrics (max, used, available)
223220

224221
# 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)
226223

227224
if used is None :
228225
continue
229226

230227
# 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)
232229

233230
# 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)
235232

236233
# Finish:
237234
now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")

manageability-and-operations/observability-and-manageability/oci-monitoring/custom-metrics/custom-metric-python-SDK-services-limit-monitoring/files/Scripts/postServiceLimitsMetricsIP.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@
4141
# Version: 0.2
4242
###
4343

44-
import oci,datetime,json,argparse
44+
import oci, datetime, json, argparse
45+
4546
from pytz import timezone
4647

4748
# Functions definition
4849

4950
# 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))
50-
def postMetric(compartment_ocid, m_name, s_name,l_name, value, a_domain=None):
51+
def postMetric(compartment_ocid, m_name, s_name,l_name, value, monitoring_client, a_domain=None):
52+
# Get the timestamp for setup the monitoring metric post information
53+
times_stamp = datetime.datetime.now(timezone('UTC'))
5154
if a_domain is None:
5255
post_metric_data_response = monitoring_client.post_metric_data(
5356
post_metric_data_details=oci.monitoring.models.PostMetricDataDetails(
@@ -92,7 +95,7 @@ def postMetric(compartment_ocid, m_name, s_name,l_name, value, a_domain=None):
9295
return post_metric_data_response
9396

9497
# getServiceLimitsUsage: gets the existing limits for a service and limit name in a compartment and, if AD specific, for its AD
95-
def getServiceLimitsUsage(s_name, l_name, compartment_ocid, a_domain=None):
98+
def getServiceLimitsUsage(s_name, l_name, compartment_ocid, limits_client, a_domain=None):
9699
if a_domain is None:
97100
# We gather the service limit usage
98101
get_resource_availability_response = limits_client.get_resource_availability(service_name = s_name, limit_name = l_name, compartment_id = compartment_ocid)
@@ -188,49 +191,43 @@ def getServiceLimitsUsage(s_name, l_name, compartment_ocid, a_domain=None):
188191

189192
a_domain = json.loads(str(AD))
190193

191-
limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid, a_domain["name"]))
192-
193-
# Get the timestamp for setup the monitoring metric post information
194-
times_stamp = datetime.datetime.now(timezone('UTC'))
194+
limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid, limits_client, a_domain["name"]))
195195

196196
# Posting custom metrics to oci monitoring for each of the metrics (max, used, available)
197197

198198
# Max limit
199-
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"])
199+
postMetric(compartment_ocid, "max_limit", s_name, l_name, limit_usage["max_limit"], monitoring_client, a_domain = a_domain["name"])
200200

201201
# Used
202-
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"])
202+
postMetric(compartment_ocid, "used", s_name, l_name, limit_usage["used"], monitoring_client, a_domain = a_domain["name"])
203203

204204
# Available
205-
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"])
205+
postMetric(compartment_ocid, "available", s_name, l_name, limit_usage["available"], monitoring_client, a_domain = a_domain["name"])
206206

207207
else:
208208
# We are in GLOBAL or REGION case
209209

210-
limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid))
210+
limit_usage = json.loads(getServiceLimitsUsage(s_name, l_name, compartment_ocid, limits_client))
211211

212212
max_limit = limit_usage["max_limit"]
213213
used = limit_usage["used"]
214214

215215
if max_limit == "null" :
216216
continue
217217

218-
# Get the timestamp for setup the monitoring metric post information
219-
times_stamp = datetime.datetime.now(timezone('UTC'))
220-
221218
# Posting custom metrics to oci monitoring for each of the metrics (max, used, available)
222219

223220
# Max limit
224-
postMetricMax = postMetric(compartment_ocid = compartment_ocid, m_name = "max_limit", s_name = s_name, l_name = l_name, value = max_limit)
221+
postMetric(compartment_ocid, "max_limit", s_name, l_name, max_limit, monitoring_client)
225222

226223
if used is None :
227224
continue
228225

229226
# Used
230-
postMetricUsed = postMetric(compartment_ocid = compartment_ocid, m_name = "used", s_name = s_name, l_name = l_name, value = used)
227+
postMetric(compartment_ocid, "used", s_name, l_name, used, monitoring_client)
231228

232229
# Available
233-
postMetricAvail = postMetric(compartment_ocid = compartment_ocid, m_name = "used", s_name = s_name, l_name = l_name, value = limit_usage["available"])
230+
postMetric(compartment_ocid, "used", s_name, l_name, limit_usage["available"], monitoring_client)
234231

235232
# Finish:
236233
now = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")

0 commit comments

Comments
 (0)