package com.skywin.appmonitor.monitor.oracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.skywin.appmonitor.db.dao.IAlarmDao;
import com.skywin.appmonitor.db.dao.IOracleDbPerfDao;
import com.skywin.appmonitor.db.vo.Alarm;
import com.skywin.appmonitor.db.vo.MonitorLimit;
import com.skywin.appmonitor.db.vo.MonitorObject;
import com.skywin.appmonitor.db.vo.OracleDbPerf;
import com.skywin.appmonitor.monitor.DataProcess;
import com.skywin.appmonitor.monitor.Monitor;
/**
* Oracle數據庫監控
* @author joe
*
*/
public class MonitorOracle implements Monitor,DataProcess {
// -- 協作者 --
private IOracleDbPerfDao oracleDbPerfDao = null;
private IAlarmDao alarmDao;
public void setOracleDbPerfDao(IOracleDbPerfDao oracleDbPerfDao) {
this.oracleDbPerfDao = oracleDbPerfDao;
}
public void setAlarmDao(IAlarmDao alarmDao) {
this.alarmDao = alarmDao;
}
// -- 協作者 --
static OracleDbPerf oracledb = null;
String appName = "Oracle監控";
/**
* 缺省的構造函數
*/
public MonitorOracle(){}
public Object monitor(MonitorObject moniObj) {
oracledb = new OracleDbPerf(); //初始化
oracledb.setMonitorObjId(moniObj.getMonitorId());
String ConStr = moniObj.getMonitorConstr();
String tmpS = "";
String aryS[] = null;
String JDriver = "oracle.jdbc.driver.OracleDriver";
String conURL = "jdbc:oracle:thin:@" + getLinkmsg(ConStr, "ip") + ":"
+ getLinkmsg(ConStr, "port") + ":"
+ getLinkmsg(ConStr, "dbname");
System.out.println(conURL);
try {
Class.forName(JDriver);
} catch (java.lang.ClassNotFoundException e) {
System.out.println("ForName :" + e.getMessage());
}
try {
Connection con = DriverManager.getConnection(conURL, getLinkmsg(
ConStr, "user"), getLinkmsg(ConStr, "password"));
Statement sta = con.createStatement();
ResultSet rs = null;
ResultSetMetaData rsmd = null;
// crteate_time 數據產生時間
java.util.Date cur_date = new java.util.Date();
oracledb.setCreateTime(cur_date);
// 等待時間
// 這個操作已經測試過
/*
* WAIT_TIME ----- ------- 0.04 0.04
*/
sta
.execute("select ROUND((AVERAGE/100),2)as wait_time from SYS.V_$SYSMETRIC_SUMMARY where METRIC_NAME in ('Response Time Per Txn')");
System.out.println("測試等待時間成功");
rs = sta.getResultSet();
while (rs.next()) {
oracledb.setWaitTime(rs.getFloat("wait_time"));
}
// 表空間
String sqlStr = "";
sqlStr = " select t.tablespace_name tb_name, d.tot_size/1024/1024 tot,"
+ "(d.tot_size - f.free_size)/1024/1024 used,"
+ " free_size/1024/1024 free,"
+ " f.max_free_extent/1024/1024 max_free_extent,"
+ " n.max_next_extent/1024/1024 max_next_extent,"
+ " round(free_size/tot_size * 100,0) pct"
+ " from dba_tablespaces t,"
+ " (select tablespace_name, sum(bytes) tot_size from dba_data_files"
+ " where status = 'AVAILABLE'"
+ " group by tablespace_name) d,"
+ " (select tablespace_name, sum(bytes) free_size, max(bytes) max_free_extent"
+ " from dba_free_space"
+ " group by tablespace_name ) f,"
+ " (select tablespace_name, max(next_extent) max_next_extent "
+ " from dba_segments"
+ " group by tablespace_name ) n"
+ " where t.tablespace_name = d.tablespace_name"
+ " and d.tablespace_name = f.tablespace_name"
+ " and f.tablespace_name = n.tablespace_name"
+ " and status = 'ONLINE'" + " order by 7,1";
sta.execute(sqlStr);
System.out.println("測試表空間成功");
rs = sta.getResultSet();
int totalsize = 0;
int freesize = 0;
while (rs.next()) {
totalsize = totalsize + rs.getInt("TOT");
freesize = freesize + rs.getInt("FREE");
}
oracledb.setDbSize(totalsize);
oracledb.setDbFreeSize(freesize);
oracledb.setDbSizeRat(1.0f * freesize / totalsize);
// 當前連接數
sqlStr = "select count(*)as connects from v$session where status='INACTIVE' and username is not null";
sta.execute(sqlStr);
System.out.println("測試當前連接數");
rs = sta.getResultSet();
while (rs.next()) {
oracledb.setConnects(rs.getInt("connects"));
}
// 鎖數量
sqlStr = "SELECT s.username, decode(l.type,'TM','TABLE LOCK','TX','ROW LOCK',NULL) LOCK_LEVEL,"
+ " o.owner,o.object_name,o.object_type,s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser"
+ " FROM v$session s,v$lock l,dba_objects o"
+ " WHERE l.sid = s.sid AND l.id1 = o.object_id(+) AND s.username is NOT NULL";
sta.execute(sqlStr);
System.out.println("測試鎖數量");
rs = sta.getResultSet();
int locks = 0;
while (rs.next()) {
locks++;
}
oracledb.setLockNum(locks);
oracledb.setLockCom("用戶鎖");
// 數據緩衝命中率
sqlStr = "SELECT a.VALUE + b.VALUE logical_reads,c.VALUE phys_reads,"
+ " round(100*(1-c.value/(a.value+b.value)),4) hit_ratio"
+ " FROM v$sysstat a,v$sysstat b,v$sysstat c"
+ " WHERE a.NAME='db block gets' AND b.NAME='consistent gets' AND c.NAME='physical reads' ";
sta.execute(sqlStr);
System.out.println("數據緩衝命中率");
rs = sta.getResultSet();
if (rs.next()) {
oracledb.setDataBufRat(rs.getFloat("hit_ratio"));
}
// SGA命中率
sqlStr = "select a.value + b.value logical_reads, c.value phys_reads,"
+ " round(100 * ((a.value+b.value)-c.value) / (a.value+b.value)) BUFFER_HIT_RATIO "
+ " from v$sysstat a, v$sysstat b, v$sysstat c"
+ " where a.statistic# = 38 and b.statistic# = 39 and c.statistic# = 40";
sta.execute(sqlStr);
System.out.println("測試SGA命中率");
rs = sta.getResultSet();
if (rs.next()) {
oracledb.setSgaRat(rs.getFloat("BUFFER_HIT_RATIO"));
}
// SGA字典緩衝命中率
sqlStr = "select parameter, gets,Getmisses , getmisses/(gets+getmisses)*100 miss_ratio,"
+ " (1-(sum(getmisses)/ (sum(gets)+sum(getmisses))))*100 hit_ratio"
+ " from v$rowcache where gets+getmisses <>0 group by parameter, gets, getmisses";
sta.execute(sqlStr);
System.out.println("測試SGA字典緩衝命中率");
rs = sta.getResultSet();
if (rs.next()) {
oracledb.setSgaDictRat(rs.getFloat("hit_ratio"));
}
// SGA共享緩存區命中率
sqlStr = "select sum(pinhits-reloads)/sum(pins)*100 hit_radio from v$librarycache ";
sta.execute(sqlStr);
System.out.println("測試SGA共享緩存區命中率");
rs = sta.getResultSet();
if (rs.next()) {
oracledb.setSgaShareRat(rs.getFloat("hit_radio"));
}
oracledb.setResult(((short) 0));
// 將數據存儲到OracleDbPerf中
this.oracleDbPerfDao.save(oracledb);
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
return oracledb;
}
public List process(MonitorObject monObj, MonitorLimit[] moniLmt) {
if (oracledb == null) {
return null;
}
/**
* Alarm_Type: 0:正常;1:預警;2:報警
*/
else{
List lst = new ArrayList();
Date date = new Date();
java.text.DateFormat format = new SimpleDateFormat("yyyyMMdd");
String flag_str = "";
Alarm alm[] = new Alarm[moniLmt.length];
for (int i = 0; i < moniLmt.length; i++) {
System.out.println(moniLmt[i].getFieldName() + "]--[" + i + "]--["
+ moniLmt.length);
alm[i] = new Alarm();
lst.add(alm[i]);
if (moniLmt[i].getFieldName().equals("wait_time")) {
if (oracledb.getWaitTime() <= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("等待時間 正常"); // 故障中文描述"
} else if (oracledb.getWaitTime() > moniLmt[i].getNormal()
&& oracledb.getWaitTime() <= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("等待時間 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("等待時間 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "wait_time";
} else if (moniLmt[i].getFieldName().equals("db_free_size")) {
if (oracledb.getDbFreeSize() >= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("剩餘表空間 正常"); // 故障中文描述"
} else if (oracledb.getDbFreeSize() < moniLmt[i].getNormal()
&& oracledb.getDbFreeSize() >= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("剩餘表空間 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("剩餘表空間 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "db_free_size";
} else if (moniLmt[i].getFieldName().equals("db_size_rat")) {
if (oracledb.getDbSizeRat() >= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("表空間可用率 正常"); // 故障中文描述"
} else if (oracledb.getDbSizeRat() < moniLmt[i].getNormal()
&& oracledb.getDbSizeRat() >= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("表空間可用率 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("表空間可用率 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "db_size_rat";
} else if (moniLmt[i].getFieldName().equals("connects")) {
if (oracledb.getConnects() <= moniLmt[i].getNormal()) {
System.out.println("connects 正常");
alm[i].setAlarmType(0);
alm[i].setDescription("連接數 正常"); // 故障中文描述"
} else if (oracledb.getConnects() > moniLmt[i].getNormal()
&& oracledb.getConnects() <= moniLmt[i].getPeralarm()) {
System.out.println("connects 預警");
alm[i].setAlarmType(1);
alm[i].setDescription("連接數 預警"); // 故障中文描述"
} else {
System.out.println("connects 告警");
alm[i].setAlarmType(2);
alm[i].setDescription("連接數 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "connects";
} else if (moniLmt[i].getFieldName().equals("lock_num")) {
if (oracledb.getLockNum() <= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("鎖數量 正常"); // 故障中文描述"
} else if (oracledb.getLockNum() > moniLmt[i].getNormal()
&& oracledb.getLockNum() <= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("鎖數量 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("鎖數量 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "lock_num";
} else if (moniLmt[i].getFieldName().equals("data_buf_rat")) {
if (oracledb.getDataBufRat() >= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("數據緩衝命中率 正常"); // 故障中文描述"
} else if (oracledb.getDataBufRat() < moniLmt[i].getNormal()
&& oracledb.getDataBufRat() >= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("數據緩衝命中率 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("數據緩衝命中率 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "data_buf_rat";
} else if (moniLmt[i].getFieldName().equals("sga_rat")) {
if (oracledb.getSgaRat() >= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("SGA命中率 正常"); // 故障中文描述"
} else if (oracledb.getSgaRat() < moniLmt[i].getNormal()
&& oracledb.getSgaRat() >= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("SGA命中率 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("SGA命中率 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "sga_rat";
} else if (moniLmt[i].getFieldName().equals("sga_dict_rat")) {
if (oracledb.getSgaDictRat() >= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("SGA字典緩衝命中率 正常"); // 故障中文描述"
} else if (oracledb.getSgaDictRat() < moniLmt[i].getNormal()
&& oracledb.getSgaDictRat() >= moniLmt[i].getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("SGA字典緩衝命中率 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("SGA字典緩衝命中率 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "sga_dict_rat";
} else if (moniLmt[i].getFieldName().equals("sga_share_rat")) {
if (oracledb.getSgaShareRat() >= moniLmt[i].getNormal()) {
alm[i].setAlarmType(0);
alm[i].setDescription("SGA共享緩存區命中率 正常"); // 故障中文描述"
} else if (oracledb.getSgaShareRat() < moniLmt[i].getNormal()
&& oracledb.getSgaShareRat() >= moniLmt[i]
.getPeralarm()) {
alm[i].setAlarmType(1);
alm[i].setDescription("SGA共享緩存區命中率 預警"); // 故障中文描述"
} else {
alm[i].setAlarmType(2);
alm[i].setDescription("SGA共享緩存區命中率 告警"); // 故障中文描述"
}
flag_str = monObj.getMonitorId() + "|"
+ monObj.getMonitorObjId() + "|" + monObj.getAppId()
+ "|" + "sga_share_rat";
}
alm[i].setMonitorId(monObj.getMonitorId()); // 監視器編號
alm[i].setMonitorName(monObj.getMonitorObjName()); // 監視器名稱
alm[i].setMonitorObjId(monObj.getMonitorObjId()); // 監視源編號
alm[i].setMonitorObjName(monObj.getMonitorObjName()); // 監視源名稱
alm[i].setAppId(monObj.getAppId()); // 應用編號
alm[i].setApplication(appName); // 應用名稱
alm[i].setStatus(1); // 告警單狀態
alm[i].setBeginTime(date); // 告警開始時間
alm[i].setAlarmNo(format.format(date) + ""); // 告警單序號
alm[i].setFlagStr(flag_str);
//存儲Alarm數據
this.alarmDao.save(alm[i]);
}
return lst;
}
}
public String getLinkmsg(String source, String finds) {
String result = "";
String[] str_array = source.split("\\|");
if (finds == "conType") {
result = str_array[0].toString().trim();
} else if (finds == "ip") {
result = str_array[1].toString().trim();
} else if (finds == "port") {
result = str_array[2].toString().trim();
} else if (finds == "user") {
result = str_array[3].toString().trim();
} else if (finds == "password") {
result = str_array[4].toString().trim();
} else if (finds == "dbname") {
result = str_array[5].toString().trim();
}
return result;
}
// --這裏是構建Spring資源文件--
protected static String basePath() {
return "WebRoot/WEB-INF/config/";
}
protected static String generatePath(String file) {
return basePath() + file;
}
protected static List applicationFiles() {
List files = new ArrayList();
files.add(generatePath("applicationContext.xml"));
files.add(generatePath("hibernateMysql-context.xml"));
files.add(generatePath("context/alarm-context.xml"));
files.add(generatePath("context/oraclePerf-context.xml"));
files.add(generatePath("monitor/monitorOracle-context.xml"));
return files;
}
// --這裏是構建Spring資源文件--
public static void main(String[] args) {
//這裏監控的Oracle數據庫
String ss = "ORACLE|10.243.1.225|1521|simpass|skywin|simpass";
//構建MonitorObject
MonitorObject obj = new MonitorObject();
obj.setMonitorConstr(ss);
obj.setMonitorObjId(2);
obj.setAppId(3);
obj.setMonitorId(2);
obj.setMonitorObjName("ORACLE");
//構建MonitorLimit
MonitorLimit monitorLimit = new MonitorLimit();
MonitorLimit[] moniLmt = new MonitorLimit[1];
moniLmt[0] = monitorLimit;
moniLmt[0].setMonitorObjId(2);
moniLmt[0].setFieldName("wait_time");
moniLmt[0].setNormal(0.01f);
moniLmt[0].setPeralarm(0.03f);
moniLmt[0].setAlarm(0.05f);
ApplicationContext context = new FileSystemXmlApplicationContext(
(String[]) applicationFiles().toArray(new String[] {}));
Monitor oracleMonitor = (Monitor) context.getBean("oracleDbMonitorService");
DataProcess oracleProcess = (DataProcess) context.getBean("oracleDbDataProcessService");
System.out.println("============================="+oracleProcess.getClass());
if (((oracledb = (OracleDbPerf) oracleMonitor.monitor(obj))!= null)) {
oracleProcess.process(obj, moniLmt);
}
}
}
Oracle和Linux系統監控
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.