/// <summary> /// 批量查询指定时间范围内指定指标的指定粒度的监控数据,目前最多支持10指标的批量查询。 /// </summary> /// <param name="cesV1Client"></param> private static void BatchListMetricData(CesClient cesClient) { BatchListMetricDataRequest batchShowMetricDataRequest = new BatchListMetricDataRequest() { Body = new BatchListMetricDataRequestBody() { Metrics = new List <MetricInfo>() { new MetricInfo() { Namespace = "SYS.ECS", Dimensions = new List <MetricsDimension>() { new MetricsDimension() { Name = "instance_id", Value = "1639371e-066f-4782-924c-40bd2db3ba07" } }, MetricName = "cpu_util" } }, From = 1587014357000, To = 1587017957000, Period = "300", Filter = "average" } }; try { BatchListMetricDataResponse batchListMetricDataResponse = cesClient.BatchListMetricData(batchShowMetricDataRequest); foreach (var batchMetricData in batchListMetricDataResponse.Metrics) { foreach (var datapoint in batchMetricData.Datapoints) { Console.WriteLine(datapoint.ToString()); } } } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
/// <summary> /// 添加一条或多条自定义指标监控数据,解决系统监控指标不能满足具体业务需求的场景。 /// </summary> /// <param name="cesV1Client"></param> private static void CreateMetricData(CesClient cesClient) { CreateMetricDataRequest createMetricDataRequest = new CreateMetricDataRequest() { Body = new List <MetricDataItem>() { new MetricDataItem() { Metric = new MetricInfo() { Namespace = "AGT.ECS", Dimensions = new List <MetricsDimension>() { new MetricsDimension() { Name = "instance_id", Value = "1639371e-066f-4782-924c-40bd2db3ba07" } }, MetricName = "mem_usedPercent" }, Ttl = 172800, CollectTime = 1587017957000, Type = "float", Value = 10.45, Unit = "%" } } }; try { CreateMetricDataResponse createMetricDataResponse = cesClient.CreateMetricData(createMetricDataRequest); Console.WriteLine(createMetricDataResponse.HttpStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
static void Main1(string[] args) { const string ak = "{your ak string}"; const string sk = "{your sk string}"; const string endpoint = "{your endpoint}"; const string projectId = "{your project id}"; var config = HttpConfig.GetDefaultConfig(); config.IgnoreSslVerification = true; var auth = new BasicCredentials(ak, sk, projectId); var cesClient = CesClient.NewBuilder() .WithCredential(auth) .WithEndPoint(endpoint) .WithHttpConfig(config).Build(); CreateEvents(cesClient); }
/// <summary> /// 查询指定时间范围指定指标的指定粒度的监控数据,可以通过参数指定需要查询的数据维度。 /// </summary> /// <param name="cesV1Client"></param> private static void ShowMetricData(CesClient cesClient) { ShowMetricDataRequest showMetricDataRequest = new ShowMetricDataRequest() { Namespace = "SYS.ECS", MetricName = "cpu_util", Dim0 = "instance_id,1639371e-066f-4782-924c-40bd2db3ba07", //Dim1 = "", //Dim2 = "", From = 1587014357000, To = 1587017957000, Period = 1, Filter = ShowMetricDataRequest.FilterEnum.AVERAGE }; try { ShowMetricDataResponse showMetricDataResponse = cesClient.ShowMetricData(showMetricDataRequest); foreach (var datapoint in showMetricDataResponse.Datapoints) { Console.WriteLine(datapoint.ToString()); } } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
/// <summary> /// 查询系统当前可监控指标列表,可以指定指标命名空间、指标名称、维度、排序方式,起始记录和最大记录条数过滤查询结果。 /// </summary> /// <param name="cesV1Client"></param> private static void ListMetrics(CesClient cesClient) { ListMetricsRequest listMetricsRequest = new ListMetricsRequest() { MetricName = "cpu_util", Namespace = "SYS.ECS", // Dim0 = "", // Dim1 = "", // Dim2 = "", // Start = "", Limit = 100, Order = ListMetricsRequest.OrderEnum.ASC }; try { ListMetricsResponse metricList = cesClient.ListMetrics(listMetricsRequest); foreach (var metric in metricList.Metrics) { Console.WriteLine(metric.ToString()); } } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
/// <summary> /// 查询指定时间范围指定事件类型的主机配置数据,可以通过参数指定需要查询的数据维度。 /// 注意:该接口提供给HANA场景下SAP Monitor查询主机配置数据,其他场景下查不到主机配置数据。 /// </summary> /// <param name="cesV1Client"></param> private static void ShowEventData(CesClient cesClient) { ShowEventDataRequest showEventDataRequest = new ShowEventDataRequest() { Namespace = "SYS.ECS", Dim0 = "instance_id,22d98f6c-16d2-4c2d-b424-50e79d82838f", //Dim1 = "", //Dim2 = "", Type = "instance_host_info", From = 1587014357000, To = 1587017957000, }; try { ShowEventDataResponse showEventDataResponse = cesClient.ShowEventData(showEventDataRequest); foreach (var eventDataInfo in showEventDataResponse.Datapoints) { Console.WriteLine(eventDataInfo.ToString()); } } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
/// <summary> /// 上报自定义事件。 /// </summary> /// <param name="cesClient"></param> private static void CreateEvents(CesClient cesClient) { CreateEventsRequest createEventsRequest = new CreateEventsRequest() { Body = new List <EventItem>() { new EventItem() { EventName = "ces_sdk_005", EventSource = "SYS.ECS", Time = 1587017957000, Detail = new EventItemDetail() { Content = "The ECS alarm", //GroupId = "rg15845342115453jqdPjrxA", ResourceId = "50596679-553d-4280-80a5-1919b117e4cc", ResourceName = "ecs-agent-nodelete", EventState = EventItemDetail.EventStateEnum.NORMAL, EventLevel = EventItemDetail.EventLevelEnum.MAJOR, EventUser = "******" } }, new EventItem() { EventName = "ces_sdk_006", EventSource = "SYS.ECS", Time = 1587017957000, Detail = new EventItemDetail() { Content = "The ECS alarm", //GroupId = "rg15845342115453jqdPjrxA", ResourceId = "326277f5-ee01-4751-959e-8e9a364b6b3c", ResourceName = "ecs-zhanfan1129", EventState = EventItemDetail.EventStateEnum.NORMAL, EventLevel = EventItemDetail.EventLevelEnum.MAJOR, EventUser = "******" } } } }; try { CreateEventsResponse createEventsResponse = cesClient.CreateEvents(createEventsRequest); foreach (var createEventsResponseBody in createEventsResponse.Body) { Console.WriteLine(createEventsResponseBody.ToString()); } } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
/// <summary> /// 创建一条告警规则。 /// </summary> /// <param name="cesClient"></param> private static void CreateAlarm(CesClient cesClient) { CreateAlarmRequest createAlarmRequest = new CreateAlarmRequest() { Body = new CreateAlarmRequestBody() { AlarmName = "ces-sdk-02", AlarmDescription = "create ECS alram", Metric = new MetricForAlarm { Namespace = "AGT.ECS", Dimensions = new List <MetricsDimension>() { new MetricsDimension() { Name = "instance_id", Value = "326277f5-ee01-4751-959e-8e9a364b6b3c" } }, MetricName = "cpu_usage_idle" }, Condition = new Condition() { Period = 1, Filter = "average", ComparisonOperator = "<", Value = 20, Unit = "%", Count = 3 }, AlarmEnabled = true, AlarmActionEnabled = true, AlarmLevel = 2, AlarmActions = new List <AlarmActions>() { new AlarmActions() { Type = "notification", NotificationList = new List <string>() { "urn:smn:br-iaas-odin1:0605767fc300d5762ffdc01c5bba0cce:ces_test" } } }, OkActions = new List <AlarmActions>() { new AlarmActions() { Type = "notification", NotificationList = new List <string>() { "urn:smn:br-iaas-odin1:0605767fc300d5762ffdc01c5bba0cce:ces_test" } } }, InsufficientdataActions = new List <AlarmActions>() { new AlarmActions() { Type = "notification", NotificationList = new List <string>() { "urn:smn:br-iaas-odin1:0605767fc300d5762ffdc01c5bba0cce:ces_test" } } } } }; try { CreateAlarmResponse createAlarmResponse = cesClient.CreateAlarm(createAlarmRequest); Console.WriteLine("alarm_id: {0}", createAlarmResponse.AlarmId); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }