mm_monitor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitattributes ADDED
@@ -0,0 +1,5 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+ *.js linguist-language=JavaScript
4
+ *.css linguist-language=JavaScript
5
+ *.html linguist-language=JavaScript
package/config.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "phone": {
3
+ // 是否启用
4
+ "state": 1,
5
+ // 用户名
6
+ "username": "",
7
+ // 密码,一般为授权码
8
+ "password": ""
9
+ },
10
+ "email": {
11
+ // 是否启用
12
+ "state": 1,
13
+ // 服务器地址
14
+ "host": "smtp.qq.com",
15
+ // 用户名
16
+ "username": "",
17
+ // 密码,一般为授权码
18
+ "password": ""
19
+ },
20
+ "listen": [
21
+ // {
22
+ // // 网站名称
23
+ // "name": "考勤系统",
24
+ // // URL地址
25
+ // "url": "",
26
+ // // 推送到邮箱
27
+ // "email": "",
28
+ // // 发送短信到
29
+ // "phone": "",
30
+ // // 推送的内容
31
+ // "content": "服务器异常,请尽快进行维护!--考勤系统"
32
+ // }
33
+ ],
34
+ "interval": 3
35
+ }
package/index.js ADDED
@@ -0,0 +1,134 @@
1
+ const Http = require('mm_https');
2
+ const SMS = require('mm_phone');
3
+ const Email = require('mm_email');
4
+
5
+ /**
6
+ * 短信通知类: 采用短信宝的API接口
7
+ */
8
+ class Monitor {
9
+ /**
10
+ * 构造函数
11
+ * @param {Object} config 配置参数
12
+ */
13
+ constructor(config) {
14
+ this.config = {
15
+ phone: {
16
+ // 是否启用
17
+ state: 1,
18
+ // 用户名
19
+ username: '',
20
+ // 密码,一般为授权码
21
+ password: ''
22
+ },
23
+ email: {
24
+ // 是否启用
25
+ state: 1,
26
+ // 服务器地址
27
+ host: 'smtp.qq.com',
28
+ // 用户名
29
+ username: 'xxxxxx@qq.com',
30
+ // 密码,一般为授权码
31
+ password: ''
32
+ },
33
+ listen: [
34
+ // {
35
+ // // 网站名称
36
+ // name: "AI考勤系统",
37
+ // // URL地址
38
+ // url: "https://xxx.xxx.com/xxx/xxx/xxx",
39
+ // // 推送到邮箱
40
+ // email: "xxx@qq.com",
41
+ // // 发送短信到
42
+ // phone: "xxx",
43
+ // // 推送的内容
44
+ // content: "【xxx】服务器异常,请尽快进行维护!--xxx系统"
45
+ // }
46
+ ],
47
+ // 心跳间隔时长
48
+ interval: 30,
49
+ interval_again: 3600
50
+ }
51
+
52
+ this.init(config);
53
+ }
54
+ }
55
+
56
+ /**
57
+ * 初始化
58
+ * @param {Object} config 配置
59
+ */
60
+ Monitor.prototype.init = async function(config) {
61
+ Object.assign(this.config, config);
62
+ this.sms = new SMS(this.config.phone);
63
+ this.email = new Email(this.config.email);
64
+ }
65
+
66
+ /**
67
+ * 检测访问是否正常
68
+ * @param {String} url 服务器地址
69
+ */
70
+ Monitor.prototype.checkServer = async function(url) {
71
+ var http = new Http();
72
+ var res = await http.get(url);
73
+ if (res.status == 200) {
74
+ return true;
75
+ } else {
76
+ return false;
77
+ }
78
+ }
79
+
80
+ /**
81
+ * 通知
82
+ * @param {Object} o 通知的对象信息
83
+ */
84
+ Monitor.prototype.notiy = async function(o) {
85
+ var {
86
+ phone,
87
+ email,
88
+ title,
89
+ content
90
+ } = o;
91
+ await this.sms.send(phone, content);
92
+ await this.email.send(title || "服务器异常", content, null, email);
93
+ }
94
+
95
+ /**
96
+ * 循环检测
97
+ */
98
+ Monitor.prototype.loop = async function() {
99
+ var lt = this.config.listen;
100
+ var hasBug = false;
101
+ for (var i = 0; i < lt.length; i++) {
102
+ var o = lt[i];
103
+ try {
104
+ var bl = await this.checkServer(o.url);
105
+ if (!bl) {
106
+ console.log("发生异常", (new Date()).toStr('yyyy-MM-dd hh:mm:ss'), o);
107
+ await this.notiy(o);
108
+ hasBug = true;
109
+ }
110
+ } catch (err) {
111
+ console.error(err);
112
+ }
113
+ $.sleep(1000);
114
+ }
115
+ if (hasBug) {
116
+ // 如果出现异常,则先暂停运行
117
+ clearInterval(this.timer);
118
+ // 等过了N久后再运行,避免连续通知
119
+ setTimeout(() => {
120
+ this.run();
121
+ }, this.config.interval_again);
122
+ }
123
+ }
124
+
125
+ /**
126
+ * 运行监听
127
+ */
128
+ Monitor.prototype.run = async function() {
129
+ this.timer = setInterval(() => {
130
+ this.loop();
131
+ }, this.config.interval * 1000);
132
+ }
133
+
134
+ module.exports = Monitor;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "mm_monitor",
3
+ "version": "1.0.0",
4
+ "description": "超级美眉监听器,用于监听网站运行是否异常并给运维人员发短信、邮箱通知",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/qiuwenwu/mm_monitor.git"
12
+ },
13
+ "keywords": [
14
+ "mvc",
15
+ "web",
16
+ "view",
17
+ "model",
18
+ "controller",
19
+ "render",
20
+ "template"
21
+ ],
22
+ "author": "邱文武",
23
+ "license": "ISC",
24
+ "bugs": {
25
+ "url": "https://github.com/qiuwenwu/mm_monitor/issues"
26
+ },
27
+ "homepage": "https://github.com/qiuwenwu/mm_monitor#readme",
28
+ "dependencies": {
29
+ "mm_email": "^1.0.0",
30
+ "mm_https": "^1.5.8",
31
+ "mm_phone": "^1.0.1"
32
+ }
33
+ }
package/test.js ADDED
@@ -0,0 +1,13 @@
1
+ const Monitor = require('./index.js');
2
+
3
+ /**
4
+ * 测试html转为json
5
+ * @param {String} file html文件
6
+ */
7
+ async function test() {
8
+ var config = "./config.json".loadJson();
9
+ var m = new Monitor(config);
10
+ m.run();
11
+ }
12
+
13
+ test();