|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -//+build,freebsd,cgo |
19 | | - |
20 | 18 | package freebsd |
21 | 19 |
|
22 | 20 | // #cgo LDFLAGS: -lkvm -lprocstat |
@@ -67,14 +65,15 @@ import "C" |
67 | 65 |
|
68 | 66 | import ( |
69 | 67 | "os" |
| 68 | + "io/ioutil" |
70 | 69 | "strconv" |
71 | 70 | "strings" |
72 | 71 | "syscall" |
73 | 72 | "time" |
74 | 73 |
|
75 | 74 | "github.com/pkg/errors" |
76 | 75 |
|
77 | | - "github.com/redanthrax/go-sysinfo/types" |
| 76 | + "github.com/elastic/go-sysinfo/types" |
78 | 77 | ) |
79 | 78 |
|
80 | 79 | func getProcInfo(op int, arg int) ([]process, error) { |
@@ -103,6 +102,10 @@ func getProcInfo(op int, arg int) ([]process, error) { |
103 | 102 | return procs, nil |
104 | 103 | } |
105 | 104 |
|
| 105 | +func (p *process) path(pa ...string) string { |
| 106 | + return p.fs.path(append([]string{strconv.Itoa(p.PID())}, pa...)...) |
| 107 | +} |
| 108 | + |
106 | 109 | func copyArray(from **C.char) []string { |
107 | 110 | if from == nil { |
108 | 111 | return nil |
@@ -215,6 +218,7 @@ func getProcCWD(p *process) (string, error) { |
215 | 218 |
|
216 | 219 | type process struct { |
217 | 220 | pid int |
| 221 | + fs procFS |
218 | 222 | kinfo C.struct_kinfo_proc |
219 | 223 | } |
220 | 224 |
|
@@ -300,6 +304,30 @@ func (p *process) User() (types.UserInfo, error) { |
300 | 304 | }, nil |
301 | 305 | } |
302 | 306 |
|
| 307 | + |
| 308 | +// NetworkStats reports network stats for an individual PID. |
| 309 | +func (p *process) NetworkCounters() (*types.NetworkCountersInfo, error) { |
| 310 | + snmpRaw, err := ioutil.ReadFile(p.path("net/snmp")) |
| 311 | + if err != nil { |
| 312 | + return nil, err |
| 313 | + } |
| 314 | + snmp, err := getNetSnmpStats(snmpRaw) |
| 315 | + if err != nil { |
| 316 | + return nil, err |
| 317 | + } |
| 318 | + |
| 319 | + netstatRaw, err := ioutil.ReadFile(p.path("net/netstat")) |
| 320 | + if err != nil { |
| 321 | + return nil, err |
| 322 | + } |
| 323 | + netstat, err := getNetstatStats(netstatRaw) |
| 324 | + if err != nil { |
| 325 | + return nil, err |
| 326 | + } |
| 327 | + |
| 328 | + return &types.NetworkCountersInfo{SNMP: snmp, Netstat: netstat}, nil |
| 329 | +} |
| 330 | + |
303 | 331 | func (p *process) PID() int { |
304 | 332 | return p.pid |
305 | 333 | } |
|
0 commit comments