通过企业微信接收告警信息
企业微信接收告警也是使用邮箱发送消息所以也需要开通邮箱POP3
1、开通邮箱POP3
设置->邮箱设置->账号->下拉到POP3服务状态设置->点击获取授权码
2、创建模版
mkdir templates
vim wechat.tmpl
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
@监控报警
监控程序:{{ .Labels.job }} <br>
告警级别:{{ .Labels.severity }}
告警类型:{{ $alert.Labels.alertname }}
故障主机:{{ $alert.Labels.instance }}
告警主题:{{ $alert.Annotations.summary }}
告警详情:{{ $alert.Annotations.message }}{{ $alert.Annotations.description}}
故障时间:{{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
@告警恢复
监控程序:{{ .Labels.job }} <br>
告警类型:{{ .Labels.alertname }}
故障主题:{{ $alert.Annotations.summary }}
告警详情:{{ $alert.Annotations.message }}{{ $alert.Annotations.description}};
故障时间:{{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢复时间:{{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05"}}
{{- if gt (len $alert.Labels.instance) 0 }}
实例信息:{{ $alert.Labels.instance }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
3、更改alertmanager.yml配置文件
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: '1499747740@qq.com'
smtp_auth_username: '1499747740@qq.com'
smtp_auth_password: 'jekavuvhbvkqffei'
smtp_require_tls: false
smtp_hello: 'qq.com'
wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
wechat_api_corp_id: 'wx3f2eb36f01a00a62'
wechat_api_secret: '' // 企业微信里面的secret
templates:
- '/usr/local/alertmanager/templates/wechat.tmpl'
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'wechat'
receivers:
- name: 'email'
email_configs:
- to: '15727630256@163.com'
send_resolved: true
html: '{{ template "email.to.html" . }}'
- name: 'wechat'
wechat_configs:
- send_resolved: true
message: '{{ template "wechat.default.message" . }}'
to_party: '1'
to_user: '@all'
agent_id: '1'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
4、检查测试alertmanager.yml文件
./amtool check-config alertmanager.yml
5、编写消耗内存程序
vim alloc_momery.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char** argv){
if(argc==3){
unsigned int memblocks = atoi(argv[1]);
unsigned int sleeptimesec = atoi(argv[2]);
printf("memblocks = %d,sleeptimesec = %d\n",memblocks,sleeptimesec);
char * allpoint[memblocks];
for(unsigned i=0;i<memblocks;i++){
printf("malloc %d block\n",i);
// 32MiB/block
allpoint[i]=(char *)malloc(sizeof(char)*1024*1024*32);
if(allpoint[i]==NULL){
printf("Failed");
}else{
memset(allpoint[i],'A',sizeof(char)*1024*1024*32);
}
sleep(1);
}
sleep(sleeptimesec);
for(unsigned i=0;i<memblocks;i++){
printf("free %d block\n",i);
free(allpoint[i]);
sleep(1);
}
return 0;
}else{
printf("args wrong,please input malloc numbers and sleep time\n");
return -1;
}
}
编译c程序
gcc -std=c99 alloc_momery.c -o alloc_momery
启动prometheus服务
systemctl start prometheus.service
启动node_exporter服务
systemctl start node_exporte.service
启动alertmanager服务
systemctl start alertmanager.service
浙公网安备 33010602011771号