进程的基本信息类
public class ProcessDefDto {
private long omcId;
private String command;//启进程命令
private int port;//端口号
private int collectType;//采集类型,0 snmp,1 tl1
public ProcessDefDto(){
}
public ProcessDefDto(long omcId,String command,int port,int collectType){
this.omcId= omcId;
this.command= command;
this.port= port;
this.collectType= collectType;
}
public void setOmcId(long omcId){
this.omcId= omcId;
}
public long getOmcId(){
return this.omcId;
}
public void setCommand(String command){
this.command= command;
}
public String getCommand(){
return this.command;
}
public void setPort(int port){
this.port= port;
}
public int getPort(){
return this.port;
}
public void setCollectType(int collectType){
this.collectType= collectType;
}
public int getCollectType(){
return this.collectType;
}
}
一系列的进程
public class ProcessManager {
private static Logger log = Logger.getLogger(MonitorThread.class);
private static Vector<ProcessDefDto> processDefs = new Vector<ProcessDefDto>();
private Map<String, String> mapCmds = new HashMap<String, String>();
/** 起始端口 **/
private String start_port="start_port";
/**启动命令 **/
private String snmp_key="snmp_cmd";
private String tl1_key="tl1_cmd";
private String tl1_parse_key="tl1_parse_cmd";
private String snmp_parse_key="snmp_parse_cmd";
private String tl1_alm_key="tl1_alm_cmd";
private String tl1_coll_key="tl1_coll_cps";
private String res_cj_key="res_cj";
private int defaultPort=6000;
public ProcessManager(Map<String, String> mapCmds){
this.mapCmds=mapCmds;
}
//初始化所有待启动进程
public boolean initprocessDefs(String areaIds){
long t1=System.currentTimeMillis();
Map<Integer,String> omcmap=PonCollectorDaoServiceProvider.getOmcConfigDao().findOmcIdByAreaIds(areaIds);
String snmp_cmd=mapCmds.get(snmp_key);
String tl1_cmd=mapCmds.get(tl1_key);
String tl1_parse_cmd=mapCmds.get(tl1_parse_key);
String snmp_parse_cmd=mapCmds.get(snmp_parse_key);
String tl1_alm_cmd=mapCmds.get(tl1_alm_key);
String tl1_coll_cps=mapCmds.get(tl1_coll_key);
String res_cj=mapCmds.get(res_cj_key);
if(mapCmds.get(start_port)!=null&&!mapCmds.get(start_port).equals("")){//配置的端口号不为空,则取配置 的端口号。
defaultPort=Integer.valueOf(mapCmds.get(start_port));
};
log.info("区域areaId:"+areaIds+",对应OMCID数:"+omcmap.size());
ServicePortManager portmgr= new ServicePortManager();
//增加SNMP采集进程
if (snmp_cmd != null && !snmp_cmd.equals("")) {
for (Integer omc : omcmap.keySet()) {
ProcessDefDto proDef = new ProcessDefDto();
int port = portmgr.initPort(defaultPort, String.valueOf(omc),
ServicePortManager.SNMPTYPE);
defaultPort = port + 1;
proDef.setOmcId(omc);
proDef.setCommand(snmp_cmd + " " + omc + " " + port);
proDef.setPort(port);
proDef.setCollectType(0);
processDefs.add(proDef);
}
} else {
log.info("SNMP启动命令没有配置");
}
//增加TL1解析进程
if (tl1_parse_cmd != null && !tl1_parse_cmd.equals("")) {
String[] areas = areaIds.split(",");
CollRmiCfg crc = PonCollectorDaoServiceProvider.getCollRmiCfgDao()
.getPortById((short) Integer.parseInt(areas[0]), (short) 3);
String ip = crc.getServerIp();
String cmds[] = tl1_parse_cmd.split("\\|\\|");
for (String area : areas) {
for (String cmd : cmds) {
ProcessDefDto proDef = new ProcessDefDto();
String newCmd = cmd.replaceAll("\\{ip\\}", ip);
proDef.setCommand(newCmd + " " + area);
proDef.setCollectType(1);
processDefs.add(proDef);
}
}
} else {
log.info("TL1解析进程启动命令没有配置");
}
//增加TL1告警进程
if (tl1_alm_cmd != null && !tl1_alm_cmd.equals("")) {
for (Integer omc : omcmap.keySet()) {
ProcessDefDto proDef = new ProcessDefDto();
int port = portmgr.initPort(defaultPort, String.valueOf(omc),
ServicePortManager.TL1TYPE);
defaultPort = port + 1;
String factory = omcmap.get(omc);
proDef.setOmcId(omc);
proDef.setCommand(tl1_alm_cmd + " " + omc + " " + factory
+ " "+port);
proDef.setPort(port);
proDef.setCollectType(1);
processDefs.add(proDef);
}
} else {
log.info("TL1启动命令没有配置");
}
public void startAllProcess(){
for(ProcessDefDto pro:processDefs){
startProc(pro);
}
}
public void stopAllProcess(){
for(ProcessDefDto pro:processDefs){
stopProc(pro);
}
}
public void startProc(ProcessDefDto pro){
ProcessUtil.startProcess(pro.getCommand());
}
public void stopProc(ProcessDefDto pro){
ProcessUtil.stopProcess(pro.getCommand());
}
public Vector<ProcessDefDto> getProcessDefs(){
return processDefs;
}
}
没有列举太多的进程信息
一个线程监控所有的进程
public class MonitorThread extends Thread{
private static Logger log = Logger.getLogger(MonitorThread.class);
public void run(){
ProcessManager pm =CollectServiceProvider.getProcessManager();
Vector<ProcessDefDto> processManagerDtos = pm.getProcessDefs();
log.info("monitor thread start...");
while (true) {
Map<String, String[]> map = ProcessUtil.getProcessInfo();
if(map!=null){
for (ProcessDefDto managerDto : processManagerDtos) {
String[] status = map.get(managerDto.getCommand());
if (ProcessUtil.isDown(status)) {// 进程满足挂起僵死条件,则进行重启
log.info("进程启动>"+managerDto.getCommand());
if(status!=null){
pm.stopProc(managerDto);
}
pm.startProc(managerDto);
}
}
}
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
log.error("InterruptedException",e);
}
}
}
}
/**
* 进程启动停止等工具类
*/
public class ProcessUtil {
private static Logger log = Logger.getLogger(ProcessUtil.class);
public final static String PROCESS_INFO_CMD_LINUX = "ps axo pid,stat,command";
public final static String PROCESS_INFO_CMD_AIX = "ps -efo pid,state,args";
public final static String PROCESS_INFO_CMD_SunOS = "/usr/ucb/ps -axww";
private final static Map<String, String> osCmdMap = new HashMap<String, String>(){ {
put("Linux", PROCESS_INFO_CMD_LINUX);
put("AIX", PROCESS_INFO_CMD_AIX);
put("SunOS", PROCESS_INFO_CMD_SunOS);
}};
private static Runtime runtime = Runtime.getRuntime();
public static boolean startProcess(String cmd){
try{
Process process = runtime.exec(cmd);
new ConsoleThread(process,cmd).start();
return true;
}catch (Exception e) {
log.error("error in start "+ cmd,e);
}
return false;
}
public static boolean stopProcess(String cmd){
try{
Map<String, String[]> map = getProcessInfo();
if(map!=null){
String[] info = map.get(cmd);
if(info!=null){
runtime.exec("kill -9 "+info[0]);
}else{
log.info("can not find process " + cmd);
}
}
return true;
}catch (Exception e) {
log.error("error in stop "+ cmd,e);
}
log.info("stop failed ");
return false;
}
public synchronized static Map<String, String[]> getProcessInfo(){
String osName = System.getProperty("os.name");
String cmd = osCmdMap.get(osName);
try{
if ("Linux".equals(osName)||"AIX".equals(osName)) {
Process p = runtime.exec(cmd);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = new String();
Map<String, String[]> map = new HashMap<String, String[]>();
while ((str = br.readLine()) != null) {
Scanner s = new Scanner(str);
String pid = s.next();
String state = s.next();
String command = str.substring(str.indexOf(state)+state.length()).trim();
map.put(command, new String[]{pid,state});
s.close();
}
br.close();
in.close();
p.destroy();
return map;
}else if("SunOS".equals(osName)){
Process p = runtime.exec(cmd);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = new String();
Map<String, String[]> map = new HashMap<String, String[]>();
while ((str = br.readLine()) != null) {
Scanner s = new Scanner(str);
String pid = s.next();
s.next();
String state = s.next();
String index = s.next();
String command = str.substring(str.indexOf(index)+index.length()).trim();
map.put(command, new String[]{pid,state});
s.close();
}
br.close();
in.close();
p.destroy();
return map;
}else{
log.debug(osName + " monitor unimplementate ");
return null;
}
}catch (Exception e) {
log.error("",e);
return null;
}
}
/**
* 进程运行是否正常
* @param status
* @return
*/
public static boolean isDown(String[] status){
if(status==null) return true;
String osName = System.getProperty("os.name");
if ("Linux".equals(osName)) {
if(status[1].equals("X") || status[1].equals("Z")){//X:死掉的进程 Z:僵尸进程
return true;
}else{
return false;
}
}else if ("AIX".equals(osName)){
if(status[1].equals("Z") || status[1].equals("T")){//Z:已取消 T:已停止
return true;
}else{
return false;
}
}else if("SunOS".equals(osName)){
if(status[1].equals("Z") || status[1].equals("T")){//Z:已取消 T:已停止
return true;
}else{
return false;
}
}else{
log.debug(osName + " monitor unimplementate ");
return false;//不处理
}
}
}
/**
* 控制台输出线程,防止被管进程由于控制台导致的进程挂起
*
*/
public class ConsoleThread extends Thread {
private Logger log = Logger.getLogger(ConsoleThread.class);
private Process process = null;
private String commad = null;
public ConsoleThread(Process process,String commad) {
this.process = process;
this.commad = commad;
}
public void run() {
if(this.process ==null) return;
log.info(this.commad +" console thread start.");
BufferedInputStream in = new BufferedInputStream(this.process.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try {
String s = br.readLine();
while (s != null) {
System.out.println(s);
s = br.readLine();
}
} catch (Exception e) {
log.error(this.commad, e);
} finally {
try {
br.close();
in.close();
} catch (Exception e) {
log.error(this.commad, e);
}
}
log.info(this.commad + " end");
}
}