dt_automate/dtcloud/hlw.go

223 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dtcloud
import (
"dt_automate/tool"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strconv"
"time"
"github.com/Esword618/unioffice/schema/soo/sml"
"github.com/Esword618/unioffice/spreadsheet"
)
// 运营平台-获取单个ecs的cpu使用情况
func Cpu() {
fileData, err := ioutil.ReadFile("dtcloud/cpu.json")
if err != nil {
log.Fatal(err)
}
var data map[string]interface{}
err = json.Unmarshal(fileData, &data)
if err != nil {
log.Fatal(err)
}
log.Println(data)
}
// 运营平台-获取单个ecs的memory使用情况
func Memory() {
fileData, err := ioutil.ReadFile("dtcloud/memory.json")
if err != nil {
log.Fatal(err)
}
var data map[string]interface{}
err = json.Unmarshal(fileData, &data)
if err != nil {
log.Fatal(err)
}
log.Println(data)
}
// 运营平台-获取租户列表
func TenantList() {
ss := spreadsheet.New()
sheet := ss.AddSheet()
// sheet.SetFrozen(true, false)
v := sheet.InitialView()
v.SetState(sml.ST_PaneStateFrozen)
v.SetXSplit(0) //冻结列
v.SetYSplit(1) //冻结行
sheet.Cell("A1").SetString("序号")
sheet.Cell("B1").SetString("租户ID")
sheet.Cell("C1").SetString("用户ID")
sheet.Cell("D1").SetString("创建者")
sheet.Cell("E1").SetString("昵称")
sheet.Cell("F1").SetString("租户用户名")
sheet.Cell("G1").SetString("使用状态")
sheet.Cell("H1").SetString("创建时间")
log.Println("生成表格列名完成")
fileData, err := ioutil.ReadFile("dtcloud/user.json")
if err != nil {
log.Fatal(err)
}
var data map[string]interface{}
err = json.Unmarshal(fileData, &data)
if err != nil {
log.Fatal(err)
}
// log.Println(data)
for _, usr := range data["data"].(map[string]interface{})["list"].([]interface{}) {
log.Println(len(sheet.Rows()))
con := len(sheet.Rows()) + 1
a, _ := strconv.ParseInt(usr.(map[string]interface{})["createTime"].(string), 10, 64)
sheet.Cell("A" + strconv.Itoa(con)).SetString(strconv.Itoa(len(sheet.Rows()) - 1))
sheet.Cell("B" + strconv.Itoa(con)).SetString(usr.(map[string]interface{})["id"].(string))
sheet.Cell("C" + strconv.Itoa(con)).SetString(usr.(map[string]interface{})["userAdminId"].(string))
sheet.Cell("D" + strconv.Itoa(con)).SetString(usr.(map[string]interface{})["creator"].(string))
sheet.Cell("E" + strconv.Itoa(con)).SetString(usr.(map[string]interface{})["tenantName"].(string))
sheet.Cell("F" + strconv.Itoa(con)).SetString(usr.(map[string]interface{})["userAdminCode"].(string))
if condition := usr.(map[string]interface{})["useStatus"].(float64); condition == 0 {
sheet.Cell("G" + strconv.Itoa(con)).SetString("正常")
} else {
sheet.Cell("G" + strconv.Itoa(con)).SetString("禁用")
}
sheet.Cell("H" + strconv.Itoa(con)).SetString(time.Unix(a/1000, 0).Format("2006-01-02 15:04:05"))
}
// 保存修改后的 Excel 文件
if err := ss.Validate(); err != nil {
log.Fatalf("验证文件时出错: %s", err)
}
if err := ss.SaveToFile("互联网区-运营平台租户列表" + time.Unix(tool.Timestamp("second"), 0).Format("20060102") + ".xlsx"); err != nil {
log.Fatalf("保存文件时出错: %s", err)
}
}
// 运营平台-获取单ecs进程使用情况
func Process() {
fileData, err := ioutil.ReadFile("dtcloud/pid.json")
if err != nil {
log.Fatal(err)
}
var data map[string]interface{}
err = json.Unmarshal(fileData, &data)
if err != nil {
log.Fatal(err)
}
log.Println(data)
}
// 运营平台-获取ecs列表及其详情
func EcsList() {
ss := spreadsheet.New()
sheet := ss.AddSheet()
// sheet.SetFrozen(true, false)
v := sheet.InitialView()
v.SetState(sml.ST_PaneStateFrozen)
v.SetXSplit(0) //冻结列
v.SetYSplit(1) //冻结行
sheet.Cell("A1").SetString("序号")
sheet.Cell("B1").SetString("创建时间")
sheet.Cell("C1").SetString("更新时间")
sheet.Cell("D1").SetString("过期时间")
sheet.Cell("E1").SetString("ECS_ID")
sheet.Cell("F1").SetString("租住ID")
sheet.Cell("G1").SetString("显示名称")
sheet.Cell("H1").SetString("租户名称")
sheet.Cell("I1").SetString("所属区域")
sheet.Cell("J1").SetString("CPU架构")
sheet.Cell("K1").SetString("系统类型")
sheet.Cell("L1").SetString("运行状态")
sheet.Cell("M1").SetString("CPU核心数")
sheet.Cell("N1").SetString("内存/GB")
sheet.Cell("O1").SetString("系统磁盘/GB")
sheet.Cell("P1").SetString("存储磁盘/GB")
sheet.Cell("Q1").SetString("宿主机IP")
sheet.Cell("R1").SetString("MAC地址")
sheet.Cell("S1").SetString("安全组")
sheet.Cell("T1").SetString("网络类型")
sheet.Cell("U1").SetString("EIP")
sheet.Cell("V1").SetString("带宽大小(入/出)")
sheet.Cell("W1").SetString("部门名称")
sheet.Cell("X1").SetString("部门ID")
sheet.Cell("Y1").SetString("主机型号")
sheet.Cell("Z1").SetString("CPU型号")
sheet.Cell("AA1").SetString("CPU模式")
sheet.Cell("AB1").SetString("nodeName")
sheet.Cell("AC1").SetString("cpu超出比率")
log.Println("生成表格列名完成")
//加载json文件
fileData, err := ioutil.ReadFile("dtcloud/ecs.json")
if err != nil {
log.Fatal(err)
}
var data map[string]interface{}
err = json.Unmarshal(fileData, &data)
if err != nil {
log.Fatal(err)
}
for _, ecs := range data["data"].(map[string]interface{})["virtualMachines"].([]interface{}) {
log.Println(len(sheet.Rows()))
con := len(sheet.Rows()) + 1
sheet.Cell("A" + strconv.Itoa(con)).SetString(strconv.Itoa(len(sheet.Rows()) - 1)) //序号
sheet.Cell("B" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["createdAt"].(string)) //创建时间
sheet.Cell("C" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["updatedAt"].(string)) //更新时间
sheet.Cell("D" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["expiredAt"].(string)) //过期时间
sheet.Cell("E" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["ecsId"].(string)) //ECS_ID
sheet.Cell("F" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["tenantId"].(string)) //租住ID
sheet.Cell("G" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["displayName"].(string)) //显示名称
sheet.Cell("H" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["tenantName"].(string)) //租户名称
sheet.Cell("I" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["region"].(string) + "/" + ecs.(map[string]interface{})["az"].(string) + "/" + ecs.(map[string]interface{})["cell"].(string)) //所属区域X/X/X
sheet.Cell("J" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["cpuArch"].(string)) //CPU架构
sheet.Cell("K" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["osType"].(string)) //系统类型
sheet.Cell("L" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["status"].(string)) //运行状态
sheet.Cell("M" + strconv.Itoa(con)).SetString(strconv.Itoa(int(ecs.(map[string]interface{})["vCpus"].(float64))) + "核") //CPU核心数
sheet.Cell("N" + strconv.Itoa(con)).SetString(strconv.Itoa(int(ecs.(map[string]interface{})["ram"].(float64))/1024/1024/1024) + "GB") //内存/GB
var diskStrsys string
var diskStrdata string
for _, disk := range ecs.(map[string]interface{})["blockDeviceMappings"].([]interface{}) {
if disk.(map[string]interface{})["isOSDisk"] != nil {
if condition := disk.(map[string]interface{})["isOSDisk"].(bool); condition == true {
diskStrsys += fmt.Sprintf("%sGB/", strconv.Itoa(int(disk.(map[string]interface{})["volumeSize"].(float64)))) //多块系统盘拼接
} else {
diskStrdata += fmt.Sprintf("%sGB/", strconv.Itoa(int(disk.(map[string]interface{})["volumeSize"].(float64)))) //多块存储盘拼接
}
} else {
diskStrdata += fmt.Sprintf("%sGB/", strconv.Itoa(int(disk.(map[string]interface{})["volumeSize"].(float64)))) //多块存储盘拼接
}
}
sheet.Cell("O" + strconv.Itoa(con)).SetString(diskStrsys) //系统磁盘/GB
sheet.Cell("P" + strconv.Itoa(con)).SetString(diskStrdata) //存储磁盘/GB
sheet.Cell("Q" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["networkMappings"].([]interface{})[0].(map[string]interface{})["ipv4Address"].(string)) //宿主机IP
sheet.Cell("R" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["networkMappings"].([]interface{})[0].(map[string]interface{})["macAddress"].(string)) //MAC地址
sheet.Cell("S" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["networkMappings"].([]interface{})[0].(map[string]interface{})["portType"].(string)) //安全组
sheet.Cell("T" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["networkMappings"].([]interface{})[0].(map[string]interface{})["networkType"].(string)) //网络类型
sheet.Cell("U" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["eipMappings"].([]interface{})[0].(map[string]interface{})["ipAddress"].(string)) //EIP
sheet.Cell("V" + strconv.Itoa(con)).SetString(strconv.Itoa(int(ecs.(map[string]interface{})["eipMappings"].([]interface{})[0].(map[string]interface{})["bandwidthInSize"].(float64))) + "/" + strconv.Itoa(int(ecs.(map[string]interface{})["eipMappings"].([]interface{})[0].(map[string]interface{})["bandwidthOutSize"].(float64)))) //带宽大小(入/出)
sheet.Cell("W" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["departmentName"].(string)) //部门名称
sheet.Cell("X" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["departmentId"].(string)) //部门ID
sheet.Cell("Y" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["flavor"].(map[string]interface{})["flavorType"].(map[string]interface{})["name"].(string)) //主机型号
sheet.Cell("Z" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["flavor"].(map[string]interface{})["flavorType"].(map[string]interface{})["cpuModel"].(string)) //CPU型号
sheet.Cell("AA" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["vcpuType"].(string)) //CPU模式
sheet.Cell("AB" + strconv.Itoa(con)).SetString(ecs.(map[string]interface{})["nodeName"].(string))
sheet.Cell("AC" + strconv.Itoa(con)).SetString(strconv.Itoa(int(ecs.(map[string]interface{})["cpuExceedRatio"].(float64)))) //cpu超出比率
}
// 保存修改后的 Excel 文件
if err := ss.Validate(); err != nil {
log.Fatalf("验证文件时出错: %s", err)
}
if err := ss.SaveToFile("互联网区-运营平台ECS列表" + time.Unix(tool.Timestamp("second"), 0).Format("20060102") + ".xlsx"); err != nil {
log.Fatalf("保存文件时出错: %s", err)
}
}