Commit 729a1e69 by liushutao

envoy

parents
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.pmc.loganalysis</groupId>
<artifactId>loganalysis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>loganalysis</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<!--HttpClient-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.53</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- ssh -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--数据库连接加密工具-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<exclusions>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- kingbase driver -->
<dependency>
<groupId>com.kingbase.jdbc</groupId>
<artifactId>kingbase8</artifactId>
<version>8.6.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/kingbase8-8.6.0.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.pmc.loganalysis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication()
public class LoganalysisApplication {
public static void main(String[] args) {
SpringApplication.run(LoganalysisApplication.class, args);
}
}
package com.pmc.loganalysis.config;
import com.pmc.loganalysis.config.multiple_data_sources.DynamicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@MapperScan("com.pmc.loganalysis.mapper") //扫描dao 映射xml
public class MybatisConfig {
@Bean("master")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource master() {
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
}
@Bean
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put("master", master());
// 将 master 数据源作为默认指定的数据源
dynamicDataSource.setDefaultDataSource(master());
// 将 master 和 slave 数据源作为指定的数据源
dynamicDataSource.setDataSources(dataSourceMap);
return dynamicDataSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource作为数据源则不能实现切换
sqlSessionFactoryBean.setDataSource(dynamicDataSource());
sqlSessionFactoryBean.setTypeAliasesPackage("com.pmc.loganalysis.model"); // 扫描Model
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("com.pmc.loganalysis.mapper.xml*.xml")); // 扫描映射文件
return sqlSessionFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager() {
// 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
return new DataSourceTransactionManager(dynamicDataSource());
}
}
package com.pmc.loganalysis.config;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import com.pmc.loganalysis.config.mybatis.BaseTenantParser;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.schema.Column;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* 单数据源配置(jeecg.datasource.open = false时生效)
*
* @Author zhoujf
*/
@Configuration
public class MybatisPlusConfig {
/**
* tenant_id 字段名
*/
public static final String tenant_field = "tenant_id";
/**
* 有哪些表需要做多租户 这些表需要添加一个字段 ,字段名和tenant_field对应的值一样
*/
private static final List<String> tenantTable = new ArrayList<String>();
static {
tenantTable.add("jee_bug_danbiao");
}
/**
* 多租户属于 SQL 解析部分,依赖 MP 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor().setLimit(-1);
/*
* 【测试多租户】 SQL 解析处理拦截器<br>
* 这里固定写成住户 1 实际情况你可以从cookie读取,因此数据看不到 【 麻花藤 】 这条记录( 注意观察 SQL )<br>
*/
List<ISqlParser> sqlParserList = new ArrayList<>();
TenantSqlParser tenantSqlParser = new BaseTenantParser();
tenantSqlParser.setTenantHandler(new TenantHandler() {
@Override
public Expression getTenantId(boolean select) {
// String tenant_id = TenantContext.getTenant();
return new LongValue(1);
}
@Override
public String getTenantIdColumn() {
return tenant_field;
}
@Override
public boolean doTableFilter(String tableName) {
//true则不加租户条件查询 false则加
// return excludeTable.contains(tableName);
if (tenantTable.contains(tableName)) {
return false;
}
return true;
}
private Expression in(String ids) {
final InExpression inExpression = new InExpression();
inExpression.setLeftExpression(new Column(getTenantIdColumn()));
final ExpressionList itemsList = new ExpressionList();
final List<Expression> inValues = new ArrayList<>(2);
for (String id : ids.split(",")) {
inValues.add(new LongValue(id));
}
itemsList.setExpressions(inValues);
inExpression.setRightItemsList(itemsList);
return inExpression;
}
});
sqlParserList.add(tenantSqlParser);
paginationInterceptor.setSqlParserList(sqlParserList);
paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
@Override
public boolean doFilter(MetaObject metaObject) {
MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
// 过滤自定义查询此时无租户信息约束【 麻花藤 】出现
if ("com.baomidou.springboot.mapper.UserMapper.selectListBySQL".equals(ms.getId())) {
return true;
}
return false;
}
});
return paginationInterceptor;
}
}
package com.pmc.loganalysis.config.multiple_data_sources;
import java.lang.annotation.*;
/**
* @ClassDesc:功能描述 动态数据源注解
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
/**
* 数据源key值
*
* @return
*/
String value();
}
package com.pmc.loganalysis.config.multiple_data_sources;
/**
* @ClassDesc:功能描述
*/
public class DataType {
//主数据源 默认
public static final String MASTER = "master";
}
package com.pmc.loganalysis.config.multiple_data_sources;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.Map;
/**
* @ClassDesc:功能描述 动态数据源实现类
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
* 如果不希望数据源在启动配置时就加载好,可以定制这个方法,从任何你希望的地方读取并返回数据源
* 比如从数据库、文件、外部接口等读取数据源信息,并最终返回一个DataSource实现类对象即可
*/
@Override
protected DataSource determineTargetDataSource() {
return super.determineTargetDataSource();
}
/**
* 如果希望所有数据源在启动配置时就加载好,这里通过设置数据源Key值来切换数据,定制这个方法
*/
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceContextHolder.getDataSourceKey();
}
/**
* 设置默认数据源
*
* @param defaultDataSource
*/
public void setDefaultDataSource(Object defaultDataSource) {
super.setDefaultTargetDataSource(defaultDataSource);
}
/**
* 设置数据源
*
* @param dataSources
*/
public void setDataSources(Map<Object, Object> dataSources) {
super.setTargetDataSources(dataSources);
// 将数据源的 key 放到数据源上下文的 key 集合中,用于切换时判断数据源是否有效
DynamicDataSourceContextHolder.addDataSourceKeys(dataSources.keySet());
}
}
package com.pmc.loganalysis.config.multiple_data_sources;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @ClassDesc:功能描述 动态数据源切换处理器
*/
@Aspect
@Order(-1) // 该切面应当先于 @Transactional 执行
@Component
public class DynamicDataSourceAspect {
/**
* 切换数据源
*mapper-spring-boot-starter
* @param point
* @param dataSource
*/
@Before("@annotation(dataSource))")
public void switchDataSource(JoinPoint point, DataSource dataSource) {
if (!DynamicDataSourceContextHolder.containDataSourceKey(dataSource.value())) {
System.out.println("DataSource [{}] doesn't exist, use default DataSource [{}] " + dataSource.value());
} else {
// 切换数据源
DynamicDataSourceContextHolder.setDataSourceKey(dataSource.value());
System.out.println("Switch DataSource to [" + DynamicDataSourceContextHolder.getDataSourceKey()
+ "] in Method [" + point.getSignature() + "]");
}
}
/**
* 重置数据源
*
* @param point
* @param dataSource
*/
@After("@annotation(dataSource))")
public void restoreDataSource(JoinPoint point, DataSource dataSource) {
// 将数据源置为默认数据源
DynamicDataSourceContextHolder.clearDataSourceKey();
System.out.println("Restore DataSource to [" + DynamicDataSourceContextHolder.getDataSourceKey()
+ "] in Method [" + point.getSignature() + "]");
}
}
package com.pmc.loganalysis.config.multiple_data_sources;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
*/
public class DynamicDataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>() {
/**
* 将 master 数据源的 key作为默认数据源的 key
*/
@Override
protected String initialValue() {
return "master";
}
};
/**
* 数据源的 key集合,用于切换时判断数据源是否存在
*/
public static List<Object> dataSourceKeys = new ArrayList<>();
/**
* 切换数据源
*
* @param key
*/
public static void setDataSourceKey(String key) {
contextHolder.set(key);
}
/**
* 获取数据源
*
* @return
*/
public static String getDataSourceKey() {
return contextHolder.get();
}
/**
* 重置数据源
*/
public static void clearDataSourceKey() {
contextHolder.remove();
}
/**
* 判断是否包含数据源
*
* @param key 数据源key
* @return
*/
public static boolean containDataSourceKey(String key) {
return dataSourceKeys.contains(key);
}
/**
* 添加数据源keys
*
* @param keys
* @return
*/
public static boolean addDataSourceKeys(Collection<? extends Object> keys) {
return dataSourceKeys.addAll(keys);
}
}
package com.pmc.loganalysis.config.mybatis;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.*;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.*;
import java.util.List;
/**
* 复写租户条件
*/
public class BaseTenantParser extends TenantSqlParser {
/**
* @param expression
* @param table
* @return
*/
protected Expression processTableAlias(Expression expression, Table table) {
String tableAliasName;
if (table.getAlias() == null) {
tableAliasName = table.getName();
} else {
tableAliasName = table.getAlias().getName();
}
// in
if (expression instanceof InExpression) {
InExpression in = (InExpression) expression;
if (in.getLeftExpression() instanceof Column) {
setTableAliasNameForColumn((Column) in.getLeftExpression(), tableAliasName);
}
// 比较操作
} else if (expression instanceof BinaryExpression) {
BinaryExpression compare = (BinaryExpression) expression;
if (compare.getLeftExpression() instanceof Column) {
setTableAliasNameForColumn((Column) compare.getLeftExpression(), tableAliasName);
} else if (compare.getRightExpression() instanceof Column) {
setTableAliasNameForColumn((Column) compare.getRightExpression(), tableAliasName);
}
// between
} else if (expression instanceof Between) {
Between between = (Between) expression;
if (between.getLeftExpression() instanceof Column) {
setTableAliasNameForColumn((Column) between.getLeftExpression(), tableAliasName);
}
}
return expression;
}
private void setTableAliasNameForColumn(Column column, String tableAliasName) {
column.setColumnName(tableAliasName + "." + column.getColumnName());
}
/**
* 默认是按 tenant_id=1 按等于条件追加
*
* @param currentExpression 现有的条件:比如你原来的sql查询条件
* @param table
* @return
*/
@Override
protected Expression builderExpression(Expression currentExpression, Table table) {
final Expression tenantExpression = this.getTenantHandler().getTenantId(true);
Expression appendExpression;
if (!(tenantExpression instanceof SupportsOldOracleJoinSyntax)) {
appendExpression = new EqualsTo();
((EqualsTo) appendExpression).setLeftExpression(this.getAliasColumn(table));
((EqualsTo) appendExpression).setRightExpression(tenantExpression);
} else {
appendExpression = processTableAlias(tenantExpression, table);
}
if (currentExpression == null) {
return appendExpression;
}
if (currentExpression instanceof BinaryExpression) {
BinaryExpression binaryExpression = (BinaryExpression) currentExpression;
if (binaryExpression.getLeftExpression() instanceof FromItem) {
processFromItem((FromItem) binaryExpression.getLeftExpression());
}
if (binaryExpression.getRightExpression() instanceof FromItem) {
processFromItem((FromItem) binaryExpression.getRightExpression());
}
} else if (currentExpression instanceof InExpression) {
InExpression inExp = (InExpression) currentExpression;
ItemsList rightItems = inExp.getRightItemsList();
if (rightItems instanceof SubSelect) {
processSelectBody(((SubSelect) rightItems).getSelectBody());
}
}
if (currentExpression instanceof OrExpression) {
return new AndExpression(new Parenthesis(currentExpression), appendExpression);
} else {
return new AndExpression(currentExpression, appendExpression);
}
}
@Override
protected void processPlainSelect(PlainSelect plainSelect, boolean addColumn) {
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof Table) {
Table fromTable = (Table) fromItem;
if (!this.getTenantHandler().doTableFilter(fromTable.getName())) {
plainSelect.setWhere(builderExpression(plainSelect.getWhere(), fromTable));
if (addColumn) {
plainSelect.getSelectItems().add(new SelectExpressionItem(new Column(this.getTenantHandler().getTenantIdColumn())));
}
}
} else {
processFromItem(fromItem);
}
List<Join> joins = plainSelect.getJoins();
if (joins != null && joins.size() > 0) {
joins.forEach(j -> {
processJoin(j);
processFromItem(j.getRightItem());
});
}
}
}
package com.pmc.loganalysis.controller;
import com.alibaba.fastjson.JSONObject;
import com.pmc.loganalysis.service.SmsPushsysService;
import com.pmc.loganalysis.util.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
/**
*/
@RestController
@RequestMapping("/pmc")
public class PmcGetData {
@Value("${shell.shell-path1}")
private String shellPath1;
@Value("${shell.log-path1}")
private String logPath1;
@Value("${before-minute}")
Integer beforeminutes;
@Resource
SmsPushsysService smsPushsysService;
@PostMapping(value = "/pmcGetEnvoyMin")
String getTravelCardRateByMin(@RequestBody JSONObject reqtiem){
//获取现在时间(开始时间)
Date nowDate = new Date ();
//获取当前时间的前一分钟
Date startDate = DateUtils.getDate(nowDate,beforeminutes);
String startTime = DateUtils.getDateStringYMDHMS(startDate,DateUtils.DATE_TIME_PATTERN);
String startTimestr = DateUtils.getDateStringYMDHMS(startDate,DateUtils.DATE_YYYYMMDDHHMMSS);
Date endDate = DateUtils.getDate(nowDate,-1);
String endTime = DateUtils.getDateStringYMDHMS(endDate,DateUtils.DATE_TIME_PATTERN);
String endTimestr = DateUtils.getDateStringYMDHMS(endDate,DateUtils.DATE_YYYYMMDDHHMMSS);
return smsPushsysService.pushTravelCardByMinRate(startTime,endTime,startTimestr,endTimestr);
}
}
package com.pmc.loganalysis.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pmc.loganalysis.model.SmsPushsys;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
/**
* @description
*/
public interface SmsPushsysMapper extends BaseMapper<SmsPushsys> {
/**
* 将统计envoy数据插入表
* @param
* @param
* @return
*/
@Insert("<script>" + " INSERT INTO xck_monitor_envoy_sum_min VALUES (#{id},#{msgDate},#{envoyReqtotal},#{createTime}) " +
"</script>")
void insertXCKProStatisEnvoyMin(@Param("id") String id, @Param("msgDate") String msgDate ,@Param("envoyReqtotal") String envoyReqtotal,@Param("createTime") String createTime);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pmc.loganalysis.mapper.SmsPushsysMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.pmc.loganalysis.model.SmsPushsys">
<result column="id" property="id" />
<result column="msg_hours_date" property="msg_hours_date" />
<result column="total" property="total" />
<result column="peak_value_min" property="peak_value_min" />
<result column="peak_value_sec" property="peak_value_sec" />
<result column="system_success_number" property="system_success_number" />
<result column="business_success_number" property="business_success_number" />
</resultMap>
</mapper>
package com.pmc.loganalysis.model;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
/**
* @description
*/
@ApiModel(value = "短信下发表")
@Getter
@Setter
public class SmsPushsys {
@ApiModelProperty(value = "id")
@TableId
private String id;
@ApiModelProperty(value = "msg_hours_date")
private String msg_hours_date;
@ApiModelProperty(value = "total")
private String total;
@ApiModelProperty(value = "peak_value_min")
private String peak_value_min;
@ApiModelProperty(value = "peak_value_sec")
private Integer peak_value_sec;
@ApiModelProperty(value = "system_success_number")
private Date system_success_number;
@ApiModelProperty(value = "business_success_number")
private Date business_success_number;
}
\ No newline at end of file
package com.pmc.loganalysis.service;
/**
* <p>
* 字典表 服务类
* </p>
*
*/
public interface SmsPushsysService {
/**
* 统计每分钟日志
*/
String pushTravelCardByMinRate(String startTimeStr, String endTimeStr, String shellPath, String logPath);
}
package com.pmc.loganalysis.service.impl;
import ch.ethz.ssh2.Connection;
import com.pmc.loganalysis.config.multiple_data_sources.DataType;
import com.pmc.loganalysis.config.multiple_data_sources.DynamicDataSourceContextHolder;
import com.pmc.loganalysis.mapper.SmsPushsysMapper;
import com.pmc.loganalysis.service.SmsPushsysService;
import com.pmc.loganalysis.util.DateUtils;
import com.pmc.loganalysis.util.shell.ExecShell;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* <p>
* 字典表 服务实现类
* </p>
*/
@Service
@Slf4j
public class SmsPushsysServiceImpl implements SmsPushsysService {
@Value("${shell.shell-path1}")
private String shellPath;
@Value("${shell.log-path1}")
private String logPath;
@Resource
private SmsPushsysMapper smsPushsysMapper;
/**
* 服务器
* @param startTimeStr 开始时间
* @param endTimeStr 结束时间
* @return
*/
@Override
public String pushTravelCardByMinRate(String startTime, String endTime,String startTimeStr, String endTimeStr){
DynamicDataSourceContextHolder.setDataSourceKey(DataType.MASTER);
if( StringUtils.isBlank(shellPath) ){
return "配置脚本信息有误";
}
// pCode = CryptoEncoder.base64Decode(pCode);
List<String> commandList1 = this.getCommandList(startTimeStr,endTimeStr,shellPath,logPath);
String finalCommand = "";
for(String str : commandList1){
finalCommand = finalCommand + str + " && ";
}
finalCommand = finalCommand.substring(0,finalCommand.length()-4);
// Connection conn = ExecShell.login(ip, user, pass);
//多条命令可以用&&分割
//String cmd = "cd /home/&&pwd&&ls";
//String result = ExecShell.execute(conn,finalCommand);
String result = ExecShell.executes(finalCommand);
if(result==null||"".equals(result)){
result="0";
}
Date nowDate = new Date ();
//获取当前时间的前一分钟
String createTime = DateUtils.getDateStringYMDHMS(nowDate,DateUtils.DATE_TIME_PATTERN_YYYYMMDDHHMMSSSSS);
smsPushsysMapper.insertXCKProStatisEnvoyMin(UUID.randomUUID().toString(),endTime,result,createTime);
log.info("统计envoy,从时间:"+startTime+"-------"+endTime+"的请求总数:"+result);
return result;
}
/**
* 连接服务器,执行shell
* @param startTimeStr 开始时间
* @param endTimeStr 结束时间
* @param path shell路径
* @param logPath 日志文件路径
* @return
*/
private List<String> getCommandList(String startTimeStr, String endTimeStr, String path,String logPath){
List<String> commandStrList = new ArrayList<>();
try {
// /usr/test/test.sh "09/Jul/2021:03:33:00" "09/Jul/2021:03:40:00' /opt/file/access-2021-10-13-11.log access.log
Date date = DateUtils.getDateByString(startTimeStr, DateUtils.DATE_YYYYMMDDHHMMSS);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String startTime = formatter.format(date);
Date date2 = DateUtils.getDateByString(endTimeStr, DateUtils.DATE_YYYYMMDDHHMMSS);
String endTime = formatter.format(date2);
// String timePath = "/app/XCK/getEnvoy.sh 2022-10-28T20:55:00.419 2022-10-28T20:59:48.419Z ";
String timePath = path+" "+startTime+" "+endTime+" ";
String command = timePath + logPath ;
commandStrList.add(command);
}catch (Exception e) {
e.printStackTrace();
}
return commandStrList;
}
}
package com.pmc.loganalysis.task;
import com.pmc.loganalysis.service.SmsPushsysService;
import com.pmc.loganalysis.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
/**
* 定时任务
* @description
*/
@EnableAsync
@Component
@EnableScheduling
@Slf4j
public class FiveIndexMonitorTask {
@Value("${before-minute}")
Integer beforeminutes;
@Resource
SmsPushsysService smsPushsysService;
/**
* 定时统计当前时间之前一分钟内的数据统计
*/
@Scheduled(cron = "${springTiming.monitor1-min-time}")
public void StatisticsTravelCardDaily() {
//获取现在时间(开始时间)
Date nowDate = new Date ();
//获取当前时间的前一分钟
Date startDate = DateUtils.getDate(nowDate,beforeminutes);
String startTime = DateUtils.getDateStringYMDHMS(startDate,DateUtils.DATE_TIME_PATTERN);
String startTimestr = DateUtils.getDateStringYMDHMS(startDate,DateUtils.DATE_YYYYMMDDHHMMSS);
Date endDate = DateUtils.getDate(nowDate,-1);
String endTime = DateUtils.getDateStringYMDHMS(endDate,DateUtils.DATE_TIME_PATTERNS);
String endTimestr = DateUtils.getDateStringYMDHMS(endDate,DateUtils.DATE_YYYYMMDDHHMMSS);
smsPushsysService.pushTravelCardByMinRate(startTime,endTime,startTimestr,endTimestr);
}
}
package com.pmc.loganalysis.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author
*/
public class DateUtils {
private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
public static String dateToStr(Date d, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(d);
}
/**
* 时间格式(yyyy-MM-dd)
*/
public final static String DATE_PATTERN = "yyyy-MM-dd";
/**
* 时间格式(yyyy/MM/dd)
*/
public final static String DATE_XIE = "yyyy/MM/dd";
public final static String DATE_YYYYMMDD = "yyyyMMdd";
public final static String DATE_YYYYMMDDHHMM = "yyyyMMddHHmm";
public final static String DATE_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public final static String DATE_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
/**
* 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public final static String DATE_TIME_PATTERNS = "yyyy-MM-dd HH:mm";
/**
* 时间格式(yyyy-MM-dd HH:mm:ss.SSS)
*/
public final static String DATE_TIME_PATTERN_YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss.SSS";
public static String format(Date date) {
return format(date, DATE_PATTERN);
}
public static String format(Date date, String pattern) {
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}
public static String format2() {
return DateUtils.format(DateUtils.getNow(), DateUtils.DATE_TIME_PATTERN);
}
public static String format2(Date date) {
return DateUtils.format(date, DateUtils.DATE_TIME_PATTERN);
}
public static String format3(long time) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN_YYYYMMDDHHMMSSSSS);
return sdf.format(time);
}
/**
* 计算距离现在多久,非精确
*
* @param date
* @return
*/
public static String getTimeBefore(Date date) {
Date now = new Date();
long l = now.getTime() - date.getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
String r = "";
if (day > 0) {
r += day + "天";
} else if (hour > 0) {
r += hour + "小时";
} else if (min > 0) {
r += min + "分";
} else if (s > 0) {
r += s + "秒";
}
r += "前";
return r;
}
/**
* 计算距离现在多久,精确
*
* @param date
* @return
*/
public static String getTimeBeforeAccurate(Date date) {
Date now = new Date();
long l = now.getTime() - date.getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
String r = "";
if (day > 0) {
r += day + "天";
}
if (hour > 0) {
r += hour + "小时";
}
if (min > 0) {
r += min + "分";
}
if (s > 0) {
r += s + "秒";
}
r += "前";
return r;
}
/**
* 获得当前日期
*
* @return
*/
public static Date getNow() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
return currDate;
}
// 字符串转化成日期
public static Date parse(String dateStr, String pattren) {
try {
return new SimpleDateFormat(pattren).parse(dateStr);
} catch (ParseException e) {
logger.error("字符串转化成日期失败", e);
}
return null;
}
/**
* date2比date1多的天数
*
* @param date1
* @param date2
* @return
*/
public static int differentDays(Date date1, Date date2) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime(date1);
int day1 = calendar.get(Calendar.DAY_OF_YEAR);
int year1 = calendar.get(Calendar.YEAR);
calendar.setTime(date2);
int day2 = calendar.get(Calendar.DAY_OF_YEAR);
int year2 = calendar.get(Calendar.YEAR);
//同一年
if (year1 == year2) {
return day2 - day1;
} else if (year1 < year2) {
//Date1<Date2
int days = 0;
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
//闰年
days += 366;
} else {
days += 365;
}
}
return days + (day2 - day1);
} else {//Date1>Date2
int days = 0;
for (int i = year2; i < year1; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
days += 366;
} else {
days += 365;
}
}
return 0 - days + (day2 - day1);
}
}
public static int compareDate(Date dt1, Date dt2) {
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
}
/**
* 获取时间
*
* @param pattern 格式化后的样式
* @param number 分钟数 -5 五分钟前 0 现在 5 五分钟后
* @return
*/
public static String getDate(String pattern, int number) {
Calendar now = Calendar.getInstance();
now.add(Calendar.MINUTE, number);
Date time = now.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(time);
}
/**
* 获取时间
*
* @param number 分钟数 -5 五分钟前 0 现在 5 五分钟后
* @return
*/
public static Date getDate(Date now, int number) {
//一秒是1000
//一分钟是60000
Date dateTime = new Date(now.getTime() + number * 60000);
return dateTime;
}
/**
* 获取指定时间的年月日时分秒字符串
* @param date 日期
* @param format 格式
* @return 结果
*/
public static String getDateStringYMDHMS(Date date, String format) {
String sdate = "";
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdate = sdf.format(date);
return sdate;
}
/**
* 转换时间字符串为Date,自定义格式
* @param sdate
* @param format
* @return
*/
public static Date getDateByString(String sdate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(sdate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 将日期转换成时间戳
* @param sdate
* @param
* @return
*/
public static long getcurrentTimeMillisByDate(String sdate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
long date = 0;
try {
date = sdf.parse(sdate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String getDateBycurrentTimeMillis(long sdate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String date = sdf.format(sdate);
return date;
}
}
package com.pmc.loganalysis.util;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
// Http请求的工具类
@Slf4j
public class HttpUtils {
private static RequestConfig requestConfig = null;
static {
// 设置请求和传输超时时间
requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
}
/**
* post请求传输json参数
*
* @param url url地址
* @param
* @return
*/
public static Map<String,Object> httpPost(String url, JSONObject jsonParam) {
Map<String,Object> map=new HashMap();
map.put("repStr","");
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
String str = "";
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
httpPost.setConfig(requestConfig);
CloseableHttpResponse result=null;
try {
if (null != jsonParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), "utf-8");
map.put("repStr",str); // 服务器响应为200,才会存入 str
} catch (Exception e) {
// 请求和返回正常,但是解析数据时有问题
log.error("post请求提交失败:" + url, e);
}
}
String statusCode=result.getStatusLine().getStatusCode()+"";
map.put("statusCode",statusCode);
} catch (Exception e) {
log.error("post请求提交失败:" + url, e);
map.put("statusCode","400");
} finally {
httpPost.releaseConnection();
}
return map;
}
/**
* 发起webservice请求
* @param url
* @param soap
* @param SOAPAction
* @return
*/
public static Map<String,Object> doPostSoap1(String url, String soap, String SOAPAction) {
Map<String,Object> map=new HashMap();
map.put("repStr","");
//请求体
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(60000)
.setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soap,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
log.info("HttpStatus {}",response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
map.put("repStr",retStr);
log.info("doPostSoap1 retStr {}",retStr);
}
}
String statusCode=response.getStatusLine().getStatusCode()+"";
map.put("statusCode",statusCode);
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
map.put("statusCode","400");
e.printStackTrace();
log.error("doPostSoap1 Exception {}",e);
}
return map;
}
/**
* 发起webservice请求
* @param url
* @param soap
* @param SOAPAction
* @return
*/
public static String doPostSoap(String url, String soap, String SOAPAction) {
//请求体
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(60000)
.setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soap,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
log.info("短信网关响应结果response:" + retStr);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return retStr;
}
@Data
public
class ResponseValue {
private int status;
private String value;
private String resultCode;
private String resultDesc;
}
}
package com.pmc.loganalysis.util;
import okhttp3.MediaType;
public enum MediaTypes {
/**
* APPLICATION_JSON
*/
APPLICATION_JSON(MediaType.get("application/json")),
/**
* APPLICATION_JSON_UTF8
*/
APPLICATION_JSON_UTF8(MediaType.get("application/json;charset=UTF-8")),
/**
* APPLICATION_XML
*/
APPLICATION_XML(MediaType.get("application/xml")),
/**
* APPLICATION_XML_UTF8
*/
APPLICATION_XML_UTF8(MediaType.get("application/xml;charset=UTF-8"));
private MediaType mediaType;
MediaTypes(MediaType mediaType) {
this.mediaType = mediaType;
}
public MediaType getMediaType() {
return mediaType;
}
}
\ No newline at end of file
package com.pmc.loganalysis.util;
public interface ResultCallBack {
/**
* 请求失败,默认抛出
*
* @param url 请求url
* @param e 异常
*/
default void onRequestFailure(String url, Exception e) {
// throw Exception("执行请求[" + url + "]失败", e);
// throw new RunTimeException("执行请求[" + url + "]失败");
}
/**
* 响应失败,默认抛出
*
* @param url 请求url
* @param code 响应码
* @param responseBody 响应体
*/
default void onResponseFailure(String url, int code, String responseBody){
// throw new HttpClientException("执行请求[" + url + "]失败");
}
/**
* 响应成功
*
* @param url 请求url
* @param responseBody 响应体
*/
void onSuccess(String url, String responseBody);
}
\ No newline at end of file
package com.pmc.loganalysis.util;
import java.security.MessageDigest;
/**
* The class used to generate check code of cooperator.
*
* @author <a href="mailto:liujie.han@hp.com">JetHan</a>
*/
public class SlpMD5 {
public static String generateCheckCode(String source) {
String result = null;
// Used to convert 16-byte hexadecimal characters.
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(source.getBytes("UTF-8"));
// MD5 calculation is a 128-bit long integer, that is with 16-byte byte.
byte tmp[] = md.digest();
char str[] = new char[16 * 2];
int k = 0;
// Convert each byte to hexadecimal of MD5.
for (int i = 0; i < 16; i++) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
// Convert the result from byte to string.
result = new String(str);
} catch (Exception e) {
}
return result;
}
}
package com.pmc.loganalysis.util.shell;
import ch.ethz.ssh2.Connection;
import com.jcraft.jsch.*;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.Calendar;
@Slf4j
public class ExecShell {
public static void shell(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
log.error("shell 执行失败" + cmd, e);
}
}
/**配置连接
* @param user
* @param passwd
* @param host
* @throws Exception
* @return
*/
public static com.jcraft.jsch.Session connect(String user, String passwd, String host) throws Exception {
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(user, host);
if (session == null) {
throw new Exception("session is null");
}
session.setPassword(passwd);
java.util.Properties config = new java.util.Properties();
//第一次登陆
config.put("StrictHostKeyChecking", "no");
config.put("Ciphers","3des-cbc");
session.setConfig(config);
log.info("登录主机host:{},user:{}",host,user);
try {
session.connect(30000);
} catch (Exception e) {
log.info("连接远程服务器失败 host:{},user:{}",host,user);
throw new Exception("连接远程端口无效或用户名密码错误");
}
return session;
}
private static String DEFAULT_CHAR_SET = "UTF-8";
private static String tipStr = "=======================%s=======================";
private static String splitStr = "=====================================================";
/**
* 执行shell脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行完后返回的结果值
*/
public static String executes( String cmd){
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
Process proc = Runtime.getRuntime().exec(cmd);
InputStream in = proc.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
int exitVal = proc.waitFor();
log.info("执行命令返回结果exitVal:"+exitVal);
}catch (Exception e) {
log.error("【执行命令失败】\n执行的命令如下:\n" + cmd + "\n" + e.getMessage());
e.printStackTrace();
}
return buffer.toString();
}
/**
* 登录主机
* @return
* 登录成功返回true,否则返回false
*/
public static Connection login(String ip, String userName, String password){
boolean isAuthenticated = false;
Connection conn = null;
long startTime = Calendar.getInstance().getTimeInMillis();
try {
conn = new Connection(ip);
// 连接主机
conn.connect();
// 认证
isAuthenticated = conn.authenticateWithPassword(userName, password);
if(isAuthenticated){
System.out.println(String.format(tipStr, "认证成功"));
} else {
System.out.println(String.format(tipStr, "认证失败"));
}
} catch (IOException e) {
System.err.println(String.format(tipStr, "登录失败"));
e.printStackTrace();
}
long endTime = Calendar.getInstance().getTimeInMillis();
System.out.println("登录用时: " + (endTime - startTime)/1000.0 + "s\n" + splitStr);
return conn;
}
/**
* 远程执行shell脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行完后返回的结果值
*/
public static String execute(Connection conn, String cmd){
String result = "";
Session session = null;
try {
if(conn != null){
// 打开一个会话
session = conn.openSession();
// 执行命令
session.execCommand(cmd);
result = processStdout(session.getStdout(), DEFAULT_CHAR_SET);
//如果为得到标准输出为空,说明脚本执行出错了
if(StringUtils.isBlank(result)){
System.err.println("【得到标准输出为空】\n执行的命令如下:\n" + cmd);
result = processStdout(session.getStderr(), DEFAULT_CHAR_SET);
}else{
System.out.println("【执行命令成功】\n执行的命令如下:\n" + cmd);
}
}
} catch (IOException e) {
System.err.println("【执行命令失败】\n执行的命令如下:\n" + cmd + "\n" + e.getMessage());
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
if (session != null) {
session.close();
}
}
return result;
}
/**
* 解析脚本执行返回的结果集
* @param in 输入流对象
* @param charset 编码
* @return
* 以纯文本的格式返回
*/
private static String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
String line = null;
while((line = br.readLine()) != null){
buffer.append(line + "\n");
}
} catch (UnsupportedEncodingException e) {
System.err.println("解析脚本出错:" + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("解析脚本出错:" + e.getMessage());
e.printStackTrace();
}
return buffer.toString();
}
}
server:
port: 8080
spring:
primary: master
datasource:
type: com.alibaba.druid.pool.DruidDataSource
master:
driverClassName: com.kingbase8.Driver
url: jdbc:kingbase8://192.168.1.107:54329/public
username: system
password: 123456
initialSize: 10
maxActive: 100
minIdle: 10
maxWait: 60002
druid:
test-while-idle: false
validation-query: select 1
shell:
#接收外围请求总量 接收外围请求失败总量_系统原因 接收外围请求响应平均时长 脚本路径
shell-path1: d:\app\XCK\getEnvoy.sh
# 当日日志文件路径
log-path1: d:\app\XCK\access.log
#定时推送频率,每1分钟
springTiming:
monitor1-min-time: "0 0/1 * * * ?"
before-minute: -2
jasypt:
encryptor:
password: erp
# 日志动态配置部分
logging:
level:
# 设定日志的级别
root: info
server:
port: 8080
spring:
primary: master
datasource:
type: com.alibaba.druid.pool.DruidDataSource
master:
driverClassName: com.kingbase8.Driver
url: jdbc:kingbase8://192.168.1.107:54329/public
username: system
password: 123456
initialSize: 10
maxActive: 100
minIdle: 10
maxWait: 60002
druid:
test-while-idle: false
validation-query: select 1
shell:
#接收外围请求总量 接收外围请求失败总量_系统原因 接收外围请求响应平均时长 脚本路径
shell-path1: /app/XCK/getEnvoy.sh
# 当日日志文件路径
log-path1: /app/XCK/access.log
#定时推送频率,每1分钟
springTiming:
monitor1-min-time: "0 0/1 * * * ?"
before-minutes: -2
# 日志动态配置部分
logging:
level:
# 设定日志的级别
root: info
spring:
profiles:
active: prod
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<springProperty scope="context" name="instanceIp" source="spring.cloud.client.ip-address" />
<springProperty scope="context" name="LOG_HOME" source="log.path"></springProperty>
<property name="FILE_NAME" value="loganalysis" />
<!-- 输出到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 输出的格式 -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%date %highlight([%-5.5level]) [%-10.10thread] [%X{user_mobile}] [%X{X-B3-TraceId}/%X{X-B3-SpanId}] %cyan([%-50.50class:%-4.4line]) - %msg%xEx%n </pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<!-- 输出到文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 配置滚动的策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志名称的格式 -->
<FileNamePattern>${LOG_HOME}/${FILE_NAME}-%d{yyyy-MM-dd}.log</FileNamePattern>
<!-- 保存的最长时间:天数 -->
<!--<MaxHistory>30</MaxHistory>-->
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%date %highlight([%-5.5level]) [%-10.10thread] [%X{user_mobile}] [%X{X-B3-TraceId}/%X{X-B3-SpanId}] %cyan([%-50.50class:%-4.4line]) - %msg%xEx%n </pattern>
<charset>utf-8</charset>
</encoder>
<!--<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>-->
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<logger name="com.pmc.loganalysis" additivity="false" level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<!-- 输出到文件 -->
<appender name="interfaceLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 配置滚动的策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志名称的格式 -->
<FileNamePattern>${LOG_HOME}/interfaceLog/interfaceLog-%d{yyyy-MM-dd}.log</FileNamePattern>
<!-- 保存的最长时间:天数 -->
<!--<MaxHistory>30</MaxHistory>-->
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[${instanceIp}]%date %highlight([%-5.5level]) [%-10.10thread] [%X{user_mobile}] [%X{X-B3-TraceId}/%X{X-B3-SpanId}] %cyan([%-50.50class:%-4.4line]) - %msg%xEx%n </pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<logger name="interfaceLogger" level="info" additivity="false">
<!--指定自定义的appender来处理-->
<appender-ref ref="interfaceLog"/>
</logger>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<jmxConfigurator />
</configuration>
package com.pmc.loganalysis;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LoganalysisApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment