三月阳光 发表于 2015-11-11 11:39:31

查看hadoop 是否运行

执行工具类 ExecCMD:主要判断其进程:jps hadoop : NameNode DataNode hbase :HMasterhive :   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[]{&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;ssh &quot; + temp.getString(&quot;hostIP&quot;) + &quot; \&quot;ps -ef | grep &quot; + param + &quot;\&quot;&quot;};
System.out.println(cmdArray);
process = Runtime.getRuntime().exec(cmdArray);</p><p>            InputStream is = process.getInputStream();</p><p>            InputStreamReader isr = new InputStreamReader(is, &quot;UTF-8&quot;);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
strlst.add(line);
if (line.contains(param + &quot; start&quot;))
{
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(&quot;HMaster&quot;, temp);
if (isStart)
{
// ssh 192.168.1.61 &quot;echo \&quot;status 'simple'\&quot; | /home/hadmin/hbase/bin/hbase shell&quot;
Process process = null;</p><p>                String cmd = &quot;echo <a>\\\&quot;status</a> 'simple'\\\&quot;&quot;;
String ip = temp.getString(&quot;hostIP&quot;);
String appPath = temp.getString(&quot;installPath&quot;);
String[] cmdArray = new String[]{&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;ssh &quot; + ip + &quot; \&quot;&quot; + cmd + &quot; | &quot; + appPath + &quot;/bin/hbase shell\&quot;&quot;};
process = Runtime.getRuntime().exec(cmdArray);</p><p>                InputStream is = process.getInputStream();</p><p>                InputStreamReader isr = new InputStreamReader(is, &quot;UTF-8&quot;);
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(&quot;home&quot;);
String value = &quot;&quot;;
try {</p><p>            Process process = null;</p><p>            String[] cmdArray = new String[]{&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;hadoop fs -du &quot; + home};
process = Runtime.getRuntime().exec(cmdArray);</p><p>            InputStream is = process.getInputStream();</p><p>            InputStreamReader isr = new InputStreamReader(is, &quot;UTF-8&quot;);
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(&quot;yyyyMMddHHmmssSSS&quot;);
String currTime = format.format(date);</p><p>      return currTime;
}
}
</p>
  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: 查看hadoop 是否运行