-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
156 lines (135 loc) · 3.02 KB
/
server.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
package main
import (
"errors"
"net/http"
"time"
"os/exec"
"fmt"
"log"
"bytes"
"strings"
"config"
"github.com/robfig/cron"
gomail "gopkg.in/gomail.v2"
)
var (
errLog string
successLog string
lastEmail time.Time
emailTime = 7200// 邮件发送间隔为两个小时
)
// 访问网站
func web(url string, timeout time.Duration) error {
c := http.Client{Timeout: timeout}
r, err := c.Get(url)
if err != nil {
return err
}
if r.StatusCode != 200 {
return errors.New("网站访问失败:" + r.Status)
}
return nil
}
// 操作
func do(ops [][]string) error {
for _, op := range ops {
cmd := exec.Command(op[0], op[1:]...)
var out bytes.Buffer
cmd.Stdout = &out
//Run执行c包含的命令,并阻塞直到完成。 这里stdout被取出,cmd.Wait()无法正确获取stdin,stdout,stderr,则阻塞在那里
if err := cmd.Run(); err != nil {
addErrLog(err.Error())
} else {
addErrLog(strings.TrimSpace(out.String()))
}
}
return nil
}
// 发送邮件
func email(conf config.Email, isErr bool) error {
if time.Now().Unix()-lastEmail.Unix() < int64(emailTime) {
return errors.New("两小时内已发送错误邮件")
}
pre := "【成功】"
content := successLog
if isErr {
content = errLog
pre = "【失败】"
}
m := gomail.NewMessage()
m.SetHeader("From", conf.From)
m.SetHeader("To", conf.To)
m.SetHeader("Subject", pre+conf.Subject)
m.SetBody("text/html", content)
d := gomail.NewDialer(conf.SMTP, conf.Port, conf.From, conf.Password)
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
return err
}
// 邮件发送成功,清空日志信息
errLog = ""
successLog = ""
if isErr {
lastEmail = time.Now()
}
return nil
}
func run(conf config.Config) {
err := web(conf.URL, time.Duration(conf.Timeout)*time.Second)
if err == nil {
addSuccessLog()
return
}
// 记录错误日志
addErrLog(err.Error())
// 尝试进行操作
do(conf.Do)
// 尝试再次连接
addErrLog("尝试再次连接")
err = web(conf.URL, time.Duration(conf.Timeout)*time.Second)
if err != nil {
addErrLog(err.Error())
} else {
addErrLog("操作执行后系统恢复成功!")
}
// 发送错误邮件
err = email(conf.Email, true)
if err != nil {
addErrLog("邮件发送失败:" + err.Error())
}
}
func addSuccessLog() {
log.Println("[info]: 系统可以正常访问")
successLog = fmt.Sprintf(
"[%s]: 系统可以正常访问 <br /> %s",
time.Now().Format("2019-03-15 19:04:05"),
successLog,
)
}
func addErrLog(str string) {
log.Println("[err]: ", str)
errLog = fmt.Sprintf(
"[%s]: %s <br /> %s",
time.Now().Format("2019-03-15 19:04:05"),
str,
errLog,
)
}
func main() {
c := cron.New()
conf := config.NewConfig()
log.Println("网站运行状况监控开始执行!")
// 监控运行
c.AddFunc(conf.CronTime, func() {
run(conf)
})
// 每晚10点发送成功邮件
c.AddFunc("0 0 22 1/1 * ? ", func() {
err := email(conf.Email, false)
if err != nil {
addErrLog("邮件发送失败:" + err.Error())
}
})
c.Start()
select {}
}