123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import org.apache.log4j.Logger;
- import java.net.InetAddress;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- public class MinitorApplication {
- private final static Logger log = Logger.getLogger(MinitorApplication.class);
- private static int errorTimes = 0;
- private static int requestTimes = 0;
- private static final String ALARM_CONTENT = "每分钟访问一次SDK接口,连续5分钟未获取到数据,请查看服务情况" + "\ud83c\udf4e";
- private static final String MINITOR_ALARM_CONTENT = "定时监控程序运行异常";
- private static final String SDK_MINITOR_ADMIN_PHONE = "18618105263";
- private static final String SDK_ADMIN_PHONE = "18592030302";
- public static void main(String[] args) {
- TimerTask timerTask = new TimerTask() {
- @Override
- public void run() {
- try {
- log.info("---调度监控程序---");
- if (errorTimes == 5 && requestTimes == 5) {
- //钉钉群通知,报警
- InetAddress address = InetAddress.getLocalHost();
- String hostAddress = address.getHostAddress();
- List<String> mobileList = new ArrayList<>();
- mobileList.add(SDK_ADMIN_PHONE);
- boolean alarmResult = AlarmUtil.alarmByDingDing(ALARM_CONTENT + "\n服务器ip:"+hostAddress, false, mobileList);
- log.info("钉钉报警发送结果:" + alarmResult);
- errorTimes = 0;
- requestTimes = 0;
- } else {
- //访问SDK接口
- boolean sdkResult = RequestUtil.requestSDK(args[0], args[1]);
- requestTimes++;
- if (!sdkResult) {
- //失败次数增加1
- errorTimes++;
- }
- }
- } catch (Exception e) {
- List<String> mobileList = new ArrayList<>();
- mobileList.add(SDK_MINITOR_ADMIN_PHONE);
- AlarmUtil.alarmByDingDing(MINITOR_ALARM_CONTENT, false, mobileList);
- log.error("定时任务执行异常,", e);
- }
- }
- };
- // 计时器
- Timer timer = new Timer();
- // 添加执行任务(延迟 0s 执行,每 60s 执行一次)
- timer.schedule(timerTask, 0, 60000);
- }
- }
|