C#接入阿里云ECSAPI实现系统服务器运维

1.前言

最近在做服务的运维,之前有说过如何对服务器上的程序进程进行管理,参考《C#对接supervisor XML-RPC API 实现进程控制》,今天说一下如何对接阿里云的ECS实现系统对服务器的控制,完善系统自运维的服务。

2.阿里云SDK的对接

2.1.添加SDK包

C#接入阿里云ECSAPI实现系统服务器运维_第1张图片

在NuGet的面板找到AlibabaCloud的程序库,进行安装

C#接入阿里云ECSAPI实现系统服务器运维_第2张图片

 2.2.实现一个SDK对接的封装类

public class AlibabaCloudSDK
    {
        /**
         * 使用AK&SK初始化账号Client
         * @param accessKeyId
         * @param accessKeySecret
         * @return Client
         * @throws Exception
         */
        public static AlibabaCloud.SDK.Cms20190101.Client CreateClient(string accessKeyId, string accessKeySecret,string url)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = accessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = accessKeySecret,
            };
            // 访问的域名
            config.Endpoint = url;
            return new AlibabaCloud.SDK.Cms20190101.Client(config);
        }

        /// 
        /// 接口公共方法封装
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string describeMetricLast(string url,string metricName,string period,string startTime,string endTime,string dimensions)
        {
            AlibabaCloud.SDK.Cms20190101.Client client = CreateClient(AliyunContant.ACCESS_KEY_ID, AliyunContant.ACCESS_KEY_SECRET, url);
            AlibabaCloud.SDK.Cms20190101.Models.DescribeMetricListRequest describeMetricListRequest = new AlibabaCloud.SDK.Cms20190101.Models.DescribeMetricListRequest
            {
                Namespace = "acs_ecs_dashboard",
                MetricName = metricName,
                Period = period,
                StartTime = startTime,
                EndTime = endTime,
                Dimensions = dimensions
            };
            AlibabaCloud.SDK.Cms20190101.Models.DescribeMetricListResponse resp = client.DescribeMetricList(describeMetricListRequest);
            return AlibabaCloud.TeaUtil.Common.ToJSONString(resp);
        }

        /// 
        /// 接口公共方法封装
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string describeMetricLast(string url, string metricName, int period, string dimensions)
        {
            DateTime startTime = DateTime.UtcNow;
            DateTime endTime = DateTime.UtcNow;
            if (period == 15)
            {
                startTime = endTime.AddMinutes(-15);
            }
            else {
                startTime = endTime.AddHours(-1.5);
            }
            AlibabaCloud.SDK.Cms20190101.Client client = CreateClient(AliyunContant.ACCESS_KEY_ID, AliyunContant.ACCESS_KEY_SECRET, url);
            AlibabaCloud.SDK.Cms20190101.Models.DescribeMetricLastRequest describeMetricLastRequest = new AlibabaCloud.SDK.Cms20190101.Models.DescribeMetricLastRequest
            {
                Namespace = "acs_ecs_dashboard",
                MetricName = metricName,
                Period = period.ToString(),
                StartTime = startTime.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                EndTime = endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                Dimensions = dimensions
            };
            AlibabaCloud.SDK.Cms20190101.Models.DescribeMetricLastResponse resp = client.DescribeMetricLast(describeMetricLastRequest);
            return AlibabaCloud.TeaUtil.Common.ToJSONString(resp.Body);
        }

        /// 
        /// 报警汇总方法
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string describeAlertLogCount(string url, long startTime, long endTime)
        {
            AlibabaCloud.SDK.Cms20190101.Client client = CreateClient(AliyunContant.ACCESS_KEY_ID, AliyunContant.ACCESS_KEY_SECRET, url);
            AlibabaCloud.SDK.Cms20190101.Models.DescribeAlertLogCountRequest describeAlertLogCountRequest = new AlibabaCloud.SDK.Cms20190101.Models.DescribeAlertLogCountRequest
            {
                Namespace = "acs_ecs_dashboard",
                GroupBy = "product,metricName",
                StartTime = startTime,
                EndTime = endTime,
                Product = "ECS"
            };
            AlibabaCloud.SDK.Cms20190101.Models.DescribeAlertLogCountResponse resp = client.DescribeAlertLogCount(describeAlertLogCountRequest);
            return AlibabaCloud.TeaUtil.Common.ToJSONString(resp);
        }

        /// 
        /// 按照云服务和监控项统计报警历史
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string describeAlertLogList(string url, long startTime, long endTime)
        {
            AlibabaCloud.SDK.Cms20190101.Client client = CreateClient(AliyunContant.ACCESS_KEY_ID, AliyunContant.ACCESS_KEY_SECRET, url);
            AlibabaCloud.SDK.Cms20190101.Models.DescribeAlertLogListRequest describeAlertLogListRequest = new AlibabaCloud.SDK.Cms20190101.Models.DescribeAlertLogListRequest
            {
                Namespace = "acs_ecs_dashboard",
                StartTime = startTime,
                EndTime = endTime,
                Product = "ECS"
            };
            AlibabaCloud.SDK.Cms20190101.Models.DescribeAlertLogListResponse resp = client.DescribeAlertLogList(describeAlertLogListRequest);
            return AlibabaCloud.TeaUtil.Common.ToJSONString(resp);
        }


        /**
        * 使用AK&SK初始化账号Client
        * @param accessKeyId
        * @param accessKeySecret
        * @return Client
        * @throws Exception
        */
        public static AlibabaCloud.SDK.Ecs20140526.Client CreateECSClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = accessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = accessKeySecret,
            };
            // 访问的域名
            config.Endpoint = "ecs.aliyuncs.com";
            return new AlibabaCloud.SDK.Ecs20140526.Client(config);
        }

        /// 
        /// 查询ECS实例详情
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string describeInstances(string regionId,string instanceIds)
        {
            AlibabaCloud.SDK.Ecs20140526.Client client = CreateECSClient(AliyunContant.ACCESS_KEY_ID, AliyunContant.ACCESS_KEY_SECRET);
            AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesRequest describeInstancesRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesRequest
            {
                RegionId = regionId,
                InstanceIds = instanceIds
            };
            AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesResponse resp = client.DescribeInstances(describeInstancesRequest);
            return AlibabaCloud.TeaUtil.Common.ToJSONString(resp);
        }
    }

 可以根据需要将对ECS的操作方法封装成一个公共类

