diff --git a/internal/collector/host.go b/internal/collector/host.go index df29af4..381076a 100644 --- a/internal/collector/host.go +++ b/internal/collector/host.go @@ -15,12 +15,13 @@ func Get(opts *Options, logger *log.Logger) (*VMMetrics, error) { if err := opts.Valid(); err != nil { return nil, err } - v, ok := hosts.Load(opts.Host) + mac := opts.mac() + v, ok := hosts.Load(mac) if ok { return v.(*VMMetrics), nil } vm := NewVMMetrics(opts) - hosts.Store(opts.Host, vm) + hosts.Store(mac, vm) return vm, nil } diff --git a/internal/collector/metrics.go b/internal/collector/metrics.go index d69cc5b..eb4a1d5 100644 --- a/internal/collector/metrics.go +++ b/internal/collector/metrics.go @@ -1,7 +1,10 @@ package collector import ( + "bytes" "context" + "crypto/sha1" + "encoding/hex" "errors" "fmt" "net/http" @@ -214,6 +217,16 @@ type Options struct { Host, Username, Password string } +func (o Options) mac() string { + var buf bytes.Buffer + defer buf.Reset() + buf.WriteString(o.Host) + buf.WriteString(o.Username) + buf.WriteString(o.Password) + arr := sha1.Sum(buf.Bytes()) + return hex.EncodeToString(arr[:]) +} + // Valid 验证请求参数 func (o *Options) Valid() error { if o.Host == "" { diff --git a/main.go b/main.go index 77ebcef..c50c28b 100644 --- a/main.go +++ b/main.go @@ -100,6 +100,7 @@ func handleMulti(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "读取指标失败") return } + // logger.Debugf("target:%s,username:%s,password:%s", opts.Host, opts.Username, opts.Password) timeout := r.FormValue("timeout") d, err := getTimeout(timeout) if err != nil {