您的位置:首页 > 运维架构

查看hadoop 是否运行

2014-09-16 16:26 141 查看
执行工具类 ExecCMD:
主要判断其进程:jps
hadoop : NameNode DataNode
hbase :  HMaster
hive :   RunJar
 
package com.winhong.test.util;<p>import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;</p><p>import org.json.JSONException;
import org.json.JSONObject;</p><p>/**
* @author kuanghj| 下午4:16:21 | 创建
*/
public class ExecCMD
{
/**
* 检查服务实例是否在运行
*
* @param param
* @return
* @throws JSONException
*/
public static boolean isCheckRun(String param, JSONObject temp) throws JSONException
{
List<String> strlst = new ArrayList<String>();
boolean isRun = false;
try {</p><p>            Process process = null;</p><p>            String[] cmdArray = new String[]{"/bin/sh", "-c", "ssh " + temp.getString("hostIP") + " \"ps -ef | grep " + param + "\""};
System.out.println(cmdArray[2]);
process = Runtime.getRuntime().exec(cmdArray);</p><p>            InputStream is = process.getInputStream();</p><p>            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
strlst.add(line);
if (line.contains(param + " start"))
{
isRun = true;
}
}
is.close();
isr.close();
return isRun;
} catch (IOException e)
{
e.printStackTrace();
}</p><p>        return isRun;
}

/**
* 监控Hbase的数据,返回监控结果
*
* @param
* @return
*/
public static List<String> monitorHbase(JSONObject temp) throws JSONException
{
List<String> strList = new ArrayList<String>();</p><p>        try {
boolean isStart = isCheckRun("HMaster", temp);
if (isStart)
{
// ssh 192.168.1.61 "echo \"status 'simple'\" | /home/hadmin/hbase/bin/hbase shell"
Process process = null;</p><p>                String cmd = "echo <a>\\\"status</a> 'simple'\\\"";
String ip = temp.getString("hostIP");
String appPath = temp.getString("installPath");
String[] cmdArray = new String[]{"/bin/sh", "-c", "ssh " + ip + " \"" + cmd + " | " + appPath + "/bin/hbase shell\""};

process = Runtime.getRuntime().exec(cmdArray);</p><p>                InputStream is = process.getInputStream();</p><p>                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
strList.add(line);
}
is.close();
isr.close();
}</p><p>            return strList;
} catch (IOException e) {
e.printStackTrace();
}</p><p>        return strList;
}

/**
*  取得用户占用空间
* @param temp
* @return
* @author kuanghj |下午4:20:58
* @version 0.1
*/
public static String getStroageData(JSONObject temp) throws JSONException
{
List<String> strlst = new ArrayList<String>();
String home = temp.getString("home");
String value = "";
try {</p><p>            Process process = null;</p><p>            String[] cmdArray = new String[]{"/bin/sh", "-c", "hadoop fs -du " + home};
process = Runtime.getRuntime().exec(cmdArray);</p><p>            InputStream is = process.getInputStream();</p><p>            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
strlst.add(line);
if (line.contains(home))
{
value = line;
break;
}
}
is.close();
isr.close();
}
catch (IOException e)
{
e.printStackTrace();
}</p><p>        return value;</p><p>    }

/**
* 取到当前时间 格试为  yyyyMMddHHmmssSSS
*
* @return
*/
public static String getCurrtime()
{
Date date = new Date();

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String currTime = format.format(date);</p><p>        return currTime;
}
}
</p>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: