Skip to content

Commit

Permalink
Split collector output (#7)
Browse files Browse the repository at this point in the history
* collector: move ntp from sysinfo to tidb-insight

* save output of `collector` to seperate file base on content sections
  • Loading branch information
AstroProfundis authored and ethercflow committed May 22, 2018
1 parent 6f65d5c commit d4fb85b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
2 changes: 2 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Meta struct {
type Metrics struct {
Meta Meta `json:"meta"`
SysInfo sysinfo.SysInfo `json:"sysinfo"`
NTP TimeStat `json:"ntp"`
Partitions []BlockDev `json:"partitions"`
ProcStats []ProcessStat `json:"proc_stats"`
}
Expand All @@ -57,6 +58,7 @@ func main() {
func (metric *Metrics) getMetrics() {
metric.Meta.getMeta()
metric.SysInfo.GetSysInfo()
metric.NTP.getNTPInfo()
metric.Partitions = GetPartitionStats()
metric.ProcStats = GetProcStats()
}
Expand Down
85 changes: 85 additions & 0 deletions collector/ntp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright © 2018 PingCAP Inc.
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
//
// Use ntpq to get basic info of NTPd on the system

package main

import (
"bytes"
"log"
"os/exec"
"strconv"
"strings"
)

type TimeStat struct {
Ver string `json:"version,omitempty"`
Sync string `json:"sync,omitempty"`
Stratum int `json:"stratum,omitempty"`
Offset float64 `json:"offset,omitempty"`
Jitter float64 `json:"jitter,omitempty"`
Status string `json:"status,omitempty"`
}

func (ts *TimeStat) getNTPInfo() {
syncd, err := exec.LookPath("ntpq")
if err != nil {
ts.Ver = err.Error()
return
}

cmd := exec.Command(syncd, "-c rv", "127.0.0.1")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
log.Fatal(err)
}

// set default sync status to none
ts.Sync = "none"

output := strings.FieldsFunc(out.String(), multi_split)
for _, kv := range output {
tmp := strings.Split(strings.TrimSpace(kv), "=")
switch {
case tmp[0] == "version":
ts.Ver = strings.Trim(tmp[1], "\"")
case tmp[0] == "stratum":
ts.Stratum, err = strconv.Atoi(tmp[1])
if err != nil {
log.Fatal(err)
}
case tmp[0] == "offset":
ts.Offset, err = strconv.ParseFloat(tmp[1], 64)
if err != nil {
log.Fatal(err)
}
case tmp[0] == "sys_jitter":
ts.Jitter, err = strconv.ParseFloat(tmp[1], 64)
if err != nil {
log.Fatal(err)
}
case strings.Contains(tmp[0], "sync"):
ts.Sync = tmp[0]
case len(tmp) > 2 && strings.Contains(tmp[1], "status"):
// sample line of tmp: ["associd", "0 status", "0618 leap_none"]
ts.Status = strings.Split(tmp[2], " ")[0]
default:
continue
}
}
}

func multi_split(r rune) bool {
switch r {
case ',':
return true
case '\n':
return true
default:
return false
}
}
10 changes: 7 additions & 3 deletions insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def format_proc_info(self, keyname=None):

# collect data with `collector` and store it to disk
def collector(self):
# TODO: check existance of output dir
# TODO: warn on non-empty output dir

# call `collector` and store data to output dir
base_dir = os.path.join(util.pwd(), "../")
collector_exec = os.path.join(base_dir, "bin/collector")
collector_outdir = fileutils.create_dir(
os.path.join(self.full_outdir, "collector"))

stdout, stderr = util.run_cmd(collector_exec)
if stderr:
Expand All @@ -77,8 +78,11 @@ def collector(self):
except json.JSONDecodeError:
# TODO: unified output: "Error collecting system info.\n%s" % stderr
return
fileutils.write_file(os.path.join(self.full_outdir, "collector.json"),
json.dumps(self.collector_data, indent=2))

# save various info to seperate .json files
for k, v in self.collector_data.items():
fileutils.write_file(os.path.join(collector_outdir, "%s.json" % k),
json.dumps(v, indent=2))

def run_perf(self, args):
if not args.perf:
Expand Down

0 comments on commit d4fb85b

Please sign in to comment.