probe/auth/info/info.go

185 lines
3.7 KiB
Go
Raw Normal View History

2018-04-12 11:54:56 +00:00
package info
import (
2018-06-14 07:24:05 +00:00
"bytes"
"compress/gzip"
2018-04-12 11:54:56 +00:00
"encoding/base64"
"encoding/json"
"net"
2018-06-13 09:27:04 +00:00
cun "git.loafle.net/commons/util-go/net"
cung "git.loafle.net/commons/util-go/net/gateway"
ocmi "git.loafle.net/overflow/commons-go/model/infra"
ocmm "git.loafle.net/overflow/commons-go/model/meta"
2018-06-01 09:10:33 +00:00
ocmn "git.loafle.net/overflow/commons-go/model/noauthprobe"
2018-04-12 11:54:56 +00:00
"github.com/shirou/gopsutil/host"
)
func GetRegistHeader(apiKey string) (string, error) {
var err error
2018-06-01 09:10:33 +00:00
nap := ocmn.NoAuthProbe{
2018-04-12 11:54:56 +00:00
APIKey: apiKey,
}
2018-06-13 09:27:04 +00:00
var infraHost *ocmi.InfraHost
if infraHost, err = getInfraHost(); nil != err {
2018-04-12 11:54:56 +00:00
return "", err
}
var buf []byte
2018-06-13 09:27:04 +00:00
if buf, err = json.Marshal(infraHost); nil != err {
2018-04-12 11:54:56 +00:00
return "", err
}
2018-06-13 09:27:04 +00:00
nap.InfraHostMeta = string(buf)
2018-04-12 11:54:56 +00:00
if buf, err = json.Marshal(nap); nil != err {
return "", err
}
2018-06-14 07:24:05 +00:00
var b bytes.Buffer
2018-06-20 07:02:23 +00:00
gWriter, err := gzip.NewWriterLevel(&b, gzip.BestSpeed)
if nil != err {
2018-06-14 07:24:05 +00:00
return "", err
}
2018-06-20 07:02:23 +00:00
if _, err := gWriter.Write(buf); nil != err {
return "", err
}
if err := gWriter.Flush(); nil != err {
2018-06-14 07:24:05 +00:00
return "", err
}
2018-06-20 07:02:23 +00:00
if err := gWriter.Close(); nil != err {
2018-06-14 07:24:05 +00:00
return "", err
}
gBuf := b.Bytes()
2018-06-13 09:27:04 +00:00
2018-06-14 07:24:05 +00:00
enc := base64.StdEncoding.EncodeToString(gBuf)
2018-04-12 11:54:56 +00:00
return enc, nil
}
2018-06-13 09:27:04 +00:00
func getInfraHost() (*ocmi.InfraHost, error) {
2018-04-12 11:54:56 +00:00
var err error
2018-06-13 09:27:04 +00:00
infraHost := &ocmi.InfraHost{}
2018-04-12 11:54:56 +00:00
2018-06-13 09:27:04 +00:00
if infraHost.InfraHostMachine, err = getInfraHostMachine(); nil != err {
2018-04-12 11:54:56 +00:00
return nil, err
}
2018-06-13 09:27:04 +00:00
if infraHost.InfraHostOS, err = getInfraHostOS(); nil != err {
return nil, err
}
if infraHost.InfraHostIPs, err = getInfraHostIPs(); nil != err {
2018-04-12 11:54:56 +00:00
return nil, err
}
2018-06-13 09:27:04 +00:00
return infraHost, nil
2018-04-12 11:54:56 +00:00
}
2018-06-13 09:27:04 +00:00
func getInfraHostMachine() (*ocmi.InfraHostMachine, error) {
meta := make(map[string]interface{})
2018-04-12 11:54:56 +00:00
2018-06-14 07:24:05 +00:00
// if i, err := cpu.Info(); nil == err {
// meta["CPU"] = i
// }
2018-04-12 11:54:56 +00:00
2018-06-14 07:24:05 +00:00
// if i, err := mem.SwapMemory(); nil == err {
// meta["Memory_Swap"] = i
// }
2018-06-13 09:27:04 +00:00
2018-06-14 07:24:05 +00:00
// if i, err := mem.VirtualMemory(); nil == err {
// meta["Memory_Virtual"] = i
// }
2018-06-13 09:27:04 +00:00
2018-06-14 07:24:05 +00:00
// if i, err := disk.Partitions(true); nil == err {
// meta["Partitions"] = i
// }
2018-06-13 09:27:04 +00:00
2018-06-14 07:24:05 +00:00
// if i, err := disk.Usage("/"); nil == err {
// meta["Disk_Usage"] = i
// }
2018-06-13 09:27:04 +00:00
buf, err := json.Marshal(meta)
if nil != err {
2018-04-12 11:54:56 +00:00
return nil, err
}
2018-06-13 09:27:04 +00:00
return &ocmi.InfraHostMachine{
Meta: string(buf),
}, nil
2018-04-12 11:54:56 +00:00
}
2018-06-13 09:27:04 +00:00
func getInfraHostOS() (*ocmi.InfraHostOS, error) {
i, err := host.Info()
if nil != err {
return nil, err
}
infraHostOS := &ocmi.InfraHostOS{}
infraHostOS.Name = i.Hostname
infraHostOS.OS = i.OS
infraHostOS.Platform = i.Platform
infraHostOS.PlatformFamily = i.PlatformFamily
infraHostOS.KernelVersion = i.KernelVersion
infraHostOS.HostID = i.HostID
return infraHostOS, nil
}
func getInfraHostIPs() ([]*ocmi.InfraHostIP, error) {
gwIP, gwIface, err := cung.DiscoverGateway()
if nil != err {
2018-04-12 11:54:56 +00:00
return nil, err
}
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
2018-06-13 09:27:04 +00:00
infraHostIPs := make([]*ocmi.InfraHostIP, 0)
2018-04-12 11:54:56 +00:00
2018-06-13 09:27:04 +00:00
for _, i := range interfaces {
if i.Flags&net.FlagLoopback != 0 || i.Flags&net.FlagUp == 0 {
continue
2018-04-12 11:54:56 +00:00
}
2018-06-13 09:27:04 +00:00
addrs, err := i.Addrs()
if nil != err {
continue
}
LOOP_ADDR:
for _, addr := range addrs {
_, ipNet, err := cun.ParseCIDR(addr.String())
if nil != err {
continue
}
2018-04-12 11:54:56 +00:00
2018-06-13 09:27:04 +00:00
infraHostIP := &ocmi.InfraHostIP{}
infraHostIP.Address = addr.String()
2018-04-12 11:54:56 +00:00
2018-06-13 09:27:04 +00:00
switch ipNet.Version() {
case 4:
infraHostIP.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV4)
case 6:
infraHostIP.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV6)
default:
continue LOOP_ADDR
}
2018-04-12 11:54:56 +00:00
2018-06-13 09:27:04 +00:00
infraHostIP.Mac = i.HardwareAddr.String()
infraHostIP.Iface = i.Name
2018-04-12 11:54:56 +00:00
2018-06-13 09:27:04 +00:00
if gwIface == i.Name {
infraHostIP.Gateway = gwIP.String()
2018-04-12 11:54:56 +00:00
}
2018-06-13 09:27:04 +00:00
infraHostIPs = append(infraHostIPs, infraHostIP)
2018-04-12 11:54:56 +00:00
}
}
2018-06-13 09:27:04 +00:00
return infraHostIPs, nil
2018-04-12 11:54:56 +00:00
}