CMDTarget.java 3.09 KB
package com.sitech.ismp.coll;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.util.Vector;

public class CMDTarget {
	String ObjectIP = null;

	static String CONTAINERCMD = "";

	public String SHELLPATH = null;

	boolean state = true;

	static {
		try {
			CONTAINERCMD = "";
		} catch (Exception e) {
			CONTAINERCMD = "";
		}
	}

	public CMDTarget() {
	}

	public void init() {
	}

	public void setContainer(String cCMD) {
		this.CONTAINERCMD = cCMD;
	}

	public boolean getState() {
		return state;
	}

	public boolean getPingState(String ipaddress) {
		/*
		 * /etc/ping -c 5 172.16.1.5 5 packets transmitted, 5 packets received,
		 * 0% packet loss round-trip min/avg/max = 0/0/0 ms
		 * 
		 */
		String cmd = "/etc/ping -c 5 " + ipaddress;
		Vector aa = execute(cmd);
		String value = "";
		if (aa.size() > 0)
			value = (String) aa.elementAt(0);
		for (int i = 1; i < aa.size(); i++) {
			value = value + "\n" + (String) aa.elementAt(i);
		}
		return true;
	}

	public void releaseResources() {
	}

	Vector recordSet = new Vector();

	public Vector getKPISet(String cmdstr) {
		// String cmd = RPCCMD + " " + ObjectIP + " " + user + " " + passwd + "
		// " + cmdstr;
		String cmd = cmdstr;
		return execute(cmd);
	}

	public String getKPIValue(String cmdstr) {
		// String cmd = RPCCMD + " " + ObjectIP + " " + user + " " + passwd + "
		// " + cmdstr;
		String cmd = cmdstr;
		Vector aa = execute(cmd);
		String value = "";
		if (aa.size() > 0)
			value = (String) aa.elementAt(0);
		for (int i = 1; i < aa.size(); i++) {
			value = value + "\n" + (String) aa.elementAt(i);
		}
		return value;
	}

	public String getCommandResult(String command) throws Exception {
		return getKPIValue(command);
	}

	public boolean init(String host, String user, String shellpath)
			throws Exception {
        ObjectIP = host;
		SHELLPATH = shellpath;
		return true;
	}

	public boolean release() {
		return true;
	}

	public String getSHELLPATH() {
		return SHELLPATH;
	}

	private Vector execute(String cCommand) {
		// String shellCommand="/nms/workdir/zang/ssh.sh "+shell_Command;
		// container
		Vector vResult = new Vector();
		try {
			String shellCommand = this.CONTAINERCMD + " " + cCommand;
			// System.out.println("Command is :" + shellCommand);
			Runtime sys = Runtime.getRuntime();
			Process process = sys.exec(shellCommand);

			DataInputStream in = new DataInputStream(process.getInputStream());
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			String line;
			byte[] cbuf = new byte[1024];
			do {
				line = reader.readLine();
				// len = reader.read(cbuf);
				if (line == null) {
					break;
				} else {
					vResult.addElement(line);
				}

				/*
				 * int len = in.read(cbuf); if(len<1){ break; } else{
				 * display(cbuf,len); //System.out.println(new String(cbuf)); }
				 */
			} while (true);
			reader.close();
			process.destroy();
		} catch (Exception e) {
			return null;
		}
		return vResult;
	}

	void display(byte[] cbuf, int len) {
		for (int i = 0; i < len; i++) {
			System.out.print(cbuf[i]);
			System.out.print(" ");
		}
	}
}