3.编写实现方法对接口提供操作实现类

public class CloudMonitor
    {
        /// 
        /// 获取一个ECS的详细信息
        /// 
        /// 监控地址
        /// 区域ID
        /// 实例IDs
        /// 
        public string getDescribeInstances(string regionId, string instanceIds)
        {
            List instanceIdList = instanceIds.Split(',').ToList();
            string jsonInstanceIds = JsonConvert.SerializeObject(instanceIdList);
            return AlibabaCloudSDK.describeInstances(regionId, jsonInstanceIds);
        }

        #region 基础监控项目
        /// 
        /// 实例云盘读BPS(Byte/s)
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getDiskReadBPS(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return  AlibabaCloudSDK.describeMetricLast(url, "DiskReadBPS", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 实例云盘写BPS(Byte/s)
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getDiskWriteBPS(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "DiskWriteBPS", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 实例云盘读IOPS(次/s)
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getDiskReadIOPS(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "DiskReadIOPS", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 实例云盘写IOPS(次/s)
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getDiskWriteIOPS(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "DiskWriteIOPS", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 经典网络-公网流入带宽
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getInternetInRate(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "InternetInRate", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 经典网络-公网流出带宽
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getInternetOutRate(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "InternetOutRate", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 经典网络-公网流出带宽使用率
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getInternetOutRatePercent(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "InternetOutRate_Percent", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 内网流入流量
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getIntranetInRate(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "IntranetInRate", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 内网流出流量
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getIntranetOutRate(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "IntranetOutRate", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }
        
        /// 
        /// 专有网络-公网流入带宽
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getVPCPublicIPInternetInRate(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "VPC_PublicIP_InternetInRate", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 专有网络-公网流出带宽
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getVPCPublicIPInternetOutRate(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "VPC_PublicIP_InternetOutRate", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 专有网络-公网流出带宽使用率
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getVPCPublicIPInternetOutRatePercent(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "VPC_PublicIP_InternetOutRate_Percent", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 获取当前的连接数量
        /// 
        /// 
        /// 
        /// 
        /// 默认60,可取60的整数倍
        /// 
        /// 
        /// 
        public string getConcurrentConnections(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "concurrentConnections", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 入方向前后端丢包率
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getPacketInDropRates(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "packetInDropRates", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 出方向前后端丢包率
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getPacketOutDropRates(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "packetOutDropRates", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }
        #endregion

        #region 操作系统级别监控项
        /// 
        /// CPU使用率
        /// 
        /// 请求地址
        /// 服务器实例id
        /// 空间命名
        /// 默认15,可取15的整数倍
        /// 开始时间
        /// 结束时间
        /// 
        public string getCPUTotal(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "cpu_total", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }
        
        /// 
        /// 内存使用率
        /// 
        /// 请求地址
        /// 服务器实例id
        /// 空间命名
        /// 时间间隔。单位:秒。取值:60、300、900
        /// 开始时间
        /// 结束时间
        /// 
        public string getMemoryUsedUtilization(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "memory_usedutilization", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 内存已用量
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getMemoryUsedSpace(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "memory_usedspace", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 内存总量
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getMemoryTotalSpace(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "memory_totalspace", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }
        
        /// 
        /// 云盘使用率
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getDiskUsageUtilization(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "diskusage_utilization", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 磁盘的已用存储空间
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getDiskUsageUsed(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "diskusage_used", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 磁盘的总存储空间
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getDiskUsageTotal(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "diskusage_total", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 过去1分钟的系统平均负载
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getload1m(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "load_1m", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 过去5分钟的系统平均负载
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getload5m(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "load_5m", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 过去15分钟的系统平均负载
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getload15m(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "load_15m", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 进程数
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getProcessNumber(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "process.number", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 各种状态下的TCP连接数包括
        /// LISTEN、SYN_SENT、ESTABLISHED、SYN_RECV、FIN_WAIT1、CLOSE_WAIT、FIN_WAIT2、LAST_ACK、TIME_WAIT、CLOSING和CLOSED
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getNetTcpConnection(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "net_tcpconnection", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 设备驱动器检测到的接收错误包的数量
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getNetworkInErrorPackages(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "networkin_errorpackages", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// 设备驱动器检测到的发送错误包的数量
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getNetWorkOutErrorPackages(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "networkout_errorpackages", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }

        /// 
        /// inode使用率,Linux操作系统才有意义
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getInodeutilization(string url, string instanceId, int period, DateTime startTime, DateTime endTime)
        {
            Dictionary dicDimensions = new Dictionary();
            dicDimensions.Add("instanceId", instanceId);
            string dimensions = JsonConvert.SerializeObject(dicDimensions);
            return AlibabaCloudSDK.describeMetricLast(url, "fs_inodeutilization", period.ToString(),
                General.Instance.GetTimeStamp(startTime).ToString(), General.Instance.GetTimeStamp(endTime).ToString(), dimensions);
        }
        #endregion

        /// 
        /// 按照云服务和监控项统计报警历史
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getDescribeAlertLogCount(string url, DateTime startTime, DateTime endTime)
        {
            return AlibabaCloudSDK.describeAlertLogCount(url, General.Instance.GetTimeStamp(startTime), General.Instance.GetTimeStamp(endTime));
        }
        
        /// 
        /// 按照云服务和监控项统计报警历史
        /// 
        /// 
        /// 
        /// 
        /// 
        public string getDescribeAlertLogList(string url, DateTime startTime, DateTime endTime)
        {
            return AlibabaCloudSDK.describeAlertLogList(url, General.Instance.GetTimeStamp(startTime), General.Instance.GetTimeStamp(endTime));
        }
    }

最后编写WebApi,提供给前端或者APP使用

4.最终实现效果

C#接入阿里云ECSAPI实现系统服务器运维_第3张图片

C#接入阿里云ECSAPI实现系统服务器运维_第4张图片

 

 

你可能感兴趣的:(运维,C#,服务器,阿里云,自动化运维)