com.alibaba.druid.support.monitor.MonitorClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of druid Show documentation
Show all versions of druid Show documentation
An JDBC datasource implementation.
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.support.monitor;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceStatValue;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.stat.JdbcSqlStatValue;
import com.alibaba.druid.support.http.stat.WebAppStat;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
import com.alibaba.druid.support.http.stat.WebAppStatValue;
import com.alibaba.druid.support.http.stat.WebURIStatValue;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.support.monitor.dao.MonitorDao;
import com.alibaba.druid.support.spring.stat.SpringMethodStatValue;
import com.alibaba.druid.support.spring.stat.SpringStat;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.druid.util.Utils;
import com.alibaba.druid.wall.WallProviderStatValue;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.alibaba.druid.util.Utils.getBoolean;
import static com.alibaba.druid.util.Utils.getInteger;
public class MonitorClient {
private final static Log LOG = LogFactory.getLog(MonitorClient.class);
private final static long DEFAULT_TIME_BETWEEN_COLLECT = 60 * 5;
private ScheduledExecutorService scheduler;
private int schedulerThreadSize = 1;
private long timeBetweenSqlCollect = DEFAULT_TIME_BETWEEN_COLLECT;
private long timeBetweenSpringCollect = DEFAULT_TIME_BETWEEN_COLLECT;
private long timeBetweenWebUriCollect = DEFAULT_TIME_BETWEEN_COLLECT;
private TimeUnit timeUnit = TimeUnit.SECONDS;
private boolean collectSqlEnable = true;
private boolean collectSqlWallEnable = true;
private boolean collectSpringMethodEnable = true;
private boolean collectWebAppEnable = true;
private boolean collectWebURIEnable = true;
private MonitorDao dao;
private String domain;
private String app;
private String cluster;
private String host;
private String ip;
private int pid;
public MonitorClient(){
String name = ManagementFactory.getRuntimeMXBean().getName();
String[] items = name.split("@");
pid = Integer.parseInt(items[0]);
host = items[1];
ip = getLocalIPAddress().getHostAddress();
configFromProperty(System.getProperties());
}
public void configFromProperty(Properties properties) {
{
Integer value = getInteger(properties, "druid.monitor.client.schedulerThreadSize");
if (value != null) {
this.setSchedulerThreadSize(value);
}
}
{
Integer value = getInteger(properties, "druid.monitor.client.timeBetweenSqlCollect");
if (value != null) {
this.setTimeBetweenSqlCollect(value);
}
}
{
Integer value = getInteger(properties, "druid.monitor.client.timeBetweenSpringCollect");
if (value != null) {
this.setTimeBetweenSpringCollect(value);
}
}
{
Integer value = getInteger(properties, "druid.monitor.client.timeBetweenWebUriCollect");
if (value != null) {
this.setTimeBetweenWebUriCollect(value);
}
}
{
Boolean value = getBoolean(properties, "druid.monitor.client.collectSqlEnable");
if (value != null) {
this.setCollectSqlEnable(value);
}
}
{
Boolean value = getBoolean(properties, "druid.monitor.client.collectSqlWallEnable");
if (value != null) {
this.setCollectSqlWallEnable(value);
}
}
{
Boolean value = getBoolean(properties, "druid.monitor.client.collectSpringMethodEnable");
if (value != null) {
this.setCollectSpringMethodEnable(value);
}
}
{
Boolean value = getBoolean(properties, "druid.monitor.client.collectWebAppEnable");
if (value != null) {
this.setCollectWebAppEnable(value);
}
}
{
Boolean value = getBoolean(properties, "druid.monitor.client.collectWebURIEnable");
if (value != null) {
this.setCollectWebURIEnable(value);
}
}
{
domain = properties.getProperty("druid.monitor.domain");
if (StringUtils.isEmpty(domain)) {
domain = "default";
}
}
{
app = properties.getProperty("druid.monitor.app");
if (StringUtils.isEmpty(app)) {
app = "default";
}
}
{
cluster = properties.getProperty("druid.monitor.cluster");
if (StringUtils.isEmpty(cluster)) {
cluster = "default";
}
}
}
public void stop() {
}
public void start() {
checkInst();
if (scheduler == null) {
scheduler = new ScheduledThreadPoolExecutor(schedulerThreadSize);
}
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
collectSql();
}
}, timeBetweenSqlCollect, timeBetweenSqlCollect, timeUnit);
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
collectSpringMethod();
}
}, timeBetweenSpringCollect, timeBetweenSpringCollect, timeUnit);
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
collectWebURI();
}
}, timeBetweenWebUriCollect, timeBetweenWebUriCollect, timeUnit);
}
public ScheduledExecutorService getScheduler() {
return scheduler;
}
public void setScheduler(ScheduledExecutorService scheduler) {
this.scheduler = scheduler;
}
public void checkInst() {
try {
dao.insertAppIfNotExits(domain, app);
dao.insertClusterIfNotExits(domain, app, cluster);
dao.insertOrUpdateInstance(domain, app, cluster, host, ip, Utils.getStartTime(), pid);
} catch (Exception ex) {
LOG.error("checkInst error", ex);
}
}
@SuppressWarnings("resource")
public void collectSql() {
if ((!collectSqlEnable) && !collectSqlWallEnable) {
return;
}
Set