-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (131 loc) · 2.9 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
package main
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/docopt/docopt-go"
"github.com/prometheus/procfs"
"github.com/prometheus/procfs/sysfs"
)
func Usage() string {
usage := `
Usage:
procolli <fpath> [--watch <ms>]
procolli (-h|--help)
Description:
Read proc, as JSON.
Options:
--watch <ms> Read and display the requested file at the requested interval
Supported Files:
General Files:
`
for _, dir := range SupportedFiles() {
usage = usage + " " + dir.Base + "\n"
}
usage = usage + " Sys Files:\n"
for _, dir := range SupportedSysFiles() {
usage = usage + " " + dir.Base + "\n"
}
usage = usage + " Pid Specific Files:\n"
for _, dir := range PidFiles(1234) {
usage = usage + " " + dir.Base + "\n"
}
return usage
}
func findGeneralProcFile(fpath string, proc procfs.FS) (*ProcFile, error) {
for _, pdir := range SupportedFiles() {
if strings.HasPrefix(fpath, pdir.Base) {
return &pdir, nil
}
}
return nil, nil
}
func findSysFile(fpath string, fs sysfs.FS) (*SysFile, error) {
for _, sdir := range SupportedSysFiles() {
if strings.HasPrefix(fpath, sdir.Base) {
return &sdir, nil
}
}
return nil, nil
}
func findPidProfFile(pid int, fpath string, pidFs procfs.Proc) (*ProcPidFile, error) {
for _, pdir := range PidFiles(pid) {
if strings.HasPrefix(fpath, pdir.Base) {
return &pdir, nil
}
}
return nil, nil
}
func handleError(err error) {
if err != nil {
panic(err)
}
}
func main() {
arguments, _ := docopt.ParseDoc(Usage())
fpath, _ := arguments.String("<fpath>")
ms, _ := arguments.Int("--watch")
proc, err := procfs.NewFS("/proc")
handleError(err)
pfile, err := findGeneralProcFile(fpath, proc)
handleError(err)
if pfile != nil {
for {
json, err := pfile.Show(fpath, proc)
handleError(err)
fmt.Println(json)
if ms == 0 {
break
} else {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
os.Exit(0)
}
sys, err := sysfs.NewFS("/sys")
handleError(err)
sfile, err := findSysFile(fpath, sys)
handleError(err)
if sfile != nil {
for {
json, err := sfile.Show(fpath, sys)
handleError(err)
fmt.Println(json)
if ms == 0 {
break
} else {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
os.Exit(0)
}
// try foo
pidPattern := regexp.MustCompile("/proc/([0-9]+)")
pidMatch := pidPattern.FindStringSubmatch(fpath)
if len(pidMatch) == 2 {
pid, err := strconv.Atoi(pidMatch[1])
handleError(err)
pidFs, err := proc.Proc(pid)
handleError(err)
pdir, err := findPidProfFile(pid, fpath, pidFs)
handleError(err)
if pdir != nil {
for {
json, err := pdir.Show(fpath, pidFs)
handleError(err)
fmt.Println(json)
if ms == 0 {
break
} else {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
}
os.Exit(0)
}
panic(errors.New("procolli: " + fpath + " is not supported. See --help for supported files."))
}