`
endeavor416
  • 浏览: 64350 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Oracle和Linux系统监控二

阅读更多

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系统监控

    通过上述工具和技术,可以实现Oracle数据库和Linux系统的全方位监控,从而确保系统的健康运行,提高业务效率,预防和快速响应可能出现的问题。在实际操作中,应结合企业的具体需求,选择合适的监控策略和工具,定期...

    oracle监控端安装包(For Linux).tar.gz

    本安装包是专为Linux操作系统设计的,下面将详细介绍如何在Linux系统上安装和配置Oracle监控端。 一、Oracle客户端 Oracle客户端是连接到Oracle数据库服务器的软件工具,它包含了执行SQL查询、PL/SQL程序以及与...

    基于Linux平台Oracle RAC集群数据库监控系统的设计与实现.pdf

    该系统使用信息化方法,在Linux系统中设置Job运行shell脚本自动收集需要监控的指标信息,并使用Web网页技术将监控结果显示到管理者计算机界面,从而简化每日固定的机械操作Linux命令。 知识点: 1. Linux操作系统...

    oracle_linux下载地址.txt

    根据提供的文件信息,我们...总之,Oracle在Linux环境下的部署不仅可以充分利用Linux系统的优点,还能享受Oracle提供的强大数据库管理和企业级支持服务。通过合理的规划和配置,可以构建出既高效又稳定的数据库系统。

    oracle在linux系统中的安装使用详解

    ### Oracle在Linux系统中的安装使用详解 #### 一、Linux系统下Oracle 10G的安装 ##### 1) 安装环境检查 在Linux环境下安装Oracle 10G数据库之前,确保系统的硬件条件和软件环境都符合Oracle的最低要求至关重要。...

    Linux上实现oracleHA双机热备

    1. 编写RHCS脚本,用于管理和监控Oracle实例。 2. 配置脚本以实现故障转移功能。 #### 六、测试 **修改RHCS添加脚本到集群**: 1. 将Oracle HA相关的脚本加入RHCS集群配置中。 2. 测试集群在故障发生时的自动恢复...

    oracle11204_linux_X64下载

    Oracle Database 11g Release 2 (11.2.0.4) 是Oracle公司推出的一款数据库管理系统,它在原有的11g版本基础上进行了多项优化和改进,提供了更稳定、高效的数据存储与管理服务。11.2.0.4版本是11g系列的一个重要补丁...

    Linux安装Oracle11g.zip

    在Linux系统上安装Oracle 11g是一项技术性较强的任务,涉及到多个步骤和注意事项。首先,让我们了解一下Oracle 11g的基本信息。Oracle 11g是Oracle公司的一款企业级数据库管理系统,它提供了高性能、高可用性和安全...

    Oracle GoldenGate 12.2.0.2.2 for Oracle on Linux x86-64.zip

    1. 下载Oracle GoldenGate 12.2.0.2.2 for Oracle on Linux x86-64的zip文件,解压缩后得到OGGCORE_12.2.0.2.2.pdf和OGG-12.2.0.2-README.txt等文件。 2. 阅读OGG-12.2.0.2-README.txt以获取安装和配置的详细步骤。 ...

    oracle 10g for linux

    总的来说,安装Oracle 10g在Linux系统上是一项细致的工作,需要对Linux系统和Oracle数据库有深入的理解。通过合理规划、精确配置和细心操作,可以顺利完成安装,并为后续的数据库管理打下坚实的基础。

    最全版本Oracle11gRPM包linux

    RPM(Red Hat Package Manager)包是Linux系统中广泛使用的软件包管理器,它使得软件的安装、升级、卸载等操作变得方便快捷。在这个“最全版本Oracle11gRPM包linux”中,包含了所有必要的RPM包,旨在简化在Linux环境...

    为oracle调整和优化linux

    《为Oracle调整和优化Linux》 Oracle数据库在企业级应用中扮演着至关重要的角色,而其性能和稳定性往往取决于运行它的...通过持续监控和调整,可以确保系统的稳定性和高可用性,满足企业对Oracle数据库的严苛需求。

    oracle vm 无坚不摧linux

    - **管理工具包**:Oracle Linux 管理工具包是专为 Linux 服务器设计的一套完整的管理工具集,包括配置管理、监控、系统管理等功能,并且对于 Basic Support 或 Premier Support 的用户是免费提供的。 - **集群软件*...

    在 Oracle Enterprise Linux 和 iSCSI 上构建您自己的 Oracle RAC 11g 集群

    Oracle Enterprise Linux 是基于 Red Hat Enterprise Linux 的操作系统,由 Oracle 公司维护,其主要优势在于与 Oracle 数据库的紧密集成和优化。对于 RAC 集群,选择 OEL 可以确保最佳的兼容性和性能。 iSCSI...

    linux之oracle10g安装

    确保你的Linux系统版本与Oracle 10g兼容,例如RHEL或CentOS的某个稳定版本。更新系统到最新补丁,关闭防火墙和Selinux,确保足够的磁盘空间,并为Oracle用户设置适当的权限。 接下来,你需要下载Oracle 10g的二进制...

    oracle linux依赖包

    1. **glibc**:GNU C库是Linux系统的核心部分,为应用程序提供基本功能,如内存管理、线程支持和I/O操作。 2. **libaio**:异步I/O库,Oracle数据库利用这个库进行高效的磁盘I/O操作,提升数据存取性能。 3. **krb...

    Unix和Linux下的Oracle数据库管理

    1. **安装**: 在Unix或Linux系统上安装Oracle需要先安装适当的软件包,如Oracle Database Server软件。这通常涉及设置环境变量,安装依赖项,以及执行安装脚本。 2. **配置**: 配置包括创建数据库实例、设置监听器、...

    legato for oracle on linux

    【legato for oracle on linux】是指在Linux操作系统中使用Legato网络存储管理器(NetWorker)为Oracle数据库提供备份解决方案。Legato NetWorker是一个全面的数据保护软件,它提供了灵活、可扩展的备份和恢复功能,...

    Oracle(linux)install.rar_linux oracle_oracle_oracle for linux_or

    本文将围绕“Oracle在Linux上的安装”这一主题,详细讲解相关知识点,帮助你理解如何在Linux系统中部署和配置Oracle数据库。 首先,安装Oracle数据库在Linux上需要对操作系统有深入的理解,因为Oracle依赖于特定的...

    Oracle-Linux紧急事件

    Linux系统作为Oracle数据库的底层平台,其稳定性也至关重要。在处理Linux紧急事件时,系统管理员需要掌握以下技能: 1. **故障诊断**:通过查看syslog、dmesg和/var/log中的日志,找出系统异常的原因。 2. **内存和...

Global site tag (gtag.js) - Google Analytics