Ejemplo n.º 1
0
        private static void Main(string[] args)
        {
            _subscriptionId    = args[0];
            _thumbprint        = args[1];
            _alertEmailAddress = args[2];
            _cloudServiceName  = args[3];
            _deploymentName    = args[4];

            SubscriptionCloudCredentials credentials = new CertificateCloudCredentials(_subscriptionId,
                                                                                       GetStoreCertificate(_thumbprint));

            var metricsClient = new MetricsClient(credentials);

            var resourceId = ResourceIdBuilder.BuildCloudServiceResourceId(_cloudServiceName, _deploymentName);

            Console.WriteLine("Resource Id: {0}", resourceId);

            GetMetricDefinitions(metricsClient, resourceId);

            var alertsClient = new AlertsClient(credentials);

            DisplayAzureAlertRules(alertsClient);

            var response = CreateAzureAlert(resourceId, alertsClient);

            Console.WriteLine("Create alert rule response: " + response.Result.StatusCode);
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private static Task <OperationResponse> CreateAzureAlert(string resourceId, AlertsClient alertsClient)
        {
            var rule = new Rule
            {
                Name        = "CPU Alert",
                Id          = Guid.NewGuid().ToString(),
                Description = "If CPU usage is greater than twenty percent then alert",
                IsEnabled   = true,
                Condition   = new ThresholdRuleCondition
                {
                    Operator   = ConditionOperator.GreaterThan,
                    Threshold  = 10,
                    WindowSize = TimeSpan.FromMinutes(15), //new TimeSpan(0,0,5,0),
                    DataSource = new RuleMetricDataSource
                    {
                        MetricName      = "Percentage CPU",
                        ResourceId      = resourceId,
                        MetricNamespace = string.Empty //"WindowsAzure.Availability"
                    }
                }
            };

            RuleAction action = new RuleEmailAction
            {
                SendToServiceOwners = true
            };

            ((RuleEmailAction)action).CustomEmails.Add(_alertEmailAddress);
            rule.Actions.Add(action);
            rule.LastUpdatedTime = DateTime.UtcNow;

            var response = alertsClient.Rules.CreateOrUpdateAsync(new RuleCreateOrUpdateParameters
            {
                Rule = rule
            }, CancellationToken.None);

            return(response);
        }
Ejemplo n.º 3
0
        private static void DisplayAzureAlertRules(AlertsClient alertsClient)
        {
            // Get the alert rules.
            var rulesResponse = alertsClient.Rules.List();
            var rules         = rulesResponse.Value;

            // Display the alert rules.
            foreach (var rule in rules)
            {
                var ruleCondition = rule.Condition as ThresholdRuleCondition;
                var metricDs      = ruleCondition.DataSource as RuleMetricDataSource;

                Console.WriteLine("Name: " + rule.Name);
                Console.WriteLine("Description: " + rule.Description);
                Console.WriteLine("Is enabled: " + rule.IsEnabled);
                Console.WriteLine("Last updated: " + rule.LastUpdatedTime);
                Console.WriteLine("Operator: " + ruleCondition.Operator
                                  + " Threshold: " + ruleCondition.Threshold + " Metric name: " + metricDs.MetricName);
                Console.WriteLine("Metric Namespace: " + metricDs.MetricNamespace);
                Console.WriteLine("ResourceId: " + metricDs.ResourceId);
                Console.WriteLine("");
            }
        }