-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
199 lines (180 loc) · 4.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"github.com/nakabonne/gosivy/diagnoser"
"github.com/nakabonne/gosivy/process"
)
const defaultScrapeInterval = time.Second
var (
flagSet = flag.NewFlagSet("gosivy", flag.ContinueOnError)
// Automatically populated by goreleaser during build
version = "unversioned"
commit = "?"
date = "?"
)
type cli struct {
debug bool
version bool
list bool
scrapeInterval time.Duration
stdout io.Writer
stderr io.Writer
diagnoser diagnoser.Diagnoser
}
func (c *cli) usage() {
format := `Usage:
gosivy [flags] <pid|host:port>
Flags:
%s
Examples:
gosivy 15788
gosivy host.xz:8080
Author:
Ryo Nakao <[email protected]>
`
fmt.Fprintf(c.stderr, format, flagSet.FlagUsages())
}
func main() {
c, err := parseFlags(os.Stdout, os.Stderr)
if err != nil {
os.Exit(0)
}
code := c.run(flagSet.Args())
if code != 0 {
c.usage()
os.Exit(code)
}
}
func parseFlags(stdout, stderr io.Writer) (*cli, error) {
c := &cli{
stdout: stdout,
stderr: stderr,
}
flagSet.BoolVarP(&c.version, "version", "v", false, "Print the current version.")
flagSet.BoolVar(&c.debug, "debug", false, "Run in debug mode.")
flagSet.BoolVarP(&c.list, "list-processes", "l", false, "Show processes where gosivy agent runs on.")
flagSet.DurationVar(&c.scrapeInterval, "scrape-interval", defaultScrapeInterval, "Interval to scrape from the agent. It must be >= 1s")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
fmt.Fprintln(c.stderr, err)
}
return nil, err
}
return c, nil
}
func (c *cli) run(args []string) int {
if c.version {
fmt.Fprintf(c.stderr, "version=%s, commit=%s, buildDate=%s, os=%s, arch=%s\n", version, commit, date, runtime.GOOS, runtime.GOARCH)
return 0
}
if err := c.validate(); err != nil {
fmt.Fprintln(c.stderr, err)
return 1
}
if err := setLogger(nil, c.debug); err != nil {
fmt.Fprintf(c.stderr, "failed to prepare for debugging: %v\n", err)
return 1
}
if c.list {
ps, err := process.FindAll()
if err != nil {
fmt.Fprintf(c.stderr, "failed to list processes: %v\n", err)
return 1
}
fmt.Fprintf(c.stderr, "%v", ps)
return 0
}
var pid string
if len(args) == 0 {
// Automatically finds the process where the agent runs on if no args given.
p, err := process.FindOne()
if err != nil {
fmt.Fprintln(c.stderr, err)
return 1
}
pid = strconv.Itoa(p.PID)
} else {
pid = args[0]
}
addr, err := targetToAddr(pid)
if err != nil {
fmt.Fprintf(c.stderr, "failed to convert args into addresses: %v\n", err)
return 1
}
if c.diagnoser == nil {
c.diagnoser = diagnoser.NewDiagnoser(addr, c.scrapeInterval, nil)
}
if err := c.diagnoser.Run(); err != nil {
fmt.Fprintf(c.stderr, "failed to start diagnoser: %s\n", err.Error())
return 1
}
return 0
}
func (c *cli) validate() error {
if c.scrapeInterval < time.Second {
return fmt.Errorf(`"--scrape-interval" must be >= 1s`)
}
return nil
}
// targetToAddr parses the target string (pid or host:port),
// and converts it into the address of a TCP end point.
func targetToAddr(target string) (*net.TCPAddr, error) {
// The case of "host:port"
if strings.Contains(target, ":") {
var err error
addr, err := net.ResolveTCPAddr("tcp", target)
if err != nil {
return nil, fmt.Errorf("couldn't parse dst address: %w", err)
}
return addr, nil
}
// The case of PID.
// Find port by pid then, connect to local
pid, err := strconv.Atoi(target)
if err != nil {
return nil, fmt.Errorf("couldn't parse PID: %w", err)
}
port, err := process.GetPort(pid)
if err != nil {
return nil, fmt.Errorf("couldn't get port for PID %v: %w", pid, err)
}
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:"+port)
return addr, nil
}
// Makes a new file under the config directory only when debug use.
func setLogger(w io.Writer, debug bool) error {
if !debug {
logrus.SetOutput(ioutil.Discard)
return nil
}
if w == nil {
var err error
cfgDir, err := process.ConfigDir()
if err != nil {
return err
}
if err := os.MkdirAll(cfgDir, os.ModePerm); err != nil {
return err
}
w, err = os.OpenFile(filepath.Join(cfgDir, "debug.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return err
}
}
logrus.SetOutput(w)
logrus.SetLevel(logrus.TraceLevel)
return nil
}