做网站的搜索引擎,建设实业公司网站设计模板,公众号管理平台入口,机关事业单位网站备案一年前给客户部署配置过grafana#xff0c;告警配置也是用的原始的#xff0c;客户在使用过程中只需要一些核心点信息#xff0c;想要实现这个就需要用Grafana的自定义告警模板以及编辑邮件模板。
通知模板
模板信息的配置中查阅了相关资料#xff0c;自己组装了一套告警配置也是用的原始的客户在使用过程中只需要一些核心点信息想要实现这个就需要用Grafana的自定义告警模板以及编辑邮件模板。
通知模板
模板信息的配置中查阅了相关资料自己组装了一套主要用于邮件标题想要实现的效果如“服务器: *** **** 内存使用率超过**告警”告警事项这儿用alertname直接固定 比如配置的是内存超过90阈值就发邮件那就把alertname设置成“内存使用率超过90告警”即可。重点是获取异常的服务器信息。 查阅相关资料后编写代码如下
{{ define email.message }}
{{- if gt (len .Alerts.Firing) 0 -}}服务器:{{ range $i, $alert : .Alerts.Firing }}{{ index $alert.Labels instance }}{{ end }}{{ range $i, $alert : .Alerts.Firing }}{{if eq $i 0 }}{{ index $alert.Labels alertname }}{{end}}{{ end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}服务器:{{ range $i, $alert : .Alerts.Resolved }}{{ index $alert.Labels instance }}{{ end }}{{ range $i, $alert : .Alerts.Resolved }}{{if eq $i 0 }}{{ index $alert.Labels alertname }}{{end}}{{ end }}
{{- end }}
{{ end }}
在Contact points-Optional Email settings-Subject配置自定义模板名称
代码编写完成后在旧版9.1上一直不行后来grafana官方资料发现告警规则中的查询表达式若是用的经典模式Classic condition在不能使用“$alert.Labels instance”获取服务器信息变更告警规则为Reduce发现可以收取到正确格式的相关邮件标题然后进入下一步更改 邮件模板
邮件模板
邮件模板主要是根据客户需要展示具体事项、服务器和事项的当前值即可。
找到grafana的安装目录默认C:\Program Files\GrafanaLabs\grafana\public\emails\)打开 ng_alert_notification.html文件 按照要求注释更改部分html代码即可。部分代码片段如下 {{ range .Labels.SortedPairs }}tr stylevertical-align: top; padding: 0; alignlefttd colspan2 classvalue styleword-break: break-word; -webkit-hyphens: auto; -moz-hyphens: auto; hyphens: auto; border-collapse: collapse !important; color: #222222; font-family: Open Sans, Helvetica Neue, Helvetica, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 19px; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; margin: 0; padding: 24px 0 0; alignleft valigntop{{if eq .Name instance }}span classvalue-heading stylefont-weight: bold;服务器:/span span classvalue-value stylepadding-left: 8px;{{ .Value }}/span{{else if eq .Name alertname }}span classvalue-heading stylefont-weight: bold;事项:/span span classvalue-value stylepadding-left: 8px;{{ .Value }}/span{{else}}{{end}}/td/tr{{ end }}{{ if gt (len .Annotations.SortedPairs) 0 }}tr stylevertical-align: top; padding: 0; alignlefttd colspan2 classannotations styleword-break: break-word; -webkit-hyphens: auto; -moz-hyphens: auto; hyphens: auto; border-collapse: collapse !important; color: #222222; font-family: Open Sans, Helvetica Neue, Helvetica, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 19px; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; margin: 0; padding: 24px 0 12px; alignleft valigntop{{ range .Annotations.SortedPairs }}{{if eq .Name description }}p stylecolor: #222222; font-family: Open Sans, Helvetica Neue, Helvetica, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 19px; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; margin: 0 0 10px; padding: 0; alignleftspan classannotations-heading stylefont-weight: bold; text-transform: capitalize;当前值:/span span classannotations-value stylepadding-left: 8px;{{ .Value }}/span/p{{end}}{{ end }}/td/tr{{ end }}
此处针对当前值9.1版本里的{{ .ValueString }}是一个字符串本人对go语言不熟以及对对照官方给出的方法在这个模板html里无法解析出服务器信息所以在告警里的annotations的Description里获取了当前值代码用“{{ printf %.2f $values.B.Value }}”当前值保留2位小数。 收取的邮件效果如下
重新在告警规则中的查询表达式中对表达式B使用Reduce表达式C使用Classic condition配置阈值发现邮件未按阈值进行邮件发送且annotations的Description里的表达式也获取不到正确的值。查询官方资料( 具体链接template-notifications/reference)发现最新版里有一个Values是KV应该是可以解析出服务器信息和对应的value值。 下载安装完最新版10.4.1在配置告警时发现和9.1有明细的变化直接配置完相关信息发送邮件可以实现按阈值进行邮件发送满足客户需要。后面再对新版本的邮件模板html进行改造 部分代码片段如下
{{ __dangerouslyInjectHTML !--[if mso | IE]table rolepresentation border0 cellpadding0 cellspacing0trtd class stylevertical-align:top;width:600px; ![endif]-- }}div classmj-column-per-100 mj-outlook-group-fix stylefont-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;table border0 cellpadding0 cellspacing0 rolepresentation stylevertical-align:top; width100%tbody{{ if .Labels.SortedPairs }}!--trtd alignleft classtxt stylefont-size:0px;padding:10px 25px;word-break:break-word;div stylefont-family: Inter, Helvetica, Arial; font-size: 13px; line-height: 150%; text-align: left; color: #000000;strongLabels/strong/div/td/tr--trtd alignleft classtxt stylefont-size:0px;padding:10px 25px;word-break:break-word;table cellpadding0 cellspacing0 width100% border0 stylecolor:#000000;font-family:Inter, Helvetica, Arial;font-size:13px;line-height:22px;table-layout:auto;width:100%;border:none;mj-raw{{ range .Labels.SortedPairs }}/mj-raw{{if eq .Name instance }}trtdstrong服务器/strong/tdtd{{ .Value }}/td/tr{{else if eq .Name alertname }}trtdstrong事项/strong/tdtd{{ .Value }}/td/tr{{ else }}{{ end }}mj-raw{{ end }}/mj-rawmj-raw{{ range $refID, $value : .Values }}/mj-raw{{ if eq $refID B }}trtdstrong当前值/strong/tdtd{{ printf %.2f $value }}/td/tr{{ end }}mj-raw{{ end }}/mj-raw/table/td/tr{{ end }}{{ if (without .Annotations.SortedPairs.Names description summary) }}trtd alignleft classtxt stylefont-size:0px;padding:10px 25px;word-break:break-word;div stylefont-family: Inter, Helvetica, Arial; font-size: 13px; line-height: 150%; text-align: left; color: #000000;strongAnnotations/strong/div/td/trtrtd alignleft classtxt stylefont-size:0px;padding:10px 25px;word-break:break-word;table cellpadding0 cellspacing0 width100% border0 stylecolor:#000000;font-family:Inter, Helvetica, Arial;font-size:13px;line-height:22px;table-layout:auto;width:100%;border:none;mj-raw{{ range .Annotations.SortedPairs }}/mj-rawmj-raw{{ if and (ne .Name description) (ne .Name summary) }}/mj-rawtrtdstrong{{ .Name }}/strong/tdtd{{ .Value }}/td/trmj-raw{{ end }}/mj-rawmj-raw{{ end }}/mj-raw/table/td/tr{{ end }}/tbody/table/div
{{ __dangerouslyInjectHTML !--[if mso | IE]/td/tr/table![endif]-- }}
邮件收取效果如图
另外关于邮件里跳转链接的配置在grafana的安装路径的grafana\conf下打开defaults.ini文件找到“[server]”,编辑 domain 192.168.**.**这样就可以从邮件里直接跳转到相关的grafana页面。 文章转载自: http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.dkqr.cn.gov.cn.dkqr.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.kspfq.cn.gov.cn.kspfq.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn