kongdeqiang
2023-07-05 4390e392275b4320e18e39a508149d24bce04f93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.boying.job;
 
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boying.entity.SnmpInfo;
import com.boying.entity.SystemInfo;
import com.boying.entity.WarnLog;
import com.boying.service.SnmpInfoService;
import com.boying.service.SystemInfoService;
import com.boying.service.WarnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName YunWeiSchheduled.java
 * @Description TODO
 * @createTime 2023年07月01日 18:09:00
 */
@Slf4j
@Component
public class YunWeiSchheduled {
 
    @Autowired
    private SystemInfoService systemInfoService;
    @Autowired
    private SnmpInfoService snmpInfoService;
    @Autowired
    private WarnService warnService;
 
 
 
    private Boolean taskFlag = false;
 
    @Scheduled(cron = "0 0/3 * * * ?")
    public void execute() throws Exception {
        if (taskFlag == true) {
            System.out.println("正在运行,强制退出-------》");
            return;
        }else{
            QueryWrapper<SystemInfo> wrapper = new QueryWrapper<>();
            wrapper.select(" id,host_name,state");
            wrapper.lambda().eq(SystemInfo::getState,"2");
            List<SystemInfo> list = systemInfoService.list(wrapper);
            for (SystemInfo systemInfo : list) {
                WarnLog warnLog = new WarnLog();
                warnLog.setId(UUID.fastUUID().toString(true));
                warnLog.setWarnDate(LocalDateTime.now());
                warnLog.setName(systemInfo.getHostName());
                warnLog.setContent("主机掉线");
                warnLog.setType(1);
                warnService.save(warnLog);
            }
            QueryWrapper<SnmpInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.select(" id,host_name,state");
            wrapper1.lambda().eq(SnmpInfo::getState,"2");
            List<SnmpInfo> list1 = snmpInfoService.list(wrapper1);
            for (SnmpInfo snmpInfo : list1) {
                WarnLog warnLog = new WarnLog();
                warnLog.setId(UUID.fastUUID().toString(true));
                warnLog.setWarnDate(LocalDateTime.now());
                warnLog.setName(snmpInfo.getHostName());
                warnLog.setContent("设备掉线");
                warnLog.setType(2);
                warnService.save(warnLog);
            }
 
        }
    }
}