Skip to content

Commit

Permalink
更新缓存vm抓取实例的key为请求参数的sha1哈希值
Browse files Browse the repository at this point in the history
  • Loading branch information
wuqingtao committed Jul 3, 2020
1 parent caee8aa commit bb8c729
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/collector/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions internal/collector/metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package collector

import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -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 == "" {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bb8c729

Please sign in to comment.