查看hadoop 是否运行

执行工具类 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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值