azure-mgmt-monitor : MonitorManagementClient object has no attribute 'metric'
I am using the azure-mgmt.-monitor package in Azure runbooks (using python 2) in order to get the CPU utilization of VMs. When running my code I get the following error:
Failed Traceback (most recent call last): File "C:\Temp\gfk0cxyx.cab\ee7ce151-9b78-4920-aa4b-5f5e1ca6f0c5", line 150, in
main() File "C:\Temp\gfk0cxyx.cab\ee7ce151-9b78-4920-aa4b-5f5e1ca6f0c5", line 28, in main cpuDict = getCPU(credentials, sub_id, .....") File "C:\Temp\gfk0cxyx.cab\ee7ce151-9b78-4920-aa4b-5f5e1ca6f0c5", line 98, in getCPU cpuData = monitor_client.metric.list(resource_id, timespan="{}/{}".format(past,present),interval="PT1M",metric="Percentage CPU",aggregation="Average")AttributeError: 'MonitorManagementClient' object has no attribute 'metric'
This is the code that I use to get the metrics:
def getCPU(credentials, sub_id, resource_id):
monitor_client = MonitorManagementClient(credentials, str(sub_id))
past = datetime.now()-timedelta(hours=12)
present = datetime.now()
cpuData = monitor_client.metric.list(resource_id, timespan="{}/{}".format(past,present),interval="PT1M",metric="Percentage CPU",aggregation="Average")
I have no problems when using the other SDK packages such as compute and resource.
Any help is appreciated, Thank you
Under MonitorManagementClient, the attribute is actually titled 'metrics' and not 'metric' and the parameter name is 'metricnames' instead of 'metric'. Let me know whether after you switch your final line to
cpuData = monitor_client.metrics.list(resource_id, timespan="{}/{}".format(past,present),interval="PT1M",metricnames="Percentage CPU",aggregation="Average")
your code works.
You can find more samples here: https://docs.microsoft.com/python/api/overview/azure/monitoring
Thank you this has solved my error.
Hello, I am encountering this error now even using what I believe to be the attribute library. Here is the code snippet throwing the error in my python 2 runbook:
incoming_metrics_data = client.metrics.list(
resource_id,
timespan="{}/{}".format(start, end),
interval='PT1H',
metricnames='IncomingMessages',
filter="EntityName eq '" + tup[1] + "'"
)
I get the error:
Traceback (most recent call last): File "junk", line 79, in <module> incoming_metrics_data = client.metrics.list(AttributeError: 'MonitorManagementClient' object has no attribute 'metrics'
Is the package in runbooks out of date?
@fredxfred Me too,I did not see the attribute metrics for MonitorManagementClient
I got it working adding some python packages in the Automation Account.
azure_common - 1.1.23 - https://pypi.org/project/azure-common/ azure_mgmt_monitor - 0.7.0- https://pypi.org/project/azure-mgmt-monitor/ msrest - 0.6.10 - https://pypi.org/project/msrest/ msrestazure - 0.6.2 - https://pypi.org/project/msrestazure/ typing - 3.7.4.1 - https://pypi.org/project/typing/