agent: update psn.NewCSV

This commit is contained in:
Gyu-Ho Lee 2017-01-10 13:05:57 -08:00
parent 8fd7ac68bd
commit b784cd07da
No known key found for this signature in database
GPG Key ID: 1DDD39C7EB70C24C
8 changed files with 81 additions and 20 deletions

View File

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

View File

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

10
glide.lock generated
View File

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

View File

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

View File

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

26
vendor/github.com/gyuho/psn/proc.go generated vendored
View File

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

View File

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

25
vendor/github.com/gyuho/psn/util.go generated vendored
View File

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