diff --git a/Gopkg.lock b/Gopkg.lock index e3897ca..cd681b5 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -34,7 +34,7 @@ "protocol/json", "registry" ] - revision = "1092f7a7ae44ef2f907579af217f1a594e48959c" + revision = "dcc3af07239b3f6fcbae3529bcb52c522b02053d" [[projects]] branch = "master" @@ -61,7 +61,7 @@ "time/scheduler/storage", "time/scheduler/task" ] - revision = "1966a985759721fd451171a78a0b8f4524afc644" + revision = "4d4017d214d2a8fdde59d774254f421991fabe7e" [[projects]] branch = "master" @@ -72,12 +72,12 @@ "config/probe", "core/annotation", "core/config", - "core/constants", "core/interfaces", "core/util", "model/data", "model/discovery", "model/domain", + "model/infra", "model/member", "model/meta", "model/noauthprobe", @@ -85,7 +85,7 @@ "model/sensorconfig", "service/probe" ] - revision = "078a39712d33c131303cf2705cca185da18cc196" + revision = "ab936be3035ee878cebd09e14d8e2f6baf56b670" [[projects]] name = "github.com/BurntSushi/toml" @@ -137,14 +137,24 @@ version = "v1.1" [[projects]] + branch = "master" name = "github.com/shirou/gopsutil" packages = [ + "cpu", + "disk", "host", "internal/common", + "mem", + "net", "process" ] - revision = "c95755e4bcd7a62bb8bd33f3a597a7c7f35e2cf3" - version = "v2.18.04" + revision = "c23bcca55e77b8389d84b09db8c5ac2b472070ef" + +[[projects]] + branch = "master" + name = "github.com/shirou/w32" + packages = ["."] + revision = "bb4de0191aa41b5507caa14b0650cdbddcd9280b" [[projects]] name = "github.com/valyala/fasthttp" @@ -187,7 +197,7 @@ "unix", "windows" ] - revision = "d0faeb539838e250bd0a9db4182d48d4a1915181" + revision = "a9e25c09b96b8870693763211309e213c6ef299d" [[projects]] name = "gopkg.in/yaml.v2" @@ -198,6 +208,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "81bd45952fac7d58527d174bfee2904c05bdba354ed4a77218d8d2dc11d28668" + inputs-digest = "769d28026d17f8f235bacca2cecf1de3a4930d13e2e43abdc29928aaf220ab7d" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index bb34342..182c200 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -54,8 +54,8 @@ name = "git.loafle.net/overflow/commons-go" [[constraint]] + branch = "master" name = "github.com/shirou/gopsutil" - version = "2.18.4" [[constraint]] name = "github.com/valyala/fasthttp" diff --git a/_build/bin/container_discovery b/_build/bin/container_discovery index 14e0603..0a8fe96 100755 Binary files a/_build/bin/container_discovery and b/_build/bin/container_discovery differ diff --git a/auth/info/info.go b/auth/info/info.go index e59d5f2..aa04b13 100644 --- a/auth/info/info.go +++ b/auth/info/info.go @@ -1,15 +1,20 @@ package info import ( - "bytes" "encoding/base64" "encoding/json" - "errors" + "log" "net" - "git.loafle.net/commons/util-go/net/gateway" + 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" ocmn "git.loafle.net/overflow/commons-go/model/noauthprobe" + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/host" + "github.com/shirou/gopsutil/mem" ) func GetRegistHeader(apiKey string) (string, error) { @@ -18,63 +23,104 @@ func GetRegistHeader(apiKey string) (string, error) { APIKey: apiKey, } - var napd *ocmn.NoAuthProbeDescription - if napd, err = getDescription(); nil != err { + var infraHost *ocmi.InfraHost + if infraHost, err = getInfraHost(); nil != err { return "", err } var buf []byte - if buf, err = json.Marshal(napd); nil != err { + if buf, err = json.Marshal(infraHost); nil != err { return "", err } - nap.Description = string(buf) + + nap.InfraHostMeta = string(buf) if buf, err = json.Marshal(nap); nil != err { return "", err } + log.Printf("nap: %s", string(buf)) + enc := base64.StdEncoding.EncodeToString(buf) return enc, nil } -func getDescription() (*ocmn.NoAuthProbeDescription, error) { +func getInfraHost() (*ocmi.InfraHost, error) { var err error - napd := &ocmn.NoAuthProbeDescription{} + infraHost := &ocmi.InfraHost{} - if napd.Host, err = getHost(); nil != err { + if infraHost.InfraHostMachine, err = getInfraHostMachine(); nil != err { + return nil, err + } + if infraHost.InfraHostOS, err = getInfraHostOS(); nil != err { + return nil, err + } + if infraHost.InfraHostIPs, err = getInfraHostIPs(); nil != err { return nil, err } - if napd.Network, err = getNetwork(); nil != err { - return nil, err - } - - return napd, nil + return infraHost, nil } -func getHost() (*ocmn.NoAuthProbeDescriptionHost, error) { - if i, err := host.Info(); nil == err { - h := &ocmn.NoAuthProbeDescriptionHost{} +func getInfraHostMachine() (*ocmi.InfraHostMachine, error) { + meta := make(map[string]interface{}) - h.Name = i.Hostname - h.OS = i.OS - h.Platform = i.Platform - h.PlatformFamily = i.PlatformFamily - h.KernelVersion = i.KernelVersion - h.HostID = i.HostID + if i, err := cpu.Info(); nil == err { + meta["CPU"] = i + } - return h, nil - } else { + if i, err := mem.SwapMemory(); nil == err { + meta["Memory_Swap"] = i + } + + if i, err := mem.VirtualMemory(); nil == err { + meta["Memory_Virtual"] = i + } + + if i, err := mem.VirtualMemory(); nil == err { + meta["Memory_Virtual"] = i + } + + if i, err := disk.Partitions(true); nil == err { + meta["Partitions"] = i + } + + if i, err := disk.Usage("/"); nil == err { + meta["Disk_Usage"] = i + } + + buf, err := json.Marshal(meta) + if nil != err { return nil, err } + + return &ocmi.InfraHostMachine{ + Meta: string(buf), + }, nil } -func getNetwork() (*ocmn.NoAuthProbeDescriptionNetwork, error) { - var ip net.IP - var iface string - var err error - if ip, iface, err = gateway.DiscoverGateway(); nil != err { +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 { return nil, err } @@ -83,39 +129,47 @@ func getNetwork() (*ocmn.NoAuthProbeDescriptionNetwork, error) { return nil, err } - idx := -1 + infraHostIPs := make([]*ocmi.InfraHostIP, 0) - for _idx, i := range interfaces { - if i.Name == iface { - idx = _idx - break + for _, i := range interfaces { + if i.Flags&net.FlagLoopback != 0 || i.Flags&net.FlagUp == 0 { + continue } - } - if -1 == idx { - return nil, errors.New("Interface of gateway is not exist") - } + addrs, err := i.Addrs() + if nil != err { + continue + } - n := &ocmn.NoAuthProbeDescriptionNetwork{} - - i := interfaces[idx] - - n.Name = i.Name - n.MacAddress = i.HardwareAddr.String() - n.Gateway = ip.String() - - if addrs, err := i.Addrs(); nil == err { - var buffer bytes.Buffer - for _idx, a := range addrs { - if 0 < _idx { - buffer.WriteString("|") + LOOP_ADDR: + for _, addr := range addrs { + _, ipNet, err := cun.ParseCIDR(addr.String()) + if nil != err { + continue } - buffer.WriteString(a.String()) + + infraHostIP := &ocmi.InfraHostIP{} + infraHostIP.Address = addr.String() + + switch ipNet.Version() { + case 4: + infraHostIP.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV4) + case 6: + infraHostIP.MetaIPType = ocmm.ToMetaIPType(ocmm.MetaIPTypeEnumV6) + default: + continue LOOP_ADDR + } + + infraHostIP.Mac = i.HardwareAddr.String() + infraHostIP.Iface = i.Name + + if gwIface == i.Name { + infraHostIP.Gateway = gwIP.String() + } + + infraHostIPs = append(infraHostIPs, infraHostIP) } - n.Address = buffer.String() - } else { - return nil, err } - return n, nil + return infraHostIPs, nil } diff --git a/auth/info/info_test.go b/auth/info/info_test.go new file mode 100644 index 0000000..dc4db54 --- /dev/null +++ b/auth/info/info_test.go @@ -0,0 +1,122 @@ +package info + +import ( + "reflect" + "testing" + + ocmi "git.loafle.net/overflow/commons-go/model/infra" +) + +func TestGetRegistHeader(t *testing.T) { + type args struct { + apiKey string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetRegistHeader(tt.args.apiKey) + if (err != nil) != tt.wantErr { + t.Errorf("GetRegistHeader() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetRegistHeader() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getInfraHost(t *testing.T) { + tests := []struct { + name string + want *ocmi.InfraHost + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getInfraHost() + if (err != nil) != tt.wantErr { + t.Errorf("getInfraHost() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getInfraHost() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getInfraHostMachine(t *testing.T) { + tests := []struct { + name string + want *ocmi.InfraHostMachine + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getInfraHostMachine() + if (err != nil) != tt.wantErr { + t.Errorf("getInfraHostMachine() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getInfraHostMachine() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getInfraHostOS(t *testing.T) { + tests := []struct { + name string + want *ocmi.InfraHostOS + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getInfraHostOS() + if (err != nil) != tt.wantErr { + t.Errorf("getInfraHostOS() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getInfraHostOS() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getInfraHostIPs(t *testing.T) { + tests := []struct { + name string + want []*ocmi.InfraHostIP + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getInfraHostIPs() + if (err != nil) != tt.wantErr { + t.Errorf("getInfraHostIPs() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getInfraHostIPs() = %v, want %v", got, tt.want) + } + }) + } +}