forked from mohae/joefriday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joefriday.go
309 lines (270 loc) · 7.32 KB
/
joefriday.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2016 The JoeFriday authors.
// 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 joefriday gets information about a system: platform, kernel,
// memory information, cpu information, cpu stats, cpu utilization, network
// information, and network usage.
//
// Ticker versions of non-static information are available to enable
// monitoring.
//
// The data can be returned as Go structs, Flatbuffer serialized bytes, or
// JSON serialized bytes. For convenience, there are deserialization
// functions for all structs that are serialized.
package joefriday
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"github.com/mohae/randchars"
)
const SysFSSystem = "/sys/devices/system"
type ResetError struct {
Err error
}
func (e *ResetError) Error() string {
if e == nil {
return "<nil>"
}
return e.Err.Error()
}
type ParseError struct {
Info string
Err error
}
func (e *ParseError) Error() string {
if e == nil {
return "<nil>"
}
s := e.Info
if e.Info != "" {
s += ": "
}
s += e.Err.Error()
return s
}
type ReadError struct {
Info string
Err error
}
func (e *ReadError) Error() string {
if e == nil {
return "<nil>"
}
s := e.Info
if e.Info != "" {
s += ": "
}
s += e.Err.Error()
return s
}
// IsReadError returns a boolean indicating whether the error is a result of
// a read problem.
func IsReadError(e error) bool {
if _, ok := e.(*ReadError); ok {
return true
}
return false
}
// IsResetError returns a boolean indicating whether the error is a result of
// a problem resetting the file buffer.
func IsResetError(e error) bool {
if _, ok := e.(*ResetError); ok {
return true
}
return false
}
// IsParseError r eturns a boolean indicating whether the error is a result of
// encountering a problem while trying to parse the file data.
func IsParseError(e error) bool {
if _, ok := e.(*ParseError); ok {
return true
}
return false
}
// Procer processes things.
type Procer interface {
ReadSlice(byte) ([]byte, error)
Reset() error
}
// A Proc holds everything related to a proc file and some processing vars.
type Proc struct {
*os.File
Buf *bufio.Reader
}
// NewProc Creates a Proc using the file handle.
func NewProc(fname string) (*Proc, error) {
f, err := os.Open(fname)
if err != nil {
return nil, err
}
return &Proc{File: f, Buf: bufio.NewReader(f)}, nil
}
// ReadSlice is a wrapper for bufio.Reader.ReadSlice.
func (p *Proc) ReadSlice(delim byte) (line []byte, err error) {
return p.Buf.ReadSlice(delim)
}
// Reset reset's the profiler's resources.
func (p *Proc) Reset() error {
_, err := p.File.Seek(0, os.SEEK_SET)
if err != nil {
return &ResetError{err}
}
p.Buf.Reset(p.File)
return nil
}
type Tocker interface {
Close() // Close the Tocker's resources
Run() // Run some code on an interval.
Stop() // Stop the Tocker.
}
type Ticker struct {
*time.Ticker
Done chan struct{} // done channel
Errs chan error // error channel
}
func NewTicker(d time.Duration) *Ticker {
return &Ticker{Ticker: time.NewTicker(d), Errs: make(chan error), Done: make(chan struct{})}
}
// Stop sends a signal to the done channel; stopping the Ticker. The Ticker
// can be restarted with Run.
func (t *Ticker) Stop() {
t.Done <- struct{}{}
}
// Close stops the ticker and closes the ticker resources.
func (t *Ticker) Close() {
t.Ticker.Stop()
close(t.Done)
close(t.Errs)
}
// Column returns a right justified string of width w.
// TODO: replace with text/tabwriter
func Column(w int, s string) string {
pad := w - len(s)
padding := make([]byte, pad)
for i := 0; i < pad; i++ {
padding[i] = 0x20
}
return fmt.Sprintf("%s%s", string(padding), s)
}
// Int64Column takes an int64 and returns a right justified string of width w.
func Int64Column(w int, v int64) string {
s := strconv.FormatInt(v, 10)
return Column(w, s)
}
// TrimTrailingSpaces removes the trailing spaces from a slice and returns
// it. Only 0x20, tabs, NL are considered space characters.
func TrimTrailingSpaces(p []byte) []byte {
for i := len(p) - 1; i >= 0; i-- {
if p[i] != 0x20 && p[i] != '\n' && p[i] != '\t' {
return p[:i+1]
}
}
// it was all spaces
return p[:0]
}
// TrimLeadingpaces removes the leading spaces from a slice and returns it.
// Only 0x20 and tabs are considered space characters.
func TrimLeadingSpaces(p []byte) []byte {
for i := 0; i < len(p); i++ {
if p[i] != 0x20 && p[i] != '\t' {
return p[i:]
}
}
// it was all spaces
return p[:0]
}
// TempFileProc is used to do Proc processing off of a temp file. Prefer using
// the Proc type instead.
type TempFileProc struct {
*Proc
// The directory holding the temp file.
Dir string
// The name of the file.
Name string
}
// NewTempFileProc creates a temporary file with data as its contents and
// returns a TempFileProc that uses the temporary file. The file will be saved
// in a randomly generated tempdir that starts with prefix. If prefix is empty
// the os.TempDir will be used as the save directory. Name is the name of the
// temporary file that will be created. If name is empty, the name will be 12
// randomly selected characters without an extension. The data will be used for
// the file and the file will be created with 0777 perms. If an error occurs,
// proc will be nil.
func NewTempFileProc(prefix, name string, data []byte) (proc *TempFileProc, err error) {
var t TempFileProc
if prefix == "" {
t.Dir = os.TempDir()
} else {
t.Dir, err = ioutil.TempDir("", prefix)
if err != nil {
return nil, err
}
}
if name == "" {
name = string(randchars.AlphaNum(12))
}
t.Name = name
err = ioutil.WriteFile(t.FullPath(), data, 0777)
if err != nil {
return nil, err
}
t.Proc, err = NewProc(t.FullPath())
if err != nil {
return nil, err
}
return &t, nil
}
// Returns the full path of the temp file for this TempFileProc.
func (p *TempFileProc) FullPath() string {
return filepath.Join(p.Dir, p.Name)
}
// ReadSlice is a wrapper for bufio.Reader.ReadSlice.
func (p *TempFileProc) ReadSlice(delim byte) (line []byte, err error) {
return p.Buf.ReadSlice(delim)
}
// Reset reset's the profiler's resources.
func (p *TempFileProc) Reset() error {
_, err := p.File.Seek(0, os.SEEK_SET)
if err != nil {
return &ResetError{err}
}
p.Buf.Reset(p.File)
return nil
}
// Remove removes the temp dir and temp file.
func (p *TempFileProc) Remove() error {
// only remove the directory if it is a subdir of the default temp dir.
if p.Dir != os.TempDir() {
os.RemoveAll(p.Dir)
}
// otherwise just remove the file
return os.RemoveAll(p.FullPath())
}
// Buffer is used to hold lines and values being processed.
type Buffer struct {
Line []byte
Val []byte
}
// NewBuffer returns an initialized Buffer.
func NewBuffer() *Buffer {
return &Buffer{Line: []byte{}, Val: make([]byte, 0, 32)}
}
// Reset resets the Buffer's slices len to 0 so that they can be re-used.
func (b *Buffer) Reset() {
b.Line = b.Line[:0]
b.Val = b.Val[:0]
}