-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (102 loc) · 2.28 KB
/
main.go
File metadata and controls
107 lines (102 loc) · 2.28 KB
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
package main
import (
"os"
"io"
"syscall"
"fmt"
"time"
"strings"
"strconv"
"flag"
)
// A simple routine to grab the 1 minute avg load
// It will be sent through the c every second
func Load(c chan float64) {
tick := time.Tick(time.Second)
f, err := os.Open("/proc/loadavg")
defer f.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
close(c)
return
}
buff := make([]byte, 1024)
var n int
var load float64
for _ = range tick {
// Make sure we are at the beginning of the file
if _, err = f.Seek(0, 0); err != nil {
fmt.Fprintln(os.Stderr, err)
close(c)
return
}
if n, err = f.Read(buff); err != io.EOF && err != nil {
fmt.Fprintln(os.Stderr, err)
close(c)
return
}
fields := strings.Fields(string(buff[:n]))
if load, err = strconv.ParseFloat(fields[0], 64); err != nil {
fmt.Fprintln(os.Stderr, err)
close(c)
return
} else {
c <- load
}
}
}
var (
pid int
help bool
suspended bool
ps *os.Process
threshold float64
sigcont os.Signal = syscall.SIGCONT
sigstop os.Signal = syscall.SIGSTOP
)
func main() {
var perr error
flag.Parse()
threshold, perr = strconv.ParseFloat(flag.Arg(0), 64)
fmt.Println(flag.Arg(1))
pid, perr = strconv.Atoi(flag.Arg(1))
if perr != nil {
fmt.Println("Suspends process when load reaches threshold")
fmt.Println("pwatch load pid")
return
}
if pid != -1 {
var err error
ps, err = os.FindProcess(pid)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not find process with pid %d\n", pid)
fmt.Fprintln(os.Stderr, err)
return
}
}
c := make(chan float64, 1)
go Load(c)
for load := range c {
if load > threshold {
if !suspended {
fmt.Println("Going down")
// signal stop
if err := ps.Signal(sigstop); err != nil {
fmt.Fprintf(os.Stderr, "Error stopping process: %s", err)
// Should we exit?
return
}
}
suspended = true
} else if suspended {
fmt.Println("Bringing up")
// signal continue
if err := ps.Signal(sigcont); err != nil {
fmt.Fprintf(os.Stderr, "Error starting process: %s", err)
// should we exit?
return
}
suspended = false
}
}
}