MinitorApplication.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import org.apache.log4j.Logger;
  2. import java.net.InetAddress;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7. public class MinitorApplication {
  8. private final static Logger log = Logger.getLogger(MinitorApplication.class);
  9. private static int errorTimes = 0;
  10. private static int requestTimes = 0;
  11. private static final String ALARM_CONTENT = "每分钟访问一次SDK接口,连续5分钟未获取到数据,请查看服务情况" + "\ud83c\udf4e";
  12. private static final String MINITOR_ALARM_CONTENT = "定时监控程序运行异常";
  13. private static final String SDK_MINITOR_ADMIN_PHONE = "18618105263";
  14. private static final String SDK_ADMIN_PHONE = "18592030302";
  15. public static void main(String[] args) {
  16. TimerTask timerTask = new TimerTask() {
  17. @Override
  18. public void run() {
  19. try {
  20. log.info("---调度监控程序---");
  21. if (errorTimes == 5 && requestTimes == 5) {
  22. //钉钉群通知,报警
  23. InetAddress address = InetAddress.getLocalHost();
  24. String hostAddress = address.getHostAddress();
  25. List<String> mobileList = new ArrayList<>();
  26. mobileList.add(SDK_ADMIN_PHONE);
  27. boolean alarmResult = AlarmUtil.alarmByDingDing(ALARM_CONTENT + "\n服务器ip:"+hostAddress, false, mobileList);
  28. log.info("钉钉报警发送结果:" + alarmResult);
  29. errorTimes = 0;
  30. requestTimes = 0;
  31. } else {
  32. //访问SDK接口
  33. boolean sdkResult = RequestUtil.requestSDK(args[0], args[1]);
  34. requestTimes++;
  35. if (!sdkResult) {
  36. //失败次数增加1
  37. errorTimes++;
  38. }
  39. }
  40. } catch (Exception e) {
  41. List<String> mobileList = new ArrayList<>();
  42. mobileList.add(SDK_MINITOR_ADMIN_PHONE);
  43. AlarmUtil.alarmByDingDing(MINITOR_ALARM_CONTENT, false, mobileList);
  44. log.error("定时任务执行异常,", e);
  45. }
  46. }
  47. };
  48. // 计时器
  49. Timer timer = new Timer();
  50. // 添加执行任务(延迟 0s 执行,每 60s 执行一次)
  51. timer.schedule(timerTask, 0, 60000);
  52. }
  53. }