提交 927467cb 编写于 作者: 很久是多久's avatar 很久是多久

增加服务采集系统

上级 0fe25e09
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>thinglinks-visual</artifactId>
<groupId>com.mqttsnet</groupId>
<version>1.0.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>thinglinks-visual-collection</artifactId>
<dependencies>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringBoot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Swagger UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<!-- thinglinks Common Log -->
<dependency>
<groupId>com.mqttsnet</groupId>
<artifactId>thinglinks-common-log</artifactId>
<version>${thinglinks.version}</version>
</dependency>
<!-- thinglinks Common Rocketmq -->
<dependency>
<groupId>com.mqttsnet</groupId>
<artifactId>thinglinks-common-rocketmq</artifactId>
<version>${thinglinks.version}</version>
</dependency>
<!-- thinglinks Common Swagger -->
<dependency>
<groupId>com.mqttsnet</groupId>
<artifactId>thinglinks-common-swagger</artifactId>
<version>${thinglinks.version}</version>
</dependency>
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.mqttsnet.thinglinks.collection;
import com.mqttsnet.thinglinks.common.security.annotation.EnableRyFeignClients;
import com.mqttsnet.thinglinks.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.CrossOrigin;
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableCaching
@EnableScheduling
@CrossOrigin(origins = "*", maxAge = 3600)
public class ThingLinksCollectionApplication {
public static void main(String[] args) {
{
SpringApplication.run(ThingLinksCollectionApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 服务采集系统启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}
}
package com.mqttsnet.thinglinks.collection.common.job;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mqttsnet.thinglinks.collection.common.mq.CollectionProducer;
import com.mqttsnet.thinglinks.collection.entity.*;
import com.mqttsnet.thinglinks.collection.util.FormatUtil;
import com.mqttsnet.thinglinks.collection.util.OshiUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 定时推送服务数据
*
* @author shisen
*/
@Component
@Slf4j
public class ScheduledJob {
public static List<AppInfo> appInfoList = Collections.synchronizedList(new ArrayList<AppInfo>());
private SystemInfo systemInfo = null;
@Autowired
private CollectionProducer collectionProducer;
@Value("${rocketmq.topic1}")
private String topic1;
@Value("${rocketmq.topic2}")
private String topic2;
@Value("${base.bindIp}")
private String bindIp;
/**
* 60秒后执行,每隔120秒执行, 单位:ms。
*/
@Scheduled(initialDelay = 59 * 1000L, fixedRate = 120 * 1000)
public void minTask() {
List<AppInfo> APP_INFO_LIST_CP = new ArrayList<AppInfo>();
APP_INFO_LIST_CP.addAll(appInfoList);
JSONObject jsonObject = new JSONObject();
LogInfo logInfo = new LogInfo();
Timestamp t = FormatUtil.getNowTime();
logInfo.setHostname(bindIp + ":Agent错误");
logInfo.setCreateTime(t);
try {
oshi.SystemInfo si = new oshi.SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
OperatingSystem os = si.getOperatingSystem();
// 操作系统信息
systemInfo = OshiUtil.os(hal.getProcessor(), os);
systemInfo.setCreateTime(t);
// 文件系统信息
List<DeskState> deskStateList = OshiUtil.file(t, os.getFileSystem());
// cpu信息
CpuState cpuState = OshiUtil.cpu(hal.getProcessor());
cpuState.setCreateTime(t);
// 内存信息
MemState memState = OshiUtil.memory(hal.getMemory());
memState.setCreateTime(t);
// 网络流量信息
NetIoState netIoState = OshiUtil.net(hal);
netIoState.setCreateTime(t);
// 系统负载信息
SysLoadState sysLoadState = OshiUtil.getLoadState(systemInfo, hal.getProcessor());
if (sysLoadState != null) {
sysLoadState.setCreateTime(t);
}
if (cpuState != null) {
jsonObject.put("cpuState", cpuState);
}
if (memState != null) {
jsonObject.put("memState", memState);
}
if (netIoState != null) {
jsonObject.put("netIoState", netIoState);
}
if (sysLoadState != null) {
jsonObject.put("sysLoadState", sysLoadState);
}
if (systemInfo != null) {
if (memState != null) {
systemInfo.setVersionDetail(systemInfo.getVersion() + ",总内存:" + oshi.util.FormatUtil.formatBytes(hal.getMemory().getTotal()));
systemInfo.setMemPer(memState.getUsePer());
} else {
systemInfo.setMemPer(0d);
}
if (cpuState != null) {
systemInfo.setCpuPer(cpuState.getSys());
} else {
systemInfo.setCpuPer(0d);
}
jsonObject.put("systemInfo", systemInfo);
}
if (deskStateList != null) {
jsonObject.put("deskStateList", deskStateList);
}
//进程信息
if (APP_INFO_LIST_CP.size() > 0) {
List<AppInfo> appInfoResList = new ArrayList<>();
List<AppState> appStateResList = new ArrayList<>();
for (AppInfo appInfo : APP_INFO_LIST_CP) {
appInfo.setHostname(bindIp);
appInfo.setCreateTime(t);
appInfo.setState("1");
String pid = FormatUtil.getPidByFile(appInfo);
if (StringUtils.isEmpty(pid)) {
continue;
}
AppState appState = OshiUtil.getLoadPid(pid, os, hal.getMemory());
if (appState != null) {
appState.setCreateTime(t);
appState.setAppInfoId(appInfo.getId());
appInfo.setMemPer(appState.getMemPer());
appInfo.setCpuPer(appState.getCpuPer());
appInfoResList.add(appInfo);
appStateResList.add(appState);
}
}
jsonObject.put("appInfoList", appInfoResList);
jsonObject.put("appStateList", appStateResList);
}
log.debug("---------------" + jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
logInfo.setInfoContent(e.toString());
} finally {
if (!StringUtils.isEmpty(logInfo.getInfoContent())) {
jsonObject.put("logInfo", logInfo);
}
collectionProducer.senJsonObject(topic2, jsonObject.toString());
}
}
/**
* 30秒后执行,每隔5分钟执行, 单位:ms。
* 获取监控进程
*/
@Scheduled(initialDelay = 28 * 1000L, fixedRate = 300 * 1000)
public void appInfoListTask() {
JSONObject jsonObject = new JSONObject();
LogInfo logInfo = new LogInfo();
Timestamp t = FormatUtil.getNowTime();
logInfo.setHostname(bindIp + ":Agent获取进程列表错误");
logInfo.setCreateTime(t);
try {
JSONObject paramsJson = new JSONObject();
paramsJson.put("hostname", bindIp);
collectionProducer.senJsonObject(topic1, jsonObject.toString());
// String resultJson = restUtil.post(commonConfig.getServerUrl() + "/appInfo/agentList", paramsJson);
// if (resultJson != null) {
// JSONArray resultArray = JSONUtil.parseArray(resultJson);
// appInfoList.clear();
// if (resultArray.size() > 0) {
// appInfoList = JSONUtil.toList(resultArray, AppInfo.class);
// }
// }
} catch (Exception e) {
e.printStackTrace();
logInfo.setInfoContent(e.toString());
} finally {
if (!StringUtils.isEmpty(logInfo.getInfoContent())) {
jsonObject.put("logInfo", logInfo);
}
collectionProducer.senJsonObject(topic2, jsonObject.toString());
// restUtil.post(commonConfig.getServerUrl() + "/agent/minTask", jsonObject);
}
}
}
package com.mqttsnet.thinglinks.collection.common.mq;
import com.mqttsnet.thinglinks.common.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* 采集数据生产
*/
@Slf4j
@Component
public class CollectionProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void senJsonObject(String topic, String json) {
log.info(DateUtils.formatYYYY_MM_DD_HH_MM_SS(LocalDateTime.now()) + ":MQ生产消息开始");
rocketMQTemplate.convertAndSend(topic, json);
log.info(DateUtils.formatYYYY_MM_DD_HH_MM_SS(LocalDateTime.now()) + ":MQ生产消息结束");
}
}
package com.mqttsnet.thinglinks.collection.config;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
/**
* 定时池
*/
@Component
public class TaskSchedulerConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
return taskScheduler;
}
}
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:AppInfo.java
* @author: shisen
* @date: 2021年12月24日
* @Description: app端口信息
*/
@Data
@ApiModel(value = "app端口信息")
public class AppInfo extends BaseEntity {
private static final long serialVersionUID = -2913111613773445949L;
@ApiModelProperty(value = "主机名")
private String hostname;
@ApiModelProperty(value = "应用进程ID")
private String appPid;
@ApiModelProperty(value = "应用进程名称")
private String appName;
@ApiModelProperty(value = "内存使用M")
private Double memPer;
@ApiModelProperty(value = "cpu使用率")
private Double cpuPer;
@ApiModelProperty(value = "进程获取途径,1进程id号,2进程pid文件")
private String appType;
@ApiModelProperty(value = "进程状态,1正常,2下线")
private String state;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:AppState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: app状态监控
*/
@ApiModel(value = "app状态监控")
@Data
public class AppState extends BaseEntity {
private static final long serialVersionUID = -2913111613773445949L;
@ApiModelProperty(value = "应用信息ID")
private String appInfoId;
@ApiModelProperty(value = "%CPU")
private Double cpuPer;
@ApiModelProperty(value = "%MEM")
private Double memPer;
@ApiModelProperty(value = "添加时间 MM-dd hh:mm:ss")
private String dateStr;
@ApiModelProperty(value = "创建时间")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import java.io.Serializable;
public class BaseEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8698319936744959815L;
private String id;
private Integer page;
private Integer pageSize;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getPage() {
if (page == null) {
page = 1;
}
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPageSize() {
if (pageSize == null) {
pageSize = 20;
}
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:CpuState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 查看CPU使用情况
*/
@ApiModel(value = "查看CPU使用情况")
@Data
public class CpuState extends BaseEntity {
private static final long serialVersionUID = -2913111613773445949L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "用户态的CPU时间(%)废弃")
private String user;
@ApiModelProperty(value = "cpu使用率")
private Double sys;
@ApiModelProperty(value = "当前空闲率")
private Double idle;
@ApiModelProperty(value = "cpu当前等待率")
private Double iowait;
@ApiModelProperty(value = "硬中断时间(%) 废弃")
private String irq;
@ApiModelProperty(value = "软中断时间(%) 废弃")
private String soft;
@ApiModelProperty(value = "添加时间 MM-dd hh:mm:ss")
private String dateStr;
@ApiModelProperty(value = "创建时间")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @ClassName:DashboardView.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 主面板概要信息
*/
@ApiModel(value = "主面板概要信息")
@Data
public class DashboardView extends BaseEntity {
private static final long serialVersionUID = -1262528746414406709L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "系统版本信息")
private String version;
@ApiModelProperty(value = "系统已经运行了多少天")
private String yxDays;
@ApiModelProperty(value = "内存已使用百分比")
private double memPer;
@ApiModelProperty(value = "更新时间")
private String dateStr;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:DeskState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 查看磁盘大小使用信息
*/
@ApiModel(value = "查看磁盘大小使用信息")
@Data
public class DeskState extends BaseEntity {
private static final long serialVersionUID = 879979812204191283L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "盘符类型")
private String fileSystem;
@ApiModelProperty(value = "分区大小")
private String size;
@ApiModelProperty(value = "已使用")
private String used;
@ApiModelProperty(value = "可用")
private String avail;
@ApiModelProperty(value = "已使用百分比")
private String usePer;
@ApiModelProperty(value = "添加时间 yyyy-MM-dd hh:mm:ss")
private String dateStr;
@ApiModelProperty(value = "创建时间")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:HostInfo.java
* @author: shisen
* @date: 2021年12月24日
* @Description: host的IP密码等信息
*/
@ApiModel(value = "host的IP密码等信息")
@Data
public class HostInfo extends BaseEntity {
private static final long serialVersionUID = 3875927332935900938L;
@ApiModelProperty(value = "host名称")
private String ip;
@ApiModelProperty(value = "用户")
private String root;
@ApiModelProperty(value = "ssh端口")
private String port;
@ApiModelProperty(value = "密码")
private String passwd;
@ApiModelProperty(value = "备注")
private String remark;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.sql.Timestamp;
/**
* @ClassName:IntrusionInfo.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 检查系统入侵信息
*/
@ApiModel(value = "检查系统入侵信息")
@Data
public class IntrusionInfo extends BaseEntity {
private static final long serialVersionUID = 879979812204191283L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "系统内核模块")
private String lsmod;
@ApiModelProperty(value = "查看passwd文件修改时间")
private String passwdInfo;
@ApiModelProperty(value = "查看系统计划任务")
private String crontab;
@ApiModelProperty(value = "检查网络,正常网卡不该在promisc模式,可能存在sniffer")
private String promisc;
@ApiModelProperty(value = "系统rpc服务")
private String rpcinfo;
@ApiModelProperty(value = "创建时间")
private Timestamp createTime;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:LogInfo.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 日志信息
*/
@ApiModel(value = "日志信息")
@Data
public class LogInfo extends BaseEntity {
private static final long serialVersionUID = 1565538727002722890L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "描述")
private String infoContent;
@ApiModelProperty(value = "0成功,1失败")
private String state;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:DiskIoState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 查看磁盘IO使用情况
*/
@ApiModel(value = "查看磁盘IO使用情况")
@Data
public class MailSet extends BaseEntity {
private static final long serialVersionUID = -8284741180883299533L;
@ApiModelProperty(value = "是否发送邮件告警,1发送0不发送")
private String sendMail;
@ApiModelProperty(value = "发送邮箱的帐号")
private String fromMailName;
@ApiModelProperty(value = "发送邮箱的密码")
private String fromPwd;
@ApiModelProperty(value = "发送邮箱的SMTP服务器")
private String smtpHost;
@ApiModelProperty(value = "发送邮箱的SMTP端口,25或465")
private String smtpPort;
@ApiModelProperty(value = "发送邮箱是否启用安全链接(SSL),1启用,0不启用")
private String smtpSSL;
@ApiModelProperty(value = "接受告警信息的邮件")
private String toMail;
@ApiModelProperty(value = "cpu使用率告警值")
private Integer cpuPer;
@ApiModelProperty(value = "mem使用率告警值")
private Integer memPer;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:MemState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 查看内存使用情况
*/
@ApiModel(value = "查看磁盘IO使用情况")
@Data
public class MemState extends BaseEntity {
private static final long serialVersionUID = -1412473355088780549L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "总计内存,M")
private String total;
@ApiModelProperty(value = "已使用多少,M")
private String used;
@ApiModelProperty(value = "未使用,M")
private String free;
@ApiModelProperty(value = "已使用百分比%")
private Double usePer;
@ApiModelProperty(value = "添加时间 yyyy-MM-dd hh:mm:ss")
private String dateStr;
@ApiModelProperty(value = "创建时间 ")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:NetIoState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 网络设备的吞吐率
*/
@ApiModel(value = "网络设备的吞吐率")
@Data
public class NetIoState extends BaseEntity {
private static final long serialVersionUID = -8314012397341825158L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "每秒钟接收的数据包,rxpck/s")
private String rxpck;
@ApiModelProperty(value = "每秒钟发送的数据包,txpck/s")
private String txpck;
@ApiModelProperty(value = "每秒钟接收的KB数,txpck/s")
private String rxbyt;
@ApiModelProperty(value = "每秒钟发送的KB数,txkB/s")
private String txbyt;
@ApiModelProperty(value = "每秒钟接收的压缩数据包,rxcmp/s")
private String rxcmp;
@ApiModelProperty(value = "每秒钟发送的压缩数据包,txcmp/s")
private String txcmp;
@ApiModelProperty(value = "每秒钟接收的多播数据包,rxmcst/s")
private String rxmcst;
@ApiModelProperty(value = "添加时间 yyyy-MM-dd hh:mm:ss")
private String dateStr;
@ApiModelProperty(value = "创建时间")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:SysLoadState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: uptime查看系统负载状态
*/
@ApiModel(value = "uptime查看系统负载状态")
@Data
public class SysLoadState extends BaseEntity {
private static final long serialVersionUID = -4863071148000213553L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "1分钟之前到现在的负载")
private Double oneLoad;
@ApiModelProperty(value = "5分钟之前到现在的负载")
private Double fiveLoad;
@ApiModelProperty(value = "15分钟之前到现在的负载")
private Double fifteenLoad;
@ApiModelProperty(value = "登录用户数量 废弃")
private String users;
@ApiModelProperty(value = "添加时间 yyyy-MM-dd hh:mm:ss")
private String dateStr;
@ApiModelProperty(value = "创建时间")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:SystemInfo.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 查看系统信息
*/
@ApiModel(value = "查看系统信息")
@Data
public class SystemInfo extends BaseEntity {
private static final long serialVersionUID = 879979812204191283L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "系统版本信息")
private String version;
@ApiModelProperty(value = "系统版本详细信息")
private String versionDetail;
@ApiModelProperty(value = "内存使用率")
private Double memPer;
@ApiModelProperty(value = "core的个数(即核数)")
private String cpuCoreNum;
@ApiModelProperty(value = "cpu使用率")
private Double cpuPer;
@ApiModelProperty(value = "CPU型号信息")
private String cpuXh;
@ApiModelProperty(value = "主机状态,1正常,2下线")
private String state;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.entity;
import com.mqttsnet.thinglinks.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @ClassName:TcpState.java
* @author: shisen
* @date: 2021年12月24日
* @Description: 查看TCP连接状态
*/
@ApiModel(value = "查看系统信息")
@Data
public class TcpState extends BaseEntity {
private static final long serialVersionUID = -299667815095138020L;
@ApiModelProperty(value = "host名称")
private String hostname;
@ApiModelProperty(value = "每秒本地发起的TCP连接数,既通过connect调用创建的TCP连接;,active/s")
private String active;
@ApiModelProperty(value = "每秒远程发起的TCP连接数,即通过accept调用创建的TCP连接,passive/s")
private String passive;
@ApiModelProperty(value = "每秒TCP重传数量,retrans/s")
private String retrans;
@ApiModelProperty(value = "添加时间")
private String dateStr;
@ApiModelProperty(value = "创建时间")
private Date createTime;
public String getDateStr() {
if (!StringUtils.isEmpty(dateStr) && dateStr.length() > 16) {
return dateStr.substring(5);
}
return dateStr;
}
}
\ No newline at end of file
package com.mqttsnet.thinglinks.collection.util;
import cn.hutool.core.io.FileUtil;
import com.mqttsnet.thinglinks.collection.entity.AppInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @ClassName:FormatUtil.java
* @author: shisen
* @date: 2021年12月24日
* @Description: FormatUtil.java
*/
public class FormatUtil {
private static Logger logger = LoggerFactory.getLogger(FormatUtil.class);
private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 格式化double数据,截取小数点后数字
*
* @param str
* @param num
* @return
*/
public static double formatDouble(double str, int num) {
java.math.BigDecimal b = new java.math.BigDecimal(str);
double myNum3 = b.setScale(num, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
return myNum3;
}
public static String delChar(String str) {
if (StringUtils.isEmpty(str)) {
return "";
}
str = str.replace("%", "");
return str;
}
/**
* 获取当前时间
*
* @return 当前日期
*/
public static Timestamp getNowTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATETIME_PATTERN);
Timestamp nowTime = Timestamp.valueOf(dateFormat.format(new Date()));
return nowTime;
}
public static Date getDateBefore(Date datetimes, int day) {
Calendar now = Calendar.getInstance();
now.setTime(datetimes);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}
/**
* m转为g
*
* @param str
* @return
*/
public static double mToG(String str) {
double result = 0;
double mod = 1024;
if (str.contains("M")) {
double f = Double.valueOf(str.replace("M", ""));
result = f / mod;
} else if (str.contains("K")) {
double f = Double.valueOf(str.replace("K", ""));
result = (f / mod) / mod;
} else if (str.contains("T")) {
double f = Double.valueOf(str.replace("T", ""));
result = f * 1024;
} else if (str.contains("G")) {
result = Double.valueOf(str.replace("G", ""));
}
return formatDouble(result, 2);
}
public static String getPidByFile(AppInfo appInfo) {
if ("1".equals(appInfo.getAppType())) {
return appInfo.getAppPid();
} else {
try {
String pid = FileUtil.readString(appInfo.getAppPid(), "UTF-8");
if (!StringUtils.isEmpty(pid)) {
return pid.trim();
}
} catch (Exception e) {
logger.error("获取PID文件错误", e);
}
}
return "";
}
}
package com.mqttsnet.thinglinks.collection.util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @ClassName:MD5Utils.java
* @author: shisen
* @date: 2021年12月24日
* @Description: Md5加密处理
*/
@SuppressWarnings("unused")
public class MD5Utils {
private static final Logger logger = LoggerFactory.getLogger(MD5Utils.class);
// 全局数组
private final static String[] strDigits = {"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
/**
* 16进制字符
*/
private final static char hexdigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 对文件全文生成MD5摘要
*
* @return MD5摘要码
*/
public static String getMD5ForFile(String filePath) {
FileInputStream fis = null;
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
File file = new File(filePath);
if (!file.exists()) {
return "";
}
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
int length = -1;
while ((length = fis.read(buffer)) != -1) {
md.update(buffer, 0, length);
}
byte[] b = md.digest();
return byteToHexString(b);
} catch (Exception ex) {
logger.error("获取MD5信息发生异常!" + ex.toString());
return null;
} finally {
try {
if (null != fis) {
fis.close();
}
} catch (IOException e) {
logger.error("获取MD5信息发生异常!" + e.toString());
}
}
}
/**
* 把byte[]数组转换成十六进制字符串表示形式
*
* @param tmp 要转换的byte[]
* @return 十六进制字符串表示形式
*/
private static String byteToHexString(byte[] tmp) {
String s;
char str[] = new char[16 * 2];
int k = 0;
for (int i = 0; i < 16; i++) {
byte byte0 = tmp[i];
str[k++] = hexdigits[byte0 >>> 4 & 0xf];
str[k++] = hexdigits[byte0 & 0xf];
}
s = new String(str);
return s;
}
// 返回形式为数字跟字符串
private static String byteToArrayString(byte bByte) {
int iRet = bByte;
// System.out.println("iRet="+iRet);
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
// 返回形式只为数字
private static String byteToNum(byte bByte) {
int iRet = bByte;
System.out.println("iRet1=" + iRet);
if (iRet < 0) {
iRet += 256;
}
return String.valueOf(iRet);
}
// 转换字节数组为16进制字串
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}
public static String GetMD5Code(String strObj) {
if (StringUtils.isEmpty(strObj)) {
return "";
}
String resultString = null;
try {
resultString = new String(strObj);
MessageDigest md = MessageDigest.getInstance("MD5");
// md.digest() 该函数返回值为存放哈希值结果的byte数组
resultString = byteToString(md.digest(strObj.getBytes()));
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return resultString;
}
}
package com.mqttsnet.thinglinks.collection.util;
import com.mqttsnet.thinglinks.collection.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import oshi.util.Util;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: OshiUtil
* @author: shisen
* @date: 2021年12月24日
* @Description: Oshi工具类
*/
@Slf4j
public class OshiUtil {
private static String bindIp;
@Value("${base.bindIp}")
public void setBindIp(String bindIp){
OshiUtil.bindIp = bindIp;
}
private static Runtime r = Runtime.getRuntime();
/**
* 获取内存使用信息
*
* @return
*/
public static MemState memory(GlobalMemory memory) throws Exception {
MemState memState = new MemState();
long total = memory.getTotal() / 1024L / 1024L;
long free = memory.getAvailable() / 1024L / 1024L;
double usePer = (double) (total - free) / (double) total;
memState.setUsePer(FormatUtil.formatDouble(usePer * 100, 1));
memState.setHostname(bindIp);
return memState;
}
/**
* 获取cpu使用率,等待率,空闲率
*
* @return
* @throws Exception
*/
public static CpuState cpu(CentralProcessor processor) throws Exception {
long[] prevTicks = processor.getSystemCpuLoadTicks();
// Wait a second...
Util.sleep(1000);
CpuState cpuState = new CpuState();
cpuState.setSys(FormatUtil.formatDouble(processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100, 1));
cpuState.setHostname(bindIp);
return cpuState;
}
/**
* 获取操作系统信息
*
* @return
* @throws Exception
*/
public static SystemInfo os(CentralProcessor processor, OperatingSystem os) throws Exception {
SystemInfo systemInfo = new SystemInfo();
systemInfo.setHostname(bindIp);
systemInfo.setCpuCoreNum(processor.getLogicalProcessorCount() + "");
String cpuInfo = processor.toString();
if (cpuInfo.indexOf("\n") > 0) {
cpuInfo = cpuInfo.substring(0, cpuInfo.indexOf("\n"));
}
systemInfo.setCpuXh(cpuInfo);
systemInfo.setVersion(os.toString());
systemInfo.setVersionDetail(os.toString());
systemInfo.setState("1");
return systemInfo;
}
/**
* 获取磁盘使用信息
*
* @throws Exception
*/
public static List<DeskState> file(Timestamp t, FileSystem fileSystem) throws Exception {
List<DeskState> list = new ArrayList<DeskState>();
List<OSFileStore> fsArray = fileSystem.getFileStores();
for (OSFileStore fs : fsArray) {
long usable = fs.getUsableSpace();
long total = fs.getTotalSpace();
DeskState deskState = new DeskState();
deskState.setFileSystem(fs.getName());
deskState.setHostname(bindIp);
deskState.setUsed(((total - usable) / 1024 / 1024 / 1024) + "G");
deskState.setAvail((usable / 1024 / 1024 / 1024) + "G");
deskState.setSize((total / 1024 / 1024 / 1024) + "G");
double usedSize = (total - usable);
double usePercent = 0d;
if (usedSize != 0 && total != 0) {
usePercent = FormatUtil.formatDouble(usedSize / total * 100D, 2);
}
deskState.setUsePer(usePercent + "%");
deskState.setCreateTime(t);
list.add(deskState);
}
return list;
}
/**
* 获取系统负载
*
* @return
*/
public static SysLoadState getLoadState(SystemInfo systemInfo, CentralProcessor processor) throws Exception {
SysLoadState sysLoadState = new SysLoadState();
if (systemInfo == null) {
return null;
}
if (systemInfo.getVersionDetail().indexOf("Microsoft") > -1) {
//windows系统不支持负载指标
return null;
}
double[] loadAverage = processor.getSystemLoadAverage(3);
sysLoadState.setOneLoad(loadAverage[0]);
sysLoadState.setHostname(bindIp);
sysLoadState.setFiveLoad(loadAverage[1]);
sysLoadState.setFifteenLoad(loadAverage[2]);
return sysLoadState;
}
/**
* 获取进程信息
*
* @return
*/
public static AppState getLoadPid(String pid, OperatingSystem os, GlobalMemory memory) throws Exception {
try {
List<Integer> pidList = new ArrayList<>();
pidList.add(Integer.valueOf(pid));
List<OSProcess> procs = os.getProcesses(pidList);
for (int i = 0; i < procs.size() && i < 5; i++) {
OSProcess p = procs.get(i);
AppState appState = new AppState();
appState.setCpuPer(FormatUtil.formatDouble(100d * (p.getKernelTime() + p.getUserTime()) / p.getUpTime(), 2));
appState.setMemPer(FormatUtil.formatDouble(100d * p.getResidentSetSize() / memory.getTotal(), 2));
return appState;
}
} catch (Exception e) {
log.error("获取进程信息错误", e);
}
return null;
}
/**
* 获取网络带宽
*
* @param hal
* @return
* @throws Exception
*/
public static NetIoState net(HardwareAbstractionLayer hal) throws Exception {
long rxBytesBegin = 0;
long txBytesBegin = 0;
long rxPacketsBegin = 0;
long txPacketsBegin = 0;
long rxBytesEnd = 0;
long txBytesEnd = 0;
long rxPacketsEnd = 0;
long txPacketsEnd = 0;
List<NetworkIF> listBegin = hal.getNetworkIFs();
for (NetworkIF net : listBegin) {
rxBytesBegin += net.getBytesRecv();
txBytesBegin += net.getBytesSent();
rxPacketsBegin += net.getPacketsRecv();
txPacketsBegin += net.getPacketsSent();
}
//暂停3秒
Thread.sleep(5000);
List<NetworkIF> listEnd = hal.getNetworkIFs();
for (NetworkIF net : listEnd) {
rxBytesEnd += net.getBytesRecv();
txBytesEnd += net.getBytesSent();
rxPacketsEnd += net.getPacketsRecv();
txPacketsEnd += net.getPacketsSent();
}
long rxBytesAvg = (rxBytesEnd - rxBytesBegin) / 3 / 1024;
long txBytesAvg = (txBytesEnd - txBytesBegin) / 3 / 1024;
long rxPacketsAvg = (rxPacketsEnd - rxPacketsBegin) / 3 / 1024;
long txPacketsAvg = (txPacketsEnd - txPacketsBegin) / 3 / 1024;
NetIoState netIoState = new NetIoState();
netIoState.setRxbyt(rxBytesAvg + "");
netIoState.setTxbyt(txBytesAvg + "");
netIoState.setRxpck(rxPacketsAvg + "");
netIoState.setTxpck(txPacketsAvg + "");
netIoState.setHostname(bindIp);
return netIoState;
}
}
Spring Boot Version: ${spring-boot.version}
Spring Application Name: ${spring.application.name}
,----,
,/ .`| ,--, ,--. ,--. ,--.
,` .' : ,--.'| ,---, ,--.'| ,----.. ,--, ,---, ,--.'| ,--/ /| .--.--.
; ; / ,--, | :,`--.' | ,--,: : | / / \ ,--.'| ,`--.' | ,--,: : |,---,': / '/ / '.
.'___,/ ,',---.'| : '| : :,`--.'`| ' :| : : | | : | : :,`--.'`| ' :: : '/ /| : /`. /
| : | | | : _' |: | '| : : | |. | ;. / : : ' : | '| : : | || ' , ; | |--`
; |.'; ; : : |.' || : |: | \ | :. ; /--` | ' | | : |: | \ | :' | / | : ;_
`----' | | | ' ' ; :' ' ;| : ' '; |; | ; __ ' | | ' ' ;| : ' '; || ; ; \ \ `.
' : ; ' | .'. || | |' ' ;. ;| : |.' .'| | : | | |' ' ;. ;: ' \ `----. \
| | ' | | : | '' : ;| | | \ |. | '_.' :' : |__ ' : ;| | | \ || | ' __ \ \ |
' : | ' : | : ;| | '' : | ; .'' ; : \ || | '.'|| | '' : | ; .'' : |. \/ /`--' /
; |.' | | ' ,/ ' : || | '`--' ' | '/ .'; : ;' : || | '`--' | | '_\.'--'. /
'---' ; : ;--' ; |.' ' : | | : / | , / ; |.' ' : | ' : | `--'---'
| ,/ '---' ; |.' \ \ .' ---`-' '---' ; |.' ; |,'
'---' '---' `---` '---' '---'
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/thinglinks-collection" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%black(thinglinks.mqttsnet.com) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger) - %cyan(%msg%n)" />
<!-- mqtts.net输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.mqttsnet.thinglinks" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
</configuration>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册