Skip to content

Commit d4fb85b

Browse files
AstroProfundisethercflow
authored andcommitted
Split collector output (#7)
* collector: move ntp from sysinfo to tidb-insight * save output of `collector` to seperate file base on content sections
1 parent 6f65d5c commit d4fb85b

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

collector/collector.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Meta struct {
3838
type Metrics struct {
3939
Meta Meta `json:"meta"`
4040
SysInfo sysinfo.SysInfo `json:"sysinfo"`
41+
NTP TimeStat `json:"ntp"`
4142
Partitions []BlockDev `json:"partitions"`
4243
ProcStats []ProcessStat `json:"proc_stats"`
4344
}
@@ -57,6 +58,7 @@ func main() {
5758
func (metric *Metrics) getMetrics() {
5859
metric.Meta.getMeta()
5960
metric.SysInfo.GetSysInfo()
61+
metric.NTP.getNTPInfo()
6062
metric.Partitions = GetPartitionStats()
6163
metric.ProcStats = GetProcStats()
6264
}

collector/ntp.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright © 2018 PingCAP Inc.
2+
//
3+
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
4+
//
5+
// Use ntpq to get basic info of NTPd on the system
6+
7+
package main
8+
9+
import (
10+
"bytes"
11+
"log"
12+
"os/exec"
13+
"strconv"
14+
"strings"
15+
)
16+
17+
type TimeStat struct {
18+
Ver string `json:"version,omitempty"`
19+
Sync string `json:"sync,omitempty"`
20+
Stratum int `json:"stratum,omitempty"`
21+
Offset float64 `json:"offset,omitempty"`
22+
Jitter float64 `json:"jitter,omitempty"`
23+
Status string `json:"status,omitempty"`
24+
}
25+
26+
func (ts *TimeStat) getNTPInfo() {
27+
syncd, err := exec.LookPath("ntpq")
28+
if err != nil {
29+
ts.Ver = err.Error()
30+
return
31+
}
32+
33+
cmd := exec.Command(syncd, "-c rv", "127.0.0.1")
34+
var out bytes.Buffer
35+
cmd.Stdout = &out
36+
err = cmd.Run()
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
// set default sync status to none
42+
ts.Sync = "none"
43+
44+
output := strings.FieldsFunc(out.String(), multi_split)
45+
for _, kv := range output {
46+
tmp := strings.Split(strings.TrimSpace(kv), "=")
47+
switch {
48+
case tmp[0] == "version":
49+
ts.Ver = strings.Trim(tmp[1], "\"")
50+
case tmp[0] == "stratum":
51+
ts.Stratum, err = strconv.Atoi(tmp[1])
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
case tmp[0] == "offset":
56+
ts.Offset, err = strconv.ParseFloat(tmp[1], 64)
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
case tmp[0] == "sys_jitter":
61+
ts.Jitter, err = strconv.ParseFloat(tmp[1], 64)
62+
if err != nil {
63+
log.Fatal(err)
64+
}
65+
case strings.Contains(tmp[0], "sync"):
66+
ts.Sync = tmp[0]
67+
case len(tmp) > 2 && strings.Contains(tmp[1], "status"):
68+
// sample line of tmp: ["associd", "0 status", "0618 leap_none"]
69+
ts.Status = strings.Split(tmp[2], " ")[0]
70+
default:
71+
continue
72+
}
73+
}
74+
}
75+
76+
func multi_split(r rune) bool {
77+
switch r {
78+
case ',':
79+
return true
80+
case '\n':
81+
return true
82+
default:
83+
return false
84+
}
85+
}

insight.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ def format_proc_info(self, keyname=None):
6262

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

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

7273
stdout, stderr = util.run_cmd(collector_exec)
7374
if stderr:
@@ -77,8 +78,11 @@ def collector(self):
7778
except json.JSONDecodeError:
7879
# TODO: unified output: "Error collecting system info.\n%s" % stderr
7980
return
80-
fileutils.write_file(os.path.join(self.full_outdir, "collector.json"),
81-
json.dumps(self.collector_data, indent=2))
81+
82+
# save various info to seperate .json files
83+
for k, v in self.collector_data.items():
84+
fileutils.write_file(os.path.join(collector_outdir, "%s.json" % k),
85+
json.dumps(v, indent=2))
8286

8387
def run_perf(self, args):
8488
if not args.perf:

0 commit comments

Comments
 (0)