From 4daefb6d793c7579f17f597f253b8a139afc00cc Mon Sep 17 00:00:00 2001 From: lily Date: Tue, 13 Sep 2016 22:57:06 +0800 Subject: [PATCH] some fixed --- .gitignore | 1 + src/com/platform/glusterfs/ClusterInfo.java | 20 +- src/com/platform/glusterfs/CopyData.java | 26 +- src/com/platform/glusterfs/RemoveData.java | 60 +---- src/com/platform/glusterfs/SetVolume.java | 28 ++- src/com/platform/glusterfs/VolumeInfo.java | 254 ++++++++++++++------ src/com/platform/utils/Constant.java | 1 + 7 files changed, 243 insertions(+), 147 deletions(-) diff --git a/.gitignore b/.gitignore index bd558629..30c0ce06 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /build .classpath +/bin/ diff --git a/src/com/platform/glusterfs/ClusterInfo.java b/src/com/platform/glusterfs/ClusterInfo.java index 04923385..add5e191 100644 --- a/src/com/platform/glusterfs/ClusterInfo.java +++ b/src/com/platform/glusterfs/ClusterInfo.java @@ -17,10 +17,7 @@ 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 [相关类/方法] @@ -29,6 +26,14 @@ import com.platform.utils.Constant; public class ClusterInfo { public static Logger log = Logger.getLogger(ClusterInfo.class); + /** + * 获取集群节点信息 + * 如果获取不正常,则返回null,如果获取正常,返回map表示节点ip和ip的状态 + * 如果ip在集群中且联通状态为PeerinCluster(Connected) + * 如果ip在集群中且但不连通为PeerinCluster(Disconnected) + * @return + * @see [类、类#方法、类#成员] + */ public Map showClusterInfo() { log.info("get cluster info"); Map peerIps = new HashMap(); @@ -98,8 +103,12 @@ public class ClusterInfo { if(peerIps==null || peerIps.size()==0){ return null; } + + if(peerip.equals(Constant.hostIp)){ + return Constant.peerincluster_connected; + } if(!peerIps.containsKey(peerip)){ - return Constant.peerincluster_disconnected; + return Constant.peerNotinCluster; } return peerIps.get(peerip); } @@ -108,6 +117,7 @@ public class ClusterInfo { public static void main(String[] args) { // PropertyConfigurator.configure("log4j.properties"); System.out.println(new ClusterInfo().showClusterInfo()); + System.out.println(new ClusterInfo().getPeerStatus("192.168.0.116")); } } diff --git a/src/com/platform/glusterfs/CopyData.java b/src/com/platform/glusterfs/CopyData.java index e129fcb1..3af2dbea 100644 --- a/src/com/platform/glusterfs/CopyData.java +++ b/src/com/platform/glusterfs/CopyData.java @@ -55,7 +55,7 @@ public class CopyData { return -3; } - reStrings=showData.showFolderData(sourceFolderName+"/"+fileName); + reStrings=showData.showFolderData(sourceFolderName+"/"+fileName); if(reStrings==null){ log.info("3202 "+sourceFolderName+"/"+fileName+" is not exists"); return -2; @@ -71,11 +71,27 @@ public class CopyData { log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running"); return 1; } - - - @Test + + public int createFolders(String folder){ + log.info("create "+folder); + String splitFolder[]=folder.substring(1).split("/"); + String cmd="mkdir "; + for(String one:splitFolder){ + cmd+="/"+one.replaceAll(" ", ""); + Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); + } + return 1; + } + + @Test + public void testcreateFolders() { + + createFolders("/aaa/vvv/ddd/www/rrrr"); + } + + //@Test public void testCopyFolderFiles() { - PropertyConfigurator.configure("log4j.properties"); + copyFolderFiles("/home", "/home/ubuntu", "system_data"); } } diff --git a/src/com/platform/glusterfs/RemoveData.java b/src/com/platform/glusterfs/RemoveData.java index 2297ed03..6f854b26 100644 --- a/src/com/platform/glusterfs/RemoveData.java +++ b/src/com/platform/glusterfs/RemoveData.java @@ -16,17 +16,7 @@ import com.platform.utils.Constant; public class RemoveData { public static Logger log = Logger.getLogger ( RemoveData.class); - public int deleteVolumeFiles(String volumeName,String fileName){ - log.info("start delete "+volumeName+" "+fileName); - int status=-1; - /** - * get mount point of volumeName - */ - String folderName=volumeName; - - status=deleteFolderFiles(folderName,fileName); - return status; - } + /** * -1 :error; 0: the filename is not exists ; 1: right @@ -34,65 +24,33 @@ public class RemoveData { * @param fileName * @return */ - public int deleteFolderFiles(String folderName,String fileName){ - log.info("start delete "+folderName+"/"+fileName); + public int deleteFolder(String folderName){ + log.info("start delete "+folderName); ShowData showData=new ShowData(); - Map reStrings=showData.showFolderData(folderName+"/"+fileName); + Map reStrings=showData.showFolderData(folderName); if(reStrings==null){ - log.error("3301 "+folderName+"/"+fileName+" is not exists"); + log.error("3301 "+folderName+" is not exists"); return -1; } - String command="rm -r "+folderName+"/"+fileName; + String command="rm -r "+folderName; // int status=runCommand.runCommand(command); Constant.ganymedSSH.execCmdNoWaitAcquiescent(command); - log.info("delete "+folderName+" "+fileName+" running"); + log.info("delete "+folderName+" running"); return 1; } - public int getFolderSize(String name) { - log.info("get "+name+" size"); - String command="du -k -d 0 "+name; - /* - * RunCommand runCommand=new RunCommand(); - - List reStrings=runCommand.runCommandWait(command); - */ - List reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(command); - - if(reStrings==null){ - log.error("3302 the "+command+" return error"); - return -1; - } - if(reStrings.size()<1){ - log.error("3303 the "+command+" return error"); - return -1; - } - - if(reStrings.size()==1 && reStrings.get(0).contains("No such file or directory")){ - log.info("3304 "+name+" is not exists"); - return 0; - } - String strSize=(reStrings.get(0).split("\t"))[0]; - int size=Integer.parseInt(strSize); - log.info(name +" size is "+size); - return size; - } -// @Test - public void test_getFolderSize() { - PropertyConfigurator.configure("log4j.properties"); - getFolderSize("/home/ubuntu"); - } + @Test public void testDeleteFolderFiles() { PropertyConfigurator.configure("log4j.properties"); - deleteFolderFiles("/home/ubuntu","system_data"); + deleteFolder("/home/ubuntu"); } } diff --git a/src/com/platform/glusterfs/SetVolume.java b/src/com/platform/glusterfs/SetVolume.java index 392d7b22..5502aaee 100644 --- a/src/com/platform/glusterfs/SetVolume.java +++ b/src/com/platform/glusterfs/SetVolume.java @@ -132,9 +132,16 @@ public class SetVolume { return 1; } - /* + /** * 为指定的volume添加brick,参数中需要指定类型、数量等 返回值:1成功 ;其他失败 * 过程中需要先检查volume是否存在,还需检查给出的brick数量与类型、count是否相符 + * <功能详细描述> + * @param volumeName + * @param brickName + * @param count + * @param type + * @return + * @see [类、类#方法、类#成员] */ public int addBrickVolume(String volumeName, List brickName, int count, String type) { // 检查是否满足添加bricks的条件 @@ -169,9 +176,16 @@ public class SetVolume { } } - /* + + /** * 为指定的volume删除brick,参数中需要指定类型、数量等 返回值:1 成功 ;其他 失败 * 过程中需要先检查volume是否存在,还需检查给出的brick数量与类型、count是否相符 + * @param volumeName + * @param brickName + * @param count + * @param type + * @return + * @see [类、类#方法、类#成员] */ public int deleteBrickVolume(String volumeName, List brickName, int count, String type) { int able = isAble(volumeName, count, type, brickName); @@ -189,7 +203,7 @@ public class SetVolume { command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " " + brick + " force"; } else if (type.equals(Constant.replica)) { command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " repli " + count + " " + brick - + " force"; + + " force"; } else if (type.equals(Constant.stripe)) { command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " stripe " + count + " " + brick + " force"; @@ -453,14 +467,14 @@ public class SetVolume { public static void main(String[] args) { SetVolume setVolume = new SetVolume(); int operation = 0; - PropertyConfigurator.configure("log4j.properties"); +// PropertyConfigurator.configure("log4j.properties"); // TODO Auto-generated method stub // 测试创建volume的代码 List bricksToCreate = new ArrayList(); - bricksToCreate.add("192.168.0.110:/v1"); - bricksToCreate.add("192.168.0.116:/v1"); - operation = setVolume.createVolume("v1", 0, "distributed", bricksToCreate, "/home/v1_point"); + bricksToCreate.add("192.168.0.110:/v2"); + bricksToCreate.add("192.168.0.116:/v2"); + operation = setVolume.createVolume("v2", 0, "distributed", bricksToCreate, "/home/v2_point"); // operation = setVolume.deleteVolume("v3"); // // // 以下是测试添加brick的代码 diff --git a/src/com/platform/glusterfs/VolumeInfo.java b/src/com/platform/glusterfs/VolumeInfo.java index 8d592921..15e4cb14 100644 --- a/src/com/platform/glusterfs/VolumeInfo.java +++ b/src/com/platform/glusterfs/VolumeInfo.java @@ -19,12 +19,20 @@ import com.platform.utils.GanymedSSH; import java.util.regex.Matcher; import java.util.regex.Pattern; +/** + * 获取volume信息 <功能详细描述> + * + * @author liliy + * @version [版本号,2016年9月13日] + * @see [相关类/方法] + * @since [产品/模块版本] + */ public class VolumeInfo { public static Logger log = Logger.getLogger(VolumeInfo.class); /** - * 显示所有volume名称 - * <功能详细描述> + * 显示所有volume名称 <功能详细描述> + * * @return * @see [类、类#方法、类#成员] */ @@ -38,8 +46,8 @@ 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 + .execCmdWaitAcquiescent(Constant.glusterVolumeInfo + "|grep ^Volume.Name"); // System.out.println(reStrings); if (reStrings == null) { log.error("1401 get result is null"); @@ -67,10 +75,10 @@ public class VolumeInfo { return volNames; } - + /** - * 给定参数volume的名称获得volume的类型 - * <功能详细描述> + * 给定参数volume的名称获得volume的类型 <功能详细描述> + * * @param volumeName * @return * @see [类、类#方法、类#成员] @@ -79,8 +87,8 @@ public class VolumeInfo { log.info("get volume type"); String volType = ""; - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, - Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + volumeName + "|grep ^Type"); + List reStrings = Constant.ganymedSSH + .execCmdWaitAcquiescent(Constant.glusterVolumeInfo + volumeName + "|grep ^Type"); // System.out.println(reStrings); if (reStrings == null) { log.error("1501 get result is null"); @@ -106,11 +114,19 @@ public class VolumeInfo { return volType; } + /** + * 获取volumeName的状态 如果出错返回null,如果volumeName不存在则返回Volume volumeName does not + * exist,正常返回状态Started,Stopped,Created + * + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ public String getVolumeStatus(String volumeName) { log.info("get volume status"); String volStatus = ""; - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, - Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + "|grep ^Status"); + String cmd = Constant.glusterVolumeInfo + " " + volumeName + " |grep ^Status"; + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); // System.out.println(reStrings); if (reStrings == null) { log.error("1701 get result is null"); @@ -120,11 +136,15 @@ public class VolumeInfo { log.error("1702 get result is nothing"); return null; } + + if (reStrings.get(0).contains("does not exist")) { + log.error("1703 " + reStrings.get(0)); + return reStrings.get(0); + } if (!(reStrings.get(0).split(":")[0].contains("Status"))) { - log.error("1703 get result string wrong"); + log.error("1704 get result string wrong"); return null; } - for (Iterator it = reStrings.iterator(); it.hasNext();) { String line = (String) it.next(); String str[] = line.split(":"); @@ -134,67 +154,70 @@ public class VolumeInfo { return volStatus; } - public Double getVolumeAllSize(String volumeName) { - log.info("get volume allSize"); - Double allSize = null; - - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, - Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $2}'"); + /** + * 获取volumeName的可用大小 + * volumeName不存在返回-1,获取大小错误返回-2 ,正常返回volumeName的可用大小 + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ + public Long getVolumeAvailableSize(String volumeName) { + log.info("get volume availableSize"); + Long allSize = 0L; + + String cmd = Constant.df + " | grep " + volumeName + "|awk '{print $4}'"; + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); // System.out.println(reStrings); if (reStrings == null) { - log.error("1801 get result is null"); - return null; + log.error("1802 get result is error"); + return -2L; } if (reStrings.size() == 0) { - log.error("1802 get result is nothing"); - return null; + log.error("1801 " + volumeName + " is not exists!"); + return -1L; } - /* - * 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]"); - allSize = Double.parseDouble(str[0]); - } - + allSize = Long.parseLong(reStrings.get(0)); return allSize; } - public Double getVolumeUseSize(String volumeName) { + /** + * 获取volumeName已用空间 + * volumeName不存在返回-1,获取大小错误返回-2 ,正常返回volumeName的已用的大小 + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ + public Long getVolumeUseSize(String volumeName) { log.info("get volume used size"); - Double usedSize = null; + Long usedSize = 0L; + if (volumeIsExists(volumeName) == false) { + log.error("1901 " + volumeName + " is not exists!"); + return -1L; - - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, - Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $3}'"); + } + + String cmd = "df | grep " + volumeName + "|awk '{print $3}'"; + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); // System.out.println(reStrings); if (reStrings == null) { log.error("1901 get result is null"); - return null; + return -2L; } if (reStrings.size() == 0) { - log.error("1902 get result is nothing"); - return null; + log.error("1902 " + volumeName + " is not exists!"); + return -1L; } - char flag = reStrings.get(0).trim().toCharArray()[0]; - if (flag < 48 || flag > 57) { - log.error("1903 get result string wrong"); - return null; - } - - for (Iterator it = reStrings.iterator(); it.hasNext();) { - String line = (String) it.next(); - String str[] = line.split("[^0-9]"); - - usedSize = Double.parseDouble(str[0]); - } - + usedSize = Long.parseLong(reStrings.get(0)); return usedSize; } + /** + * 获取volumeName的bricks + * 返回一个bircks的list ip:path,如果volumeName不存在返回null + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ public List getVolumeBricks(String volumeName) { log.info("get volume bricks"); @@ -205,38 +228,81 @@ public class VolumeInfo { log.error("1601 get volume bricks wrong"); return null; } - + if (reStrings.size()==0) { + log.error("1602 "+volumeName+" is not exists!"); + return null; + } return reStrings; } + /** + * 获取volumeName所有挂载点 + * <功能详细描述> + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ public List getVolumeMountPoint(String volumeName) { log.info("get volume MountPoint"); List mountPoints = new ArrayList<>(); - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $6}'"); + String cmd=Constant.df + "|grep " + volumeName + "|awk '{print $6}'"; + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); // System.out.println(reStrings); - if(reStrings==null){ + if (reStrings == null) { log.error("11001 get result string wrong"); return null; } - if(reStrings.size()==0){ - log.error("11002 "+volumeName+" is no mountpoint"); - return mountPoints; + if (reStrings.size() == 0) { + log.error("11002 " + volumeName + " is not exists or no mountpoint"); + return null; } + +// char flag = reStrings.get(0).trim().toCharArray()[0]; +// if (flag != '/') { +// log.error("11003 get result string wrong"); +// return null; +// } + + for (String mountPoint : reStrings) { + mountPoint = mountPoint.replaceAll(" ", ""); + mountPoints.add(mountPoint); + } + return mountPoints; + } + + public String getOneVolumeMountPoint(String volumeName) { + log.info("get one volume MountPoint"); - char flag = reStrings.get(0).trim().toCharArray()[0]; - if (flag != '/') { - log.error("11003 get result string wrong"); + String mountPoint=null; + String cmd=Constant.df + "|grep " + volumeName + "|awk '{print $6}'"; + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); + // System.out.println(reStrings); + if (reStrings == null) { + log.error("11001 get result string wrong"); return null; } - - for(String mountPoint:reStrings){ - mountPoint=mountPoint.replaceAll(" ", ""); - mountPoints.add(mountPoint); + if (reStrings.size() == 0) { + log.error("11002 " + volumeName + " is not exists or no mountpoint"); + return null; } - return mountPoints; + +// char flag = reStrings.get(0).trim().toCharArray()[0]; +// if (flag != '/') { +// log.error("11003 get result string wrong"); +// return null; +// } + + mountPoint=reStrings.get(0); + return mountPoint; } + /** + * 获取volumeName的所有brick中数据占用空间的大小 + * 返回一个map表示bricks和数据大小 + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ public Map getVolumebricksDataSize(String volumeName) { List bricks = getVolumeBricks(volumeName); Map brick_size = new HashMap<>(); @@ -256,7 +322,7 @@ public class VolumeInfo { return null; } if (reStrings.size() == 0) { - log.error("1902 get result is nothing"); + log.error("1902 "+brick+" is not exits!"); return null; } Pattern pattern = Pattern.compile("[0-9]*"); @@ -270,6 +336,13 @@ public class VolumeInfo { return brick_size; } + /** + * 获取volumeName的所有brick中可用空间大小 + * 返回一个map表示bricks和可用空间大小 + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ public Map getVolumebricksAvailableSize(String volumeName) { List bricks = getVolumeBricks(volumeName); Map brick_size = new HashMap<>(); @@ -303,12 +376,30 @@ public class VolumeInfo { return brick_size; } - //@Test + /** + * 判断volumeName是否存在,存在返回true,不存在返回false <功能详细描述> + * + * @param volumeName + * @return + * @see [类、类#方法、类#成员] + */ + public boolean volumeIsExists(String volumeName) { + List volumes = showAllVolumeName(); + if (volumes == null) { + return false; + } + if (volumes.contains(volumeName)) { + return true; + } + return false; + } + + // @Test public void test_getVolumebricksDataSize() { System.out.println(getVolumebricksDataSize("gfs_ftp")); } - //@Test + // @Test public void test_getVolumebricksAvailableSize() { System.out.println(getVolumebricksAvailableSize("gfs_ftp")); } @@ -317,10 +408,15 @@ public class VolumeInfo { public void test_getVolumeBricks() { getVolumeBricks("gfs_ftp"); } - - @Test - public void test_getVolumeMountPoint() { - System.out.println(getVolumeMountPoint("gfs_ftp")); - System.out.println(getVolumeMountPoint("v1")); - } + + @Test + public void test_getVolumeStatus() { + System.out.println(getVolumeStatus("gs_fp")); + } + + // @Test + public void test_getVolumeMountPoint() { + System.out.println(getVolumeMountPoint("gfs_ftp")); + System.out.println(getVolumeMountPoint("v1")); + } } diff --git a/src/com/platform/utils/Constant.java b/src/com/platform/utils/Constant.java index a80816c9..b1d439f6 100644 --- a/src/com/platform/utils/Constant.java +++ b/src/com/platform/utils/Constant.java @@ -14,6 +14,7 @@ public class Constant { public static String df = "df -k "; public static String peerincluster_connected = "PeerinCluster(Connected)"; public static String peerincluster_disconnected = "PeerinCluster(Disconnected)"; + public static String peerNotinCluster = "PeerNotinCluster"; public static String distributed = "distributed"; public static String replica = "replica"; public static String stripe = "stripe";