Search in sources :

Example 1 with WallProviderStatValue

use of com.alibaba.druid.wall.WallProviderStatValue in project druid by alibaba.

the class MonitorClient method collectSql.

@SuppressWarnings("resource")
public void collectSql() {
    if ((!collectSqlEnable) && !collectSqlWallEnable) {
        return;
    }
    Set<Object> dataSources = DruidDataSourceStatManager.getInstances().keySet();
    List<DruidDataSourceStatValue> statValueList = new ArrayList<DruidDataSourceStatValue>(dataSources.size());
    List<WallProviderStatValue> wallStatValueList = new ArrayList<WallProviderStatValue>();
    for (Object item : dataSources) {
        if (!(item instanceof DruidDataSource)) {
            continue;
        }
        DruidDataSource dataSource = (DruidDataSource) item;
        if (collectSqlEnable) {
            DruidDataSourceStatValue statValue = dataSource.getStatValueAndReset();
            statValueList.add(statValue);
        }
        if (collectSqlWallEnable) {
            WallProviderStatValue wallStatValue = dataSource.getWallStatValue(true);
            if (wallStatValue != null && wallStatValue.getCheckCount() > 0) {
                wallStatValueList.add(wallStatValue);
            }
        }
    }
    MonitorContext ctx = createContext();
    if (statValueList.size() > 0) {
        dao.saveSql(ctx, statValueList);
    }
    if (wallStatValueList.size() > 0) {
        dao.saveSqlWall(ctx, wallStatValueList);
    }
}
Also used : DruidDataSourceStatValue(com.alibaba.druid.pool.DruidDataSourceStatValue) WallProviderStatValue(com.alibaba.druid.wall.WallProviderStatValue) ArrayList(java.util.ArrayList) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 2 with WallProviderStatValue

use of com.alibaba.druid.wall.WallProviderStatValue in project druid by alibaba.

the class MonitorDaoJdbcImpl method saveSqlWall.

@Override
public void saveSqlWall(MonitorContext ctx, List<WallProviderStatValue> statList) {
    save(wallProviderStatBeanInfo, ctx, statList);
    for (WallProviderStatValue providerStat : statList) {
        save(wallSqlStatBeanInfo, ctx, providerStat.getWhiteList());
        save(wallSqlStatBeanInfo, ctx, providerStat.getBlackList());
        save(wallTableStatBeanInfo, ctx, providerStat.getTables());
        save(wallFunctionStatBeanInfo, ctx, providerStat.getFunctions());
    }
}
Also used : WallProviderStatValue(com.alibaba.druid.wall.WallProviderStatValue)

Example 3 with WallProviderStatValue

use of com.alibaba.druid.wall.WallProviderStatValue in project druid by alibaba.

the class WallProviderTest method test_getSqlStat.

public void test_getSqlStat() throws Exception {
    String whiteSql_1 = "select * from t1 where fid = 1";
    String whiteSql_2 = "select * from t1 where fid = 2";
    String blackSql_1 = "select * from t2 where len(fid) = 1 OR 1 = 1";
    String blackSql_2 = "select * from t2 where len(fid) = 2 OR 1 = 1";
    MySqlWallProvider provider = new MySqlWallProvider();
    Assert.assertTrue(provider.checkValid(whiteSql_1));
    Assert.assertTrue(provider.checkValid(whiteSql_2));
    Assert.assertEquals(1, provider.getSqlList().size());
    Assert.assertFalse(provider.checkValid(blackSql_1));
    Assert.assertFalse(provider.checkValid(blackSql_2));
    Assert.assertEquals(2, provider.getSqlList().size());
    Assert.assertNotNull(provider.getSqlStat(whiteSql_1));
    Assert.assertNotNull(provider.getSqlStat(blackSql_1));
    Assert.assertSame(provider.getSqlStat(blackSql_1), provider.getSqlStat(blackSql_2));
    WallSqlStat whiteStat_1 = provider.getSqlStat(whiteSql_1);
    WallSqlStat whiteStat_2 = provider.getSqlStat(whiteSql_2);
    Assert.assertSame(whiteStat_1, whiteStat_2);
    provider.addFetchRowCount(whiteStat_1, 3);
    WallTableStat wallTableStat = provider.getTableStat("t1");
    Assert.assertNotNull(wallTableStat);
    Assert.assertEquals(3, wallTableStat.getFetchRowCount());
    Assert.assertTrue(provider.checkValid(whiteSql_1));
    Assert.assertTrue(provider.checkValid(whiteSql_2));
    Assert.assertFalse(provider.checkValid(blackSql_1));
    Assert.assertFalse(provider.checkValid(blackSql_2));
    for (int i = 1000; i < 1000 * 2; ++i) {
        String sql_x = "select * from t1 where fid = " + i;
        Assert.assertTrue(provider.checkValid(sql_x));
        Assert.assertSame(whiteStat_1, provider.getSqlStat(sql_x));
    }
    Assert.assertEquals(1, provider.getWhiteList().size());
    Assert.assertEquals(2, provider.getSqlList().size());
    Assert.assertFalse(provider.checkValid("slelc"));
    {
        WallProviderStatValue statValue = provider.getStatValue(true);
        Assert.assertNotNull(statValue);
        Assert.assertEquals(2, statValue.getTables().size());
        Assert.assertEquals(1, statValue.getFunctions().size());
        Assert.assertEquals(1009, statValue.getCheckCount());
        Assert.assertEquals(2, statValue.getBlackListHitCount());
        Assert.assertEquals(4, statValue.getHardCheckCount());
        Assert.assertEquals(1, statValue.getSyntaxErrorCount());
        Assert.assertEquals(5, statValue.getViolationCount());
        Assert.assertEquals(1003, statValue.getWhiteListHitCount());
        Assert.assertEquals(1, statValue.getWhiteList().size());
        Assert.assertEquals(2, statValue.getBlackList().size());
    }
    {
        WallProviderStatValue statValue = provider.getStatValue(true);
        Assert.assertNotNull(statValue);
        Assert.assertEquals(0, statValue.getTables().size());
        Assert.assertEquals(0, statValue.getFunctions().size());
        Assert.assertEquals(0, statValue.getCheckCount());
        Assert.assertEquals(0, statValue.getBlackListHitCount());
        Assert.assertEquals(0, statValue.getHardCheckCount());
        Assert.assertEquals(0, statValue.getSyntaxErrorCount());
        Assert.assertEquals(0, statValue.getViolationCount());
        Assert.assertEquals(0, statValue.getWhiteListHitCount());
        Assert.assertEquals(0, statValue.getWhiteList().size());
        Assert.assertEquals(0, statValue.getBlackList().size());
    }
}
Also used : WallTableStat(com.alibaba.druid.wall.WallTableStat) MySqlWallProvider(com.alibaba.druid.wall.spi.MySqlWallProvider) WallSqlStat(com.alibaba.druid.wall.WallSqlStat) WallProviderStatValue(com.alibaba.druid.wall.WallProviderStatValue)

Aggregations

WallProviderStatValue (com.alibaba.druid.wall.WallProviderStatValue)3 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 DruidDataSourceStatValue (com.alibaba.druid.pool.DruidDataSourceStatValue)1 WallSqlStat (com.alibaba.druid.wall.WallSqlStat)1 WallTableStat (com.alibaba.druid.wall.WallTableStat)1 MySqlWallProvider (com.alibaba.druid.wall.spi.MySqlWallProvider)1 ArrayList (java.util.ArrayList)1