监控类MonitorHandler.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author admin_z by 2020/4/22
* @ClassName MonitorHandler
*/
public class MonitorHandler {
private static final Log log = LogFactory.getLog(MonitorHandler.class);
private static String URL = "xxxxx";
private static String CLASS_NAME = "xxxxx";
public static void main(String[] args) throws IOException, InterruptedException {
SendEmailHandler sendEmailHandler = new SendEmailHandler();
Map<String, String> monitor = monitor(CLASS_NAME);
while (true) {
try {
Map<String, String> newData = monitor(CLASS_NAME);
for (Map.Entry<String, String> entry : newData.entrySet()) {
if (!monitor.get(entry.getKey()).equals(entry.getValue())) {
//发送邮件
sendEmailHandler.sendFileMail("测试邮件", entry.getValue());
monitor.put(entry.getKey(), entry.getValue());
log.info("发送邮件");
}
}
}catch (Exception e){
log.error(e.getLocalizedMessage());
}
log.info("20s持续监控中");
Thread.sleep(20000);
}
}
//监控
private static Map<String, String> monitor(String... classNames) throws IOException {
Map<String, String> data = new HashMap<>();
//通过延迟2000毫秒然后再去请求可解决js异步加载获取不到数据的问题
Document doc = Jsoup.connect(URL).timeout(2000).get();
for (String className : classNames) {
Elements elementsByClass = doc.getElementsByClass(className);
//data.put(className, elementsByClass.get(1).toString());
}
return data;
}
}
邮件发送类SendEmailHandler.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @author admin_z by 2020/4/22
* @ClassName EmailUtils
*/
public class SendEmailHandler {
/**
* 日志
**/
private static final Log log = LogFactory.getLog(SendEmailHandler.class);
private final String SEND_USER = "xxxxx@qq.com";
private final String SEND_password = "xxxxxxxxx";
private final String GET_USER = "xxxx@qq.com";
private final String HOST = "smtp.qq.com";
private JavaMailSenderImpl senderImpl = null;
public void sendFileMail(String subject, String content) {
initMessageHelper();
try {
// 建立HTML邮件消息
MimeMessage mailMessage = senderImpl.createMimeMessage();
// true表示开始附件模式.如果邮件不需要附件设置成false即可
MimeMessageHelper messageHelper = new MimeMessageHelper(
mailMessage, true, "utf-8");
// 设置收信人的email地址
messageHelper.setTo(GET_USER);
// 设置寄信人的email地址{与上面登陆的邮件一致}
messageHelper.setFrom(SEND_USER);
// 设置邮件发送内容的主题
messageHelper.setSubject(subject);
// true 表示启动HTML格式的邮件
messageHelper.setText("<html><title>这是一封邮件</title><body>"
+ content + "</body></html>", true);
// 如不需要附件,这里可以省略---------------------------------------START
// 读取附件一
FileSystemResource file1 = new FileSystemResource(new File(
"e:/test.jpg"));
// 读取附件二
FileSystemResource file2 = new FileSystemResource(new File(
"e:/测试.txt"));
// 添加附件一
//messageHelper.addAttachment("test.jpg", file1);
// 添加附件二
// 附件名有中文可能出现乱码
//messageHelper
// .addAttachment(MimeUtility.encodeWord("测试.txt"), file2);
// 如不需要附件,这里可以省略------------------------------------------END
// 发送邮件
senderImpl.send(mailMessage);
log.info("Email Send Success!");
} catch (Exception e) {
log.error("Email Send Error!" + e.getMessage());
}
}
private void initMessageHelper() {
if (senderImpl == null) {
senderImpl = new JavaMailSenderImpl();
// 设置自己登陆email的服务商提供的host
senderImpl.setHost(HOST);
// 设置自己登陆邮箱账号
senderImpl.setUsername(SEND_USER);
// 邮箱密码
senderImpl.setPassword(SEND_password);
}
}
public static void main(String[] agrs) {
SendEmailHandler sendEmailHandler = new SendEmailHandler();
// 注意测试需要修改您自己的邮件服务商host,登陆邮件用户,邮件密码,附件,收信人地址
sendEmailHandler.sendFileMail("测试邮件", "<H1>测试邮件标题</H1>");
}
}
linux 下编译 java文件
- javac -cp “lib/*:.” -d classes/ SendEmailHandler.java
-cp :指定依赖路径
“lib/*:.”:指定lib 路径 以及当前目录下所依赖的class文件
-d 指定编译生成的class文件存放路径
执行class文件
- nohup java -classpath .:lib/* MonitorHandler > a.log 2>&1 &
nohup配合&:不挂断的将命令执行到后台
>a.log:日志输出到指定文件
2>&1:标准错误信息也重定向至a.log文件中
-classpath:指定依赖路径
发布到docker容器中
Dockerfile文件
FROM openjdk:8-jdk-alpine3.9
MAINTAINER XXXXX XXXX@qq.com
WORKDIR /root
RUN mkdir java
ADD java/ java
WORKDIR java
CMD ["sh","run.sh"]
run.sh启动文件
···
#!/bin/bash
javac -cp “lib/" SendEmailHandler.java
javac -cp "lib/:.” MonitorHandler.java
nohup java -classpath .:lib/* MonitorHandler > a.log 2>&1 &
···
-
构建镜像
sudo docker build -f thridDockerfile . -
编辑tag
sudo docker tag 05459766d590 monitor:v1 -
启动镜像
sudo docker run --name monitor -d 05459766d590 -
docker run --net=host --name first2 -it 8c9ffe437d7d
–net=host 不会为容器单独创建网络空间 容器可以直接访问主机上所有的网络信息
此时使用 -p 指定端口无效