OraMon.java
5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package se.lil.om;
import java.sql.*;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
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();
int getDataCalls = 0;
int getDataSucess = 0;
public int getDataCalled() {
return getDataCalls;
}
public int getDataSucceeded() {
return getDataSucess;
}
public int getDataFailed() {
return getDataCalls - getDataSucess;
}
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");
}
long lastRTns = 0;
public long getLastRTms() {
return lastRTns / (1000 * 1000);
}
long lastFetchTSns = 0;
public boolean getData(long ageTs) throws Throwable {
if ((System.nanoTime() - lastFetchTSns) > (ageTs * 1000 * 1000 * 1000)) return getData();
else return false;
}
public long getAgeTs() {
return (System.nanoTime() - lastFetchTSns)/(1000*1000*1000);
}
Lock lock = new ReentrantLock();
public boolean getData() throws Throwable {
// Start thread safe
// Try to get a lock
boolean haveLock = lock.tryLock();
if( haveLock == false) {
// We could not get the lock, someone else is updating. Wait for it to complete by waiting for a lock, then unlock and return.
try {
lock.lock();
} finally {
lock.unlock();
}
return false;
}
// We do have the lock. Do the rest in a try catch everything and if we catch anything, re throw the catch but always release the lock.
try {
long startTSns = System.nanoTime();
getDataCalls++;
if(conn == null) open();
if(conn.isClosed()) open();
boolean stale = true;
try {
if (conn.isValid(20)) {
stale = false;
}
} catch (SQLException e) {
stale = true;
}
if(stale) {
open();
}
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();
getDataSucess++;
lastFetchTSns = System.nanoTime();
lastRTns = lastFetchTSns - startTSns;
return true;
} catch (Throwable e) {
throw (e);
} finally {
lock.unlock();
}
// End thread safe
}
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");
Properties jdbcProps = new Properties();
jdbcProps.put("user", conUser);
jdbcProps.put("password", conPass);
jdbcProps.put("v$session.program","LIL Oracle Monitor");
this.conn = DriverManager.getConnection(conString, jdbcProps);
}
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;
Properties jdbcProps = new Properties();
jdbcProps.put("user", conUser);
jdbcProps.put("password", conPass);
jdbcProps.put("v$session.program","LIL Oracle Monitor");
this.conn = DriverManager.getConnection(conString, jdbcProps);
}
public void close() throws Throwable {
if(conn != null) conn.close();
}
}