-
Notifications
You must be signed in to change notification settings - Fork 17
/
environment_linux.go
165 lines (155 loc) · 4.4 KB
/
environment_linux.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
// Copyright (C) 2014 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monitor
import "C"
import (
"fmt"
"io"
"io/ioutil"
"os"
"syscall"
)
func registerPlatformEnvironment(group *MonitorGroup) {
group.Chain("rusage", MonitorFunc(
func(cb func(name string, val float64)) {
var rusage syscall.Rusage
err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
if err != nil {
logger.Errorf("failed getting rusage data: %s", err)
return
}
MonitorStruct(&rusage, cb)
}))
group.Chain("proc.stat", MonitorFunc(
func(cb func(name string, val float64)) {
var stat procSelfStat
err := readProcSelfStat(&stat)
if err != nil {
logger.Errorf("failed getting /proc/self/stat data: %s", err)
return
}
MonitorStruct(&stat, cb)
}))
group.Chain("proc.statm", MonitorFunc(
func(cb func(name string, val float64)) {
var stat procSelfStatm
err := readProcSelfStatm(&stat)
if err != nil {
logger.Errorf("failed getting /proc/self/statm data: %s", err)
return
}
MonitorStruct(&stat, cb)
}))
}
func fdCount() (count int, err error) {
f, err := os.Open("/proc/self/fd")
if err != nil {
return 0, err
}
defer f.Close()
for {
names, err := f.Readdirnames(4096)
count += len(names)
if err != nil {
if err == io.EOF {
return count, nil
}
return count, err
}
}
}
func openProc() (*os.File, error) {
return os.Open("/proc/self/exe")
}
type procSelfStat struct {
Pid C.int
Comm string
State byte
Ppid C.int
Pgrp C.int
Session C.int
TtyNr C.int
Tpgid C.int
Flags C.uint
Minflt C.ulong
Cminflt C.ulong
Majflt C.ulong
Cmajflt C.ulong
Utime C.ulong
Stime C.ulong
Cutime C.long
Cstime C.long
Priority C.long
Nice C.long
NumThreads C.long
Itrealvalue C.long
Starttime C.ulonglong
Vsize C.ulong
Rss C.long
Rsslim C.ulong
Startcode C.ulong
Endcode C.ulong
Startstack C.ulong
Kstkesp C.ulong
Kstkeip C.ulong
Signal C.ulong
Blocked C.ulong
Sigignore C.ulong
Sigcatch C.ulong
Wchan C.ulong
Nswap C.ulong
Cnswap C.ulong
ExitSignal C.int
Processor C.int
RtPriority C.uint
Policy C.uint
DelayAcctBlkioTicks C.ulonglong
GuestTime C.ulong
CguestTime C.long
}
func readProcSelfStat(s *procSelfStat) error {
data, err := ioutil.ReadFile("/proc/self/stat")
if err != nil {
return err
}
_, err = fmt.Sscanf(string(data), "%d %s %c %d %d %d %d %d %d %d %d "+
"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "+
"%d %d %d %d %d %d %d %d %d %d", &s.Pid, &s.Comm, &s.State, &s.Ppid,
&s.Pgrp, &s.Session, &s.TtyNr, &s.Tpgid, &s.Flags, &s.Minflt, &s.Cminflt,
&s.Majflt, &s.Cmajflt, &s.Utime, &s.Stime, &s.Cutime, &s.Cstime,
&s.Priority, &s.Nice, &s.NumThreads, &s.Itrealvalue, &s.Starttime,
&s.Vsize, &s.Rss, &s.Rsslim, &s.Startcode, &s.Endcode, &s.Startstack,
&s.Kstkesp, &s.Kstkeip, &s.Signal, &s.Blocked, &s.Sigignore, &s.Sigcatch,
&s.Wchan, &s.Nswap, &s.Cnswap, &s.ExitSignal, &s.Processor, &s.RtPriority,
&s.Policy, &s.DelayAcctBlkioTicks, &s.GuestTime, &s.CguestTime)
return err
}
type procSelfStatm struct {
Size int
Resident int
Share int
Text int
Lib int
Data int
Dt int
}
func readProcSelfStatm(s *procSelfStatm) error {
data, err := ioutil.ReadFile("/proc/self/statm")
if err != nil {
return err
}
_, err = fmt.Sscanf(string(data), "%d %d %d %d %d %d %d", &s.Size,
&s.Resident, &s.Share, &s.Text, &s.Lib, &s.Data, &s.Dt)
return err
}