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系统监控
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.