OraMon.java 3.6 KB
package se.lil.om;

import java.sql.*;

public class OraMon {
	Connection conn = null;
	String conString = "jdbc:oracle:thin:@//hostname:1521/SID";
	String conUser = "system";
	String conPass = "passw0rd";
	String dbName = null;
	
	Collector col = new Collector();
	
	public String getConString() {
		return this.conString;
	}
	
	//NUM_CPUS
	int numCpus = 0;
	public long getNumberOfCPUs() throws Throwable {
		return numCpus;
	}
	
	//DB CPU
	LongDelta cpuTime = new LongDelta();
	public double getCPUPercent() throws Throwable {
		double perSec = cpuTime.getPerSecondValue();
		double totSec = numCpus * 1000;
		double percent = (perSec / totSec) * 100;
		return percent;
	}
	
	public long getCPUTimePerSecond() throws Throwable {
		return cpuTime.getPerSecondValue();
	}

	public long getPerSecondValue(String name) throws Throwable {
		return col.getPerSecValue(name);
	}
	
	public String getDBName() throws Throwable {
		return this.dbName;
	}
	
	public double getBufferCacheHitRatioPercent() throws Throwable {
		double conGetsCache = getPerSecondValue("consistent gets from cache");
		double dbBlocksCache = getPerSecondValue("db block gets from cache");
		double physReadsCache = getPerSecondValue("physical reads cache");
		if (conGetsCache + dbBlocksCache + physReadsCache == 0) {
			return 100;
		}
		if (conGetsCache + dbBlocksCache == 0 && physReadsCache > 0) {
			return 0;
		}
		double buffCacheHitRatio = 1 - (physReadsCache/(conGetsCache + dbBlocksCache));
		return buffCacheHitRatio * 100;
	}
	
	public double getCacheHitRatioPercent() throws Throwable {
		double conGets = getPerSecondValue("consistent gets");
		double dbBlocks = getPerSecondValue("db block gets");
		double physReads = getPerSecondValue("physical reads");
		if (conGets + dbBlocks + physReads == 0) {
			return 100;
		}
		if (conGets + dbBlocks == 0 && physReads > 0) {
			return 0;
		}
		double cacheHitRatio = 1 - (physReads/(conGets + dbBlocks));
		return cacheHitRatio * 100;
	}
	
	public long getLogicalReadsPerSecond() throws Throwable {
		return getPerSecondValue("consistent gets") + getPerSecondValue("db block gets");
	}
	
 	public void getData() throws Throwable {
		Statement stmt = conn.createStatement();
		ResultSet rset = null;
		
		//Get the database name once
		if(this.dbName == null) {
			rset = stmt.executeQuery("select value from V$SYSTEM_PARAMETER where name = 'db_name'");
			rset.next();
			this.dbName = rset.getString(1);
			rset.close(); 
		}
		
		//Get values for CPU calculation
		rset = stmt.executeQuery("select value from V$OSSTAT where STAT_NAME = 'NUM_CPUS'");
		rset.next();
		this.numCpus = rset.getInt(1);
		rset.close(); 
		
		cpuTime.update(stmt.executeQuery("select systimestamp, value from V$SYS_TIME_MODEL where stat_name='DB CPU'"), true);

		// Get the entire V_$SYSSTAT table from DB
		col.update(stmt.executeQuery("select systimestamp, name, value from V$SYSSTAT"));
		
		stmt.close(); 
	}
	
	public OraMon() {
		// TODO Auto-generated constructor stub
	}
	
	public OraMon(String con, String user, String pass) {
		this.conString = con;
		this.conUser = user;
		this.conPass = pass;
	}
	
	public void open() throws Throwable {
		Class.forName ("oracle.jdbc.OracleDriver");
		this.conn = DriverManager.getConnection(conString, conUser, conPass);
	}
	
	public void open(String con, String user, String pass) throws Throwable {
		Class.forName ("oracle.jdbc.OracleDriver");
		if(con != null) this.conString = con;
		if(user != null) this.conUser = user;
		if(pass != null) this.conPass = pass;
		this.conn = DriverManager.getConnection(conString, conUser, conPass);
	}
	
	public void close() throws Throwable {
		if(conn != null) conn.close();
	}
}