package com.skywin.appmonitor.monitor.linux;
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.IAlarmHistDao;
import com.skywin.appmonitor.db.dao.IApplicationDao;
import com.skywin.appmonitor.db.dao.IMonitorConfigDao;
import com.skywin.appmonitor.db.dao.IUnixPerfDao;
import com.skywin.appmonitor.db.vo.Alarm;
import com.skywin.appmonitor.db.vo.AlarmHist;
import com.skywin.appmonitor.db.vo.MonitorLimit;
import com.skywin.appmonitor.db.vo.MonitorObject;
import com.skywin.appmonitor.db.vo.UnixPerf;
import com.skywin.appmonitor.monitor.DataProcess;
import com.skywin.appmonitor.monitor.Monitor;
/**
* Linux操作系统监控
* @author joe
*
*/
public class MonitorLinux implements Monitor,DataProcess{
//--协作者--
private IMonitorConfigDao monitorConfigDao = null;
private IApplicationDao applicationDao = null;
private IAlarmHistDao alarmHistoryDao = null;
private IUnixPerfDao unixPerfDao = null;
public void setUnixPerfDao(IUnixPerfDao unixPerfDao) {
this.unixPerfDao = unixPerfDao;
}
public void setAlarmHistoryDao(IAlarmHistDao alarmHistoryDao) {
this.alarmHistoryDao = alarmHistoryDao;
}
public void setMonitorConfigDao(IMonitorConfigDao monitorConfigDao) {
this.monitorConfigDao = monitorConfigDao;
}
public void setApplicationDao(IApplicationDao applicationDao) {
this.applicationDao = applicationDao;
}
//--协作者--
String filesystem;
String blocks;
String used;
String available;
String userate;
String mountedon;
String total;
Date date = new Date();
static UnixPerf unixPerf = null;
String csdisk_size_detail = null;
java.text.DateFormat format = new SimpleDateFormat("yyyyMMdd");
JavaTelnet telnet = null;
//缺省构造函数
public MonitorLinux() {}
/*
* (non-Javadoc)
* @see com.skywin.Monitor#monitor(com.skywin.MonitorObject)
*/
public Object monitor(MonitorObject monObj) {
//获取监控操作系统的类型
if (getSysVersion(monObj) == "Linux") {
unixPerf = saveLinuxData(monObj);
}else if (getSysVersion(monObj) == "SunOS") {
unixPerf = saveSunOSData(monObj);
}else if (getSysVersion(monObj) == "HP-UX") {
unixPerf = saveHPUXData(monObj);
} else if (getSysVersion(monObj) == "UnixWare") {
unixPerf = saveUnixData(monObj);
}
return unixPerf;
}
/*
* (non-Javadoc)
* @see com.skywin.Monitor#process(com.skywin.MonitorObject, com.skywin.MonitorLimit[])
*/
public List process(MonitorObject monObj, MonitorLimit[] moniLmt) {
List alarm_array = new ArrayList();
String monitorName = "";
String app_name = "";
//根据监控配置ID获取对应的监视器名称
monitorName = this.monitorConfigDao.get(monObj.getMonitorId()).getMonitorName();
//根据应用ID获取对应的应用名称
app_name = this.applicationDao.get(monObj.getAppId()).getAppName();
/*
alarm_type: 0 正常 1.预警 2:告警
unixPerf在monitor(com.skywin.MonitorObject)方法中已经初始化啦
*/
if (unixPerf != null) {
// 下面进行AlarmHistory存储
for (int i = 0; i < moniLmt.length; i++) {
if (moniLmt[i].getFieldName().equals("mem_size")) { // 一般磁盘空间没有告紧
Alarm alarm = new Alarm(); // 创建一个Alarm对象
AlarmHist alarmHist = new AlarmHist(); // 创建一个AlarmHistory对象
// 小于正常情况
if (unixPerf.getMemSize() < moniLmt[i].getNormal()) {
alarm.setAlarmType(0);
alarmHist.setAlarmType(0);
}
// 大于预报警情况
if (unixPerf.getMemSize() > moniLmt[i].getPeralarm()) {
alarm.setAlarmType(1);
alarmHist.setAlarmType(1);
}
// 大于报警情况
if (unixPerf.getMemSize() > moniLmt[i].getAlarm()) {
alarm.setAlarmType(2);
alarmHist.setAlarmType(2);
}
//-----------------------Alarm------------------------
alarm.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarm.setMonitorName(monitorName); // 监视器名称
alarm.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarm.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarm.setAppId(monObj.getAppId()); // 应用编号
alarm.setDescription(csdisk_size_detail); // 故障中文描述
alarm.setApplication(app_name); // 应用名称
alarm.setStatus(1); // 告警单状态
alarm.setBeginTime(date); // 告警开始时间
alarm.setAlarmNo(format.format(date) + ""); // 告警单序号
String flag_str = monObj.getMonitorId() +
monObj.getMonitorObjId()
+ monObj.getAppId() + "mem_size";
alarm.setFlagStr(flag_str);
alarm_array.add(alarm); // alarm类放入数组
//为何这里不存储Alarm? 2009-06-10 15:28 PM
//-----------------------Alarm------------------------
//-----------------------AlarmHistory-----------------
alarmHist.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarmHist.setMonitorName(monitorName); // 监视器名称
alarmHist.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarmHist.setMonitorObjName(monObj.getMonitorObjName());// 监视源名称
alarmHist.setAppId(monObj.getAppId()); // 应用编号
alarmHist.setApplication(app_name); // 应用名称
alarmHist.setStatus(1); // 告警单状态
alarmHist.setBeginTime(date); // 告警开始时间
alarmHist.setAlarmNo(format.format(date) + ""); // 告警单序号
alarmHist.setDescription(csdisk_size_detail); // 故障中文描述
this.alarmHistoryDao.save(alarmHist);
//-----------------------AlarmHistory-----------------
}
if (moniLmt[i].getFieldName() == "free_mem_size") { // 检测空闲内存
Alarm alarm = new Alarm();
AlarmHist alarmHist = new AlarmHist();
if (unixPerf.getFreeMemSize() < moniLmt[i].getNormal()) {
alarm.setAlarmType(0);
alarmHist.setAlarmType(0);
}
if (unixPerf.getFreeMemSize() > moniLmt[i].getPeralarm()) {
alarm.setAlarmType(1);
alarmHist.setAlarmType(1);
}
if (unixPerf.getFreeMemSize() > moniLmt[i].getAlarm()) {
alarm.setAlarmType(2);
alarmHist.setAlarmType(2);
}
//-----------------------Alarm------------------------
alarm.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarm.setMonitorName(monitorName); // 监视器名称
alarm.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarm.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarm.setAppId(monObj.getAppId()); // 应用编号
alarm.setApplication(app_name); // 应用名称
alarm.setStatus(1); // 告警单状态
alarm.setBeginTime(date); // 告警开始时间
alarm.setAlarmNo(format.format(date) + ""); // 告警单序号
alarm.setDescription(csdisk_size_detail); // 故障中文描述
String flag_str = monObj.getMonitorId() +
monObj.getMonitorObjId()
+ monObj.getAppId() + "free_mem_size";
alarm.setFlagStr(flag_str);
alarm_array.add(alarm); // alarm类放入数组
//-----------------------Alarm------------------------
//-----------------------AlarmHistory-----------------
alarmHist.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarmHist.setMonitorName(monitorName); // 监视器名称
alarmHist.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarmHist.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarmHist.setAppId(monObj.getAppId()); // 应用编号
alarmHist.setApplication(app_name); // 应用名称
alarmHist.setStatus(1); // 告警单状态
alarmHist.setBeginTime(date); // 告警开始时间
alarmHist.setDescription(csdisk_size_detail); // 故障中文描述
alarmHist.setAlarmNo(format.format(date) + ""); // 告警单序号
this.alarmHistoryDao.save(alarmHist);
//-----------------------AlarmHistory-----------------
}
if (moniLmt[i].getFieldName() == "mem_rate") { // 内存使用率检测
Alarm alarm = new Alarm();
AlarmHist alarmHist = new AlarmHist();
if (unixPerf.getMemRate() < moniLmt[i].getNormal()) {
alarm.setAlarmType(0);
alarmHist.setAlarmType(0);
}
if (unixPerf.getMemRate() > moniLmt[i].getPeralarm()) {
alarm.setAlarmType(1);
alarmHist.setAlarmType(1);
}
if (unixPerf.getMemRate() > moniLmt[i].getAlarm()) {
alarm.setAlarmType(2);
alarmHist.setAlarmType(2);
}
//-----------------------Alarm------------------------
alarm.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarm.setMonitorName(monitorName); // 监视器名称
alarm.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarm.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarm.setAppId(monObj.getAppId()); // 应用编号
alarm.setApplication(app_name); // 应用名称
alarm.setStatus(1); // 告警单状态
alarm.setBeginTime(date); // 告警开始时间
alarm.setAlarmNo(format.format(date) + ""); // 告警单序号
alarm.setDescription(csdisk_size_detail); // 故障中文描述
String flag_str = monObj.getMonitorId() +
monObj.getMonitorObjId()
+ monObj.getAppId() + "mem_rate";
alarm.setFlagStr(flag_str);
alarm_array.add(alarm); // alarm类放入数组
//-----------------------Alarm------------------------
//-----------------------AlarmHistory-----------------
alarmHist.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarmHist.setMonitorName(monitorName); // 监视器名称
alarmHist.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarmHist.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarmHist.setAppId(monObj.getAppId()); // 应用编号
alarmHist.setApplication(app_name); // 应用名称
alarmHist.setStatus(1); // 告警单状态
alarmHist.setBeginTime(date); // 告警开始时间
alarmHist.setAlarmNo(format.format(date) + ""); // 告警单序号
alarmHist.setDescription(csdisk_size_detail); // 故障中文描述
this.alarmHistoryDao.save(alarmHist);
//-----------------------AlarmHistory-----------------
}
if (moniLmt[i].getFieldName() == "disk_size_total") { // 磁盘容量检测
Alarm alarm = new Alarm();
AlarmHist alarmHist = new AlarmHist();
if (unixPerf.getDiskSizeTotal() < moniLmt[i].getNormal()) {
alarm.setAlarmType(0);
alarmHist.setAlarmType(0);
}
if (unixPerf.getDiskSizeTotal() > moniLmt[i].getPeralarm()) {
alarm.setAlarmType(1);
alarmHist.setAlarmType(1);
}
if (unixPerf.getDiskSizeTotal() > moniLmt[i].getAlarm()) {
alarm.setAlarmType(2);
alarmHist.setAlarmType(2);
}
//-----------------------Alarm------------------------
alarm.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarm.setMonitorName(monitorName); // 监视器名称
alarm.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarm.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarm.setAppId(monObj.getAppId()); // 应用编号
alarm.setApplication(app_name); // 应用名称
alarm.setStatus(1); // 告警单状态
alarm.setBeginTime(date); // 告警开始时间
alarm.setAlarmNo(format.format(date) + ""); // 告警单序号
alarm.setDescription(csdisk_size_detail); // 故障中文描述
String flag_str = monObj.getMonitorId() +
monObj.getMonitorObjId()
+ monObj.getAppId() + "disk_size_total";
alarm.setFlagStr(flag_str);
alarm_array.add(alarm); // alarm类放入数组
//-----------------------Alarm------------------------
//-----------------------AlarmHistory-----------------
alarmHist.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarmHist.setMonitorName(monitorName); // 监视器名称
alarmHist.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarmHist.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarmHist.setAppId(monObj.getAppId()); // 应用编号
alarmHist.setApplication(app_name); // 应用名称
alarmHist.setStatus(1); // 告警单状态
alarmHist.setBeginTime(date); // 告警开始时间
alarmHist.setAlarmNo(format.format(date) + ""); // 告警单序号
alarmHist.setDescription(csdisk_size_detail); // 故障中文描述
this.alarmHistoryDao.save(alarmHist);
//-----------------------AlarmHistory-----------------
}
if (moniLmt[i].getFieldName() == "disk_size_occu") { // 已使用磁盘检测(失败)
//这里编码有异常:2009-06-10 10:05 AM
Alarm alarm = new Alarm();
AlarmHist alarmHist = new AlarmHist();
System.out.println("getDiskSizeDetail:" +
unixPerf.getDiskSizeDetail().toString());
String[] driveData = unixPerf.getDiskSizeDetail().split(
"\\$");
System.out.println("0:" + driveData[0].toString());
System.out.println("1:" + driveData[1].toString());
System.out.println("2:" + driveData[2].toString());
for (int k = 0; k < driveData.length - 1; k++) {
String[] driveDetail = driveData[k].split("\\|");
System.out.println("=============出问题的地方4:" + driveDetail[2].toString());
if (Integer.parseInt(driveDetail[2].toString()) <
moniLmt[i].getNormal()) {
alarm.setAlarmType(0);
alarmHist.setAlarmType(0);
}
if (Integer.parseInt(driveDetail[2].toString()) >
moniLmt[i].getPeralarm()) {
alarm.setAlarmType(1);
alarmHist.setAlarmType(1);
}
if (Integer.parseInt(driveDetail[2].toString()) >
moniLmt[i].getAlarm()) {
alarm.setAlarmType(2);
alarmHist.setAlarmType(2);
break;
}
}
//-----------------------Alarm------------------------
alarm.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarm.setMonitorName(monitorName); // 监视器名称
alarm.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarm.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarm.setAppId(monObj.getAppId()); // 应用编号
alarm.setApplication(app_name); // 应用名称
alarm.setStatus(1); // 告警单状态
alarm.setBeginTime(date); // 告警开始时间
alarm.setAlarmNo(format.format(date) + ""); // 告警单序号
alarm.setDescription(csdisk_size_detail); // 故障中文描述
String flag_str = monObj.getMonitorId() +
monObj.getMonitorObjId()
+ monObj.getAppId() + "disk_size_occu";
alarm.setFlagStr(flag_str);
alarm_array.add(alarm); // alarm类放入数组
//-----------------------Alarm------------------------
//-----------------------AlarmHistory-----------------
alarmHist.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarmHist.setMonitorName(monitorName); // 监视器名称
alarmHist.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarmHist.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarmHist.setAppId(monObj.getAppId()); // 应用编号
alarmHist.setApplication(app_name); // 应用名称
alarmHist.setStatus(1); // 告警单状态
alarmHist.setBeginTime(date); // 告警开始时间
alarmHist.setAlarmNo(format.format(date) + ""); // 告警单序号
alarmHist.setDescription(csdisk_size_detail); // 故障中文描述
this.alarmHistoryDao.save(alarmHist);
//-----------------------AlarmHistory-----------------
}
if (moniLmt[i].getFieldName() == "disk_size_rate") { // 磁盘使用率检测(失败)
Alarm alarm = new Alarm();
AlarmHist alarmHist = new AlarmHist();
String[] driveData = unixPerf.getDiskSizeDetail().split(
"\\$");
for (int j = 0; j < driveData.length - 1; j++) {
String[] driveDetail = driveData[j].split("\\|");
if (Integer.parseInt(driveDetail[3].toString()) <
moniLmt[i].getNormal()) {
alarm.setAlarmType(0);
alarmHist.setAlarmType(0);
}
if (Integer.parseInt(driveDetail[3].toString()) >
moniLmt[i].getPeralarm()) {
alarm.setAlarmType(1);
alarmHist.setAlarmType(1);
}
if (Integer.parseInt(driveDetail[3].toString()) >
moniLmt[i].getAlarm()) {
alarm.setAlarmType(2);
alarmHist.setAlarmType(2);
break;
}
}
//-----------------------Alarm------------------------
alarm.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarm.setMonitorName(monitorName); // 监视器名称
alarm.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarm.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarm.setAppId(monObj.getAppId()); // 应用编号
alarm.setApplication(app_name); // 应用名称
alarm.setStatus(1); // 告警单状态
alarm.setBeginTime(date); // 告警开始时间
alarm.setAlarmNo(format.format(date) + ""); // 告警单序号
alarm.setDescription(csdisk_size_detail); // 故障中文描述
String flag_str = monObj.getMonitorId() +
monObj.getMonitorObjId()
+ monObj.getAppId() + "disk_size_rate";
alarm.setFlagStr(flag_str);
alarm_array.add(alarm); // alarm类放入数组
//-----------------------Alarm------------------------
//-----------------------AlarmHistory-----------------
alarmHist.setMonitorId(monObj.getMonitorId()); // 监视器编号
alarmHist.setMonitorName(monitorName); // 监视器名称
alarmHist.setMonitorObjId(monObj.getMonitorObjId()); // 监视源编号
alarmHist.setMonitorObjName(monObj.getMonitorObjName()); // 监视源名称
alarmHist.setAppId(monObj.getAppId()); // 应用编号
alarmHist.setApplication(app_name); // 应用名称
alarmHist.setStatus(1); // 告警单状态
alarmHist.setBeginTime(date); // 告警开始时间
alarmHist.setAlarmNo(format.format(date) + ""); // 告警单序号
alarmHist.setDescription(csdisk_size_detail); // 故障中文描述
this.alarmHistoryDao.save(alarmHist);
//-----------------------AlarmHistory-----------------
}
}
}
return alarm_array;
}
/**
* 获取类似这类格式的数据:"TCP|10.243.1.130|23|root|gmccpt08";
* @param source
* @param finds
* @return
*/
public String getLinkmsg(String source, String finds) {
String result = "";
String[] str_array = source.split("\\|");
if (finds == "conType") {
result = str_array[0].toString().trim();
}
if (finds == "ip") {
result = str_array[1].toString().trim();
}
if (finds == "port") {
result = str_array[2].toString().trim();
}
if (finds == "user") {
result = str_array[3].toString().trim();
}
if (finds == "password") {
result = str_array[4].toString().trim();
}
return result;
}
/**
* 获取当前OS的版本信息
* @param monObj
* @return
*/
public String getSysVersion(MonitorObject monObj) {
String linkmsg = monObj.getMonitorConstr();
String result = "";
String sver = "";
try {
//通过Telnet到远程主机获取当前操作系统的类型(版本)
//所以这里先登录初始化InputStream实例之后才能执行下面的操作
telnet = new JavaTelnet();
int isucc = 0;
isucc = telnet.login(getLinkmsg(linkmsg, "ip"),
Integer.parseInt(getLinkmsg(linkmsg, "port")),
getLinkmsg(linkmsg, "user"),
getLinkmsg(linkmsg, "password"));
//'uname -a':SunOS portal-app-3 5.8 Generic_117350-60 sun4u sparc SUNW,Sun-Fire
telnet.sendCommand("uname -a", "SysVersion");
sver = telnet.getSysVersion;
//current os version: SunOS portal-app-3 5.8 Generic_117350-60 sun4u sparc SUNW,Sun-Fire
System.out.println("--------------------current os version: "+ sver);
if (sver.contains("Linux")) {
result = "Linux";
} else if (sver.contains("HP-UX")) {
result = "HP-UX";
} else if (sver.contains("SunOS")) {
result = "SunOS";
} else if (sver.contains("UnixWare")) {
result = "UnixWare";
}
} catch (Exception e) {
}
return result;
}
/**
* String数据解析
* @param s
* @return
*/
public String defaultArray(String s) {
String[] str_array = s.split("\n");
String str = "";
for (int i = 0; i < str_array.length - 1; i++) {
String field = str_array[i].toString();
String[] values = field.split("\\s+");
if ((values.length != 6) && (values.length == 1)) {
str = str + str_array[i].toString() + " " + str_array[i +
1].toString().trim() + "\n";
continue;
} else if ((values.length != 6) && (values.length == 5)) {
continue;
} else if (values.length == 6) {
if ((values[0].toString().equals(null)) ||
(values[0].toString().equals(""))) {
continue;
}
str = str + str_array[i].toString() + "\n";
}
}
System.out.println("str:" + str);
return str;
}
/**
* 存储SunOS操作使用的情况
* @param monObj:监视源
* @return:Unix操作系统性能
*/
public UnixPerf saveSunOSData(MonitorObject monObj) {
int mon_obj_id = 0;
mon_obj_id = monObj.getMonitorObjId();
String linkmsg = monObj.getMonitorConstr();
String kkk = "";
ArrayList list_c = new ArrayList();
ArrayList list_m = new ArrayList();
ArrayList list_d = new ArrayList();
Float fcpu_use = 0.0f;
int fmem_size = 0;
int free_mem_size = 0;
Float fmem_rate = 0.0f;
int idisk_size_total = 0;
int idisk_size_occu = 0;
Float fdisk_size_rate = 0.0f;
String sdisk_size_detail = "";
String msg = "";
String comm = "";
try {
System.out.println("--------------------------------------SunOS");
//得到CPU使用信息
/* 1:间隔时间
* 3:执行次数
* root@portal-app-3 # sar 1 3
SunOS portal-app-3 5.8 Generic_117350-60 sun4u 06/10/09
14:31:36 %usr %sys %wio %idle
14:31:38 20 8 0 72
14:31:39 19 6 0 75
14:31:40 26 10 0 63
Average 22 8 0 70
*/
telnet.sendCommand("sar 1 3", "CPU");
System.out.println("============ cup:"+telnet.getedCpu);
if (telnet.getedCpu.contains("Average")) {
int i = 0;
i = telnet.getedCpu.indexOf("Average");
kkk = telnet.getedCpu.substring(i, telnet.getedCpu.length()).
trim();
list_c = telnet.myStringTokenizer(kkk);
System.out.println("=========================== cpu情况:"+list_c);
fcpu_use = 100 - new Float((String) list_c.get(4));
System.out.println("=========================== cpu使用率:"+fcpu_use);
}
//得到内存使用情况
/* 1:间隔时间
* 3:执行次数
* root@portal-app-3 # sar -r 1 3
SunOS portal-app-3 5.8 Generic_117350-60 sun4u 06/10/09
14:37:27 freemem freeswap
14:37:28 379480 14438340
14:37:29 379480 14438340
14:37:30 368918 14037274
Average 375893 14302128
*/
telnet.sendCommand("sar -r 1 3", "Mem");
System.out.println("============= 内存信息:"+telnet.getedMem);
if (telnet.getedMem.contains("Average")) {
int i = 0;
i = telnet.getedMem.indexOf("Average");
kkk = telnet.getedMem.substring(i, telnet.getedMem.length()).
trim();
list_m = telnet.myStringTokenizer(kkk);
free_mem_size = Integer.parseInt((String) (list_m.get(1)));
System.out.println("=========================== 空闲内存:"+free_mem_size);
}
//得到总内存--> Memory size: 16256 Megabytes
/*
* root@portal-app-3 # /usr/sbin/prtconf | grep 'Memory'
Memory size: 8192 Megabytes
*/
telnet.sendCommand("/usr/sbin/prtconf | grep 'Memory'",
"SunTotalMem");
System.out.println("=============内存容量: "+telnet.getSunTotalMem);
if (telnet.getSunTotalMem.contains("Memory size:")) {
int i = 0;
i = telnet.getSunTotalMem.indexOf("Memory size:");
kkk = telnet.getSunTotalMem.substring(i,
telnet.getSunTotalMem.length()).trim();
list_m = telnet.myStringTokenizer(kkk);
fmem_size = new Integer((String) list_m.get(2)) * 1024;
float f = Float.parseFloat(free_mem_size + "");
float t = Float.parseFloat(fmem_size + "");
fmem_rate = 1 - f / t;
}
//得到磁盘使用情况
/*
* root@portal-app-3 # df -k
文件系统 千字节 用了 可用 容量 挂接在
/dev/vx/dsk/rootvol 52376415 26497663 25354988 52% /
/proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
fd 0 0 0 0% /dev/fd
/dev/vx/dsk/var 10086988 7225838 2760281 73% /var
swap 7579448 40 7579408 1% /var/run
swap 7581696 2288 7579408 1% /tmp
portal-nfs:/global/nfs 412876013 121333046 287414207 30% /san/portal
*/
ArrayList aDrivetotal = new ArrayList();
ArrayList aUsed = new ArrayList();
telnet.sendCommand("df -k", "Drive");
System.out.println("磁盘使用情况:====================="+telnet.getedDrive);
if (telnet.getedDrive.contains("Filesystem")||(1==1)) {
String s = defaultArray(telnet.getedDrive);
String[] str_array = s.split("\n");
SunMicSysData[] lx = new SunMicSysData[str_array.length];
for (int i = 0; i < str_array.length - 1; i++) {
String field = "";
field = str_array[i].toString();
String[] values = field.split("\\s+");
System.out.println("0:" + values[0].toString());
System.out.println("1:" + values[1].toString());
System.out.println("2:" + values[2].toString());
System.out.println("3:" + values[3].toString());
System.out.println("4:" + values[4].toString());
System.out.println("5:" + values[5].toString());
for (int j = 0; j < values.length; j++) {
list_d.add(values[j]);
}//end for
if (list_d.size() == 6) {
lx[i] = new SunMicSysData();
lx[i].filesystem = list_d.get(0).toString();
lx[i].blocks = list_d.get(1).toString();
lx[i].used = list_d.get(2).toString();
sdisk_size_detail = sdisk_size_detail + comm +
lx[i].filesystem + "|" +
lx[i].blocks + "|" + lx[i].used +
"|" + lx[i].userate;
System.out.println("sdisk_size_detail:" +
sdisk_size_detail);
csdisk_size_detail = csdisk_size_detail + comm + "Disk_Name:" +
lx[i].filesystem + "|Disk_Space:" +
lx[i].blocks + "|Disk_Used_Space:" +
lx[i].used + "|Used_Rate:" +
lx[i].userate + "%";
comm = "$";
}//end if (list_d.size() == 6)
try {
int idriveTotal = Integer.parseInt(list_d.get(1).
toString())
+
Integer.parseInt(list_d.get(2).toString());
aDrivetotal.add(idriveTotal);
aUsed.add(list_d.get(2).toString());
System.out.println(idriveTotal + "");
} catch (Exception e) {
}
list_d.clear();
}
}//end if (telnet.getedDrive.contains("Filesystem") || (1==1))
for (int i = 0; i < aDrivetotal.size(); i++) {
idisk_size_total = idisk_size_total +
Integer.parseInt(aDrivetotal.get(i).toString());
}//end for
System.out.println("总的磁盘量: "+ idisk_size_total);
for (int i = 0; i < aUsed.size(); i++) {
idisk_size_occu = idisk_size_occu +
Integer.parseInt(aUsed.get(i).toString());
}//end for
System.out.println("已使用磁盘空间: "+idisk_size_occu);
float iD = Float.parseFloat(idisk_size_total + "");
float u = Float.parseFloat(idisk_size_occu + "");
if (iD == 0 || u == 0) {
fdisk_size_rate = 0f;
} else {
fdisk_size_rate = iD / u;
System.out.println("磁盘使用率"+fdisk_size_rate);
}
//释放Telnet连接
telnet.disconnect();
} catch (Exception e) {
msg = e.toString();
e.printStackTrace();
}
//将下面的信息存储到UnixPerf中去
unixPerf = new UnixPerf(); //构建一个UnixPerf实例
unixPerf.setMonitorObjId(mon_obj_id);
unixPerf.setCpuUse(fcpu_use);
unixPerf.setMemSize(fmem_size);
unixPerf.setFreeMemSize(free_mem_size);
unixPerf.setMemRate(fmem_rate);
unixPerf.setDiskSizeTotal(idisk_size_total);
unixPerf.setDiskSizeOccu(idisk_size_occu);
unixPerf.setDiskSizeRate(fdisk_size_rate);
unixPerf.setDiskSizeDetail(sdisk_size_detail);
unixPerf.setCreateTime(date);
int j = 1;
unixPerf.setResult(j);
unixPerf.setErrMsg(msg);
this.unixPerfDao.save(unixPerf);
return unixPerf;
}
/**
* 存储HP Unix操作系统的使用情况
* @param monObj
* @return
*/
public UnixPerf saveHPUXData(MonitorObject monObj) {
int mon_obj_id = 0;
mon_obj_id = monObj.getMonitorObjId();
String linkmsg = monObj.getMonitorConstr();
String kkk = "";
ArrayList list_c = new ArrayList();
ArrayList list_m = new ArrayList();
ArrayList list_d = new ArrayList();
Float fcpu_use = 0.0f;
int fmem_size = 0;
int free_mem_size = 0;
Float fmem_rate = 0.0f;
int idisk_size_total = 0;
int idisk_size_occu = 0;
Float fdisk_size_rate = 0.0f;
ArrayList list1 = new ArrayList(); ;
String msg = "";
try {
System.out.println("HPUX");
//得到CPU
telnet.sendCommand("sar 1 3", "CPU");
System.out.println(telnet.getedCpu);
if (telnet.getedCpu.contains("Average")) {
int i = 0;
i = telnet.getedCpu.indexOf("Average");
kkk = telnet.getedCpu.substring(i, telnet.getedCpu.length()).
trim();
list_c = telnet.myStringTokenizer(kkk);
fcpu_use = 100 - new Float((String) list_c.get(4));
}
//得到内存
telnet.sendCommand("top -n2 -d1", "Mem");
System.out.println(telnet.getedMem);
if (telnet.getedMem.contains("Memory:")) {
int i = 0;
i = telnet.getedMem.indexOf("Memory:");
kkk = telnet.getedMem.substring(i, telnet.getedMem.length()).
trim();
list_m = telnet.myStringTokenizer(kkk);
String s1 = list_m.get(1).toString();
fmem_size = new Integer(s1.substring(0, s1.length() - 1));
String s2 = list_m.get(2).toString();
int fmen_real = new Integer(s2.substring(1, s1.length()));
free_mem_size = fmem_size - fmen_real;
float f1 = Float.parseFloat(fmen_real + "");
float f2 = Float.parseFloat(fmem_size + "");
fmem_rate = f1 / f2;
free_mem_size = fmem_size -
new Integer(s2.substring(1, s1.length() - 1));
}
//得到磁盘
ArrayList aDrivetotal = new ArrayList();
ArrayList aUsed = new ArrayList();
telnet.sendCommand("df -k", "Drive");
String s1 = telnet.getedDrive.substring(5, telnet.getedDrive.length()).
trim();
String[] str_array = s1.split("\n");
for (int p = 0; p < str_array.length; p++) {
String[] values = str_array[p].split("\\s+");
for (int hk = 0; hk < values.length; hk++) {
System.out.println(values[hk].toString());
list1.add(values[hk].toString());
}
}
for (int y = 0; y < list1.size(); y++) {
System.out.println(list1.get(y).toString() +
"list1.get(y).toString()");
}
} catch (Exception e) {
msg = e.toString();
e.printStackTrace();
}
unixPerf = new UnixPerf(); //构建一个UnixPerf实例
unixPerf.setMonitorObjId(mon_obj_id);
unixPerf.setCpuUse(fcpu_use);
unixPerf.setMemSize(fmem_size);
unixPerf.setFreeMemSize(free_mem_size);
unixPerf.setMemRate(fmem_rate);
unixPerf.setDiskSizeTotal(idisk_size_total);
unixPerf.setDiskSizeOccu(idisk_size_occu);
unixPerf.setDiskSizeRate(fdisk_size_rate);
unixPerf.setCreateTime(date);
int j = 1;
unixPerf.setResult(j);
unixPerf.setErrMsg(msg);
this.unixPerfDao.save(unixPerf);
return unixPerf;
}
/**
* 存储Linux操作系统的使用情况
* @param monObj
* @return
*/
public UnixPerf saveLinuxData(MonitorObject monObj) {
int mon_obj_id = 0;
mon_obj_id = monObj.getMonitorObjId();
String linkmsg = monObj.getMonitorConstr();
String kkk = "";
ArrayList list_c = new ArrayList();
ArrayList list_m = new ArrayList();
ArrayList list_d = new ArrayList();
Float fcpu_use = 0.0f;
int fmem_size = 0;
int free_mem_size = 0;
Float fmem_rate = 0.0f;
int idisk_size_total = 0;
int idisk_size_occu = 0;
Float fdisk_size_rate = 0.0f;
String sdisk_size_detail = "";
String msg = "";
String comm = "";
try {
System.out.println("linux");
//得到CPU
telnet.sendCommand("sar 1", "CPU");
if (telnet.getedCpu.contains("Average:")) {
int i = 0;
i = telnet.getedCpu.indexOf("Average:");
kkk = telnet.getedCpu.substring(i, telnet.getedCpu.length()).
trim();
list_c = telnet.myStringTokenizer(kkk);
fcpu_use = 100 - new Float((String) list_c.get(6));
}
//得到内存
telnet.sendCommand("sar -r 1", "Mem");
System.out.println(telnet.getedMem);
if (telnet.getedMem.contains("Average:")) {
int i = 0;
i = telnet.getedMem.indexOf("Average:");
kkk = telnet.getedMem.substring(i, telnet.getedMem.length()).
trim();
list_m = telnet.myStringTokenizer(kkk);
Integer abc;
fmem_size = new Integer((String) list_m.get(1)) +
new Integer((String) list_m.get(2));
free_mem_size = Integer.parseInt(
Oracle和Linux系统监控二
最新推荐文章于 2024-09-14 09:01:07 发布