|
|
@@ -5,33 +5,25 @@ import (
|
|
|
"fmt"
|
|
|
. "github.com/coroot/coroot-node-agent/utils/modelse"
|
|
|
"math"
|
|
|
+ "math/big"
|
|
|
"os"
|
|
|
"path"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-func init() {
|
|
|
-
|
|
|
+func SaveNodeInfo(n *NodeInfoT) {
|
|
|
+ if n != nil {
|
|
|
+ NODE_INFO = n
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-var (
|
|
|
- APP_ID_INT64 int64
|
|
|
- HOST_ID_INT64 int64
|
|
|
- ACCOUNT_ID_INT int
|
|
|
- INSTANCE_ID_INT64 int64
|
|
|
-
|
|
|
- APP_ID_BYTE HashByte
|
|
|
- HOST_ID_BYTE HashByte
|
|
|
- INSTANCE_ID_BYTE HashByte
|
|
|
-)
|
|
|
-
|
|
|
func BuildInt64ID(str string) ID_STRING {
|
|
|
- srcCode := md5.Sum([]byte(str))
|
|
|
- code := fmt.Sprintf("%x", srcCode)
|
|
|
-
|
|
|
- id_string := md5ToDec(code)
|
|
|
- return ID_STRING(id_string)
|
|
|
+ //srcCode := md5.Sum([]byte(str))
|
|
|
+ //code := fmt.Sprintf("%x", srcCode)
|
|
|
+ //id_string := md5ToDec(code)
|
|
|
+ //return ID_STRING(id_string)
|
|
|
+ return ID_STRING(hashTo16DigitString(str))
|
|
|
}
|
|
|
|
|
|
func hexStringToBPFBytes(str string, out *HashByte) {
|
|
|
@@ -46,17 +38,16 @@ func hexStringToBPFBytes(str string, out *HashByte) {
|
|
|
|
|
|
// todo account id
|
|
|
func GetAccountID() int {
|
|
|
- if ACCOUNT_ID_INT != 0 {
|
|
|
- return ACCOUNT_ID_INT
|
|
|
+ if NODE_INFO != nil && NODE_INFO.AccountID != 0 {
|
|
|
+ return NODE_INFO.AccountID
|
|
|
}
|
|
|
- ACCOUNT_ID_INT = 110
|
|
|
- return ACCOUNT_ID_INT
|
|
|
+ NODE_INFO.AccountID = 110
|
|
|
+ return NODE_INFO.AccountID
|
|
|
}
|
|
|
|
|
|
-// todo HostId
|
|
|
-func GetHostID() (int64, HashByte) {
|
|
|
- if HOST_ID_INT64 != 0 {
|
|
|
- return HOST_ID_INT64, HOST_ID_BYTE
|
|
|
+func GetHostID() int64 {
|
|
|
+ if NODE_INFO != nil && NODE_INFO.HostID != 0 {
|
|
|
+ return NODE_INFO.HostID
|
|
|
}
|
|
|
uuid := SystemUUID()
|
|
|
// todo AccountId
|
|
|
@@ -66,33 +57,39 @@ func GetHostID() (int64, HashByte) {
|
|
|
host_id_string := md5ToDec(code)
|
|
|
id, err := strconv.ParseInt(host_id_string, 10, 64)
|
|
|
if err != nil {
|
|
|
- return 0, HashByte{}
|
|
|
+ return 0
|
|
|
}
|
|
|
|
|
|
- HOST_ID_INT64 = id
|
|
|
+ NODE_INFO.HostID = id
|
|
|
|
|
|
- var charArray HashByte
|
|
|
- hexStringToBPFBytes(host_id_string, &charArray)
|
|
|
- HOST_ID_BYTE = charArray
|
|
|
+ //var charArray HashByte
|
|
|
+ //hexStringToBPFBytes(host_id_string, &charArray)
|
|
|
+ //HOST_ID_BYTE = charArray
|
|
|
// 将rune切片复制到char数组中
|
|
|
//for i := 0; i < HASH_SIZE; i++ {
|
|
|
// charArray[i] = []byte(host_id_string)[i]
|
|
|
//}
|
|
|
- return id, charArray
|
|
|
+ return id
|
|
|
}
|
|
|
|
|
|
-func GetIntHostID() int64 {
|
|
|
- id, _ := GetHostID()
|
|
|
- return id
|
|
|
+func GetHostIDBPFString() HashByte {
|
|
|
+ host_id_string := strconv.FormatInt(GetHostID(), 10)
|
|
|
+ var charArray HashByte
|
|
|
+ hexStringToBPFBytes(host_id_string, &charArray)
|
|
|
+ return charArray
|
|
|
}
|
|
|
|
|
|
func SystemUUID() string {
|
|
|
+ if NODE_INFO != nil && NODE_INFO.SystemUUID != "" {
|
|
|
+ return NODE_INFO.SystemUUID
|
|
|
+ }
|
|
|
for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
|
|
|
payload, err := os.ReadFile(path.Join("/proc/1/root", p))
|
|
|
if err != nil {
|
|
|
continue
|
|
|
}
|
|
|
id := strings.TrimSpace(strings.Replace(string(payload), "\n", "", -1))
|
|
|
+ NODE_INFO.SystemUUID = id
|
|
|
return id
|
|
|
}
|
|
|
return ""
|
|
|
@@ -113,3 +110,22 @@ func md5ToDec(str string) string {
|
|
|
}
|
|
|
return strCode
|
|
|
}
|
|
|
+
|
|
|
+func hashTo16DigitString(appName string) string {
|
|
|
+ // 计算 MD5 哈希
|
|
|
+ hash := md5.Sum([]byte(appName))
|
|
|
+ // 将哈希值转换为一个大整数
|
|
|
+ bigInt := new(big.Int).SetBytes(hash[:])
|
|
|
+ // 转换为十进制字符串
|
|
|
+ intStr := bigInt.String()
|
|
|
+ // 如果结果少于 16 位,补零
|
|
|
+ if len(intStr) < 16 {
|
|
|
+ intStr = fmt.Sprintf("%016s", intStr) // 左侧补零
|
|
|
+ // 如果第一个字符是 0,将其替换为 1
|
|
|
+ if intStr[0] == '0' {
|
|
|
+ intStr = "1" + intStr[1:]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 截取前 16 位
|
|
|
+ return intStr[:16]
|
|
|
+}
|