Comments (6)
Please try the below, your code with small changes
I moved out the connection, added retry
import oci
from datetime import datetime
from datetime import timedelta
###############################
# Fetch Metric Data
###############################
def fetch_metrics_data(days, metric_names, monitoring_client, metric_aggregate):
data = []
start_date = datetime.now() - timedelta(days=days)
while start_date < datetime.now():
end_date = start_date + timedelta(days=1)
print(f"Running for {start_date.strftime('%Y-%m-%d')} - {end_date.strftime('%Y-%m-%d')}")
for metric_name in metric_names:
summarize_metrics_data_details = oci.monitoring.models.SummarizeMetricsDataDetails(
namespace="oci_computeagent",
start_time=start_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
end_time=min(end_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
query=f"{metric_name}[24h].{metric_aggregate}",
resolution='24h',
)
res = monitoring_client.summarize_metrics_data(
compartment_id=config['tenancy'],
summarize_metrics_data_details=summarize_metrics_data_details,
compartment_id_in_subtree=True,
retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY
).data
data.append(res)
start_date = end_date
return data
###############################
# Main
###############################
config_file = "~/.oci/config"
config = oci.config.from_file(config_file, "DEFAULT")
monitoring_client = oci.monitoring.MonitoringClient(config)
metric_names = ['NetworksBytesOut']
metrics_data = fetch_metrics_data(30, metric_names, monitoring_client, "avg()")
print(metrics_data)
from oci-python-sdk.
Here is an example, you can continue developing based on the documentation
Please test and close the ticket
import oci
from datetime import datetime
from datetime import timedelta
###############################
# Fetch Metric Data
###############################
def fetch_metrics_data(monitoring_client, namespace, metric_names, metric_aggregate, days):
data = []
start_date = datetime.now() - timedelta(days=days)
while start_date < datetime.now():
end_date = start_date + timedelta(days=1) + timedelta(seconds=1)
print(f"Running for {namespace} {start_date.strftime('%Y-%m-%d')} - {end_date.strftime('%Y-%m-%d')}")
for metric_name in metric_names:
summarize_metrics_data_details = oci.monitoring.models.SummarizeMetricsDataDetails(
namespace=namespace,
start_time=start_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
end_time=min(end_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
query=f"{metric_name}[24h].{metric_aggregate}",
resolution='24h',
)
res = monitoring_client.summarize_metrics_data(
compartment_id=config['tenancy'],
summarize_metrics_data_details=summarize_metrics_data_details,
compartment_id_in_subtree=True,
retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY
).data
data.append(res)
start_date = end_date
return data
###############################
# Main
###############################
metric_data = []
config_file = "~/.oci/config"
config = oci.config.from_file(config_file, "DEFAULT")
monitoring_client = oci.monitoring.MonitoringClient(config)
# Compute Metrics = https://docs.oracle.com/en-us/iaas/Content/Compute/References/computemetrics.htm#Availabl
metric_data += fetch_metrics_data(monitoring_client, "oci_computeagent", ['CpuUtilization','DiskBytesRead1','DiskBytesWritten1'], "avg()", 1)
# Volume Metrics = https://docs.oracle.com/en-us/iaas/Content/Block/References/volumemetrics.htm
metric_data += fetch_metrics_data(monitoring_client, "oci_blockstore", ['VolumeReadThroughput'], "avg()", 1)
# Exadata Database Metrics = https://docs.oracle.com/en-us/iaas/exadatacloud/exacs/metrics-for-exadata-database-service-on-dedicated-infrastructure-in-the-monitoring-service.html
metric_data += fetch_metrics_data(monitoring_client, "oci_database_cluster", ['CpuUtilization'], "avg()", 5)
for metric_array in metric_data:
for metric in metric_array:
for datapoint in metric.aggregated_datapoints:
resource = ""
pnamespace = metric.namespace
metric_name = metric.metadata['displayName']
if pnamespace == "oci_database_cluster":
resource = metric.dimensions['hostName']
elif pnamespace == "oci_computeagent":
resource = metric.dimensions['resourceDisplayName']
elif pnamespace == "oci_blockstore":
resource = metric.dimensions['resourceId']
shape = metric.dimensions['shape'] if 'shape' in metric.dimensions else ""
ptime = str(datapoint.timestamp)[0:10]
value = str(datapoint.value)
print(f"{pnamespace},{metric_name},{resource},{shape},{ptime},{value}")
from oci-python-sdk.
HI Gopichand,
Can you please give an example ? share the code you are using
Thank you
from oci-python-sdk.
when, I was querying the average, max, and min metrics I sometimes got the 429 error and 502 error. @adizohar can you help me to how can I fix those errors.
import oci def fetch_metrics_data(): config = oci.config.from_file() monitoring_client = oci.monitoring.MonitoringClient(config) start_date = datetime.now() - timedelta(days=30) metric_names = ['NetworksBytesOut'] # Adjust metric names as needed while start_date < datetime.now(): end_date = start_date + timedelta(days=1) for metric_name in metric_names: query_string = f"{metric_name}[24h].avg()" res = monitoring_client.summarize_metrics_data( compartment_id=config['tenancy'], summarize_metrics_data_details=oci.monitoring.models.SummarizeMetricsDataDetails( namespace="oci_computeagent", start_time=start_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), end_time=min(end_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), query=query_string, resolution='24h', ), compartment_id_in_subtree=True, ).data print(res) start_date = end_date return res # Example usage metrics_data = fetch_metrics_data() print(metrics_data)
** error
Traceback (most recent call last): File "/home/ubuntu/Desktop/oci-api-tests/DB/monitoring.py", line 51, in <module> metrics_data = fetch_metrics_data() File "/home/ubuntu/Desktop/oci-api-tests/DB/monitoring.py", line 34, in fetch_metrics_data res = monitoring_client.summarize_metrics_data( File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/monitoring/monitoring_client.py", line 2462, in summarize_metrics_data return self.base_client.call_api( File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/base_client.py", line 535, in call_api response = self.request(request, allow_control_chars, operation_name, api_reference_link) File "/home/ubuntu/.local/lib/python3.8/site-packages/circuitbreaker.py", line 159, in wrapper return call(function, *args, **kwargs) File "/home/ubuntu/.local/lib/python3.8/site-packages/circuitbreaker.py", line 170, in call return func(*args, **kwargs) File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/base_client.py", line 724, in request self.raise_transient_service_error(request, response, service_code, message, operation_name, api_reference_link, target_service, request_endpoint, client_version, timestamp, deserialized_data) File "/home/ubuntu/.local/lib/python3.8/site-packages/oci/base_client.py", line 906, in raise_transient_service_error raise exceptions.TransientServiceError( oci.exceptions.TransientServiceError: {'target_service': 'monitoring', 'status': 429, 'code': 'TooManyRequests', 'opc-request-id': '963A06315DE64B46B09B7E0BEF15F798/D184C7DEC18FA19744B7A1A193AAAF49/1D3BD7E43A052BC4C5F39268F1AC4F76', 'message': 'Maximum rate exceeded', 'operation_name': 'summarize_metrics_data', 'timestamp': '2024-04-24T21:07:22.151334+00:00', 'client_version': 'Oracle-PythonSDK/2.126.0', 'request_endpoint': 'POST https://telemetry.ap-mumbai-1.oraclecloud.com/20180401/metrics/actions/summarizeMetricsData', 'logging_tips': 'To get more info on the failing request, refer to https://docs.oracle.com/en-us/iaas/tools/python/latest/logging.html for ways to log the request/response details.', 'troubleshooting_tips': "See https://docs.oracle.com/iaas/Content/API/References/apierrors.htm#apierrors_429__429_toomanyrequests for more information about resolving this error. Also see https://docs.oracle.com/iaas/api/#/en/monitoring/20180401/MetricData/SummarizeMetricsData for details on this operation's requirements. If you are unable to resolve this monitoring issue, please contact Oracle support and provide them this full error message."}
HI Gopichand, Can you please give an example ? share the code you are using Thank you
@adizohar I have added the example, can you please look that.
from oci-python-sdk.
Thankyou @adizohar.
But how can we get Database metrics and volume metrics with the same API call?
@adizohar can you help me at the time how get Instance metrics, volume metrics and Database metrics.
from oci-python-sdk.
It is working fine thankyou @adizohar
from oci-python-sdk.
Related Issues (20)
- problems running in CLI OCI HOT 3
- Issue with accessing oci.container_instances.ContainerInstanceClient response data HOT 2
- volume backups are not getting all records in this list_volume_backups api call HOT 8
- Report with properties of PDBs HOT 4
- Error using FlowLogCaptureFilterRuleDetails
- Missing dependency on sseclient
- oci python SDK while using instance-principals, raising exceptions.ServiceError NotAuthorizedOrNotFound HOT 7
- a little typo in the documentation how to run CLI with help option $ python3 oci_capacity_reporter_gui.py -help HOT 1
- Error in log-analytics storage purge-storage-data HOT 4
- Focus report how we can get it. HOT 1
- call mysql_client_composite_ops.copy_backup_and_wait_for_state display AttributeError: 'DbBackupsClient' object has no attribute 'get_work_request'
- usageapi not working for ap-mumbai region HOT 8
- update python cryptography dependency again (42 -> 43)
- muscat region instance not reachable with the api_config parameters from python script. HOT 7
- Date for adding `response_format` from cohere api HOT 2
- Optimizer client api givinig wrong data(list resource action api) HOT 8
- ShowSubscription - 401 - Not Authorized HOT 14
- implementation of price calculator in application HOT 1
- Upgrade to use python package cryptography 43 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oci-python-sdk.