-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
system.go
68 lines (59 loc) · 1.41 KB
/
system.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
package glutton
import (
"errors"
"fmt"
"net"
"os"
"runtime"
"strings"
"time"
"github.com/glaslos/lsof"
"github.com/google/gopacket/pcap"
)
func countOpenFiles() (int, error) {
if runtime.GOOS == "linux" {
lines, err := lsof.ReadPID(os.Getpid())
return len(lines) - 1, err
}
return 0, errors.New("operating system type not supported for this command")
}
func (g *Glutton) startMonitor(quit chan struct{}) {
ticker := time.NewTicker(10 * time.Second)
go func() {
for {
select {
case <-ticker.C:
openFiles, err := countOpenFiles()
if err != nil {
fmt.Printf("Failed :%s", err)
}
runningRoutines := runtime.NumGoroutine()
g.Logger.Info(fmt.Sprintf("running Go routines: %d, open files: %d", openFiles, runningRoutines))
case <-quit:
g.Logger.Info("monitoring stopped...")
ticker.Stop()
return
}
}
}()
}
func getNonLoopbackIPs(ifaceName string) ([]net.IP, error) {
nonLoopback := []net.IP{}
ifs, err := pcap.FindAllDevs()
if err != nil {
return nonLoopback, err
}
for _, iface := range ifs {
if strings.EqualFold(iface.Name, ifaceName) {
for _, addr := range iface.Addresses {
if !addr.IP.IsLoopback() && addr.IP.To4() != nil {
nonLoopback = append(nonLoopback, addr.IP)
}
}
}
}
if len(nonLoopback) == 0 {
return nonLoopback, fmt.Errorf("unable to find any non-loopback addresses for: %s", ifaceName)
}
return nonLoopback, nil
}