From e0c1bebb13ae338f1baf8ec99c387965418de2a2 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Wed, 8 Dec 2021 23:55:51 +0800 Subject: [PATCH] modify n9eetc dir --- .gitignore | 1 + docker/initsql/{n9e.sql => a-n9e.sql} | 0 docker/initsql/{ibex.sql => b-ibex.sql} | 0 docker/initsql/{init.sql => c-init.sql} | 0 docker/n9eetc/metrics.yaml | 32 +++++++++++++++++- docker/n9eetc/script/notify.py | 44 +++++++++++++++++++++++-- docker/n9eetc/template/dingtalk.tpl | 4 +-- docker/n9eetc/template/feishu.tpl | 6 ++++ docker/n9eetc/template/mailbody.tpl | 9 +++++ docker/n9eetc/template/wecom.tpl | 4 +-- docker/n9eetc/webapi.conf | 6 +++- 11 files changed, 98 insertions(+), 8 deletions(-) rename docker/initsql/{n9e.sql => a-n9e.sql} (100%) rename docker/initsql/{ibex.sql => b-ibex.sql} (100%) rename docker/initsql/{init.sql => c-init.sql} (100%) create mode 100644 docker/n9eetc/template/feishu.tpl diff --git a/.gitignore b/.gitignore index 5ff558d9..7581513e 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ _test /build /dist /etc/*.local.yml +/etc/*.local.conf /etc/plugins/*.local.yml /data* /tarball diff --git a/docker/initsql/n9e.sql b/docker/initsql/a-n9e.sql similarity index 100% rename from docker/initsql/n9e.sql rename to docker/initsql/a-n9e.sql diff --git a/docker/initsql/ibex.sql b/docker/initsql/b-ibex.sql similarity index 100% rename from docker/initsql/ibex.sql rename to docker/initsql/b-ibex.sql diff --git a/docker/initsql/init.sql b/docker/initsql/c-init.sql similarity index 100% rename from docker/initsql/init.sql rename to docker/initsql/c-init.sql diff --git a/docker/n9eetc/metrics.yaml b/docker/n9eetc/metrics.yaml index 59887bcc..a91cb2ac 100644 --- a/docker/n9eetc/metrics.yaml +++ b/docker/n9eetc/metrics.yaml @@ -1 +1,31 @@ -cpu_usage_idle: CPU空闲率(单位:%) \ No newline at end of file +cpu_usage_idle: CPU空闲率(单位:%) +cpu_usage_active: CPU使用率(单位:%) +cpu_usage_system: CPU内核态时间占比(单位:%) +cpu_usage_user: CPU用户态时间占比(单位:%) +cpu_usage_nice: 低优先级用户态CPU时间占比,也就是进程nice值被调整为1-19之间的CPU时间。这里注意,nice可取值范围是-20到19,数值越大,优先级反而越低(单位:%) +cpu_usage_iowait: CPU等待I/O的时间占比(单位:%) +cpu_usage_irq: CPU处理硬中断的时间占比(单位:%) +cpu_usage_softirq: CPU处理软中断的时间占比(单位:%) +cpu_usage_steal: 在虚拟机环境下有该指标,表示CPU被其他虚拟机争用的时间占比,超过20就表示争抢严重(单位:%) +cpu_usage_guest: 通过虚拟化运行其他操作系统的时间,也就是运行虚拟机的CPU时间占比(单位:%) +cpu_usage_guest_nice: 以低优先级运行虚拟机的时间占比(单位:%) + +disk_free: 硬盘分区剩余量(单位:byte) +disk_used: 硬盘分区使用量(单位:byte) +disk_used_percent: 硬盘分区使用率(单位:%) +disk_total: 硬盘分区总量(单位:byte) +disk_inodes_free: 硬盘分区inode剩余量 +disk_inodes_used: 硬盘分区inode使用量 +disk_inodes_total: 硬盘分区inode总量 + +diskio_io_time: 从设备视角来看I/O请求总时间,队列中有I/O请求就计数(单位:毫秒),counter类型,需要用函数求rate才有使用价值 +diskio_iops_in_progress: 已经分配给设备驱动且尚未完成的IO请求,不包含在队列中但尚未分配给设备驱动的IO请求,gauge类型 +diskio_merged_reads: 相邻读请求merge读的次数,counter类型 +diskio_merged_writes: 相邻写请求merge写的次数,counter类型 +diskio_read_bytes: 读取的byte数量,counter类型,需要用函数求rate才有使用价值 +diskio_read_time: 读请求总时间(单位:毫秒),counter类型,需要用函数求rate才有使用价值 +diskio_reads: 读请求次数,counter类型,需要用函数求rate才有使用价值 +diskio_weighted_io_time: 从I/O请求视角来看I/O等待总时间,如果同时有多个I/O请求,时间会叠加(单位:毫秒) +diskio_write_bytes: 写入的byte数量,counter类型,需要用函数求rate才有使用价值 +diskio_write_time: 写请求总时间(单位:毫秒),counter类型,需要用函数求rate才有使用价值 +diskio_writes: 写请求次数,counter类型,需要用函数求rate才有使用价值 diff --git a/docker/n9eetc/script/notify.py b/docker/n9eetc/script/notify.py index f6d2115a..3d72e653 100755 --- a/docker/n9eetc/script/notify.py +++ b/docker/n9eetc/script/notify.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: UTF-8 -*- import sys import json @@ -11,7 +11,8 @@ notify_channel_funcs = { "sms":"sms", "voice":"voice", "dingtalk":"dingtalk", - "wecom":"wecom" + "wecom":"wecom", + "feishu":"feishu" } mail_host = "smtp.163.com" @@ -118,6 +119,45 @@ class Sender(object): except urllib2.HTTPError, error: print(error) + @classmethod + def send_feishu(cls, payload): + users = payload.get('event').get("notify_users_obj") + + tokens = {} + phones = {} + + for u in users: + if u.get("phone"): + phones[u.get("phone")] = 1 + + contacts = u.get("contacts") + if contacts.get("feishu_robot_token", ""): + tokens[contacts.get("feishu_robot_token", "")] = 1 + + opener = urllib2.build_opener(urllib2.HTTPHandler()) + method = "POST" + + for t in tokens: + url = "https://open.feishu.cn/open-apis/bot/v2/hook/{}".format(t) + body = { + "msg_type": "text", + "content": { + "text": payload.get('tpls').get("feishu.tpl", "feishu.tpl not found") + }, + "at": { + "atMobiles": phones.keys(), + "isAtAll": False + } + } + request = urllib2.Request(url, data=json.dumps(body)) + request.add_header("Content-Type",'application/json;charset=utf-8') + request.get_method = lambda: method + try: + connection = opener.open(request) + print(connection.read()) + except urllib2.HTTPError, error: + print(error) + @classmethod def send_sms(cls, payload): users = payload.get('event').get("notify_users_obj") diff --git a/docker/n9eetc/template/dingtalk.tpl b/docker/n9eetc/template/dingtalk.tpl index 95019eba..59ece247 100644 --- a/docker/n9eetc/template/dingtalk.tpl +++ b/docker/n9eetc/template/dingtalk.tpl @@ -2,5 +2,5 @@ 规则名称: {{.RuleName}}{{if .RuleNote}} 规则备注: {{.RuleNote}}{{end}} 监控指标: {{.TagsJSON}} -触发时间: {{timeformat .TriggerTime}} -触发时值: {{.TriggerValue}} \ No newline at end of file +{{if .IsRecovered}}恢复时间:{{timeformat .LastEvalTime}}{{else}}触发时间: {{timeformat .TriggerTime}} +触发时值: {{.TriggerValue}}{{end}} \ No newline at end of file diff --git a/docker/n9eetc/template/feishu.tpl b/docker/n9eetc/template/feishu.tpl new file mode 100644 index 00000000..59ece247 --- /dev/null +++ b/docker/n9eetc/template/feishu.tpl @@ -0,0 +1,6 @@ +级别状态: S{{.Severity}} {{if .IsRecovered}}Recovered{{else}}Triggered{{end}} +规则名称: {{.RuleName}}{{if .RuleNote}} +规则备注: {{.RuleNote}}{{end}} +监控指标: {{.TagsJSON}} +{{if .IsRecovered}}恢复时间:{{timeformat .LastEvalTime}}{{else}}触发时间: {{timeformat .TriggerTime}} +触发时值: {{.TriggerValue}}{{end}} \ No newline at end of file diff --git a/docker/n9eetc/template/mailbody.tpl b/docker/n9eetc/template/mailbody.tpl index a45e35b6..fa5e2d64 100644 --- a/docker/n9eetc/template/mailbody.tpl +++ b/docker/n9eetc/template/mailbody.tpl @@ -162,6 +162,13 @@ 监控指标: {{.TagsJSON}} + + {{if .IsRecovered}} + + 恢复时间: + {{timeformat .LastEvalTime}} + + {{else}} 触发时值: {{.TriggerValue}} @@ -172,6 +179,8 @@ {{timeformat .TriggerTime}} + {{end}} + PromQL: diff --git a/docker/n9eetc/template/wecom.tpl b/docker/n9eetc/template/wecom.tpl index 1513cbd5..d1c7410a 100644 --- a/docker/n9eetc/template/wecom.tpl +++ b/docker/n9eetc/template/wecom.tpl @@ -2,5 +2,5 @@ **规则标题**: {{.RuleName}}{{if .RuleNote}} **规则备注**: {{.RuleNote}}{{end}} **监控指标**: {{.TagsJSON}} -**触发时间**: {{timeformat .TriggerTime}} -**触发时值**: {{.TriggerValue}} \ No newline at end of file +{{if .IsRecovered}}**恢复时间**:{{timeformat .LastEvalTime}}{{else}}**触发时间**: {{timeformat .TriggerTime}} +**触发时值**: {{.TriggerValue}}{{end}} \ No newline at end of file diff --git a/docker/n9eetc/webapi.conf b/docker/n9eetc/webapi.conf index 9223bca2..b8ee1516 100644 --- a/docker/n9eetc/webapi.conf +++ b/docker/n9eetc/webapi.conf @@ -8,7 +8,7 @@ RunMode = "release" AdminRole = "Admin" # Linkage with notify.py script -NotifyChannels = [ "email", "dingtalk", "wecom" ] +NotifyChannels = [ "email", "dingtalk", "wecom", "feishu" ] [[ContactKeys]] Label = "Wecom Robot Token" @@ -18,6 +18,10 @@ Key = "wecom_robot_token" Label = "Dingtalk Robot Token" Key = "dingtalk_robot_token" +[[ContactKeys]] +Label = "Feishu Robot Token" +Key = "feishu_robot_token" + [Log] # log write dir Dir = "logs"