java开发中的异常捕获的重要性
今天发现主机监控的内存采集不采集了,查看一下发现原来局方把sar的权限给取消了,导致采集方法报错,但是内存的采集是利用的vmstat 2 10 这个命令,为什么会导致内存采集不上来呢?
查看源代码发现
public Vector getMemory(HashMap params) {logger.info("begin getMemory");init(params);// 得到unit_id中使用的主机名称String neat_host_name = rpctarget.getNeat_host_name();if (neat_host_name == null || neat_host_name.equals("")) {// 获取主机名称String host_name = this.getHostName();// 得到unit_id中使用的主机名称neat_host_name = Formater.neatenunitid(host_name);}// 保存采集结果,并返回值CollBase collResult = new CollBase();// 本方法得到的kpi值的unit_id,均以PRE_UNITID + "-12"开头String memory_PRE_UNITID = PRE_UNITID + "-12";// 增加采集时间指标String pattern = "yyyy-MM-dd-HH-mm-ss";SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "","CM-00-01-001-22", dateFormat.format(new java.util.Date()));Vector memoryout = rpctarget.getKPISet("vmstat 2 10");String memoryrun = (String) memoryout.elementAt(memoryout.size() - 1);try {// PM-00-01-002-01 内存的使用率 主机内存的使用量与内存总量的比值Vector mem_num = rpctarget.getKPISet("prtconf |grep 'Good Memory Size:'");String memory = (String) mem_num.elementAt(0);int free_mem = Integer.parseInt(split(memoryrun, 3)) * 4 / 1024; // 空余内存// 单位:Mint total_mem = Integer.parseInt(split(memory, 3)); // 总内存 单位:Mdouble use_per = 100.0 - (free_mem * 100.0) / total_mem; // 内存使用率String mem_use_per = this.getByScale(new Double(use_per).toString());collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-01", Formater.formatDecimalKpivalue(mem_use_per));// 系统内存使用率// 用户内存使用率String sys_user_rate = Float.parseFloat(mem_use_per) * 0.7 + "";String sys_use_rate = Float.parseFloat(mem_use_per) * 0.3 + "";collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-07", Formater.formatDecimalKpivalue(sys_user_rate));collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-06", Formater.formatDecimalKpivalue(sys_use_rate));} catch (Exception e) {e.printStackTrace();}String value = "";// M-00-01-002-02 内存交换请求数value = split(memoryrun, 7);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-02", Formater.formatDecimalKpivalue(value));// PM-00-01-002-03 内存交换页换进率value = split(memoryrun, 5);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-03", Formater.formatDecimalKpivalue(value));// PM-00-01-002-04 内存交换页换出率value = split(memoryrun, 6);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-04", Formater.formatDecimalKpivalue(value));// PM-00-01-002-05 内存队列数value = split(memoryrun, 0);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-05", Formater.formatDecimalKpivalue(value));// PM-00-01-002-08 文件系统数据缓冲命中率// Vector filerate_result = rpctarget.getKPISet("sar -b 2 2");// if (filerate_result != null && filerate_result.size() > 0) {// String filerate_string = (String) filerate_result// .elementAt(filerate_result.size() - 1);// int r_rate = Integer.parseInt(split(filerate_string, 3));// int w_rate = Integer.parseInt(split(filerate_string, 6));// value = String.valueOf((r_rate + w_rate) / 2);// collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name// + "-memory", "PM-00-01-002-08",// Formater.formatDecimalKpivalue(value));// } else {// logger.error("error when exe 'sar -b 2 2' , null or nothing return");// }// Vector filerate_result = rpctarget.getKPISet("sar -b 2 2");String commond = "sh ibmaix/cachehitrate.sh";Vector filerate_result = this.execute(commond);if (filerate_result != null && filerate_result.size() > 0) {String filerate_string = (String) filerate_result.elementAt(0);float fault = Float.parseFloat(split(filerate_string, 3));float odio = Float.parseFloat(split(filerate_string, 4));value = Formater.formatDecimalKpivalue(String.valueOf(fault* 100 / (fault + odio)));collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-08", Formater.formatDecimalKpivalue(value));} else {logger.error("error when exe 'sar -b 2 2' , null or nothing return");}logger.info("end getMemory");return collResult.getKPISet();}出现问题的原因就是在下面的代码编程当中没有捕获异常,导致整个方法都采集不到值,真是城池失火殃及鱼池呀。做程序员一定要注意。
修改后的代码为:
public Vector getMemory(HashMap params) {logger.info("begin getMemory");init(params);// 得到unit_id中使用的主机名称String neat_host_name = rpctarget.getNeat_host_name();if (neat_host_name == null || neat_host_name.equals("")) {// 获取主机名称String host_name = this.getHostName();// 得到unit_id中使用的主机名称neat_host_name = Formater.neatenunitid(host_name);}// 保存采集结果,并返回值CollBase collResult = new CollBase();// 本方法得到的kpi值的unit_id,均以PRE_UNITID + "-12"开头String memory_PRE_UNITID = PRE_UNITID + "-12";// 增加采集时间指标String pattern = "yyyy-MM-dd-HH-mm-ss";SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "","CM-00-01-001-22", dateFormat.format(new java.util.Date()));Vector memoryout = rpctarget.getKPISet("vmstat 2 10");String memoryrun = (String) memoryout.elementAt(memoryout.size() - 1);try {// PM-00-01-002-01 内存的使用率 主机内存的使用量与内存总量的比值Vector mem_num = rpctarget.getKPISet("prtconf |grep 'Good Memory Size:'");String memory = (String) mem_num.elementAt(0);int free_mem = Integer.parseInt(split(memoryrun, 3)) * 4 / 1024; // 空余内存// 单位:Mint total_mem = Integer.parseInt(split(memory, 3)); // 总内存 单位:Mdouble use_per = 100.0 - (free_mem * 100.0) / total_mem; // 内存使用率String mem_use_per = this.getByScale(new Double(use_per).toString());collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-01", Formater.formatDecimalKpivalue(mem_use_per));// 系统内存使用率// 用户内存使用率String sys_user_rate = Float.parseFloat(mem_use_per) * 0.7 + "";String sys_use_rate = Float.parseFloat(mem_use_per) * 0.3 + "";collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-07", Formater.formatDecimalKpivalue(sys_user_rate));collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-06", Formater.formatDecimalKpivalue(sys_use_rate));} catch (Exception e) {e.printStackTrace();}String value = "";// M-00-01-002-02 内存交换请求数value = split(memoryrun, 7);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-02", Formater.formatDecimalKpivalue(value));// PM-00-01-002-03 内存交换页换进率value = split(memoryrun, 5);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-03", Formater.formatDecimalKpivalue(value));// PM-00-01-002-04 内存交换页换出率value = split(memoryrun, 6);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-04", Formater.formatDecimalKpivalue(value));// PM-00-01-002-05 内存队列数value = split(memoryrun, 0);collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name + "-memory","PM-00-01-002-05", Formater.formatDecimalKpivalue(value));// PM-00-01-002-08 文件系统数据缓冲命中率// Vector filerate_result = rpctarget.getKPISet("sar -b 2 2");// if (filerate_result != null && filerate_result.size() > 0) {// String filerate_string = (String) filerate_result// .elementAt(filerate_result.size() - 1);// int r_rate = Integer.parseInt(split(filerate_string, 3));// int w_rate = Integer.parseInt(split(filerate_string, 6));// value = String.valueOf((r_rate + w_rate) / 2);// collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name// + "-memory", "PM-00-01-002-08",// Formater.formatDecimalKpivalue(value));// } else {// logger.error("error when exe 'sar -b 2 2' , null or nothing return");// }// Vector filerate_result = rpctarget.getKPISet("sar -b 2 2");String commond = "sh ibmaix/cachehitrate.sh";Vector filerate_result = this.execute(commond);if (filerate_result != null && filerate_result.size() > 0) {try{String filerate_string = (String) filerate_result.elementAt(0);float fault = Float.parseFloat(split(filerate_string, 3));float odio = Float.parseFloat(split(filerate_string, 4));value = Formater.formatDecimalKpivalue(String.valueOf(fault* 100 / (fault + odio)));collResult.addKPI(memory_PRE_UNITID + ":" + neat_host_name+ "-memory", "PM-00-01-002-08", Formater.formatDecimalKpivalue(value));}catch(Exception e){e.printStackTrace();}} else {logger.error("error when exe 'sar -b 2 2' , null or nothing return");}logger.info("end getMemory");return collResult.getKPISet();}这样就把内存的利用率采集到了。