This commit is contained in:
crusader 2018-06-13 18:27:04 +09:00
parent 6f55a0a6fd
commit 07c068fa89
5 changed files with 253 additions and 67 deletions

26
Gopkg.lock generated
View File

@ -34,7 +34,7 @@
"protocol/json", "protocol/json",
"registry" "registry"
] ]
revision = "1092f7a7ae44ef2f907579af217f1a594e48959c" revision = "dcc3af07239b3f6fcbae3529bcb52c522b02053d"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -61,7 +61,7 @@
"time/scheduler/storage", "time/scheduler/storage",
"time/scheduler/task" "time/scheduler/task"
] ]
revision = "1966a985759721fd451171a78a0b8f4524afc644" revision = "4d4017d214d2a8fdde59d774254f421991fabe7e"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -72,12 +72,12 @@
"config/probe", "config/probe",
"core/annotation", "core/annotation",
"core/config", "core/config",
"core/constants",
"core/interfaces", "core/interfaces",
"core/util", "core/util",
"model/data", "model/data",
"model/discovery", "model/discovery",
"model/domain", "model/domain",
"model/infra",
"model/member", "model/member",
"model/meta", "model/meta",
"model/noauthprobe", "model/noauthprobe",
@ -85,7 +85,7 @@
"model/sensorconfig", "model/sensorconfig",
"service/probe" "service/probe"
] ]
revision = "078a39712d33c131303cf2705cca185da18cc196" revision = "ab936be3035ee878cebd09e14d8e2f6baf56b670"
[[projects]] [[projects]]
name = "github.com/BurntSushi/toml" name = "github.com/BurntSushi/toml"
@ -137,14 +137,24 @@
version = "v1.1" version = "v1.1"
[[projects]] [[projects]]
branch = "master"
name = "github.com/shirou/gopsutil" name = "github.com/shirou/gopsutil"
packages = [ packages = [
"cpu",
"disk",
"host", "host",
"internal/common", "internal/common",
"mem",
"net",
"process" "process"
] ]
revision = "c95755e4bcd7a62bb8bd33f3a597a7c7f35e2cf3" revision = "c23bcca55e77b8389d84b09db8c5ac2b472070ef"
version = "v2.18.04"
[[projects]]
branch = "master"
name = "github.com/shirou/w32"
packages = ["."]
revision = "bb4de0191aa41b5507caa14b0650cdbddcd9280b"
[[projects]] [[projects]]
name = "github.com/valyala/fasthttp" name = "github.com/valyala/fasthttp"
@ -187,7 +197,7 @@
"unix", "unix",
"windows" "windows"
] ]
revision = "d0faeb539838e250bd0a9db4182d48d4a1915181" revision = "a9e25c09b96b8870693763211309e213c6ef299d"
[[projects]] [[projects]]
name = "gopkg.in/yaml.v2" name = "gopkg.in/yaml.v2"
@ -198,6 +208,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "81bd45952fac7d58527d174bfee2904c05bdba354ed4a77218d8d2dc11d28668" inputs-digest = "769d28026d17f8f235bacca2cecf1de3a4930d13e2e43abdc29928aaf220ab7d"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -54,8 +54,8 @@
name = "git.loafle.net/overflow/commons-go" name = "git.loafle.net/overflow/commons-go"
[[constraint]] [[constraint]]
branch = "master"
name = "github.com/shirou/gopsutil" name = "github.com/shirou/gopsutil"
version = "2.18.4"
[[constraint]] [[constraint]]
name = "github.com/valyala/fasthttp" name = "github.com/valyala/fasthttp"

Binary file not shown.

View File

@ -1,15 +1,20 @@
package info package info
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "log"
"net" "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" 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/host"
"github.com/shirou/gopsutil/mem"
) )
func GetRegistHeader(apiKey string) (string, error) { func GetRegistHeader(apiKey string) (string, error) {
@ -18,63 +23,104 @@ func GetRegistHeader(apiKey string) (string, error) {
APIKey: apiKey, APIKey: apiKey,
} }
var napd *ocmn.NoAuthProbeDescription var infraHost *ocmi.InfraHost
if napd, err = getDescription(); nil != err { if infraHost, err = getInfraHost(); nil != err {
return "", err return "", err
} }
var buf []byte var buf []byte
if buf, err = json.Marshal(napd); nil != err { if buf, err = json.Marshal(infraHost); nil != err {
return "", err return "", err
} }
nap.Description = string(buf)
nap.InfraHostMeta = string(buf)
if buf, err = json.Marshal(nap); nil != err { if buf, err = json.Marshal(nap); nil != err {
return "", err return "", err
} }
log.Printf("nap: %s", string(buf))
enc := base64.StdEncoding.EncodeToString(buf) enc := base64.StdEncoding.EncodeToString(buf)
return enc, nil return enc, nil
} }
func getDescription() (*ocmn.NoAuthProbeDescription, error) { func getInfraHost() (*ocmi.InfraHost, error) {
var err 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 return nil, err
} }
if napd.Network, err = getNetwork(); nil != err { return infraHost, nil
return nil, err
}
return napd, nil
} }
func getHost() (*ocmn.NoAuthProbeDescriptionHost, error) { func getInfraHostMachine() (*ocmi.InfraHostMachine, error) {
if i, err := host.Info(); nil == err { meta := make(map[string]interface{})
h := &ocmn.NoAuthProbeDescriptionHost{}
h.Name = i.Hostname if i, err := cpu.Info(); nil == err {
h.OS = i.OS meta["CPU"] = i
h.Platform = i.Platform }
h.PlatformFamily = i.PlatformFamily
h.KernelVersion = i.KernelVersion
h.HostID = i.HostID
return h, nil if i, err := mem.SwapMemory(); nil == err {
} else { 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 nil, err
} }
return &ocmi.InfraHostMachine{
Meta: string(buf),
}, nil
} }
func getNetwork() (*ocmn.NoAuthProbeDescriptionNetwork, error) { func getInfraHostOS() (*ocmi.InfraHostOS, error) {
var ip net.IP i, err := host.Info()
var iface string if nil != err {
var err error return nil, err
if ip, iface, err = gateway.DiscoverGateway(); 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 return nil, err
} }
@ -83,39 +129,47 @@ func getNetwork() (*ocmn.NoAuthProbeDescriptionNetwork, error) {
return nil, err return nil, err
} }
idx := -1 infraHostIPs := make([]*ocmi.InfraHostIP, 0)
for _idx, i := range interfaces { for _, i := range interfaces {
if i.Name == iface { if i.Flags&net.FlagLoopback != 0 || i.Flags&net.FlagUp == 0 {
idx = _idx continue
break }
addrs, err := i.Addrs()
if nil != err {
continue
}
LOOP_ADDR:
for _, addr := range addrs {
_, ipNet, err := cun.ParseCIDR(addr.String())
if nil != err {
continue
}
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)
} }
} }
if -1 == idx { return infraHostIPs, nil
return nil, errors.New("Interface of gateway is not exist")
}
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("|")
}
buffer.WriteString(a.String())
}
n.Address = buffer.String()
} else {
return nil, err
}
return n, nil
} }

122
auth/info/info_test.go Normal file
View File

@ -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)
}
})
}
}