Skip to content

Commit

Permalink
InformixDB: Onstat support for couple of item and Minor refactoring (#31
Browse files Browse the repository at this point in the history
)

* InformixDB: Onstat support for couple of item and Minor refactoring

* InformixDB: Minor refactoring
  • Loading branch information
debnathbikram authored May 14, 2024
1 parent 9a0aa6b commit ab6c126
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 59 deletions.
12 changes: 7 additions & 5 deletions rdb/config/config-informix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ instances:
#Data collector properties:
#Either use `poll.interval` or `custom.poll.interval`
poll.interval: 30
#All three category value is Mandatory if leveraging Custom Poll Rate
custom.poll.interval:
high: 10 #polling_value_in_sec
medium: 120 #polling_value_in_sec
low: 300 #polling_value_in_sec
high: 300 #polling_value_in_sec
medium: 45 #polling_value_in_sec
low: 20 #polling_value_in_sec
callback.interval: 20
otel.backend.url: http://127.0.0.1:4317

#Custom Input
custom.input:
db.names: 'testDb1, testDb2, testDb3' # Provide the DB Names in Comma separated way

#Time Frame for which you want to get the expensive query. Default value 900 Sec
db.sql.elapsed.timeframe: 900 # Values should be in Seconds
13 changes: 13 additions & 0 deletions rdb/scripts/informix/active_session_count.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
# (c) Copyright IBM Corp. 2024
# (c) Copyright Instana Inc.

result=$(cd $1 &&
export INFORMIXDIR=$2 &&
export ONCONFIG=$3 &&
export INFO RMIXSERVER=$4 &&
export PATH=$INFORMIXDIR/bin:$PATH &&
export INFORMIXSQLHOSTS=$INFORMIXDIR/etc/$5 &&
./onstat -g ses active | awk 'BEGIN {RS=""; FS="\n"} NR==2 {linecount = NF -2; if (linecount>0) print linecount; else print 0}')

echo $result
2 changes: 1 addition & 1 deletion rdb/scripts/informix/session_count.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ result=$(cd $1 &&
export INFO RMIXSERVER=$4 &&
export PATH=$INFORMIXDIR/bin:$PATH &&
export INFORMIXSQLHOSTS=$INFORMIXDIR/etc/$5 &&
./onstat -g ses | tail -n +7 | head -n -1 | wc -l)
./onstat -g ses | awk 'BEGIN {RS=""; FS="\n"} NR==2 {linecount = NF -2; if (linecount>0) print linecount; else print 0}')

echo $result
13 changes: 13 additions & 0 deletions rdb/scripts/informix/task_wait_count.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
# (c) Copyright IBM Corp. 2024
# (c) Copyright Instana Inc.

result=$(cd $1 &&
export INFORMIXDIR=$2 &&
export ONCONFIG=$3 &&
export INFO RMIXSERVER=$4 &&
export PATH=$INFORMIXDIR/bin:$PATH &&
export INFORMIXSQLHOSTS=$INFORMIXDIR/etc/$5 &&
./onstat -g wai | awk 'BEGIN {RS=""; FS="\n"} NR==2 {linecount = NF -2; if (linecount>0) print linecount; else print 0}')

