File size: 11,017 Bytes
a757bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import json
import os
import time
from typing import Tuple, Union, Optional

from .mods import PUSH_DATA_PATH, TaskTemplateConfig, TaskConfig
from .send_tool import WxAccountMsg
from .base_task import BaseTask, BaseTaskViewMsg
from .util import read_file, DB, GET_CLASS, write_file


class NginxLoadTask(BaseTask):
    def __init__(self):
        super().__init__()
        self.source_name = "nginx_load_push"
        self.template_name = "负载均衡告警"
        self._tip_counter = None

    @property
    def tip_counter(self) -> dict:
        if self._tip_counter is not None:
            return self._tip_counter
        tip_counter = '{}/load_balance_push.json'.format(PUSH_DATA_PATH)
        if os.path.exists(tip_counter):
            try:
                self._tip_counter = json.loads(read_file(tip_counter))
            except json.JSONDecodeError:
                self._tip_counter = {}
        else:
            self._tip_counter = {}
        return self._tip_counter

    def save_tip_counter(self):
        tip_counter = '{}/load_balance_push.json'.format(PUSH_DATA_PATH)
        write_file(tip_counter, json.dumps(self.tip_counter))

    def get_title(self, task_data: dict) -> str:
        if task_data["project"] == "all":
            return "负载节点异常告警"
        return "负载节点[{}]异常告警".format(task_data["project"])

    def check_task_data(self, task_data: dict) -> Union[dict, str]:
        all_upstream_name = DB("upstream").field("name").select()
        if isinstance(all_upstream_name, str) and all_upstream_name.startswith("error"):
            return '没有负载均衡配置,无法设置告警'
        all_upstream_name = [i["name"] for i in all_upstream_name]
        if not bool(all_upstream_name):
            return '没有负载均衡配置,无法设置告警'
        if task_data["project"] not in all_upstream_name and task_data["project"] != "all":
            return '没有该负载均衡配置,无法设置告警'

        cycle = []
        for i in task_data["cycle"].split("|"):
            if bool(i) and i.isdecimal():
                code = int(i)
                if 100 <= code < 600:
                    cycle.append(str(code))
        if not bool(cycle):
            return '没有指定任何错误码,无法设置告警'

        task_data["cycle"] = "|".join(cycle)
        task_data["interval"] = 300
        return task_data

    def get_keyword(self, task_data: dict) -> str:
        return task_data["project"]

    def _check_func(self, upstream_name: str, codes: str, interval:int) -> list:
        import PluginLoader
        get_obj = GET_CLASS()
        get_obj.upstream_name = upstream_name
        # 调用外部插件检查负载均衡的健康状况
        upstreams = PluginLoader.plugin_run("load_balance", "get_check_upstream", get_obj)
        access_codes = [int(i) for i in codes.split("|") if bool(i.strip())]
        res_list = []
        for upstream in upstreams:
            # 检查每个节点,返回有问题的节点信息
            res = upstream.check_nodes(access_codes, return_nodes=True)
            for ping_url in res:
                if ping_url in self.tip_counter:
                    self.tip_counter[ping_url].append(int(time.time()))
                    idx = 0
                    for i in self.tip_counter[ping_url]:
                        # 清理超过4分钟的记录
                        if time.time() - i > interval * 4:
                            idx += 1
                    self.tip_counter[ping_url] = self.tip_counter[ping_url][idx:]
                    print("self.tip_counter[ping_url]",self.tip_counter[ping_url])
                    # 如果一个节点连续三次出现在告警列表中,则视为需要告警
                    if len(self.tip_counter[ping_url]) >= 3:
                        res_list.append(ping_url)
                        self.tip_counter[ping_url] = []
                else:
                    self.tip_counter[ping_url] = [int(time.time()), ]
        self.save_tip_counter()
        return res_list

    def get_push_data(self, task_id: str, task_data: dict) -> Optional[dict]:
        err_nodes = self._check_func(task_data["project"], task_data["cycle"], task_data.get("interval", 300))
        if not err_nodes:
            return None
        pj = "负载均衡:【{}】".format(task_data["project"]) if task_data["project"] != "all" else "负载均衡"
        nodes = '、'.join(err_nodes)
        self.title = self.get_title(task_data)
        return {
            "msg_list": [
                ">通知类型:负载均衡告警",
                ">告警内容:<font color=#ff0000>{}配置下的节点【{}】出现访问错误,请及时关注节点情况并处理。</font> ".format(
                    pj, nodes),
            ],
            "pj": pj,
            "nodes": nodes
        }

    def filter_template(self, template: dict) -> Optional[dict]:
        if not os.path.exists("/www/server/panel/plugin/load_balance/load_balance_main.py"):
            return None
        all_upstream = DB("upstream").field("name").select()
        if isinstance(all_upstream, str) and all_upstream.startswith("error"):
            return None
        all_upstream_name = [i["name"] for i in all_upstream]
        if not all_upstream_name:
            return None
        for name in all_upstream_name:
            template["field"][0]["items"].append({
                "title": name,
                "value": name
            })
        return template

    def to_sms_msg(self, push_data: dict, push_public_data: dict) -> Tuple[str, dict]:
        return '', {}

    def to_wx_account_msg(self, push_data: dict, push_public_data: dict) -> WxAccountMsg:
        msg = WxAccountMsg.new_msg()
        msg.thing_type = "负载均衡告警"
        msg.msg = "负载均衡出现节点异常,请登录面板查看"
        return msg

    def task_config_create_hook(self, task: dict) -> None:
        old_config_file = "/www/server/panel/class/push/push.json"
        try:
            old_config = json.loads(read_file(old_config_file))
        except:
            return
        if "load_balance_push" not in old_config:
            old_config["load_balance_push"] = {}
        old_data = {
            "push_count": task["number_rule"].get("day_num", 2),
            "cycle": task["task_data"].get("cycle", "200|301|302|403|404"),
            "interval": task["task_data"].get("interval", 600),
            "title": task["title"],
            "status": task['status'],
            "module": ",".join(task["sender"])
        }
        for k, v in old_config["load_balance_push"].items():
            if v["project"] == task["task_data"]["project"]:
                v.update(old_data)
        else:
            old_data["project"] = task["task_data"]["project"]
            old_config["load_balance_push"][int(time.time())] = old_data

        write_file(old_config_file, json.dumps(old_config))

    def task_config_update_hook(self, task: dict) -> None:
        return self.task_config_create_hook(task)

    def task_config_remove_hook(self, task: dict) -> None:
        old_config_file = "/www/server/panel/class/push/push.json"
        try:
            old_config = json.loads(read_file(old_config_file))
        except:
            return
        if "load_balance_push" not in old_config:
            old_config["load_balance_push"] = {}
        old_config["load_balance_push"] = {
            k: v for k, v in old_config["load_balance_push"].items()
            if v["project"] != task["task_data"]["project"]
        }

        write_file(old_config_file, json.dumps(old_config))


