作者所在的項(xiàng)目是與運(yùn)營商合作的,多數(shù)情況下,生產(chǎn)環(huán)境的服務(wù)器都是部署在內(nèi)網(wǎng)棒掠,這讓監(jiān)控變得非常困難,內(nèi)網(wǎng)的訪問限制控制非常嚴(yán)格屁商,很多時間都需要使用VPN才能遠(yuǎn)程接入烟很,那么在這種情況下颈墅,自動化監(jiān)控變得非常的遙不可及。
大家試想一下雾袱,在這樣的場景下如何監(jiān)控內(nèi)網(wǎng)的業(yè)務(wù)呢恤筛?目前我想的的方案主要有如下幾個
1.開通一個監(jiān)控端口給外網(wǎng),同時在內(nèi)網(wǎng)部署一個監(jiān)控服務(wù)芹橡,通過這個端口把數(shù)據(jù)發(fā)到郵箱或者接入中心監(jiān)控平臺毒坛。
2.如果條件不允許開通任何端口到外網(wǎng),可以使用程序模擬登陸VPN林说,點(diǎn)擊瀏覽器煎殷,去抓圖或者去查詢數(shù)據(jù)庫的數(shù)據(jù)
3.其它方案
今天我將介紹第二種方案,先上代碼:
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
public class ScreenAction {
/**
* 刷新并且保存截圖
*
* @param filePath 文件路徑
* @param fileName 文件名稱
* @throws Exception
*/
public static void reFrshAndcaptureScreen(String filePath, String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F5);
BufferedImage image = robot.createScreenCapture(screenRectangle);
//保存路徑
File screenFile = new File(filePath);
if (!screenFile.exists()) {
screenFile.mkdir();
}
File f = new File(screenFile, fileName);
ImageIO.write(image, "png", f);
//自動打開
// if (Desktop.isDesktopSupported()
// && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)){
// Desktop.getDesktop().open(f);
// }
}
public static void main(String[] args) {
try {
int i = 5;
for (int i1 = 0; i1 < i; i1++) {
reFrshAndcaptureScreen("d:\\你好", DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".PNG");
Thread.sleep(2000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意pom.xlm里要引入hutool工具類:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.4.3</version>
</dependency>
Robot 還可以模擬鼠標(biāo)的移動與點(diǎn)擊腿箩,大家可以自行嘗試一下豪直,方法就是定位到桌面的軟件位置坐標(biāo),調(diào)用鼠標(biāo)點(diǎn)擊函數(shù)就可以實(shí)現(xiàn)珠移。
robot.mouseMove();
robot.mousePress();
實(shí)現(xiàn)的思路是先定位到軟件的位置mouseMove弓乙,然后調(diào)用mousePress
最后再調(diào)用屏幕截圖,使用郵件API把相關(guān)的截圖通過郵件發(fā)送到管理員郵箱就可以實(shí)現(xiàn)監(jiān)控啦钧惧。