echo $result
3 changes: 3 additions & 0 deletions rdb/src/main/java/com/instana/dc/rdb/impl/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class Constants {
public static final String MEMORY_UTILIZATION_SCRIPT = "memory_utilization.sh";
public static final String DISK_READ_SCRIPT = "disk_read.sh";
public static final String DISK_WRITE_SCRIPT = "disk_write.sh";
public static final String TASK_WAIT_COUNT_SCRIPT = "task_wait_count.sh";
public static final String TOTAL_SESSION_COUNT_SCRIPT = "session_count.sh";
public static final String ACTIVE_SESSION_COUNT_SCRIPT = "active_session_count.sh";

private Constants() {
//Private constructor
Expand Down
64 changes: 35 additions & 29 deletions rdb/src/main/java/com/instana/dc/rdb/impl/informix/InformixDc.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
Expand All @@ -27,34 +28,37 @@
import java.util.logging.Logger;

import static com.instana.dc.rdb.DbDcUtil.*;
import static com.instana.dc.rdb.impl.Constants.ACTIVE_SESSION_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.DISK_READ_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.DISK_WRITE_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.IO_READ_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.IO_WRITE_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.MEMORY_UTILIZATION_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.SQL_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.TASK_WAIT_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.TOTAL_SESSION_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.TRANSACTION_COUNT_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.DISK_READ_SCRIPT;
import static com.instana.dc.rdb.impl.Constants.DISK_WRITE_SCRIPT;
import static com.instana.dc.rdb.impl.informix.InformixUtil.DB_HOST_AND_VERSION_SQL;


public class InformixDc extends AbstractDbDc {
private static final Logger LOGGER = Logger.getLogger(InformixDc.class.getName());
public static final String SESSION_COUNT_SCRIPT = "session_count.sh";
private static final int DEFAULT_ELAPSED_TIME = 900;
private String tableSpaceSizeQuery;
private String tableSpaceUsedQuery;
private String tableSpaceUtilizationQuery;
private String tableSpaceMaxQuery;
private String sqlElapsedTimeQuery;
private boolean customPollRateEnabled = true;
private ScheduledExecutorService executorService;
private final BasicDataSource dataSource;
private final OnstatCommandExecutor onstatCommandExecutor;

private final MetricsCollector metricCollector;

public InformixDc(Map<String, Object> properties, String dbSystem, String dbDriver) throws SQLException {
super(properties, dbSystem, dbDriver);
parseCustomAttributes(properties);
onstatCommandExecutor = new OnstatCommandExecutor(getDbPath(), getServerName());
OnstatCommandExecutor onstatCommandExecutor = new OnstatCommandExecutor(getDbPath(), getServerName());
setDbPassword(InformixUtil.decodePassword(getDbPassword()));
setDbConnUrl();

Expand All @@ -69,15 +73,15 @@ public InformixDc(Map<String, Object> properties, String dbSystem, String dbDriv
}

private BasicDataSource getDataSource() {
final BasicDataSource dataSource;
dataSource = new BasicDataSource();
dataSource.setDriverClassName(getDbDriver());
dataSource.setUsername(getDbUserName());
dataSource.setPassword(getDbPassword());
dataSource.setUrl(getDbConnUrl());
dataSource.setInitialSize(3);
dataSource.setMaxIdle(1);
return dataSource;
final BasicDataSource basicDataSource;
basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(getDbDriver());
basicDataSource.setUsername(getDbUserName());
basicDataSource.setPassword(getDbPassword());
basicDataSource.setUrl(getDbConnUrl());
basicDataSource.setInitialSize(3);
basicDataSource.setMaxIdle(1);
return basicDataSource;
}

/**
Expand All @@ -94,12 +98,11 @@ private void registerMetricsMetadata() {
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_TABLESPACE_MAX_NAME,
new MetricDataConfig(tableSpaceMaxQuery, MetricCollectionMode.SQL, List.class, DB_TABLESPACE_MAX_KEY));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_SQL_ELAPSED_TIME_NAME,
new MetricDataConfig(InformixUtil.SQL_ELAPSED_TIME_SQL, MetricCollectionMode.SQL, List.class, DB_SQL_ELAPSED_TIME_KEY, SemanticAttributes.SQL_TEXT.getKey()));
new MetricDataConfig(sqlElapsedTimeQuery, MetricCollectionMode.SQL, List.class, DB_SQL_ELAPSED_TIME_KEY, SemanticAttributes.SQL_TEXT.getKey()));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_INSTANCE_COUNT_NAME,
new MetricDataConfig(InformixUtil.INSTANCE_COUNT_SQL, MetricCollectionMode.SQL, Number.class));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_INSTANCE_ACTIVE_COUNT_NAME,
new MetricDataConfig(InformixUtil.INSTANCE_ACTIVE_COUNT_SQL, MetricCollectionMode.SQL, Number.class));

MetricsDataConfigRegister.subscribeMetricDataConfig(DB_DATABASE_LOG_ENABLED_NAME,
new MetricDataConfig(InformixUtil.DB_DATABASE_LOG_ENABLED_SQL, MetricCollectionMode.SQL, List.class, DB_DATABASE_LOG_ENABLED_KEY));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_DATABASE_BUFF_LOG_ENABLED_NAME,
Expand All @@ -121,9 +124,9 @@ private void registerMetricsMetadata() {
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_TRANSACTION_RATE_NAME,
new MetricDataConfig(DB_TRANSACTION_RATE_NAME, TRANSACTION_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_SESSION_COUNT_NAME,
new MetricDataConfig(DB_SESSION_COUNT_NAME, SESSION_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
new MetricDataConfig(DB_SESSION_COUNT_NAME, TOTAL_SESSION_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_SESSION_ACTIVE_COUNT_NAME,
new MetricDataConfig(DB_SESSION_ACTIVE_COUNT_NAME, SESSION_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
new MetricDataConfig(DB_SESSION_ACTIVE_COUNT_NAME, ACTIVE_SESSION_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_IO_READ_RATE_NAME,
new MetricDataConfig(DB_IO_READ_RATE_NAME, IO_READ_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_IO_WRITE_RATE_NAME,
Expand All @@ -134,14 +137,16 @@ private void registerMetricsMetadata() {
new MetricDataConfig(DB_DISK_READ_COUNT_NAME, DISK_READ_SCRIPT, MetricCollectionMode.CMD, Number.class));
MetricsDataConfigRegister.subscribeMetricDataConfig(DB_DISK_WRITE_COUNT_NAME,
new MetricDataConfig(DB_DISK_WRITE_COUNT_NAME, DISK_WRITE_SCRIPT, MetricCollectionMode.CMD, Number.class));

MetricsDataConfigRegister.subscribeMetricDataConfig(DB_TASK_WAIT_COUNT_NAME,
new MetricDataConfig(DB_TASK_WAIT_COUNT_NAME, TASK_WAIT_COUNT_SCRIPT, MetricCollectionMode.CMD, Number.class));
}

/**
* Util method to parse the user input
*
* @param properties : user inputs
*/
@SuppressWarnings("unchecked")
private void parseCustomPollRate(Map<String, Object> properties) {
Map<String, Object> customInput = (Map<String, Object>) properties.get("custom.poll.interval");
if (null == customInput || customInput.isEmpty()) {
Expand Down Expand Up @@ -190,17 +195,16 @@ private void scheduleCustomPollRate(int pollInterval, IntervalType intervalType)
*
* @param properties : Config data
*/
@SuppressWarnings("unchecked")
private void parseCustomAttributes(Map<String, Object> properties) {
Map<String, Object> customInput = (Map<String, Object>) properties.get("custom.input");
String[] dbNames = ((String) customInput.get("db.names")).split(",");
StringBuilder sb = new StringBuilder(Constants.SINGLE_QUOTES + dbNames[0] + Constants.SINGLE_QUOTES);
for (int i = 1; i < dbNames.length; i++) {
sb.append(Constants.COMMA).append(Constants.SINGLE_QUOTES).append(dbNames[i].trim()).append(Constants.SINGLE_QUOTES);
}
tableSpaceSizeQuery = String.format(InformixUtil.TABLESPACE_SIZE_SQL, sb);
tableSpaceUsedQuery = String.format(InformixUtil.TABLESPACE_USED_SQL, sb);
tableSpaceUtilizationQuery = String.format(InformixUtil.TABLESPACE_UTILIZATION_SQL, sb);
tableSpaceMaxQuery = String.format(InformixUtil.TABLESPACE_MAX_SQL, sb);
Map<String, Object> customInput = (Map<String, Object>) properties.getOrDefault("custom.input", Collections.emptyMap());
long elapsedTimeFrame = Long.parseLong((customInput.getOrDefault("db.sql.elapsed.timeframe", DEFAULT_ELAPSED_TIME)).toString());
StringBuilder databaseName = new StringBuilder(Constants.SINGLE_QUOTES + getDbName() + Constants.SINGLE_QUOTES);
tableSpaceSizeQuery = String.format(InformixUtil.TABLESPACE_SIZE_SQL, databaseName);
tableSpaceUsedQuery = String.format(InformixUtil.TABLESPACE_USED_SQL, databaseName);
tableSpaceUtilizationQuery = String.format(InformixUtil.TABLESPACE_UTILIZATION_SQL, databaseName);
tableSpaceMaxQuery = String.format(InformixUtil.TABLESPACE_MAX_SQL, databaseName);
sqlElapsedTimeQuery = String.format(InformixUtil.SQL_ELAPSED_TIME_SQL, elapsedTimeFrame, databaseName);
}

private void setDbConnUrl() {
Expand Down Expand Up @@ -248,6 +252,7 @@ private void getallMetrics() {
shortPollingInterval();
}

@SuppressWarnings("unchecked")
private void mediumPollingInterval() {
getRawMetric(DB_SQL_COUNT_NAME).setValue((Number) metricCollector.collectMetrics(DB_SQL_COUNT_NAME));
getRawMetric(DB_SQL_RATE_NAME).setValue((Number) metricCollector.collectMetrics(DB_SQL_RATE_NAME));
Expand All @@ -271,6 +276,7 @@ private void shortPollingInterval() {
getRawMetric(DB_DISK_READ_COUNT_NAME).setValue((Number) metricCollector.collectMetrics(DB_DISK_READ_COUNT_NAME));
}

@SuppressWarnings("unchecked")
private void longPollingInterval() {
getRawMetric(DB_TABLESPACE_SIZE_NAME).setValue((List<SimpleQueryResult>) metricCollector.collectMetrics(DB_TABLESPACE_SIZE_NAME));
getRawMetric(DB_TABLESPACE_USED_NAME).setValue((List<SimpleQueryResult>) metricCollector.collectMetrics(DB_TABLESPACE_USED_NAME));
Expand Down
Loading

0 comments on commit ab6c126

Please sign in to comment.