def load_load_template():
    from .mods import load_task_template_by_config
    load_task_template_by_config(
        [{
            "id": "50",
            "ver": "1",
            "used": True,
            "source": "nginx_load_push",
            "title": "负载均衡",
            "load_cls": {
                "load_type": "path",
                "cls_path": "mod.base.push_mod.load_push",
                "name": "NginxLoadTask"
            },
            "template": {
                "field": [
                    {
                        "attr": "project",
                        "name": "负载名称",
                        "type": "select",
                        "default": "all",
                        "unit": "",
                        "suffix": (
                            "<i style='color: #999;font-style: initial;font-size: 12px;margin-right: 5px'>*</i>"
                            "<span style='color:#999'>选中的负载配置中,出现节点访问失败时,触发告警</span>"
                        ),
                        "items": [
                            {
                                "title": "所有已配置的负载",
                                "value": "all"
                            }
                        ]
                    },
                    {
                        "attr": "cycle",
                        "name": "成功的状态码",
                        "type": "textarea",
                        "unit": "",
                        "suffix": (
                            "<br><i style='color: #999;font-style: initial;font-size: 12px;margin-right: 5px'>*</i>"
                            "<span style='color:#999'>状态码以竖线分隔,如:200|301|302|403|404</span>"
                        ),
                        "width": "400px",
                        "style": {
                            'height': '70px',
                        },
                        "default": "200|301|302|403|404"
                    }
                ],
                "sorted": [
                    [
                        "project"
                    ],
                    [
                        "cycle"
                    ]
                ],
            },
            "default": {
                "project": "all",
                "cycle": "200|301|302|403|404"
            },
            "advanced_default": {
                "number_rule": {
                    "day_num": 3
                }
            },
            "send_type_list": [
                "wx_account",
                "dingding",
                "feishu",
                "mail",
                "weixin",
                "webhook"
            ],
            "unique": False,
            "tags": ["plugin"],
            "description": "每隔一段时间检查负载均衡插件中,设置的节点是否可以正常访问,当访问异常时发送告警通知"
        }]
    )


class ViewMsgFormat(BaseTaskViewMsg):

    def get_msg(self, task: dict) -> Optional[str]:
        if task["template_id"] == "50":
            return "<span>节点访问异常时,推送告警信息(每日推送{}次后不再推送)<span>".format(
                task.get("number_rule", {}).get("day_num"))
        return None


NginxLoadTask.VIEW_MSG = ViewMsgFormat