Installation of Azure Monitoring libraries for python
Sometimes you need a “strong” tool for Azure management and Azure Monitoring. In this case you have the Azure Monitoring libraries for python which could be installed with pip install azure-mgmt-monitor
. Then when you are lucky you can use it immediately (MS documentation here). What happens if you are facing an unexpected issue when you would like to use this beautiful opportunity? You start to find the solution…
Situation
Here I would like to show you a strange situation…and its solution. 🙂
I wanted to manage the Azure subscription related metrics and alerts from Python. I have started the discovering of capabilities of this area and I am facing an serious issue. (I was following this article: Azure Monitoring libraries for python)
After this line I receive an error message.
for metric in client.metric_definitions.list(resource_id): # azure.monitor.models.MetricDefinition print("{}: id={}, unit={}".format( metric.name.localized_value, metric.name.value, metric.unit ))
The error message is this:
AttributeError: 'MonitorManagementClient' object has no attribute 'metric_definitions'
Investigation
Then I checked the client object and I realized there is not “metric_definitions” in “MonitorManagementClient”
>>> dir(client) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_client', '_deserialize', '_serialize', 'activity_log_alerts', 'alert_rule_incidents', 'alert_rules', 'autoscale_settings', 'config', 'log_profiles', 'service_diagnostic_settings']
And I checked the installed package version of “azure-mgmt-monitor” with pip freeze
(result: azure-mgmt-monitor==0.4.0) and print(azure.mgmt.monitor.__version__)
(result: 0.2.1)
Solution
After some hours I had decided I will fully reinstall the package…and at that point I have found the solution. Both packages were installed to my computer. This situation caused the unexpected error.
To fix this issue you just need to follow these steps:
- Check the version of packages:
pip freeze
(result: azure-mgmt-monitor==0.4.0) - Uninstall the whole “azure-mgmt-monitor” package:
pip uninstall azure-mgmt-monitor
- Check the version of packages again:
pip freeze
(result: azure-mgmt-monitor==0.2.1) - Uninstall the whole “azure-mgmt-monitor” package again:
pip uninstall azure-mgmt-monitor
- The “azure-mgmt-monitor” was gone now.
- Reinstall “azure-mgmt-monitor” package:
pip install azure-mgmt-monitor
- Check the version of packages again:
pip freeze
(result: azure-mgmt-monitor==0.4.0)
Checking
Finally I check the “metric_definitions” attribute at “MonitorManagementClient” object.
>>> dir(client) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_client', '_deserialize', '_serialize', 'action_groups', 'activity_log_alerts', 'activity_logs', 'alert_rule_incidents', 'alert_rules', 'autoscale_settings', 'config', 'diagnostic_settings', 'diagnostic_settings_category', 'event_categories', 'log_profiles', 'metric_definitions', 'metrics', 'operations', 'tenant_activity_logs']
This means we win and the required version of “azure-mgmt-monitor” is available from now.
Now you can list, create and modify all of your resources related metrics and alerts with python…
You can find the details of this investigation on Github