From b784cd07da534616b0c7b800d349e75418955d1b Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Tue, 10 Jan 2017 13:05:57 -0800 Subject: [PATCH] agent: update psn.NewCSV --- agent/server.go | 4 ++-- agent/system_metrics.go | 12 +++++++++--- glide.lock | 10 +++++----- glide.yaml | 6 +++--- vendor/github.com/gyuho/psn/filter.go | 5 +++-- vendor/github.com/gyuho/psn/proc.go | 26 +++++++++++++++++++++++-- vendor/github.com/gyuho/psn/proc_csv.go | 13 ++++++++++--- vendor/github.com/gyuho/psn/util.go | 25 ++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 20 deletions(-) diff --git a/agent/server.go b/agent/server.go index 0b550d2c..d3e9ac59 100644 --- a/agent/server.go +++ b/agent/server.go @@ -172,8 +172,8 @@ func (t *transporterServer) Transfer(ctx context.Context, r *agentpb.Request) (* plog.Infof("exiting %q", t.cmd.Path) }() - if err := collectMetrics(&globalFlags, t); err != nil { - plog.Errorf("collectMetrics error %v", err) + if err := startMetrics(&globalFlags, t); err != nil { + plog.Errorf("startMetrics error %v", err) return nil, err } diff --git a/agent/system_metrics.go b/agent/system_metrics.go index 61057923..f9aadf4b 100644 --- a/agent/system_metrics.go +++ b/agent/system_metrics.go @@ -17,13 +17,14 @@ package agent import ( "fmt" "os" + "path/filepath" "time" "github.com/gyuho/psn" ) -// collectMetrics starts collecting metrics. -func collectMetrics(fs *flags, t *transporterServer) error { +// startMetrics starts collecting metrics. +func startMetrics(fs *flags, t *transporterServer) error { if fs == nil || t == nil || t.cmd == nil { return fmt.Errorf("cannot find process to track (%+v, %+v)", fs, t) } @@ -34,7 +35,12 @@ func collectMetrics(fs *flags, t *transporterServer) error { return err } - c := psn.NewCSV(fs.systemMetricsLog, t.pid, fs.diskDevice, fs.networkInterface) + epath := filepath.Join(homeDir(), "etcd-client-num") + if err := toFile(fmt.Sprintf("%d", t.req.ClientNum), epath); err != nil { + return err + } + + c := psn.NewCSV(fs.systemMetricsLog, t.pid, fs.diskDevice, fs.networkInterface, epath) if err := c.Add(); err != nil { return err } diff --git a/glide.lock b/glide.lock index ca95a550..f155dd26 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 79e108074acefd584e2963e1c13d08ad27ac91ad481d791e98f5b99760f398b7 -updated: 2017-01-09T15:13:09.585723425-08:00 +hash: 9d9acbb855d6174855f681a9e5c363dc921c450a3be4b6eb2bcf0ddcba45bd41 +updated: 2017-01-10T13:05:19.177943713-08:00 imports: - name: bitbucket.org/zombiezen/gopdf version: 1c63dc69751bc45441c2ce1f56b631c55294b4d5 @@ -25,7 +25,7 @@ imports: - name: github.com/cheggaaa/pb version: 6e9d17711bb763b26b68b3931d47f24c1323abab - name: github.com/coreos/etcd - version: 8adfc06084c28320079a09ae4d08f09b29eebd04 + version: f0fa5ec507b9e5fa39612627829189b2984f00bd subpackages: - auth/authpb - client @@ -96,12 +96,12 @@ imports: - name: github.com/gyuho/dataframe version: 573cd728a011e5473510a6a1df0f39023c305e04 - name: github.com/gyuho/psn - version: be5928dfa765874e1d3a3f9e70fcaa6a6fcd250f + version: e11046c0ff777d30e27936a5a3acff8a56fa7fb0 subpackages: - process - schema - name: github.com/hashicorp/consul - version: a9afa0c27f484dd19fe59a80444e64e5352c4408 + version: 3e9c96d9965eae8a375f68433de9e0a4144ce823 subpackages: - api - name: github.com/hashicorp/go-cleanhttp diff --git a/glide.yaml b/glide.yaml index 6377378c..9773451d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -9,7 +9,7 @@ import: - package: github.com/cheggaaa/pb version: 6e9d17711bb763b26b68b3931d47f24c1323abab - package: github.com/coreos/etcd - version: 8adfc06084c28320079a09ae4d08f09b29eebd04 + version: f0fa5ec507b9e5fa39612627829189b2984f00bd subpackages: - auth/authpb - client @@ -53,11 +53,11 @@ import: - package: github.com/gyuho/dataframe version: 573cd728a011e5473510a6a1df0f39023c305e04 - package: github.com/gyuho/psn - version: be5928dfa765874e1d3a3f9e70fcaa6a6fcd250f + version: e11046c0ff777d30e27936a5a3acff8a56fa7fb0 subpackages: - process - package: github.com/hashicorp/consul - version: a9afa0c27f484dd19fe59a80444e64e5352c4408 + version: 3e9c96d9965eae8a375f68433de9e0a4144ce823 subpackages: - api - package: github.com/samuel/go-zookeeper diff --git a/vendor/github.com/gyuho/psn/filter.go b/vendor/github.com/gyuho/psn/filter.go index 060d0ccd..72644e8b 100644 --- a/vendor/github.com/gyuho/psn/filter.go +++ b/vendor/github.com/gyuho/psn/filter.go @@ -9,8 +9,9 @@ import ( type EntryFilter struct { ProgramMatchFunc func(string) bool program string - PID int64 - TopLimit int + + PID int64 + TopLimit int // for ss TCP bool diff --git a/vendor/github.com/gyuho/psn/proc.go b/vendor/github.com/gyuho/psn/proc.go index 8bd609a1..a724c4e0 100644 --- a/vendor/github.com/gyuho/psn/proc.go +++ b/vendor/github.com/gyuho/psn/proc.go @@ -2,6 +2,7 @@ package psn import ( "fmt" + "io/ioutil" "time" ) @@ -24,10 +25,13 @@ type Proc struct { TransmitPacketsDiff uint64 ReceiveBytesNumDiff uint64 TransmitBytesNumDiff uint64 + + // Extra exists to support customized data query. + Extra []byte } // GetProc returns current 'Proc' data. -func GetProc(pid int64, diskDevice string, networkInterface string) (Proc, error) { +func GetProc(pid int64, diskDevice string, networkInterface string, extraPath string) (Proc, error) { proc := Proc{UnixTS: time.Now().Unix()} errc := make(chan error) @@ -75,9 +79,23 @@ func GetProc(pid int64, diskDevice string, networkInterface string) (Proc, error } errc <- nil }() + go func() { + f, err := openToRead(extraPath) + if err != nil { + errc <- err + return + } + b, err := ioutil.ReadAll(f) + if err != nil { + errc <- err + return + } + proc.Extra = b + errc <- nil + }() cnt := 0 - for cnt != 3 { + for cnt != 4 { err := <-errc if err != nil { return Proc{}, err @@ -118,6 +136,8 @@ func init() { "TRANSMIT-PACKETS-DIFF", "RECEIVE-BYTES-NUM-DIFF", "TRANSMIT-BYTES-NUM-DIFF", + + "EXTRA", ) for i, v := range ProcHeader { @@ -173,5 +193,7 @@ func (p *Proc) ToRow() (row []string) { row[37] = fmt.Sprintf("%d", p.ReceiveBytesNumDiff) // RECEIVE-BYTES-NUM-DIFF row[38] = fmt.Sprintf("%d", p.TransmitBytesNumDiff) // TRANSMIT-BYTES-NUM-DIFF + row[39] = string(p.Extra) // EXTRA + return } diff --git a/vendor/github.com/gyuho/psn/proc_csv.go b/vendor/github.com/gyuho/psn/proc_csv.go index cf8e9db4..5d804125 100644 --- a/vendor/github.com/gyuho/psn/proc_csv.go +++ b/vendor/github.com/gyuho/psn/proc_csv.go @@ -21,12 +21,15 @@ type CSV struct { MinUnixTS int64 MaxUnixTS int64 + // ExtraPath contains extra information. + ExtraPath string + // Rows are sorted by unix seconds. Rows []Proc } // NewCSV returns a new CSV. -func NewCSV(fpath string, pid int64, diskDevice string, networkInterface string) *CSV { +func NewCSV(fpath string, pid int64, diskDevice string, networkInterface string, extraPath string) *CSV { return &CSV{ FilePath: fpath, PID: pid, @@ -39,14 +42,15 @@ func NewCSV(fpath string, pid int64, diskDevice string, networkInterface string) MinUnixTS: 0, MaxUnixTS: 0, - Rows: []Proc{}, + ExtraPath: extraPath, + Rows: []Proc{}, } } // Add is to be called periodically to add a row to CSV. // It only appends to CSV. And it estimates empty rows by unix seconds. func (c *CSV) Add() error { - cur, err := GetProc(c.PID, c.DiskDevice, c.NetworkInterface) + cur, err := GetProc(c.PID, c.DiskDevice, c.NetworkInterface, c.ExtraPath) if err != nil { return err } @@ -94,6 +98,9 @@ func (c *CSV) Add() error { // estimate the previous ones based on 'prev' and 'cur' mid := prev + // Extra; just use the previous value + mid.Extra = prev.Extra + // PSEntry; just use average since some metrisc might decrease mid.PSEntry.FD = prev.PSEntry.FD + (cur.PSEntry.FD-prev.PSEntry.FD)/2 mid.PSEntry.Threads = prev.PSEntry.Threads + (cur.PSEntry.Threads-prev.PSEntry.Threads)/2 diff --git a/vendor/github.com/gyuho/psn/util.go b/vendor/github.com/gyuho/psn/util.go index 7a62df99..9f486601 100644 --- a/vendor/github.com/gyuho/psn/util.go +++ b/vendor/github.com/gyuho/psn/util.go @@ -2,6 +2,7 @@ package psn import ( "os" + "runtime" "strconv" "strings" "time" @@ -53,3 +54,27 @@ func openToOverwrite(fpath string) (*os.File, error) { } return f, nil } + +func toFile(data []byte, fpath string) error { + f, err := openToOverwrite(fpath) + if err != nil { + f, err = os.Create(fpath) + if err != nil { + return err + } + } + _, err = f.Write(data) + f.Close() + return err +} + +func homeDir() string { + if runtime.GOOS == "windows" { + home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + if home == "" { + home = os.Getenv("USERPROFILE") + } + return home + } + return os.Getenv("HOME") +}