文章目录
前言
在最近工作中,涉及到需要统计京东云云服务器资源信息并进行汇总,但是在京东云的云服务器导出资源界面中只能导出云服务器的cpu、内存、实例id、创建时间、所属区域、主机名、ipv4地址等,如果需要磁盘信息,还需要重新导一份磁盘信息表,但是磁盘信息表中只有磁盘实例ID、磁盘大小等信息,还需要自行使用execl的vslookup函数进行对比,将每台服务器对应的磁盘信息获取出来。当服务器上百台的时候这个操作极大的降低了运维效率,使得工作复杂化,因此,写了一个golang调用京东云sdk的脚本,实现获取服务器的ID、ipv4、cpu、mem、系统盘、数据盘大小并保存到本地的execl文件中。
提示:以下是本篇文章正文内容,下面是使用到的golang脚本
一、本地开发环境安装京东云sdk及execl库
go get github.com/jdcloud-api/jdcloud-sdk-go/core github.com/gofrs/uuid
go get github.com/xuri/excelize/v2
二、golang脚本解读
1.初始化京东云VM客户端
// 1 初始化JD Cloud VM客户端
func initVmClient() *VmClient {
accessKey := "xxxx" //京东云账户的ak
secretKey := "xxx" //京东云账户的sk
credentials := NewCredentials(accessKey, secretKey) //采用NewCredentials函数来实例化credentials对象
return NewVmClient(credentials) //调用 NewVmClient(credentials) 来创建并返回一个新的虚拟机服务客户端,这个客户端可能被设计用来与京东云的虚拟机服务进行交互
2.获取云服务器信息
func getAllInstances(client *VmClient) ([]vm.Instance, error) { //定义getAllInstances函数并接收一个client参数,返回一个切片类型的vm.Instance用于保存所有主机信息及错误
pageNumber := 1 //页码
pageSize := 50 //每页数量
totalCount := 0 //总计
instanceList := make([]vm.Instance, 0) //采用make初始化一个vm.Instance类型的切片,且长度为0
op := "eq" //用于设置过滤条件的变量,这里设置了一个过滤条件,只取状态为 "running" 的主机。
filter := models.Filter{
Name: "status", Operator: &op, Values: []string{"running"},
} //设置过滤字段 只取status=running的主机
//循环+分页获取云主机总数并保存到切片instanceList中
for {
//NewDescribeInstancesRequestWithAllParams(regionId string, pageNumber *int, pageSize *int, tags []models.TagFilter, filters []models.Filter)
req := NewDescribeInstancesRequestWithAllParams("cn-north-1", nil, nil, []vm.TagFilter{}, []models.Filter{filter}) //在循环中创建了一个 NewDescribeInstancesRequestWithAllParams 请求对象,用于描述带有所有参数的实例请求,其中传入了地域ID、页码、页大小和过滤条件。
req.SetPageNumber(pageNumber) //设置分页
req.SetPageSize(pageSize)
resp, err := client.DescribeInstances(req) //调用 client.DescribeInstances(req) 向京东云发送请求,并接收响应。
if err != nil || resp.Error.Code != 0 { //判断响应是否正常
return nil, fmt.Errorf("describe instance failed. %v, %v, %v", err, resp.Error.Code, resp.Error.Message)
}
//获取返回的实例总数
totalCount = resp.Result.TotalCount
//检查是否已经获取了所有实例,如果是则跳出循环,否则增加页码继续获取实例信息。
instanceList = append(instanceList, resp.Result.Instances...)
if len(instanceList) >= totalCount {
break
}
pageNumber++
}
return instanceList, nil // 最终返回获取到的实例列表 instanceList 和一个空的错误对象,表示成功获取实例列表
}
3.获取主机信息并写入本地的execl文件
func writeInstancesToExcel(instanceList []vm.Instance) error {
file := excelize.NewFile() //创建一个文件对象
sheetName := "Instances" //设置sheet名
index, _ := file.NewSheet(sheetName) //新创建一个sheet页
// 写入表头
headers := []string{"Instance ID", "Private IP", "System Disk Size", "Data Disk Sizes", "CPU", "Memory"}
for i, header := range headers {
cell := fmt.Sprintf("%s%d", string('A'+i), 1)
file.SetCellValue(sheetName, cell, header) //设置表头信息
}
// 写入主机信息
for i, instance := range instanceList {
rowIndex := i + 2 // 行索引从2开始,因为第一行是表头
instanceId := instance.InstanceId //获取服务器ID
instanceIp := instance.PrivateIpAddress //获取服务器ip
instanceSystemDisk := fmt.Sprintf("%d GB", instance.SystemDisk.CloudDisk.DiskSizeGB) //系统盘
instanceDataDisks := "" //数据盘
for _, disk := range instance.DataDisks { //根据最外层循环获取每一个实例对应的数据盘信息instance.DataDisks是一个切片类型
instanceDataDisks += fmt.Sprintf("%d GB", disk.CloudDisk.DiskSizeGB) //拿到数据盘大小
//fmt.Println(instanceDataDisks) //输出 :'100 GB'
}
if len(instanceDataDisks) > 0 { //判断当前实例的数据盘是否大于0,如果大于0,则赋值给定义的字符串instanceDataDisks
instanceDataDisks = instanceDataDisks[:]
}
//instance.InstanceType 用于获取实例的类型或规格信息。通常情况下,实例的类型会包括CPU、内存、存储等配置信息
instanceType := instance.InstanceType
cpu, memory := getInstanceSpecs(instanceType) //调用getInstanceSpecs函数。这个函数定义了服务的实例规格
instanceCpu := fmt.Sprintf("%d Core", cpu) //cpu
instanceMemory := fmt.Sprintf("%d GB", memory) //内存
//按照对应的列写入对应的数据到execl文件中
file.SetCellValue(sheetName, fmt.Sprintf("A%d", rowIndex), instanceId)
file.SetCellValue(sheetName, fmt.Sprintf("B%d", rowIndex), instanceIp)
file.SetCellValue(sheetName, fmt.Sprintf("C%d", rowIndex), instanceSystemDisk)
file.SetCellValue(sheetName, fmt.Sprintf("D%d", rowIndex), instanceDataDisks)
file.SetCellValue(sheetName, fmt.Sprintf("E%d", rowIndex), instanceCpu)
file.SetCellValue(sheetName, fmt.Sprintf("F%d", rowIndex), instanceMemory)
}
//保存sheet页
file.SetActiveSheet(index)
err := file.SaveAs("instances.xlsx")
if err != nil {
return fmt.Errorf("failed to save Excel file: %v", err)
}
return nil
}
4.获取主机规格
以下规格均来自京东云文档手册,详情见京东云文档
`https://docs.jdcloud.com/cn/virtual-machines/instance-type-family`
不同规格对应不同的cpu、mem
`此处只写了一部分规则信息,并不完全但够用,如果不包含请查看上述文档连接进行自行添加即可`
func getInstanceSpecs(instanceType string) (int, int) {
// 这里可以根据实际情况查询实例规格与CPU、内存的对应关系
switch instanceType {
case "g.s1.micro":
return 1, 1
case "g.s1.small":
return 1, 2
case "g.s2.micro":
return 1, 1
case "g.s2.small":
return 1, 2
case "g.s2.medium":
return 1, 4
case "g.s2.large":
return 2, 8
case "g.s2.xlarge":
return 4, 16
case "g.s2.2xlarge":
return 8, 32
case "g.s3.micro":
return 1, 1
case "g.s3.small":
return 1, 2
case "g.s3.medium":
return 1, 4
case "g.s3.large":
return 2, 8
case "g.s3.xlarge":
return 4, 16
case "g.s3.2xlarge":
return 8, 32
case "g.n1.medium":
return 1, 4
case "g.n1.large":
return 2, 8
case "g.n1.xlarge_m":
return 4, 12
case "g.n1.xlarge":
return 4, 16
case "g.n1.2xlarge":
return 8, 32
case "g.n1.4xlarge":
return 16, 64
case "g.n1.8xlarge":
return 32, 128
case "g.n2a.large":
return 2, 8
case "g.n2a.xlarge":
return 4, 16
case "g.n2a.2xlarge":
return 8, 32
case "g.n2a.4xlarge":
return 16, 64
case "g.n2a.8xlarge":
return 32, 128
case "g.n2a.16xlarge":
return 64, 256
case "g.n2a.32xlarge":
return 128, 512
case "g.n2.medium":
return 1, 4
case "g.n2.large":
return 2, 8
case "g.n2.xlarge":
return 4, 16
case "g.n2.2xlarge":
return 8, 32
case "g.n2.4xlarge":
return 16, 64
case "g.n2.8xlarge":
return 32, 128
case "g.n2.16xlarge":
return 64, 256
case "g.n2.18xlarge":
return 72, 288
case "g.n3.medium":
return 1, 4
case "g.n3.large":
return 2, 8
case "g.n3.xlarge":
return 4, 16
case "g.n3.2xlarge":
return 8, 32
case "g.n3.3xlarge":
return 12, 48
case "g.n3.4xlarge":
return 16, 64
case "g.n3.6xlarge":
return 24, 96
case "g.n3.8xlarge":
return 32, 128
case "g.n3.12xlarge":
return 48, 192
case "g.n3.16xlarge":
return 64, 256
case "g.n3.18xlarge":
return 72, 288
case "g.n4.medium":
return 1, 4
case "g.n4.large":
return 2, 8
case "g.n4.xlarge":
return 4, 16
case "g.n4.2xlarge":
return 8, 32
case "g.n4.3xlarge":
return 12, 48
case "g.n4.4xlarge":
return 16, 64
case "g.n4.6xlarge":
return 24, 96
case "g.n4.8xlarge":
return 32, 128
case "g.n4.12xlarge":
return 48, 192
case "g.n4.16xlarge":
return 64, 256
case "g.n4.18xlarge":
return 96, 352
case "g.n5.medium":
return 1, 4
case "g.n5.large":
return 2, 8
case "g.n5.xlarge":
return 4, 16
case "g.n5.2xlarge":
return 8, 32
case "g.n5.3xlarge":
return 12, 48
case "g.n5.4xlarge":
return 16, 64
case "g.n5.6xlarge":
return 24, 96
case "g.n8.8xlarge":
return 32, 128
case "g.n5.12xlarge":
return 48, 192
case "g.n5.16xlarge":
return 64, 256
case "g.n5.18xlarge":
return 72, 288
case "g.n5.24xlarge":
return 96, 384
case "g.n5.46xlarge":
return 184, 736
case "g.n4se.medium":
return 1, 4
case "g.n4se.large":
return 2, 8
case "g.n4se.xlarge":
return 4, 16
case "g.n4se.2xlarge":
return 8, 32
case "g.n4se.3xlarge":
return 12, 48
case "g.n4se.4xlarge":
return 16, 64
case "g.n4se.6xlarge":
return 24, 96
case "g.n4se.8xlarge":
return 32, 128
case "g.n4se.12xlarge":
return 48, 192
case "g.n4se.16xlarge":
return 64, 256
case "g.n4se.24xlarge":
return 96, 352
case "c.s3.large":
return 2, 4
case "c.s3.xlarge":
return 4, 8
case "c.s3.2xlarge":
return 8, 16
case "c.s2.large":
return 2, 4
case "c.s2.xlarge":
return 4, 8
case "c.s2.2xlarge":
return 8, 16
case "c.c5.large":
return 2, 2
case "c.c5.xlarge":
return 4, 4
case "c.c5.2xlarge":
return 8, 8
case "c.c5.3xlarge":
return 12, 12
case "c.c5.4xlarge":
return 16, 16
case "c.c4.large":
return 2, 2
case "c.c4.xlarge":
return 4, 4
case "c.c4.2xlarge":
return 8, 8
case "c.c4.3xlarge":
return 12, 12
case "c.c4.4xlarge":
return 16, 16
case "c.c2.large":
return 2, 2
case "c.c2.xlarge":
return 4, 4
case "c.c2.2xlarge":
return 8, 8
case "c.c2.3xlarge":
return 12, 12
case "c.c2.4xlarge":
return 16, 16
case "c.n5.large":
return 2, 4
case "c.n5.xlarge":
return 4, 8
case "c.n5.2xlarge":
return 8, 16
case "c.n5.3xlarge":
return 12, 24
case "c.n5.4xlarge":
return 16, 32
case "c.n5.6xlarge":
return 24, 48
case "c.n5.8xlarge":
return 32, 64
case "c.n5.12xlarge":
return 48, 96
case "c.n5.16xlarge":
return 64, 128
case "c.n5.18xlarge":
return 72, 144
case "c.n5.24xlarge":
return 96, 192
case "c.n5.46xlarge":
return 184, 268
case "c.n4.large":
return 2, 4
case "c.n4.xlarge":
return 4, 8
case "c.n4.2xlarge":
return 8, 16
case "c.n4.3xlarge":
return 12, 24
case "c.n4.4xlarge":
return 16, 32
case "c.n4.6xlarge":
return 24, 48
case "c.n4.8xlarge":
return 32, 64
case "c.n4.12xlarge":
return 48, 96
case "c.n4.16xlarge":
return 64, 128
case "c.n4.24xlarge":
return 96, 192
case "c.n3.large":
return 2, 4
case "c.n3.xlarge":
return 4, 8
case "c.n3.2xlarge":
return 8, 16
case "c.n3.3xlarge":
return 12, 24
case "c.n3.4xlarge":
return 16, 32
case "c.n3.6xlarge":
return 24, 48
case "c.n3.8xlarge":
return 32, 64
case "c.n3.12xlarge":
return 48, 96
case "c.n3.16xlarge":
return 64, 128
case "c.n3.22xlarge":
return 88, 176
case "c.n2.large":
return 2, 4
case "c.n2.xlarge":
return 4, 8
case "c.n2.2xlarge":
return 8, 16
case "c.n2.4xlarge":
return 16, 32
case "c.n2.8xlarge":
return 32, 64
case "c.n2.16xlarge":
return 64, 128
case "c.n2.18xlarge":
return 72, 144
case "c.n2a.large":
return 2, 4
case "c.n2a.xlarge":
return 4, 8
case "c.n2a.2xlarge":
return 8, 16
case "c.n2a.4xlarge":
return 16, 32
case "c.n2a.8xlarge":
return 32, 64
case "c.n2a.16xlarge":
return 64, 128
case "c.n2a.32xlarge":
return 128, 256
case "c.n1.large":
return 2, 4
case "c.n1.xlarge_m":
return 4, 8
case "c.n1.xlarge":
return 4, 8
case "c.n1.2xlarge_s":
return 8, 8
case "c.n1.2xlarge":
return 8, 16
case "c.n1.4xlarge_m":
return 16, 16
case "c.n1.4xlarge":
return 16, 32
case "c.n1.8xlarge":
return 32, 64
case "c.n4se.large":
return 2, 4
case "c.n4se.xlarge":
return 4, 8
case "c.n4se.2xlarge":
return 8, 16
case "c.n4se.3xlarge":
return 12, 24
case "c.n4se.4xlarge":
return 16, 32
case "c.n4se.6xlarge":
return 24, 48
case "c.n4se.8xlarge":
return 32, 64
case "c.n4se.12xlarge":
return 48, 96
case "c.n4se.16xlarge":
return 64, 128
case "c.n4se.24xlarge":
return 96, 192
case "m.n5.large":
return 2, 16
case "m.n5.xlarge":
return 4, 32
case "m.n5.2xlarge":
return 8, 64
case "m.n5.3xlarge":
return 12, 96
case "m.n5.4xlarge":
return 16, 128
case "m.n5.6xlarge":
return 24, 192
case "m.n5.8xlarge":
return 32, 256
case "m.n5.12xlarge":
return 48, 384
case "m.n5.16xlarge":
return 64, 512
case "m.n5.18xlarge":
return 72, 576
case "m.n4.large":
return 2, 16
case "m.n4.xlarge":
return 4, 32
case "m.n4.2xlarge":
return 8, 64
case "m.n4.3xlarge":
return 12, 96
case "m.n4.4xlarge":
return 16, 128
case "m.n4.6xlarge":
return 24, 192
case "m.n4.8xlarge":
return 32, 256
case "m.n3.large":
return 2, 16
case "m.n3.xlarge":
return 4, 32
case "m.n3.2xlarge":
return 8, 64
case "m.n3.3xlarge":
return 12, 96
case "m.n3.4xlarge":
return 16, 128
case "m.n3.6xlarge":
return 24, 192
case "m.n3.8xlarge":
return 32, 256
case "m.n3.16xlarge":
return 64, 512
case "m.n2.large":
return 2, 16
case "m.n2.xlarge":
return 4, 32
case "m.n2.2xlarge":
return 8, 64
case "m.n2.4xlarge":
return 16, 128
case "m.n2.8xlarge":
return 32, 256
case "m.n2.16xlarge":
return 64, 512
case "m.n2.18xlarge":
return 72, 576
case "m.n2a.large":
return 2, 16
case "m.n2a.xlarge":
return 4, 32
case "m.n2a.2xlarge":
return 8, 64
case "m.n2a.4xlarge":
return 16, 128
case "m.n2a.8xlarge":
return 32, 256
case "m.n2a.16xlarge":
return 64, 512
case "m.n1.small":
return 1, 8
case "m.n1.medium":
return 2, 12
case "m.n1.large":
return 2, 16
case "m.n1.xlarge":
return 4, 32
case "m.n1.2xlarge":
return 8, 64
case "m.n1.4xlarge":
return 16, 128
case "m.n4se.large":
return 2, 16
case "m.n4se.xlarge":
return 4, 32
case "m.n4se.2xlarge":
return 8, 64
case "m.n4se.3xlarge":
return 12, 96
case "m.n4se.4xlarge":
return 16, 128
case "m.n4se.6xlarge":
return 24, 192
case "m.n4se.8xlarge":
return 32, 256
case "m.n4se.24xlarge":
return 96, 724
case "m.e3.large":
return 2, 34
case "m.e3.xlarge":
return 4, 68
case "m.e3.2xlarge":
return 8, 136
case "m.e3.4xlarge":
return 16, 272
case "m.e3.8xlarge":
return 32, 544
case "m.e3.16xlarge":
return 64, 1088
case "m.e3.31xlarge":
return 124, 2108
case "t.e2.small":
return 2, 1
case "t.e2.large":
return 2, 2
case "t.c2.large":
return 2, 4
case "t.g2.large":
return 2, 8
case "t.e2.xlarge":
return 4, 4
case "t.c2.xlarge":
return 4, 8
case "t.g2.xlarge":
return 4, 16
case "t.e2.2xlarge":
return 8, 8
case "t.c2.2xlarge":
return 8, 16
case "t.g2.2xlarge":
return 8, 32
default:
return 0, 0
}
}
三、整体脚本示例
/*
用途: 该脚本的作用是调用京东云golang sdk,获取云主机ID、云主机IP、云主机CPU、MEM、系统盘、数据盘并保存到本地execl文件中
*/
package main
//导包
import (
"fmt"
. "github.com/jdcloud-api/jdcloud-sdk-go/core"
"github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/apis"
. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/client"
vm "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/models"
"github.com/xuri/excelize/v2"
)
// 1 初始化JD VM客户端
func initVmClient() *VmClient {
accessKey := "xxxx"
secretKey := "xxxx"
credentials := NewCredentials(accessKey, secretKey)
return NewVmClient(credentials)
}
// 2 获取所有主机信息
func getAllInstances(client *VmClient) ([]vm.Instance, error) {
pageNumber := 1
pageSize := 50
totalCount := 0
instanceList := make([]vm.Instance, 0)
op := "eq"
filter := models.Filter{
Name: "status", Operator: &op, Values: []string{"running"},
} //设置过滤字段 只取status=running的主机
//循环+分页获取云主机总数并保存到切片instanceList中
for {
//NewDescribeInstancesRequestWithAllParams(regionId string, pageNumber *int, pageSize *int, tags []models.TagFilter, filters []models.Filter)
req := NewDescribeInstancesRequestWithAllParams("cn-north-1", nil, nil, []vm.TagFilter{}, []models.Filter{filter})
req.SetPageNumber(pageNumber)
req.SetPageSize(pageSize)
resp, err := client.DescribeInstances(req)
if err != nil || resp.Error.Code != 0 {
return nil, fmt.Errorf("describe instance failed. %v, %v, %v", err, resp.Error.Code, resp.Error.Message)
}
totalCount = resp.Result.TotalCount
instanceList = append(instanceList, resp.Result.Instances...)
if len(instanceList) >= totalCount {
break
}
pageNumber++
}
return instanceList, nil
}
// 3、将主机信息写入Excel文件
func writeInstancesToExcel(instanceList []vm.Instance) error {
file := excelize.NewFile()
sheetName := "Instances" //设置sheet名
index, _ := file.NewSheet(sheetName)
// 写入表头
headers := []string{"Instance ID", "Private IP", "System Disk Size", "Data Disk Sizes", "CPU", "Memory"}
for i, header := range headers {
cell := fmt.Sprintf("%s%d", string('A'+i), 1)
file.SetCellValue(sheetName, cell, header)
}
// 写入主机信息
for i, instance := range instanceList {
rowIndex := i + 2 // 行索引从2开始,因为第一行是表头
instanceId := instance.InstanceId //服务器ID
instanceIp := instance.PrivateIpAddress //服务器ip
instanceSystemDisk := fmt.Sprintf("%d GB", instance.SystemDisk.CloudDisk.DiskSizeGB) //系统盘
instanceDataDisks := "" //数据盘
for _, disk := range instance.DataDisks { //根据最外层循环获取每一个实例对应的数据盘信息
instanceDataDisks += fmt.Sprintf("%d GB", disk.CloudDisk.DiskSizeGB) //拿到数据盘大小
//fmt.Println(instanceDataDisks) //输出 :'100 GB'
}
if len(instanceDataDisks) > 0 { //判断当前实例的数据盘是否大于0,如果大于0,则赋值给定义的字符串instanceDataDisks
instanceDataDisks = instanceDataDisks[:]
}
instanceType := instance.InstanceType
cpu, memory := getInstanceSpecs(instanceType) //调用函数
instanceCpu := fmt.Sprintf("%d Core", cpu) //cpu
instanceMemory := fmt.Sprintf("%d GB", memory) //内存
//按照对应的列写入对应的数据
file.SetCellValue(sheetName, fmt.Sprintf("A%d", rowIndex), instanceId)
file.SetCellValue(sheetName, fmt.Sprintf("B%d", rowIndex), instanceIp)
file.SetCellValue(sheetName, fmt.Sprintf("C%d", rowIndex), instanceSystemDisk)
file.SetCellValue(sheetName, fmt.Sprintf("D%d", rowIndex), instanceDataDisks)
file.SetCellValue(sheetName, fmt.Sprintf("E%d", rowIndex), instanceCpu)
file.SetCellValue(sheetName, fmt.Sprintf("F%d", rowIndex), instanceMemory)
}
//保存sheet页
file.SetActiveSheet(index)
err := file.SaveAs("instances.xlsx")
if err != nil {
return fmt.Errorf("failed to save Excel file: %v", err)
}
return nil
}
// 服务器cpu、mem对应关系
func getInstanceSpecs(instanceType string) (int, int) {
// 这里可以根据实际情况查询实例规格与CPU、内存的对应关系 详情见京东云文档https://docs.jdcloud.com/cn/virtual-machines/instance-type-family
switch instanceType {
case "g.s1.micro":
return 1, 1
case "g.s1.small":
return 1, 2
case "g.s2.micro":
return 1, 1
case "g.s2.small":
return 1, 2
case "g.s2.medium":
return 1, 4
case "g.s2.large":
return 2, 8
case "g.s2.xlarge":
return 4, 16
case "g.s2.2xlarge":
return 8, 32
case "g.s3.micro":
return 1, 1
case "g.s3.small":
return 1, 2
case "g.s3.medium":
return 1, 4
case "g.s3.large":
return 2, 8
case "g.s3.xlarge":
return 4, 16
case "g.s3.2xlarge":
return 8, 32
case "g.n1.medium":
return 1, 4
case "g.n1.large":
return 2, 8
case "g.n1.xlarge_m":
return 4, 12
case "g.n1.xlarge":
return 4, 16
case "g.n1.2xlarge":
return 8, 32
case "g.n1.4xlarge":
return 16, 64
case "g.n1.8xlarge":
return 32, 128
case "g.n2a.large":
return 2, 8
case "g.n2a.xlarge":
return 4, 16
case "g.n2a.2xlarge":
return 8, 32
case "g.n2a.4xlarge":
return 16, 64
case "g.n2a.8xlarge":
return 32, 128
case "g.n2a.16xlarge":
return 64, 256
case "g.n2a.32xlarge":
return 128, 512
case "g.n2.medium":
return 1, 4
case "g.n2.large":
return 2, 8
case "g.n2.xlarge":
return 4, 16
case "g.n2.2xlarge":
return 8, 32
case "g.n2.4xlarge":
return 16, 64
case "g.n2.8xlarge":
return 32, 128
case "g.n2.16xlarge":
return 64, 256
case "g.n2.18xlarge":
return 72, 288
case "g.n3.medium":
return 1, 4
case "g.n3.large":
return 2, 8
case "g.n3.xlarge":
return 4, 16
case "g.n3.2xlarge":
return 8, 32
case "g.n3.3xlarge":
return 12, 48
case "g.n3.4xlarge":
return 16, 64
case "g.n3.6xlarge":
return 24, 96
case "g.n3.8xlarge":
return 32, 128
case "g.n3.12xlarge":
return 48, 192
case "g.n3.16xlarge":
return 64, 256
case "g.n3.18xlarge":
return 72, 288
case "g.n4.medium":
return 1, 4
case "g.n4.large":
return 2, 8
case "g.n4.xlarge":
return 4, 16
case "g.n4.2xlarge":
return 8, 32
case "g.n4.3xlarge":
return 12, 48
case "g.n4.4xlarge":
return 16, 64
case "g.n4.6xlarge":
return 24, 96
case "g.n4.8xlarge":
return 32, 128
case "g.n4.12xlarge":
return 48, 192
case "g.n4.16xlarge":
return 64, 256
case "g.n4.18xlarge":
return 96, 352
case "g.n5.medium":
return 1, 4
case "g.n5.large":
return 2, 8
case "g.n5.xlarge":
return 4, 16
case "g.n5.2xlarge":
return 8, 32
case "g.n5.3xlarge":
return 12, 48
case "g.n5.4xlarge":
return 16, 64
case "g.n5.6xlarge":
return 24, 96
case "g.n8.8xlarge":
return 32, 128
case "g.n5.12xlarge":
return 48, 192
case "g.n5.16xlarge":
return 64, 256
case "g.n5.18xlarge":
return 72, 288
case "g.n5.24xlarge":
return 96, 384
case "g.n5.46xlarge":
return 184, 736
case "g.n4se.medium":
return 1, 4
case "g.n4se.large":
return 2, 8
case "g.n4se.xlarge":
return 4, 16
case "g.n4se.2xlarge":
return 8, 32
case "g.n4se.3xlarge":
return 12, 48
case "g.n4se.4xlarge":
return 16, 64
case "g.n4se.6xlarge":
return 24, 96
case "g.n4se.8xlarge":
return 32, 128
case "g.n4se.12xlarge":
return 48, 192
case "g.n4se.16xlarge":
return 64, 256
case "g.n4se.24xlarge":
return 96, 352
case "c.s3.large":
return 2, 4
case "c.s3.xlarge":
return 4, 8
case "c.s3.2xlarge":
return 8, 16
case "c.s2.large":
return 2, 4
case "c.s2.xlarge":
return 4, 8
case "c.s2.2xlarge":
return 8, 16
case "c.c5.large":
return 2, 2
case "c.c5.xlarge":
return 4, 4
case "c.c5.2xlarge":
return 8, 8
case "c.c5.3xlarge":
return 12, 12
case "c.c5.4xlarge":
return 16, 16
case "c.c4.large":
return 2, 2
case "c.c4.xlarge":
return 4, 4
case "c.c4.2xlarge":
return 8, 8
case "c.c4.3xlarge":
return 12, 12
case "c.c4.4xlarge":
return 16, 16
case "c.c2.large":
return 2, 2
case "c.c2.xlarge":
return 4, 4
case "c.c2.2xlarge":
return 8, 8
case "c.c2.3xlarge":
return 12, 12
case "c.c2.4xlarge":
return 16, 16
case "c.n5.large":
return 2, 4
case "c.n5.xlarge":
return 4, 8
case "c.n5.2xlarge":
return 8, 16
case "c.n5.3xlarge":
return 12, 24
case "c.n5.4xlarge":
return 16, 32
case "c.n5.6xlarge":
return 24, 48
case "c.n5.8xlarge":
return 32, 64
case "c.n5.12xlarge":
return 48, 96
case "c.n5.16xlarge":
return 64, 128
case "c.n5.18xlarge":
return 72, 144
case "c.n5.24xlarge":
return 96, 192
case "c.n5.46xlarge":
return 184, 268
case "c.n4.large":
return 2, 4
case "c.n4.xlarge":
return 4, 8
case "c.n4.2xlarge":
return 8, 16
case "c.n4.3xlarge":
return 12, 24
case "c.n4.4xlarge":
return 16, 32
case "c.n4.6xlarge":
return 24, 48
case "c.n4.8xlarge":
return 32, 64
case "c.n4.12xlarge":
return 48, 96
case "c.n4.16xlarge":
return 64, 128
case "c.n4.24xlarge":
return 96, 192
case "c.n3.large":
return 2, 4
case "c.n3.xlarge":
return 4, 8
case "c.n3.2xlarge":
return 8, 16
case "c.n3.3xlarge":
return 12, 24
case "c.n3.4xlarge":
return 16, 32
case "c.n3.6xlarge":
return 24, 48
case "c.n3.8xlarge":
return 32, 64
case "c.n3.12xlarge":
return 48, 96
case "c.n3.16xlarge":
return 64, 128
case "c.n3.22xlarge":
return 88, 176
case "c.n2.large":
return 2, 4
case "c.n2.xlarge":
return 4, 8
case "c.n2.2xlarge":
return 8, 16
case "c.n2.4xlarge":
return 16, 32
case "c.n2.8xlarge":
return 32, 64
case "c.n2.16xlarge":
return 64, 128
case "c.n2.18xlarge":
return 72, 144
case "c.n2a.large":
return 2, 4
case "c.n2a.xlarge":
return 4, 8
case "c.n2a.2xlarge":
return 8, 16
case "c.n2a.4xlarge":
return 16, 32
case "c.n2a.8xlarge":
return 32, 64
case "c.n2a.16xlarge":
return 64, 128
case "c.n2a.32xlarge":
return 128, 256
case "c.n1.large":
return 2, 4
case "c.n1.xlarge_m":
return 4, 8
case "c.n1.xlarge":
return 4, 8
case "c.n1.2xlarge_s":
return 8, 8
case "c.n1.2xlarge":
return 8, 16
case "c.n1.4xlarge_m":
return 16, 16
case "c.n1.4xlarge":
return 16, 32
case "c.n1.8xlarge":
return 32, 64
case "c.n4se.large":
return 2, 4
case "c.n4se.xlarge":
return 4, 8
case "c.n4se.2xlarge":
return 8, 16
case "c.n4se.3xlarge":
return 12, 24
case "c.n4se.4xlarge":
return 16, 32
case "c.n4se.6xlarge":
return 24, 48
case "c.n4se.8xlarge":
return 32, 64
case "c.n4se.12xlarge":
return 48, 96
case "c.n4se.16xlarge":
return 64, 128
case "c.n4se.24xlarge":
return 96, 192
case "m.n5.large":
return 2, 16
case "m.n5.xlarge":
return 4, 32
case "m.n5.2xlarge":
return 8, 64
case "m.n5.3xlarge":
return 12, 96
case "m.n5.4xlarge":
return 16, 128
case "m.n5.6xlarge":
return 24, 192
case "m.n5.8xlarge":
return 32, 256
case "m.n5.12xlarge":
return 48, 384
case "m.n5.16xlarge":
return 64, 512
case "m.n5.18xlarge":
return 72, 576
case "m.n4.large":
return 2, 16
case "m.n4.xlarge":
return 4, 32
case "m.n4.2xlarge":
return 8, 64
case "m.n4.3xlarge":
return 12, 96
case "m.n4.4xlarge":
return 16, 128
case "m.n4.6xlarge":
return 24, 192
case "m.n4.8xlarge":
return 32, 256
case "m.n3.large":
return 2, 16
case "m.n3.xlarge":
return 4, 32
case "m.n3.2xlarge":
return 8, 64
case "m.n3.3xlarge":
return 12, 96
case "m.n3.4xlarge":
return 16, 128
case "m.n3.6xlarge":
return 24, 192
case "m.n3.8xlarge":
return 32, 256
case "m.n3.16xlarge":
return 64, 512
case "m.n2.large":
return 2, 16
case "m.n2.xlarge":
return 4, 32
case "m.n2.2xlarge":
return 8, 64
case "m.n2.4xlarge":
return 16, 128
case "m.n2.8xlarge":
return 32, 256
case "m.n2.16xlarge":
return 64, 512
case "m.n2.18xlarge":
return 72, 576
case "m.n2a.large":
return 2, 16
case "m.n2a.xlarge":
return 4, 32
case "m.n2a.2xlarge":
return 8, 64
case "m.n2a.4xlarge":
return 16, 128
case "m.n2a.8xlarge":
return 32, 256
case "m.n2a.16xlarge":
return 64, 512
case "m.n1.small":
return 1, 8
case "m.n1.medium":
return 2, 12
case "m.n1.large":
return 2, 16
case "m.n1.xlarge":
return 4, 32
case "m.n1.2xlarge":
return 8, 64
case "m.n1.4xlarge":
return 16, 128
case "m.n4se.large":
return 2, 16
case "m.n4se.xlarge":
return 4, 32
case "m.n4se.2xlarge":
return 8, 64
case "m.n4se.3xlarge":
return 12, 96
case "m.n4se.4xlarge":
return 16, 128
case "m.n4se.6xlarge":
return 24, 192
case "m.n4se.8xlarge":
return 32, 256
case "m.n4se.24xlarge":
return 96, 724
case "m.e3.large":
return 2, 34
case "m.e3.xlarge":
return 4, 68
case "m.e3.2xlarge":
return 8, 136
case "m.e3.4xlarge":
return 16, 272
case "m.e3.8xlarge":
return 32, 544
case "m.e3.16xlarge":
return 64, 1088
case "m.e3.31xlarge":
return 124, 2108
case "t.e2.small":
return 2, 1
case "t.e2.large":
return 2, 2
case "t.c2.large":
return 2, 4
case "t.g2.large":
return 2, 8
case "t.e2.xlarge":
return 4, 4
case "t.c2.xlarge":
return 4, 8
case "t.g2.xlarge":
return 4, 16
case "t.e2.2xlarge":
return 8, 8
case "t.c2.2xlarge":
return 8, 16
case "t.g2.2xlarge":
return 8, 32
default:
return 0, 0
}
}
func main() { //程序主入口
client := initVmClient() //初始化客户端实例对象
instanceList, err := getAllInstances(client) //获取服务器信息并保存到切片中,如果获取出错,则打印错误并终止运行
if err != nil {
fmt.Println(err)
return
}
//将主要信息保存到execl文件中,如果写入失败打印错误并终止运行
err = writeInstancesToExcel(instanceList)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Instances saved to instances.xlsx")
}
四、结果展示
总结
以上就是今天分享给大家的一篇golang调用京东云sdk获取服务器信息的知识,希望能对大家有所帮助,如果你是用的其他云厂商,则需要导入云厂商自己的golang-sdk,并修改自己的ak/sk,然后本地执行go run main.go
即可获取到相关信息.