diff --git a/WebContent/WEB-INF/config/getTreedata.sh b/WebContent/WEB-INF/config/getTreedata.sh new file mode 100644 index 00000000..d486a449 --- /dev/null +++ b/WebContent/WEB-INF/config/getTreedata.sh @@ -0,0 +1,17 @@ +#!/bin/bash +function ergodic(){ + for file in `ls $1` + do + if [ "$file" != "app" -a -d $1"/"$file ] + then + ergodic $1"/"$file + else + local path=$1"/"$file + echo $path + fi + done +} + +IFS=$'\n' #这个必须要,否则会在文件名中有空格时出错 +INIT_PATH="."; +ergodic $1 \ No newline at end of file diff --git a/WebContent/WEB-INF/config/log4j-config.xml b/WebContent/WEB-INF/config/log4j-config.xml index 0a2755c5..d02e5918 100644 --- a/WebContent/WEB-INF/config/log4j-config.xml +++ b/WebContent/WEB-INF/config/log4j-config.xml @@ -14,7 +14,7 @@ - + @@ -23,7 +23,7 @@ - + diff --git a/src/com/platform/glusterfs/ClusterInfo.java b/src/com/platform/glusterfs/ClusterInfo.java index c23145e2..fdf94b55 100644 --- a/src/com/platform/glusterfs/ClusterInfo.java +++ b/src/com/platform/glusterfs/ClusterInfo.java @@ -14,6 +14,16 @@ import org.apache.log4j.PropertyConfigurator; import com.platform.utils.Constant; +/** + * 获取集群节点信息 + * 如果获取不正常,则返回null,如果获取正常,返回map表示节点ip和ip的状态 + * 如果ip在集群中且联通状态为PeerinCluster(Connected) + * 如果ip在集群中且但不连通为PeerinCluster(Disconnected) + * @author liliy + * @version [版本号,2016年9月12日] + * @see [相关类/方法] + * @since [产品/模块版本] + */ public class ClusterInfo { public static Logger log = Logger.getLogger(ClusterInfo.class); @@ -21,11 +31,6 @@ public class ClusterInfo { log.info("get cluster info"); Map peerIps = new HashMap(); - /* - String command = "echo \"" + Constant.rootPasswd + "\"|sudo -S gluster peer status"; - RunCommand runCommand = new RunCommand(); - List reStrings = runCommand.runCommandWait(command); - */ List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, Constant.port, Constant.glusterPeerStatus); if (reStrings == null) { log.error("1101 command get result is null"); @@ -76,7 +81,26 @@ public class ClusterInfo { } return peerIps; } - + + /** + * 根据给定的ip获的ip的状态,即是否在集群中并联通 + * 如果ip不在集群中,返回null + * 如果ip在集群中且联通状态为PeerinCluster(Connected) + * 如果ip在集群中且但不连通为PeerinCluster(Disconnected) + * @param peerip + * @return + * @see [类、类#方法、类#成员] + */ + public String getPeerStatus(String peerip){ + Map peerIps=showClusterInfo(); + if(peerIps==null || peerIps.size()==0){ + return null; + } + if(!peerIps.containsKey(peerip)){ + return Constant.peerincluster_disconnected; + } + return peerIps.get(peerip); + } public static void main(String[] args) { diff --git a/src/com/platform/glusterfs/GetTreeData.java b/src/com/platform/glusterfs/GetTreeData.java index 48b2ff05..2e391ff4 100644 --- a/src/com/platform/glusterfs/GetTreeData.java +++ b/src/com/platform/glusterfs/GetTreeData.java @@ -6,8 +6,10 @@ import java.util.List; import java.util.Map; import org.junit.Test; - import com.platform.entities.FolderNode; +import com.platform.utils.Constant; +import com.platform.utils.FileOperateHelper; +import com.platform.utils.GanymedSSH; /** * <一句话功能简述> 获得GFS某个目录下的子目录 @@ -31,7 +33,7 @@ public class GetTreeData { String names[]=name.split("/"); String only_name=names[names.length-1]; FolderNode fileNode = new FolderNode(only_name); - + fileNode.setPath(name); Map files = showData.showFolderData(name); if(files==null || files.size()==0){ return fileNode; @@ -56,6 +58,41 @@ public class GetTreeData { return fileNode; } + + /** + * <一句话功能简述> 获得所以子目录 + * <功能详细描述> + * @param name + * @return + * @see [类、类#方法、类#成员] + */ + public FolderNode getDatasWithShell(String name) { + if(name.endsWith("/")){ + name=name.substring(0, name.length()); + } + String names[]=name.split("/"); + String only_name=names[names.length-1]; + FolderNode fileNode = new FolderNode(name); + fileNode.setPath(name); + + String shellComment=new FileOperateHelper().fileReaderAndendline(Constant.fileGetTreeData); + String sh_path="/getTreedata.sh"; + String cmd="echo -e "+shellComment+" > "+sh_path+" & chmod +x "+sh_path; + Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); +// Map files = showData.showFolderData(name); + List files=Constant.ganymedSSH.execCmdWaitAcquiescent(sh_path+" "+name); + if(files==null){ + return null; + } + if(files.size()==0){ + return fileNode; + } + for(String file:files){ + + } + + return fileNode; + } @Test public void test_getTreeData() { diff --git a/src/com/platform/glusterfs/SetCluster.java b/src/com/platform/glusterfs/SetCluster.java index ccc3a76f..f0d607db 100644 --- a/src/com/platform/glusterfs/SetCluster.java +++ b/src/com/platform/glusterfs/SetCluster.java @@ -1,5 +1,11 @@ package com.platform.glusterfs; -public class SetCluster { +import org.apache.log4j.Logger; +public class SetCluster { + public static Logger log = Logger.getLogger ( SetCluster.class); + + public int addPeer(String peerip){ + return 0; + } } diff --git a/src/com/platform/glusterfs/ShowData.java b/src/com/platform/glusterfs/ShowData.java index 919f7c7a..5996467b 100644 --- a/src/com/platform/glusterfs/ShowData.java +++ b/src/com/platform/glusterfs/ShowData.java @@ -17,13 +17,14 @@ import com.platform.utils.Constant; public class ShowData { public static Logger log = Logger.getLogger ( ShowData.class); + /** - * get the data of volumeName Map s1 is data name and s2 is type file or folder + * get the data of volumeName Map s1 is data name and s2 is type file or folder + * <功能详细描述> * @param volumeName * @return + * @see [类、类#方法、类#成员] */ - - public Map showVolumeFiles(String volumeName){ log.info("start show the data"); Map data_type=new HashMap(); diff --git a/src/com/platform/glusterfs/VolumeInfo.java b/src/com/platform/glusterfs/VolumeInfo.java index 72b1b712..66205970 100644 --- a/src/com/platform/glusterfs/VolumeInfo.java +++ b/src/com/platform/glusterfs/VolumeInfo.java @@ -1,7 +1,5 @@ package com.platform.glusterfs; - - import java.io.File; import java.util.ArrayList; import java.util.HashMap; @@ -11,8 +9,11 @@ import java.util.Map; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; +import org.junit.Test; +import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper; import com.platform.utils.Constant; +import com.platform.utils.GanymedSSH; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -20,6 +21,12 @@ import java.util.regex.Pattern; public class VolumeInfo { public static Logger log = Logger.getLogger(VolumeInfo.class); + /** + * 显示所有volume名称 + * <功能详细描述> + * @return + * @see [类、类#方法、类#成员] + */ public List showAllVolumeName() { log.info("get volume name"); List volNames = new ArrayList(); @@ -30,18 +37,18 @@ public class VolumeInfo { * runCommand = new RunCommand(); List reStrings = * runCommand.runCommandWait(command); */ - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, Constant.glusterVolumeInfo + "|grep ^Volume.Name"); + List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, + Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + "|grep ^Volume.Name"); // System.out.println(reStrings); - if(reStrings==null){ + if (reStrings == null) { log.error("1401 get result is null"); return null; } - if(reStrings.size()==0){ + if (reStrings.size() == 0) { log.error("1402 get result is nothing"); return null; } - if(reStrings.get(0).contains(Constant.noVolume)){ + if (reStrings.get(0).contains(Constant.noVolume)) { reStrings.clear(); return reStrings; } @@ -59,28 +66,26 @@ public class VolumeInfo { return volNames; } - + + /** + * 给定参数volume的名称获得volume的类型 + * <功能详细描述> + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ public String getVolumeType(String volumeName) { log.info("get volume type"); String volType = ""; - /* - * ======= - * - * >>>>>>> origin/AlexKie String command = "echo \"" + - * Constant.rootPasswd + "\" |sudo -S gluster volume info " + - * volumeName + "|grep ^Type"; RunCommand runCommand = new RunCommand(); - * List reStrings = runCommand.runCommandWait(command); <<<<<<< - * HEAD - */ - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, Constant.glusterVolumeInfo + volumeName + "|grep ^Type"); + List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, + Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + volumeName + "|grep ^Type"); // System.out.println(reStrings); - if(reStrings==null){ + if (reStrings == null) { log.error("1501 get result is null"); return null; } - if(reStrings.size()==0){ + if (reStrings.size() == 0) { log.error("1502 get result is nothing"); return null; } @@ -96,31 +101,21 @@ public class VolumeInfo { String str[] = line.split(":"); volType = str[1]; } - volType=volType.replaceAll(" ", ""); + volType = volType.replaceAll(" ", ""); return volType; } public String getVolumeStatus(String volumeName) { log.info("get volume status"); String volStatus = ""; - - /* - * ======= - * - * >>>>>>> origin/AlexKie String command = "echo \"" + - * Constant.rootPasswd + "\" |sudo -S gluster volume info " + - * volumeName + "|grep ^Status"; RunCommand runCommand = new - * RunCommand(); List reStrings = - * runCommand.runCommandWait(command); <<<<<<< HEAD - */ - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, Constant.glusterVolumeInfo + "|grep ^Status"); + List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, + Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + "|grep ^Status"); // System.out.println(reStrings); - if(reStrings==null){ + if (reStrings == null) { log.error("1701 get result is null"); return null; } - if(reStrings.size()==0){ + if (reStrings.size() == 0) { log.error("1702 get result is nothing"); return null; } @@ -134,41 +129,30 @@ public class VolumeInfo { String str[] = line.split(":"); volStatus = str[1].replaceAll(" ", ""); } - + return volStatus; } public Double getVolumeAllSize(String volumeName) { log.info("get volume allSize"); Double allSize = null; - /* - * ======= // waiting for testing... public Double - * getVolumeAllSize(String volumeName) { log.info("get volume allSize"); - * Double allSize = null; - * - * >>>>>>> origin/AlexKie String command = "echo \"" + - * Constant.rootPasswd + "\" |sudo -S df -h|grep " + volumeName + - * "|awk '{print $2}'"; RunCommand runCommand = new RunCommand(); - * List reStrings = runCommand.runCommandWait(command); <<<<<<< - * HEAD - */ - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $2}'"); + + List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, + Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $2}'"); // System.out.println(reStrings); - if(reStrings==null){ + if (reStrings == null) { log.error("1801 get result is null"); return null; } - if(reStrings.size()==0){ + if (reStrings.size() == 0) { log.error("1802 get result is nothing"); return null; } - char flag = reStrings.get(0).trim().toCharArray()[0]; - if (flag < 48 || flag > 57) { - log.error("1803 get result string wrong"); - return null; - } - + /* + * char flag = reStrings.get(0).trim().toCharArray()[0]; if (flag < 48 + * || flag > 57) { log.error("1803 get result string wrong"); return + * null; } + */ for (Iterator it = reStrings.iterator(); it.hasNext();) { String line = (String) it.next(); String str[] = line.split("[^0-9]"); @@ -182,23 +166,15 @@ public class VolumeInfo { log.info("get volume used size"); Double usedSize = null; - /* - * ======= - * - * >>>>>>> origin/AlexKie String command = "echo \"" + - * Constant.rootPasswd + "\" |sudo -S df -h|grep " + volumeName + - * "|awk '{print $3}'"; RunCommand runCommand = new RunCommand(); - * List reStrings = runCommand.runCommandWait(command); <<<<<<< - * HEAD - */ - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $3}'"); + + List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, + Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $3}'"); // System.out.println(reStrings); - if(reStrings==null){ + if (reStrings == null) { log.error("1901 get result is null"); return null; } - if(reStrings.size()==0){ + if (reStrings.size() == 0) { log.error("1902 get result is nothing"); return null; } @@ -218,44 +194,23 @@ public class VolumeInfo { return usedSize; } - - public List getVolumeMountPoint(String volumeName) { - log.info("get volume mountPoint"); - // String mountPoint = ""; + public List getVolumeBricks(String volumeName) { + log.info("get volume bricks"); - /* - * ======= - * - * >>>>>>> origin/AlexKie String command = "echo \"" + - * Constant.rootPasswd + "\" |sudo -S df -h|grep " + volumeName + - * "|awk '{print $6}'"; RunCommand runCommand = new RunCommand(); - * List reStrings = runCommand.runCommandWait(command); <<<<<<< - * HEAD - */ - String cmd="gluster volume info "+volumeName+" |grep ^Brick'[0-9]\\+' |awk '{print $2}'"; - List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(volumeName); + String cmd = "gluster volume info " + volumeName + " |grep ^Brick'[0-9]\\+' |awk '{print $2}'"; + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); // System.out.println(reStrings); - if(reStrings==null){ - log.error("1601 get result string wrong"); + if (reStrings == null) { + log.error("1601 get volume bricks wrong"); return null; } - + return reStrings; } - public String getVolumeBricks(String volumeName) { - log.info("get volume bricks"); - // String mountPoint = ""; - - /* - * ======= - * - * >>>>>>> origin/AlexKie String command = "echo \"" + - * Constant.rootPasswd + "\" |sudo -S df -h|grep " + volumeName + - * "|awk '{print $6}'"; RunCommand runCommand = new RunCommand(); - * List reStrings = runCommand.runCommandWait(command); <<<<<<< - * HEAD - */ + public List getVolumeMountPoint(String volumeName) { + log.info("get volume MountPoint"); + List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $6}'"); // System.out.println(reStrings); @@ -273,24 +228,92 @@ public class VolumeInfo { log.error("11003 get result string wrong"); return null; } - - Iterator it = reStrings.iterator(); - String mountPoint = (String) it.next(); + List mountPoints = new ArrayList<>(); + for(String mountPoint:reStrings){ mountPoint=mountPoint.replaceAll(" ", ""); - return mountPoint; + mountPoints.add(mountPoint); + } + return mountPoints; + } + + public Map getVolumebricksDataSize(String volumeName) { + List bricks = getVolumeBricks(volumeName); + Map brick_size = new HashMap<>(); + if (bricks == null) { + return null; + } + for (String brick : bricks) { + String ipAndpath[] = brick.split(":"); + String ip = ipAndpath[0]; + String path = ipAndpath[1]; + String cmd = "du -d 0 " + path + "|awk '{print $1}'"; + List reStrings = Constant.ganymedSSH.execCmdWait(ip, Constant.rootUser, Constant.rootPasswd, + Constant.port, cmd); + // System.out.println(reStrings); + if (reStrings == null) { + log.error("1901 get result is null"); + return null; + } + if (reStrings.size() == 0) { + log.error("1902 get result is nothing"); + return null; + } + Pattern pattern = Pattern.compile("[0-9]*"); + Matcher isNum = pattern.matcher(reStrings.get(0)); + if (!isNum.matches()) { + log.error("1903 " + reStrings.get(0) + " is unexpect"); + return null; + } + brick_size.put(brick, Double.parseDouble(reStrings.get(0))); + } + return brick_size; + } + + public Map getVolumebricksAvailableSize(String volumeName) { + List bricks = getVolumeBricks(volumeName); + Map brick_size = new HashMap<>(); + if (bricks == null) { + return null; + } + for (String brick : bricks) { + String ipAndpath[] = brick.split(":"); + String ip = ipAndpath[0]; + String path = ipAndpath[1]; + String cmd = "df " + path + "|awk '{print $4}'"; + List reStrings = Constant.ganymedSSH.execCmdWait(ip, Constant.rootUser, Constant.rootPasswd, + Constant.port, cmd); + // System.out.println(reStrings); + if (reStrings == null) { + log.error("1901 get result is null"); + return null; + } + if (reStrings.size() == 0) { + log.error("1902 get result is nothing"); + return null; + } + Pattern pattern = Pattern.compile("[0-9]*"); + Matcher isNum = pattern.matcher(reStrings.get(1)); + if (!isNum.matches()) { + log.error("1903 " + reStrings.get(1) + " is unexpect"); + return null; + } + brick_size.put(brick, Double.parseDouble(reStrings.get(1))); + } + return brick_size; + } + + @Test + public void test_getVolumebricksDataSize() { + System.out.println(getVolumebricksDataSize("gfs_ftp")); + } + + @Test + public void test_getVolumebricksAvailableSize() { + System.out.println(getVolumebricksAvailableSize("gfs_ftp")); } - - public static void main(String[] args) { - PropertyConfigurator.configure("log4j.properties"); - - System.out.println(new VolumeInfo().showAllVolumeName()); - System.out.println(new VolumeInfo().getVolumeType("v1")); - - System.out.println(new VolumeInfo().getVolumeStatus("v1")); - System.out.println(new VolumeInfo().getVolumeMountPoint("v1")); - - System.out.println(new VolumeInfo().getVolumeAllSize("v1")); - System.out.println(new VolumeInfo().getVolumeUseSize("v1")); + // @Test + public void test_getVolumeBricks() { + getVolumeBricks("gfs_ftp"); } } diff --git a/src/com/platform/utils/Constant.java b/src/com/platform/utils/Constant.java index 0a72c14d..fdfee0d0 100644 --- a/src/com/platform/utils/Constant.java +++ b/src/com/platform/utils/Constant.java @@ -12,9 +12,11 @@ public class Constant { public static String glusterVolumeInfo = "gluster volume info "; public static String df = "df -k "; public static String peerincluster_connected="PeerinCluster(Connected)"; + public static String peerincluster_disconnected="PeerinCluster(Disconnected)"; public static String distributed="distributed"; public static String replica="replica"; public static String stripe="stripe"; public static String noVolume="No volumes present"; public static GanymedSSH ganymedSSH=new GanymedSSH(hostIp, rootUser, rootPasswd, port); + public static String fileGetTreeData="./WEB-INF/config/getTreedata.sh"; } diff --git a/src/com/platform/utils/FileOperateHelper.java b/src/com/platform/utils/FileOperateHelper.java index b2b9fa9b..d2eed1b5 100644 --- a/src/com/platform/utils/FileOperateHelper.java +++ b/src/com/platform/utils/FileOperateHelper.java @@ -59,4 +59,28 @@ public class FileOperateHelper { } return sb.toString(); } + + /** + * 文件读取方法 + * @param path + * @return + */ + @SuppressWarnings("resource") + public static String fileReaderAndendline(String path) { + StringBuffer sb = new StringBuffer(); + String tempString = ""; + try { + File file = new File(path); + if (!file.exists()) + return ""; + FileInputStream fis = new FileInputStream(file); + BufferedReader br = new BufferedReader(new InputStreamReader(fis)); + while ((tempString = br.readLine()) != null) { + sb.append(tempString+"\n"); + } + } catch (Exception e) { + // TODO: handle exception + } + return sb.toString(); + } } diff --git a/src/com/platform/utils/GanymedSSH.java b/src/com/platform/utils/GanymedSSH.java index 47a3e605..5db5d2a3 100644 --- a/src/com/platform/utils/GanymedSSH.java +++ b/src/com/platform/utils/GanymedSSH.java @@ -62,7 +62,7 @@ public class GanymedSSH { Session sess = null; try { - conn = getOpenedConnection(host, username, password, port); + sess = conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd);