azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Azure CLI Monitor 2.0 az monitor alert-rules create !! Can't. windowSize condition not accepted Azure CLI 2.0

Open jefffall opened this issue 9 years ago • 11 comments

Can't create an alert rule due to windowSize in conditions file being required up in the Azure cloud.

Yet the Python Azure CLI declares the Key windowSize as invalid.

az monitor alert-rules create --is-enabled IS_ENABLED --name JeffFallRuleTestCPU --resource-group HCM-Monitoring-RG --alert-rule-resource-name /subscriptions/my_subscription/resourceGroups/Monitoring-/providers/Microsoft.Compute/virtualMachines/myVM --location westus --condition @{condition} u'windowSize' Traceback (most recent call last): File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/main.py", line 36, in main cmd_result = APPLICATION.execute(args) File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/application.py", line 201, in execute result = expanded_arg.func(params) File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/commands/init.py", line 403, in _execute_command raise ex KeyError: u'windowSize'

Here is the conditions file I am using:

{ "odatatype": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition", "operator": "GreaterThan", "threshold": "90.0", "windowSize" : "PT5M" }

windowSize is required by the Azure Cloud Monitoring but rejected by the Azure CLI

Looks to be in these files:

[ZZZM00650289A:~/lib] me% find . | xargs grep -s "windowSize" * ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/location_threshold_rule_condition.py: 'window_size': {'key': 'windowSize', 'type': 'duration'}, Binary file ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/location_threshold_rule_condition.pyc matches ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/management_event_aggregation_condition.py: 'window_size': {'key': 'windowSize', 'type': 'duration'}, Binary file ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/management_event_aggregation_condition.pyc matches ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/threshold_rule_condition.py: 'windowSize': {'required': True}, ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/threshold_rule_condition.py: 'window_size': {'key': 'windowSize', 'type': 'duration'}, Binary file ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/threshold_rule_condition.pyc matches

jefffall avatar Apr 27 '17 19:04 jefffall

with this condition file: (this is the file listed as @{condition}

more condition

{ "odatatype": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition", "operator": "GreaterThan", "threshold": "90.0" }

I have removed the windowSize key and the request passes thru the Azure CLI 2.0 and the Azure Cloud rejects the request as shown:

This request does go up to the Azure cloud as there a measurable delay:

az monitor alert-rules create --is-enabled IS_ENABLED --name JeffFallRuleTestCPU --resource-group HCM-Monitoring-RG --alert-rule-resource-name /subscriptions/my_subscription/resourceGroups/my_resource_group/providers/Microsoft.Compute/virtualMachines/my_vm --location westus --condition @{condition} The WindowSize must be between 5 minutes and 1 day. (The line below comes back in RED) The WindowSize must be between 5 minutes and 1 day.

jefffall avatar Apr 27 '17 19:04 jefffall

Thank you @jefffall for your feedback.

@vishrutshah what do you think?

lmazuel avatar Apr 27 '17 20:04 lmazuel

It most likely needs to be "window_size"

tjprescott avatar Apr 27 '17 20:04 tjprescott

I believe this has been fixed, but it's a very new fix (which will convert the casing from camel to snake)

tjprescott avatar Apr 27 '17 20:04 tjprescott

tried window_size. Looking in the Python code - window_size seems to be what is used internally after the key windowSize.

But here goes a try:

the conditions file changed to:

{ "odatatype": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition", "operator": "GreaterThan", "threshold": "90.0", "window_size" : "PT5M" }

az monitor alert-rules create --is-enabled IS_ENABLED --name JeffFallRuleTestCPU --resource-group HCM-Monitoring-RG --alert-rule-resource-name /subscriptions/my_resourxe/resourceGroups/MonitoringGroup/providers/Microsoft.Compute/virtualMachines/hcm99mon01 --location westus --condition @{condition}

and the below line in RED Request content is not well formed or supported.

jefffall avatar Apr 27 '17 20:04 jefffall

Seems the key "windowSize" is required up at the Azure Cloud level.

The windowSize key is in the Python code and seems to convert to an internal variable of window_size of type duration.

When I use window_size in the conditions file in the conditions JSON - the conditions seems to parse OK at the Azure CLI 2.0 level and the request looks to go out to Azure as there is delay.

And I am thinking the error retuned form Azure Cloud is:

(in red)

Request content is not well formed or supported.

Really do appreciate your support on this issue.

jefffall avatar Apr 27 '17 20:04 jefffall

% find . | xargs grep -s "windowSize" * ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/location_threshold_rule_condition.py: 'window_size': {'key': 'windowSize', 'type': 'duration'}, Binary file ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/location_threshold_rule_condition.pyc matches ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/management_event_aggregation_condition.py: 'window_size': {'key': 'windowSize', 'type': 'duration'}, Binary file ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/management_event_aggregation_condition.pyc matches ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/threshold_rule_condition.py: 'windowSize': {'required': True}, ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/threshold_rule_condition.py: 'window_size': {'key': 'windowSize', 'type': 'duration'}, Binary file ./azure-cli/lib/python2.7/site-packages/azure/mgmt/monitor/models/threshold_rule_condition.pyc matches

Did some digging into the Python code

_validation = { 'odatatype': {'required': True}, 'failed_location_count': {'required': True}, 'window_size': {'required': True}, }

_attribute_map = {
    'odatatype': {'key': 'odata\\.type', 'type': 'str'},
    'data_source': {'key': 'dataSource', 'type': 'RuleDataSource'},
    'window_size': {'key': 'windowSize', 'type': 'duration'},
    'failed_location_count': {'key': 'failedLocationCount', 'type': 'int'},
}

def __init__(self, failed_location_count, data_source=None, window_size=None):
    super(LocationThresholdRuleCondition, self).__init__()
    self.data_source = data_source
    self.window_size = window_size
    self.failed_location_count = failed_location_count
    self.odatatype = 'Microsoft.Azure.Management.Insights.Models.LocationThresholdRuleCondition'

jefffall avatar Apr 27 '17 20:04 jefffall

Command arguments ['monitor', 'alert-rules', 'create', '--is-enabled', 'IS_ENABLED', '--name', 'JeffFallRuleTestCPU', '--resource-group', 'H-Monitoring-G', '--alert-rule-resource-name', '/subscriptions/supressed/resourceGroups/H-Monitoring-G/providers/Microsoft.Compute/virtualMachines/myvm', '--location', 'westus', '--condition', '@condition'] Current cloud config: {'endpoints': {'active_directory': 'https://login.microsoftonline.com', 'active_directory_graph_resource_id': 'https://graph.windows.net/', 'active_directory_resource_id': 'https://management.core.windows.net/', 'batch_resource_id': 'https://batch.core.windows.net/', 'gallery': 'https://gallery.azure.com/', 'management': 'https://management.core.windows.net/', 'resource_manager': 'https://management.azure.com/', 'sql_management': 'https://management.core.windows.net:8443/'}, 'is_active': True, 'name': 'AzureCloud', 'profile': 'latest', 'suffixes': {'azure_datalake_analytics_catalog_and_job_endpoint': 'azuredatalakeanalytics.net', 'azure_datalake_store_file_system_endpoint': 'azuredatalakestore.net', 'keyvault_dns': '.vault.azure.net', 'sql_server_hostname': '.database.windows.net', 'storage_endpoint': 'core.windows.net'}} Registered application event handler 'CommandTableParams.Loaded' at <function add_id_parameters at 0x1048af6e0> Registered application event handler 'CommandTable.Loaded' at <function add_id_parameters at 0x1048af6e0> Successfully loaded command table from module 'monitor'. Application event 'CommandTable.Loaded' with event data {'command_table': {'monitor alert-rules create': <azure.cli.core.commands.CliCommand object at 0x1048a0e10>}} Application event 'CommandParser.Loaded' with event data {'parser': AzCliCommandParser(prog='az', usage=None, description=None, version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)} Application event 'CommandTableParams.Loaded' with event data {'command_table': {'monitor alert-rules create': <azure.cli.core.commands.CliCommand object at 0x1048a0e10>}} Application event 'CommandParser.Parsed' with event data {'args': Namespace(_command_package='monitor', _jmespath_query=None, _log_verbosity_debug=False, _log_verbosity_verbose=False, _output_format='json', _parser=AzCliCommandParser(prog='az monitor alert-rules create', usage=None, description='Creates or updates an alert rule. Request method: PUT Request URI: https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/microsoft.insights/alertRules/{alert-rule-name}?api-version={api-ve [...] Getting management service client client_type=MonitorManagementClient msrest.pipeline : Adding 'log_request' callback before event: 'request' msrest.pipeline : Callback to overwrite original call: False msrest.pipeline : Adding 'log_response' callback after event: 'response' msrest.pipeline : Callback to overwrite original call: False u'windowSize' Traceback (most recent call last): File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/main.py", line 36, in main cmd_result = APPLICATION.execute(args) File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/application.py", line 201, in execute result = expanded_arg.func(params) File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/commands/init.py", line 403, in _execute_command raise ex KeyError: u'windowSize' [ZZZM00650289A:~] c5256381%

jefffall avatar Apr 27 '17 20:04 jefffall

Tried a little debugging

az monitor alert-rules create --is-enabled IS_ENABLED --name JeffFallRuleTestCPU --resource-group HCM-Monitoring-RG --alert-rule-resource-name /subscriptions/mysub/resourceGroups/H-Monitoring-G/providers/Microsoft.Compute/virtualMachines/myVM --location westus --condition @{condition} ----------------- debug kwargs ------------------- {'is_enabled': 'IS_ENABLED', 'resource_group_name': 'HCM-Monitoring-RG', 'description': None, 'parameters': <azure.mgmt.monitor.models.alert_rule_resource.AlertRuleResource object at 0x10a8dd9d0>, 'tags': None, 'actions': None, 'alert_rule_resource_name': '/subscriptions/3fd8391b-f9e7-4d54-bf96-f7fb9844c4c1/resourceGroups/HCM-Monitoring-RG/providers/Microsoft.Compute/virtualMachines/hcm99mon01', 'rule_name': 'JeffFallRuleTestCPU', 'location': 'westus', 'condition': {u'operator': u'GreaterThan', u'threshold': u'90.0', u'windowSize': u'PT5M', u'odatatype': u'Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition'}, 'name': None} u'windowSize' Traceback (most recent call last): File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/main.py", line 36, in main cmd_result = APPLICATION.execute(args) File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/application.py", line 201, in execute result = expanded_arg.func(params) File "/Users/c5256381/lib/azure-cli/lib/python2.7/site-packages/azure/cli/core/commands/init.py", line 407, in _execute_command raise ex KeyError: u'windowSize'

Seems that u'windowSize': u'PT5M', is passing in and then blows up as not in some Dict somewhere

jefffall avatar Apr 27 '17 21:04 jefffall

Hi @jefffall, suffice to say, this is a painful experience. Please use this as a template:

{
      "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
      "data_source": {
        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
        "resource_uri": "{your resource URI}",
        "metric_name": "Percentage CPU"
      },
      "operator": "GreaterThan",
      "threshold": 90,
      "window_size": "PT15M",
      "time_aggregation": "Average"
    }

You can consult here: https://docs.microsoft.com/en-us/rest/api/monitor/alertrules

In the latest update to CLI 2.0 the keys can use either this_format or thisFormat, but since you probably have an older version, use this_format for the keys.

tjprescott avatar Apr 27 '17 21:04 tjprescott

I opened an issue in the CLI repo https://github.com/Azure/azure-cli/issues/3052 since the fault lies within the CLI and not the SDK. We'll work to prioritize improving this experience, because it is just bad.

tjprescott avatar Apr 27 '17 21:04 tjprescott