-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathartemis-master.go
198 lines (178 loc) · 4.48 KB
/
artemis-master.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
package main
import (
"bytes"
"fmt"
"github.com/takama/daemon"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
)
const (
name = "artemis"
description = "artemis hids service"
procsFile = "cgroup.procs"
memoryLimitFile = "memory.limit_in_bytes"
swapLimitFile = "memory.swappiness"
cpuLimitFile = "cpu.cfs_quota_us"
Name = "artemis"
memoLimit = 50 // 50M
mcgroupRoot = "/sys/fs/cgroup/memory/" + Name
cpuLimit = 5 // 5%
cpucgroupRoot = "/sys/fs/cgroup/cpu/" + Name
)
var infoLog, errLog *log.Logger
type Service struct {
daemon.Daemon
}
func (service *Service) Manage() (string, error) {
usage := "Usage: ./artemis-master install | remove | start | stop | status"
if len(os.Args) > 1 {
command := os.Args[1]
switch command {
case "install":
exist, _ := PathExists(mcgroupRoot)
if exist {
fmt.Printf("has dir![%v]\n", mcgroupRoot)
} else {
err := os.Mkdir(mcgroupRoot, os.ModePerm)
if err != nil {
fmt.Printf("mkdir failed![%v]\n", err)
} else {
fmt.Printf("mkdir success!\n")
}
}
exist, _ = PathExists(cpucgroupRoot)
if exist {
fmt.Printf("has dir![%v]\n", cpucgroupRoot)
} else {
err := os.Mkdir(cpucgroupRoot, os.ModePerm)
if err != nil {
fmt.Printf("mkdir failed![%v]\n", err)
} else {
fmt.Printf("mkdir success!\n")
}
}
mPath := filepath.Join(mcgroupRoot, memoryLimitFile)
writeFile(mPath, memoLimit*1024*1024)
sPath := filepath.Join(mcgroupRoot, swapLimitFile)
writeFile(sPath, 0)
cPath := filepath.Join(cpucgroupRoot, cpuLimitFile)
writeFile(cPath, cpuLimit*1000)
return service.Install()
case "remove":
return service.Remove()
case "start":
return service.Start()
case "stop":
return service.Stop()
case "status":
return service.Status()
default:
return usage, nil
}
}
go startCmd("/usr/local/artemis/artemis-agent")
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
for {
select {
case killSignal := <-interrupt:
errLog.Println("artemis-master got signal:", killSignal)
if killSignal == os.Interrupt {
err := "artemis-master was interruped by system signal"
errLog.Println(err)
return err, nil
}
err := "artemis-master was killed"
return err, nil
}
}
return usage, nil
}
func init() {
errFile, err := os.OpenFile("/var/log/artemis-master.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("open log file failed, err:", err)
}
infoLog = log.New(io.MultiWriter(os.Stdout, errFile), "Info:", log.Ldate|log.Ltime|log.Lshortfile)
errLog = log.New(io.MultiWriter(os.Stderr, errFile), "Error:", log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
srv, err := daemon.New(name, description, daemon.SystemDaemon, "nil")
if err != nil {
errLog.Fatalln("Error: ", err)
}
service := &Service{srv}
status, err := service.Manage()
if err != nil {
errLog.Fatalln("Error: ", err)
}
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func writeFile(path string, value int) {
if err := ioutil.WriteFile(path, []byte(fmt.Sprintf("%d", value)), 0755); err != nil {
log.Panic(err)
}
}
type ExitStatus struct {
Signal os.Signal
Code int
}
func startCmd(command string) {
restart := make(chan ExitStatus, 1)
runner := func() {
cmd := exec.Cmd{
Path: command,
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
errLog.Panic(err)
}
infoLog.Println("add pid", cmd.Process.Pid, "to file cgroup.procs")
mPath := filepath.Join(mcgroupRoot, procsFile)
writeFile(mPath, cmd.Process.Pid)
cpuPath := filepath.Join(cpucgroupRoot, procsFile)
writeFile(cpuPath, cmd.Process.Pid)
if err := cmd.Wait(); err != nil {
errLog.Println("cmd return with error:", err)
errLog.Println(stderr.String())
}
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
options := ExitStatus{
Code: status.ExitStatus(),
}
if status.Signaled() {
options.Signal = status.Signal()
}
cmd.Process.Kill()
restart <- options
}
go runner()
for {
status := <-restart
switch status.Signal {
case os.Kill:
errLog.Println("artemis-agent is killed by system")
default:
errLog.Println("artemis-agent exit with code:", status.Code)
return
}
errLog.Println("restart artemis-agent..")
go runner()
}
}