Azkaban是一个工作流任务调度系统,可以以特定的顺序执行一组命令,轻量级且方便使用。本文将为大家介绍如何使用邮件和电话报警让工作流自动调度更加万无一失。
邮件报警
注册一个126邮箱,并在邮箱的账号管理中开启SMTP服务,开启SMTP服务后,页面会显示一个授权密码,这个密码一定记住,因为只会出现这一次。
后续配置步骤如下:
1、在azkaban-web节点上编辑
azkaban-web/conf/azkaban.properties,添加如下内容:
- #这里设置邮件发送服务器,需要申请邮箱,切开通stmp服务,以下只是例子
- mail.sender=helloworld@126.com
- mail.host=smtp.126.com
- mail.user=helloworld@126.com
- mail.password=用邮箱的授权码
-
- #这里设置工作流成功或者失败默认向哪里发送服务
- job.failure.email=helloworld@126.com
- job.success.email=helloworld@126.com
2、保存并重启web-server
- $bin/shutdown-web.sh
- $bin/start-web.sh
3、 编辑basic.flow,加入如下属性:
- nodes:
- - name: jobA
- type: command
- config:
- command: echo "This is an emailtest."
- failure.emails: helloworld@126.com
- success.emails: helloworld@126.com
- notify.emails: helloworld@126.com
4、 将azkaban.project和basic.flow压缩成email.zip
5、 创建工程→上传文件→执行作业→查看结果
6、 观察邮箱,发现执行成功或失败的邮件
自定义报警
有时任务执行失败后邮件报警接收不及时,需要自定义报警装置,比如电话报警。此时需要首先找一个电话通知服务商,比如http://www.onealert.com/,购买相应服务后,获取通知API。然后进行MailAlter二次开发。
1、 onealert官网注册一个账号,配置告警RestAPI,
2、 配置分派策略和通知策略,
3、 开发自定义告警插件
新建一个普通的java项目,命名为alerter,在项目的lib里添加4个jar包,,并将4个jar包 Add as Library。
新建PhoneAlerter类,实现azkaban.alert.Alerter接口,代码如下:
- packagecom.atguigu;
-
- importazkaban.alert.Alerter;
- importazkaban.executor.ExecutableFlow;
- importazkaban.executor.Executor;
- importazkaban.executor.ExecutorManagerException;
- importazkaban.sla.SlaOption;
- importazkaban.utils.Props;
- importcom.google.gson.JsonObject;
- importorg.apache.log4j.Logger;
-
- importjava.util.List;
-
- publicclass PhoneAlterter implements Alerter {
-
- private static final Logger logger =Logger.getLogger(PhoneAlterter.class);
-
- private String appKey;
- private String url;
-
- public PhoneAlterter(Props props) {
- appKey = props.getString("my.alert.appKey","");
- url =props.getString("my.alert.url", "");
- logger.info("Appkey: " +appKey);
- logger.info("URL: " + url);
- }
-
- /**
- * 成功的通知
- *
- * @param exflow
- * @throws Exception
- */
- @Override
- public void alertOnSuccess(ExecutableFlowexflow) throws Exception {
-
- }
-
- /**
- * 出现问题的通知
- *
- * @param exflow
- * @param extraReasons
- * @throws Exception
- */
- @Override
- public void alertOnError(ExecutableFlowexflow, String... extraReasons) throws Exception {
- //一般来说网络电话服务都是通过HTTP请求发送的,这里可以调用shell发送HTTP请求
- JsonObject alert = new JsonObject();
- alert.addProperty("app",appKey);
- alert.addProperty("eventId",exflow.getId());
- alert.addProperty("eventType", "trigger");
- alert.addProperty("alarmContent", exflow.getId() + "fails!");
- alert.addProperty("priority","2");
- String[] cmd = new String[8];
- cmd[0] = "curl";
- cmd[1] = "-H";
- cmd[2] = "Content-type:application/json";
- cmd[3] = "-X";
- cmd[4] = "POST";
- cmd[5] = "-d";
- cmd[6] = alert.toString();
- cmd[7] = url;
- logger.info("Sending phonealert!");
- Runtime.getRuntime().exec(cmd);
-
- }
-
- /**
- * 首次出现问题的通知
- *
- * @param exflow
- * @throws Exception
- */
- @Override
- public voidalertOnFirstError(ExecutableFlow exflow) throws Exception {
-
- }
-
- @Override
- public void alertOnSla(SlaOption slaOption,String slaMessage) throws Exception {
-
- }
-
- @Override
- public void alertOnFailedUpdate(Executorexecutor, List
executions, ExecutorManagerException e) { -
- }
-
- }
新建
azkaban-web/plugin/alerter/phone-alerter文件夹,并在内部新建conf和lib两个目录。
在新建的conf目录里,新建plugin.properties文件,添加内容如下:
- #name一定要设置email,用以覆盖默认的邮件报警
- alerter.name=email
- alerter.external.classpaths=lib
- alerter.class=com.atguigu.PhoneAlterter
- #这两个参数和你使用的AlertAPI有关系
- my.alert.appKey=xxxxxx-xxxxxxxxxxxx
- my.alert.url=xxxxx
将代码jar包上传到azkaban-web/lib文件夹,并重启web服务。
- $bin/shutdown-web.sh
- $bin/start-web.sh中4、 测试,执行任务,等待报警。
4、 测试,执行任务,等待报警。