一、CDH配置CM进行邮件告警
1、修改Cloudera Management Service的配置
输入"alert"进行搜索:
基本配置如下
邮件服务器主机名称: 根据自己实际情况选择,可用smtp.163.com、smtp.qq.com等;
邮件服务器用户名: 对应邮件服务器的邮箱账号,比如 xxx@qq.com等;
邮件服务器密码: 对应用户名的密码,如果使用QQ邮箱作为“邮件服务器用户名”,那么这里的密码要填写QQ邮箱的授权码,具体获取授权码的方法可百度,很简单;
邮件发件人地址: 发件人地址一般可以与邮件服务器用户名一样;
邮件收件人: 接收报警信息的地址,可为任意可用邮件地址,只要网络能通,如果使用多个收件箱,可以用英文逗号分割;
其他配置,看自己需求更改即可;
2、保存配置,重启Cloudera Management Service
3、测试告警
如果是测试集群,可直接停止掉某个关键服务,看是否能收到告警邮件;
也可以使用 【管理->警报->发送测试警报】
这里我使用”发送测试警报”的方式:
点击“发送测试警报”,然后稍等一下,已收到告警邮件
二、HDP中配置Ambari进行邮件告警
1、登录ambari管理页面,找到Manage Notifications选项
2、点击+ 创建一个告警
3、配置告警
Email To: 邮件接收邮箱地址,如果使用多个收件箱,可以用英文逗号分割;
SMTP Server:邮件服务器地址;
SMTP Port:25
Email From:用于发送邮件的地址;
选中”Use authentication”,填写发件箱地址和密码;
4、定义发送邮件格式
下载 alert-templates.xml ,地址:
这里列出alert-templates.xml 文件的全部内容:

<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <alert-templates> <alert-template type="EMAIL"> <subject> <![CDATA[Alert Summary: OK[$summary.getOkCount()], Warning[$summary.getWarningCount()], Critical[$summary.getCriticalCount()], Unknown[$summary.getUnknownCount()]]]> </subject> <body> <![CDATA[ #set( $alertStates = ["OK", "WARNING", "CRITICAL", "UNKNOWN"] ) #set( $services = $summary.getServices() ) <html> <style type="text/css"> html { font-family:sans-serif; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; } body { margin:10px; } footer,header { display:block; } table { border-spacing:0; border-collapse:collapse; } td,th { padding:10px; } .panel { margin-bottom:20px; background-color:#fff; border:1px solid transparent; border-radius:4px; -webkit-box-shadow:0 1px 1px rgba(0,0,0,.05); box-shadow:0 1px 1px rgba(0,0,0,.05); } .panel-body { padding:15px; } .panel-heading { padding:10px 15px; border-bottom:1px solid transparent; border-top-left-radius:3px; border-top-right-radius:3px; } .panel-title { margin-top:0; margin-bottom:0; font-size:16px; color:inherit; } .panel-default { border-color:#ddd; } .panel-default > .panel-heading { color:#333; background-color:#f5f5f5; border-color:#ddd; } .panel-primary { border-color: #337ab7; } .panel-primary > .panel-heading { color: #fff; background-color: #337ab7; border-color: #337ab7; } .label { display:inline; padding:.3em 1em; font-size:75%; font-weight:bold; line-height:1; color:#fff; text-align:center; white-space:nowrap; vertical-align:baseline; border-radius:.25em; } .label-unknown { background-color:#777; } .label-primary { background-color:#337ab7; } .label-ok { background-color:#5cb85c; } .label-warning { background-color:#f0ad4e; } .label-critical { background-color:#d9534f; } .label-small { font-size:12px; } .ambari-footer{ font-family: Arial, Helvetica, sans-serif; font-size: 12px; } </style> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">Services Reporting Alerts</h3> </div> <div class="panel-body"> <table> #foreach( $alertState in $alertStates ) #if( $summary.getServicesByAlertState($alertState) ) <tr> <td> <span class="label label-$alertState">$alertState</span> </td> <td> $summary.getServicesByAlertState($alertState) </td> </tr> #end #end </table> </div> </div> #foreach( $service in $services ) <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">$service</h3> </div> <div class="panel-body"> <table> #foreach( $alertState in $alertStates ) #foreach( $alert in $summary.getAlerts($service,$alertState) ) <tr> <td> <span class="label label-$alertState">$alertState</span> </td> <td> $alert.getAlertDefinition().getLabel() <div class="label-small"> $alert.getAlertText() </div> <div class="label-small"> Cluster: $alert.getAlertDefinition().getCluster().getClusterName() </div> #if( $alert.getHostName() ) #if( $ambari.hasUrl() ) <div class="label-small"> Host: <a href=$ambari.getUrl()/#/main/hosts/$alert.getHostName()/summary>$ambari.getUrl()/#/main/hosts/$alert.getHostName()/summary</a> </div> #else <div class="label-small"> Host: $alert.getHostName() </div> #end #end </td> </tr> #end #end </table> </div> </div> #end <div class="ambari-footer"> This notification was sent to $dispatch.getTargetName() <br/> Apache Ambari $ambari.getServerVersion() #if( $ambari.hasUrl() ) <br/> Ambari Server link: <a href=$ambari.getUrl()>$ambari.getUrl()</a> #end </div> </html> ]]> </body> </alert-template> <alert-template type="SNMP"> <subject> <![CDATA[[$alert.getAlertState()] $alert.getAlertName()]]> </subject> <body> <![CDATA[ [Alert] $alert.getAlertName() [Service] $alert.getServiceName() #if( $alert.hasComponentName() ) [Component] $alert.getComponentName() #end #if( $alert.hasHostName() ) [Host] $alert.getHostName() #end $alert.getAlertText()]]> </body> </alert-template> </alert-templates>
将alert-templates.xml 文件放到Ambari服务器的 /var/lib/ambari-server/resources/ 下;
并且赋予755权限;
chmod 755 /var/lib/ambari-server/resources/alert-templates.xml
5、修改ambari的配置文件
编辑 /etc/ambari-server/conf/ambari.properties
在文件最后一行增加: alerts.template.file=/var/lib/ambari-server/resources/alert-templates.xml
6、重启ambari server
ambari-server restart
7、测试告警
这里是测试集群,直接停掉了hbase的Hmaster
稍等片刻,已收到告警: