This commit is contained in:
lili 2016-09-08 14:54:39 +08:00
parent 6e68a67686
commit 355e6a9b78
28 changed files with 1908 additions and 4 deletions

View File

@ -9,6 +9,11 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="build/classes"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target/

View File

@ -25,8 +25,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>

View File

@ -1,2 +1,5 @@
eclipse.preferences.version=1
encoding//WebContent/WEB-INF/config/config.properties=UTF-8
encoding//src/com/platform/utils/GanymedSSH.java=UTF-8
encoding/<project>=UTF-8
separateDerivedEncodings=true

View File

@ -1,7 +1,101 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
org.eclipse.jdt.core.compiler.problem.deadCode=warning
org.eclipse.jdt.core.compiler.problem.deprecation=warning
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore
org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedImport=warning
org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.7

View File

@ -0,0 +1,3 @@
eclipse.preferences.version=1
org.eclipse.jpt.core.platform=generic2_1
org.eclipse.jpt.jpa.core.discoverAnnotatedClasses=false

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

View File

@ -0,0 +1,7 @@
<root>
<facet id="jpt.jpa">
<node name="libprov">
<attribute name="provider-id" value="jpa-no-op-library-provider"/>
</node>
</facet>
</root>

View File

@ -7,4 +7,5 @@
<installed facet="java" version="1.7"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="jpt.jpa" version="2.1"/>
</faceted-project>

37
pom.xml Normal file
View File

@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>extendGlusterfs</groupId>
<artifactId>extendGlusterfs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>extendGlusterfs</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build209</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="aggregation-platform">
</persistence-unit>
</persistence>

View File

@ -0,0 +1,45 @@
/**
* 文件名 : SetGlusterfs.java
* 版权 : <版权/公司名>
* 描述 : <描述>
* @author liliy
* 版本 : <版本>
* 修改时间 2016年9月8日
* 修改内容 <修改内容>
*/
package com.platform.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* <一句话功能简述>
* <功能详细描述>
* @author liliy
* @version [版本号2016年9月8日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Controller
public class SetGlusterfsController {
@RequestMapping("/tes")
public Object defaultHandler(HttpServletRequest req, HttpServletResponse request) {
//<EFBFBD><EFBFBD><EFBFBD>?ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
try {
System.out.println("fsdfds");
return "listAll";
} catch (Exception e) {
e.printStackTrace();
return "result";
}
}
}

View File

@ -0,0 +1,44 @@
/**
* 文件名 : ShowGlusterfsController.java
* 版权 : <版权/公司名>
* 描述 : <描述>
* @author liliy
* 版本 : <版本>
* 修改时间 2016年9月8日
* 修改内容 <修改内容>
*/
package com.platform.controller;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.platform.glusterfs.ClusterInfo;
/**
* <一句话功能简述>
* <功能详细描述>
* @author liliy
* @version [版本号2016年9月8日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Controller
public class ShowGlusterfsController {
/**
* <一句话功能简述>
* <功能详细描述>
* @return
* @see [#方法#成员]
*/
@RequestMapping("/showPeerStatus")
public Map<String,String> showPeerStatus(){
Map<String, String> peer_status=new ClusterInfo().showClusterInfo();
return peer_status;
}
}

View File

@ -0,0 +1,86 @@
package com.platform.glusterfs;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.platform.utils.Constant;
public class ClusterInfo {
public static Logger log = Logger.getLogger(ClusterInfo.class);
public Map<String, String> showClusterInfo() {
log.info("get cluster info");
Map<String, String> peerIps = new HashMap<String, String>();
/*
String command = "echo \"" + Constant.rootPasswd + "\"|sudo -S gluster peer status";
RunCommand runCommand = new RunCommand();
List<String> reStrings = runCommand.runCommandWait(command);
*/
List<String> 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");
return null;
}
if (reStrings.size() == 0) {
log.error("1102 command get result is nothing");
return null;
}
if (!(reStrings.get(0).split(":")[0].contains("Number of Peers"))) {
log.error("1103 get result string wrong");
return null;
}
// System.out.print(reStrings.get(0));
int flag = 0;
String ipString = "";
String state = "";
for (Iterator it2 = reStrings.iterator(); it2.hasNext();) {
String line = (String) it2.next();
line=line.replaceAll(" +", " ");
String keyValue[] = line.split(":");
if (keyValue[0].equals("Hostname")) {
if (keyValue.length < 2) {
log.error("1105 command get result is wrong");
continue;
}
ipString = keyValue[1].replaceAll(" ", "");
flag = 1;
} else if (flag == 1 && keyValue[0].equals("State")) {
if (keyValue.length < 2) {
log.error("1106 command get result is wrong");
continue;
}
state = keyValue[1].replaceAll(" ", "");
flag = 0;
peerIps.put(ipString, state);
}
}
return peerIps;
}
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
System.out.println(new ClusterInfo().showClusterInfo());
}
}

View File

@ -0,0 +1,71 @@
package com.platform.glusterfs;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
import com.platform.utils.Constant;
public class CopyData {
public static Logger log = Logger.getLogger(CopyData.class);
public int copyVolumeFiles(String sourceVolumeName, String destVolumeName, String fileName) {
log.info("start copy " + fileName + " from " + sourceVolumeName + " to " + destVolumeName);
int status = -1;
/**
* get mount point of volumeName
*/
String sourceFolderName = sourceVolumeName;
String destFolderName = destVolumeName;
status = copyFolderFiles(sourceFolderName, destFolderName, fileName);
return status;
}
/**
* -1 :error; -2: the filename is not exists ;-3 :destFolderName ; 1: right
* not exists
*
* @param folderName
* @param fileName
* @return
*/
public int copyFolderFiles(String sourceFolderName, String destFolderName, String fileName) {
int progress=0;
log.info("start copy " + fileName + " from " + sourceFolderName + " to " + destFolderName);
ShowData showData=new ShowData();
Map<String,String> reStrings=showData.showFolderData(destFolderName);
if(reStrings==null){
log.info("3201 "+destFolderName+" is not exists");
return -3;
}
reStrings=showData.showFolderData(sourceFolderName+"/"+fileName);
if(reStrings==null){
log.info("3202 "+sourceFolderName+"/"+fileName+" is not exists");
return -2;
}
String command = "cp -r " + sourceFolderName + "/" + fileName+" "+destFolderName;
/*
* RunCommand runCommand = new RunCommand();
List<String> reStrings = runCommand.runCommandWait(command);
*/
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running");
return 1;
}
@Test
public void testCopyFolderFiles() {
PropertyConfigurator.configure("log4j.properties");
copyFolderFiles("/home", "/home/ubuntu", "system_data");
}
}

View File

@ -0,0 +1,62 @@
package com.platform.glusterfs;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
public class GetTreeData {
ShowData showData = new ShowData();
public GetTreeData() {
// TODO Auto-generated constructor stub
}
public FileOrFolder getDatas(String name) {
FileOrFolder fileOrFolder = new FileOrFolder(name);
Map<String, String> files = showData.showFolderData(name);
if(files==null || files.size()==0){
return fileOrFolder;
}
fileOrFolder.isFolder = files.size();
fileOrFolder.children = new ArrayList<FileOrFolder>();
for (Map.Entry<String, String> entry : files.entrySet()) {
int number = Integer.parseInt(entry.getValue());
if (number == 1) {
fileOrFolder.children.add(new FileOrFolder(entry.getKey(), number));
}
if (number > 1) {
FileOrFolder temp=getDatas(name+"/"+entry.getKey());
fileOrFolder.children.add(temp);
}
}
return fileOrFolder;
}
public static void main(String[] args) {
GetTreeData getTreeData=new GetTreeData();
FileOrFolder fileOrFolder=getTreeData.getDatas("/home");
System.out.println(fileOrFolder);
}
}
class FileOrFolder {
String name;
int isFolder; // 1 is file and other integer is folder show children number
List<FileOrFolder> children;
public FileOrFolder(String name) {
// TODO Auto-generated constructor stub
this.name = name;
}
public FileOrFolder(String name, int isFolder) {
// TODO Auto-generated constructor stub
this.name = name;
this.isFolder = isFolder;
}
}

View File

@ -0,0 +1,98 @@
package com.platform.glusterfs;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
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
* @param folderName
* @param fileName
* @return
*/
public int deleteFolderFiles(String folderName,String fileName){
log.info("start delete "+folderName+"/"+fileName);
ShowData showData=new ShowData();
Map<String,String> reStrings=showData.showFolderData(folderName+"/"+fileName);
if(reStrings==null){
log.error("3301 "+folderName+"/"+fileName+" is not exists");
return -1;
}
String command="rm -r "+folderName+"/"+fileName;
// int status=runCommand.runCommand(command);
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("delete "+folderName+" "+fileName+" 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<String> reStrings=runCommand.runCommandWait(command);
*/
List<String> 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");
}
}

View File

@ -0,0 +1,5 @@
package com.platform.glusterfs;
public class SetCluster {
}

View File

@ -0,0 +1,493 @@
/**
* @author 李乾坤
* 进行volume的一系列操作如创建开启停止volume为volume添加或删除brick
*/
package com.platform.glusterfs;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.platform.utils.Constant;
public class SetVolume {
public static Logger log = Logger.getLogger(SetVolume.class);
/*
* 创建volume 返回值创建并挂载成功 1
*
*/
public int createVolume(String volumeName, int count, String type, List bricks, String mountPoint) {
log.info("Creat new volume");
// 判断创建volume的条件是否满足
int able = isAbleCreateVolume(volumeName, count, type, bricks, mountPoint);
if (able == 1) {
String command = null;
// 将brics从List变量中提取出来并连接成可以在命令行中使用的格式
String commandarg = concat(bricks);
/*
* verify the type
*/
if (type.equals(Constant.distributed)) {
command = "gluster volume create " + volumeName + " " + commandarg + "force";
} else if (type.equals(Constant.replica) || type.equals(Constant.stripe)) {
command = "gluster volume create " + volumeName + " " + type + " " + count + " " + commandarg + "force";
}
// 执行命令
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, command);
// 创建成功时返回信息格式volume create: volumename success:
if (reStrings == null || reStrings.size() == 0) {
log.error("3106 " + command + " run return error");
return -7;
}
if (reStrings.get(0).contains("volume create: " + volumeName + ": " + "success:")) {
log.info("create volume "+volumeName+" successed!");
// 创建成功则启动并进行挂载
if (startVolume(volumeName) == 0) {
log.info("start volume "+volumeName+" successed!");
if ((new ShowData().showFolderData(mountPoint)) == null) {
Constant.ganymedSSH.execCmdWaitAcquiescent("mkdir " + mountPoint);
}
// 进行挂载
String command3 = "mount -t glusterfs " + Constant.hostIp + ":/" + volumeName + " " + mountPoint;
List<String> reStrings3 = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser,
Constant.rootPasswd, Constant.port, command3);
// 这里需要添加解析挂载命令返回字符串的语句由于我的系统有问题导致挂载成功后返回WARNING: getfattr
// not found, certain checks will be skipped..
// 所以这句代码未经测试
// 成功挂载时没有任何返回信息
if (reStrings3.size() == 0 || reStrings.get(0).contains("GlusterFS is already mounted"))
{
log.info("mount point successed!");
return 1;
}
}
} else {
log.error("3104 volume create failed with error" + reStrings.get(0));
// System.out.println(reStrings.get(0));
return -7;
}
return 1;
} else {
log.error("给出的参数不满足创建条件");
// System.out.println("给出的参数不满足创建条件");
return able;
}
}
/**
* 删除volume
*
* @param volumeName
* @return 1 表示成功 -1表示volume name不存在-2表示停止volume 失败
*/
public int deleteVolume(String volumeName) {
int status = 0;
List<String> volumeNames = new VolumeInfo().showAllVolumeName();
if (!volumeNames.contains(volumeName)) {
log.error("3801 " + volumeName + " is not exists !");
return -1;
}
String mountPoint = new VolumeInfo().getVolumeMountPoint(volumeName);
if (stopVolume(volumeName) != 0) {
log.error("3802 " + volumeName + " stop failed !");
return -2;
}
log.info("stop "+volumeName+" successed!");
if (mountPoint != null) {
String command = "umount " + mountPoint;
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("umount "+mountPoint+" successed!");
}
String command="echo -e \"y\"| gluster volume delete "+volumeName;
List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(command);
if(reStrings==null || reStrings.size()>0 || !(reStrings.get(0).contains("volume delete: "+volumeName+": success"))){
log.error("3803 : delete volume "+volumeName+" failed !");
return -3;
}
log.info("delete "+volumeName+" successed!");
return 1;
}
/*
* 为指定的volume添加brick,参数中需要指定类型数量等 返回值1成功 ;其他失败
* 过程中需要先检查volume是否存在还需检查给出的brick数量与类型count是否相符
*/
public int addBrickVolume(String volumeName, List<String> brickName, int count, String type) {
// 检查是否满足添加bricks的条件
int able = isAble(volumeName, count, type, brickName);
if (able != 1) {
return able;
}
String command = "";
log.info("add brick to the specified volume");
String brick = concat(brickName);
if (type.equals(Constant.distributed))
command = "gluster volume add-brick " + volumeName + " " + brick + "force";
else if (type.equals(Constant.replica))
command = "gluster volume add-brick " + volumeName + " " + "replica " + count + " " + brick + "force";
else if (type.equals(Constant.stripe))
command = "gluster volume add-brick " + volumeName + " " + "stripe " + count + " " + brick + "force";
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, command);
// 添加成功的返回信息是volume add-brick: success
if (reStrings != null && reStrings.size() > 0 && reStrings.get(0).contains("volume add-brick: success")) {
log.info("添加brick成功");
return 1;
} else {
log.error("3205 add brick failed,please check the system");
// System.out.println("3202 add brick failed,please check the system");
return -5;
}
}
/*
* 为指定的volume删除brick,参数中需要指定类型数量等 返回值1 成功 ;其他 失败
* 过程中需要先检查volume是否存在还需检查给出的brick数量与类型count是否相符
*/
public int deleteBrickVolume(String volumeName, List<String> brickName, int count, String type) {
int able = isAble(volumeName, count, type, brickName);
if (able != 1) {
return able;
}
String command = null;
log.info("delete brick of the specified volume");
String brick = concat(brickName);
if (type.equals(Constant.distributed)) {
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";
} else if (type.equals(Constant.stripe)) {
command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " stripe " + count + " " + brick
+ " force";
}
if (command == null) {
log.error("3305 remove brick failed,please check the system");
return -5;
}
log.info("即将执行删除命令");
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, command);
// System.out.println(reStrings);
log.info("删除命令执行完毕");
// 删除成功的返回信息是volume remove-brick: success
if (reStrings.get(0).contains("volume remove-brick: success")) {
{
log.info("删除brick成功");
return 1;
}
} else {
log.error("3305 remove brick failed,please check the system");
return -5;
}
}
/*
* 停止指定volume 参数中需给出volume的名字 返回值 0 成功 -1 失败
* 需要先检查volume是否存在然后判断volume的状态是否已经是停止状态
*/
public int stopVolume(String volumeName) {
log.info("stop volume");
// 首先需要判断volume是否存在调用其他函数返回所有volume的名字
boolean volumeExist = false;
List<String> volume = new VolumeInfo().showAllVolumeName();
for (String temp : volume) {
if (temp.equals(volumeName)) {
volumeExist = true;
break;
}
}
if (!volumeExist) {
// volume不存在
log.error("3501 the volume doesnot exist");
System.out.println("3501 the volume doesnot exist");
return -1;
} else {
// volume存在则需判断volume的状态是否已经为stop
if (new VolumeInfo().getVolumeStatus(volumeName).equals("Stopped")) {
log.error("3502 the volume is already stoped");
System.out.println("3502 the volume is already stoped");
return -1;
} else {
String command = "echo -e \"y\"| gluster volume stop " + volumeName;
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, command);
// 标记操作结果operation = 1 操作成功
// operation = 0 操作失败
int operation = 0;
for (String temp2 : reStrings) {
if (temp2.contains("volume stop: " + volumeName + ": " + "success")) {
operation = 1;
break;
}
System.out.println("operation: " + operation);
}
if (operation == 1) {
return 0;
} else {
log.error("3503 stop " + volumeName + " failed");
System.out.println("3503 stop " + volumeName + " failed");
return -1;
}
}
}
}
/*
* 开启指定volume 参数中需给出volume的名字 返回值 0 成功 -1 失败
* 需要先检查volume是否存在然后判断volume的状态是否已经是开启状态
*/
public int startVolume(String volumeName) {
log.info("start volume");
boolean volumeExist = false;
List<String> volume = new VolumeInfo().showAllVolumeName();
for (String temp : volume) {
if (temp.equals(volumeName)) {
volumeExist = true;
break;
}
}
if (volumeExist) {
if (!(new VolumeInfo().getVolumeStatus(volumeName).equals("Started"))) {
String command = "gluster volume start " + volumeName;
int operation = 0;
// 执行命令
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, command);
for (String temp2 : reStrings) {
if (temp2.equals("volume start: " + volumeName + ": " + "success")) {
operation = 1;
}
}
if (operation == 1) {
return 0;
} else {
log.error("3602 start volume failed");
System.out.println("3602 start volume failed");
return -1;
}
} else {
log.error("volume已经开启");
System.out.println("volume已经开启");
return -1;
}
} else {
log.error("3601 the volume does not exist");
// System.out.println("3601 the volume does not exist");
return -1;
}
}
// 需要将存于List变量中的brick的位置组装成可以在glusterfs命令行中直接使用的格式
public String concat(List<String> brickName) {
StringBuffer result = new StringBuffer();
int len = brickName.size();
for (int i = 0; i < len; i++) {
result.append(brickName.get(i));
result.append(" ");
}
return result.toString();
}
/*
* 只在创建volume时使用此函数 创建volume时对不同数据卷brick的数量需要满足和count的关系
* 首先判断它们是否满足关系在不满足的关系的情况下是肯定无法完成操作的 1:可以创建 ;-1brick的ip不在集群中或者未连接; -2 -3
* -4 :类型与brick数目不匹配 ; -5 :volumeName 已经存在-6挂载点存在且不为空不能作为挂载点 -7未知错误
*/
public int isAbleCreateVolume(String volumeName, int count, String type, List<String> bricks, String mountPoint) {
int status = 0;
int length = bricks.size();
if (type.equals(Constant.distributed)) {
if (count != 0) {
log.error("3101 the kind of distributed requires the arg of count to be 0");
return -2;
}
}
if (type.equals(Constant.stripe)) {
if (length % count != 0) {
log.error("3102 the number of bricks should be the same as or the times of the stripe count");
return -3;
}
}
if (type.equals(Constant.replica)) {
if ((length % count) != 0) {
log.error(
"3103 the number of bricks should be the same as the replicate count or the times of replicate count");
return -4;
}
}
Map peer_status = new ClusterInfo().showClusterInfo();
peer_status.put(Constant.hostIp, Constant.peerincluster_connected);
for (String brick : bricks) {
brick = brick.split(":")[0];
if (!(peer_status.containsKey(brick) && peer_status.get(brick).equals(Constant.peerincluster_connected))) {
log.error("3105 birck " + brick + " ip is not in cluster");
return -1;
}
}
List<String> volumeNames = new VolumeInfo().showAllVolumeName();
if(volumeNames==null){
log.error("3108 showAllVolumeName return error");
return -7;
}
if (volumeNames.contains(volumeName)) {
log.error("3106 " + volumeName + " is already exists! ");
return -5;
}
Map datas = new ShowData().showFolderData(mountPoint);
if (datas != null && datas.size() > 0) {
log.error("3107 " + mountPoint + " is not exists or not empty ! ");
return -6;
}
return 1;
}
/**
* 添加或删除volume的brick时首先需要判断volume是否存在然后需要判断volume类型count及brick数目
*
* @param volumeName
* @param count
* @param type
* @param bricks
* @return 1 满足条件可以添加;-1 :volume name is not exists;-2,-3,-4 类型与brick数量不匹配
*/
public int isAble(String volumeName, int count, String type, List bricks) {
List<String> volumeNames = new VolumeInfo().showAllVolumeName();
if (!volumeNames.contains(volumeName)) {
log.error("3201" + volumeName + " is not exists! ");
return -1;
}
int length = bricks.size();
if (type.equals("distribute")) {
if (count == 0)
return 1;
else {
log.error("3202 the kind of distributed requires the arg of count to be 0");
// System.out.println(" the kind of distributed requires the
// arg of count to be 0");
return -2;
}
}
if (type.equals("stripe")) {
if (length % count == 0)
return 1;
else {
log.error("3203 the number of bricks should be the same as or the times of the stripe count");
// System.out.println(" the number of bricks should be the
// same as or the times of the stripe count");
return -3;
}
}
if (type.equals("replicate")) {
if ((length % count) == 0)
return 1;
else {
log.error(
"3204 the number of bricks should be the same as the replicate count or the times of replicate count");
return -4;
}
}
return 1;
}
public static void main(String[] args) {
SetVolume setVolume = new SetVolume();
int operation = 0;
PropertyConfigurator.configure("log4j.properties");
// TODO Auto-generated method stub
// 测试创建volume的代码
List<String> bricksToCreate = new ArrayList<String>();
bricksToCreate.add("192.168.0.110:/v1");
bricksToCreate.add("192.168.0.116:/v1");
operation = setVolume.createVolume("v1", 0, "distributed", bricksToCreate, "/home/v1_point");
// operation = setVolume.deleteVolume("v3");
//
// // 以下是测试添加brick的代码
//
// List<String> bricksToAdd = new ArrayList<String>();
// bricksToAdd.add("192.168.191.23:/v3");
// operation = setVolume.addBrickVolume("v3", bricksToAdd, 0,
// "distribute");
// System.out.println(operation);
// 以下代码是测试删除brick的代码
// List<String> bricksToAdd= new ArrayList<String>();
// bricksToAdd.add("192.168.191.23:/v3");
// operation =
// setVolume.deleteBrickVolume("v3",bricksToAdd,0,"distribute");
// System.out.println(operation);
// 以下是测试start volume的代码
// String volumeToStart = "testcreate" ;
// int startOperation = startVolume(volumeToStart);
// System.out.println(startOperation);
// 以下是测试stop volume
String volumeToStop = "v3";
// int startOperation = setVolume.stopVolume(volumeToStop);
// 以下是测试创建volume并完成挂载的代码
// List<String> bricksToCreate= new ArrayList<String>();
// bricksToCreate.add("192.168.214.135:/home/create");
// bricksToCreate.add("192.168.214.138:/home/create");
//
// int operation =
// createVolume("createAndmount",0,"distribute",bricksToCreate,"/mnt/create");
// System.out.println(operation);
}
}

View File

@ -0,0 +1,104 @@
package com.platform.glusterfs;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
import com.platform.utils.Constant;
public class ShowData {
public static Logger log = Logger.getLogger ( ShowData.class);
/**
* get the data of volumeName Map<string s1,string s2> s1 is data name and s2 is type file or folder
* @param volumeName
* @return
*/
public Map<String,String> showVolumeFiles(String volumeName){
log.info("start show the data");
Map<String,String> data_type=new HashMap<String, String>();
/**
* get mount point of volumeName
*/
String folderName=volumeName;
data_type=showFolderData(volumeName);
return data_type;
}
/**
* get the data of folder name
* Map<String,String> is folder name and type 1 is file and others is folder
* @param FolderName
* @return
*/
public Map<String,String> showFolderData(String folderName){
log.info(" start get "+folderName+" data");
Map<String,String> data_type=new HashMap<String, String>();
String command="ls -l "+folderName;
/*
RunCommand runCommand=new RunCommand();
List<String> reStrings=runCommand.runCommandWait(command);
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, Constant.port, command);
if(reStrings==null){
log.error("2101 command get result is null");
return null;
}
if(reStrings.size()==0){
log.info("2102 the folder is empty");
return data_type;
}
if(reStrings.get(0).contains("No such file or directory")){
log.info("2103 the "+folderName+" is not exists");
return null;
}
/**
* remove first line total number
*/
reStrings.remove(0);
for(Iterator it2 = reStrings.iterator();it2.hasNext();){
String line=(String)it2.next();
line=line.replaceAll(" +", " ");
String keyValue[]=line.split(" ");
if(keyValue.length<9){
log.error("2104 "+line+" length is short");
continue;
}
data_type.put(keyValue[8], keyValue[1]);
}
log.info(" get "+folderName+" data successed");
return data_type;
}
/**
*
* <一句话功能简述>
* <功能详细描述>
* @see [#方法#成员]
*/
@Test
public void testShowData(){
System.out.println(showFolderData("/home"));
}
}

View File

@ -0,0 +1,93 @@
package com.platform.glusterfs;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.platform.utils.Constant;
public class SizeInfo {
public static Logger log = Logger.getLogger(ClusterInfo.class);
VolumeInfo volumeInfo = new VolumeInfo();
/**
* 获取集群<EFBFBD>?<EFBFBD><EFBFBD>volume size
* 返回值-1<EFBFBD>? 0没有volume longsize大小
* @return
*/
public long showAllSize() {
log.info("get AllSize ");
List<String> volumeNames = volumeInfo.showAllVolumeName();
if (volumeNames == null) {
log.error("1201 showAllVolumeName error");
return -1;
}
if (volumeNames.size() == 0) {
log.error("1202 It is not exist any volume");
return 0;
}
List<String> reStrings = null;
long size = 0L;
for (String str : volumeNames) {
String command = "df |grep " + str + "|awk \'{print $2}\'";
reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, Constant.port,
command);
if (reStrings.isEmpty()) {
log.error("1203 The brick is unmount");
} else {
size += Long.parseLong(reStrings.get(0));
}
}
return size;
}
/**
* 返回集群已用大小
* @return
* 返回值-1<EFBFBD>? 0没有volume longsize大小
*/
public long showUseSize() {
log.info("get UseSize ");
List<String> volumeNames = volumeInfo.showAllVolumeName();
List<String> reStrings = null;
long size = 0L;
if (volumeNames == null) {
log.error("1201 showAllVolumeName error");
return -1;
}
if (volumeNames.size() == 0) {
log.error("1202 It is not exist any volume");
return 0;
}
for (String str : volumeNames) {
String command = "df |grep " + str + "|awk \'{print $3}\'";
reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, command);
if (reStrings.isEmpty()) {
log.error("1202 The brick is unmount");
} else {
size += Integer.valueOf(reStrings.get(0));
}
}
return size;
}
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
System.out.println(new SizeInfo().showAllSize());
System.out.println(new SizeInfo().showUseSize());
}
}

View File

@ -0,0 +1,271 @@
package com.platform.glusterfs;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.platform.utils.Constant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VolumeInfo {
public static Logger log = Logger.getLogger(VolumeInfo.class);
public List<String> showAllVolumeName() {
log.info("get volume name");
List<String> volNames = new ArrayList<String>();
/*
* String command = "echo \"" + Constant.rootPasswd +
* "\" |sudo -S gluster volume info|grep ^Volume.Name"; RunCommand
* runCommand = new RunCommand(); List<String> reStrings =
* runCommand.runCommandWait(command);
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, Constant.glusterVolumeInfo + "|grep ^Volume.Name");
// System.out.println(reStrings);
if(reStrings==null){
log.error("1401 get result is null");
return null;
}
if(reStrings.size()==0){
log.error("1402 get result is nothing");
return null;
}
if(reStrings.get(0).contains(Constant.noVolume)){
reStrings.clear();
return reStrings;
}
if (!(reStrings.get(0).split(":")[0].contains("Volume Name"))) {
log.error("1403 get result string wrong");
return null;
}
String nameInfo = "";
for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next();
String str[] = line.split(":");
volNames.add(str[1].replaceAll(" ", ""));
}
return volNames;
}
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<String> reStrings = runCommand.runCommandWait(command); <<<<<<<
* HEAD
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, Constant.glusterVolumeInfo + volumeName + "|grep ^Type");
// System.out.println(reStrings);
if(reStrings==null){
log.error("1501 get result is null");
return null;
}
if(reStrings.size()==0){
log.error("1502 get result is nothing");
return null;
}
if (!(reStrings.get(0).split(":")[0].contains("Type"))) {
log.error("1503 get result string wrong");
return null;
}
// System.out.println(reStrings);
for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next();
String str[] = line.split(":");
volType = str[1];
}
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<String> reStrings =
* runCommand.runCommandWait(command); <<<<<<< HEAD
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, Constant.glusterVolumeInfo + "|grep ^Status");
// System.out.println(reStrings);
if(reStrings==null){
log.error("1701 get result is null");
return null;
}
if(reStrings.size()==0){
log.error("1702 get result is nothing");
return null;
}
if (!(reStrings.get(0).split(":")[0].contains("Status"))) {
log.error("1703 get result string wrong");
return null;
}
for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next();
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<String> reStrings = runCommand.runCommandWait(command); <<<<<<<
* HEAD
*/
List<String> 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){
log.error("1801 get result is null");
return null;
}
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;
}
for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next();
String str[] = line.split("[^0-9]");
allSize = Double.parseDouble(str[0]);
}
return allSize;
}
public Double getVolumeUseSize(String volumeName) {
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<String> reStrings = runCommand.runCommandWait(command); <<<<<<<
* HEAD
*/
List<String> 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){
log.error("1901 get result is null");
return null;
}
if(reStrings.size()==0){
log.error("1902 get result is nothing");
return null;
}
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]);
}
return usedSize;
}
public String getVolumeMountPoint(String volumeName) {
log.info("get volume mountPoint");
// String mountPoint = "";
/*
* =======
*
* >>>>>>> origin/AlexKie String command = "echo \"" +
* Constant.rootPasswd + "\" |sudo -S df -h|grep " + volumeName +
* "|awk '{print $6}'"; RunCommand runCommand = new RunCommand();
* List<String> reStrings = runCommand.runCommandWait(command); <<<<<<<
* HEAD
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $6}'");
// System.out.println(reStrings);
if(reStrings==null){
log.error("11001 get result string wrong");
return null;
}
if(reStrings.size()==0){
log.error("11002 "+volumeName+" is no mountpoint");
return null;
}
char flag = reStrings.get(0).trim().toCharArray()[0];
if (flag != '/') {
log.error("11003 get result string wrong");
return null;
}
Iterator it = reStrings.iterator();
String mountPoint = (String) it.next();
mountPoint=mountPoint.replaceAll(" ", "");
return mountPoint;
}
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"));
}
}

View File

@ -6,7 +6,7 @@ import com.platform.entities.OracleConnectorParams;
import com.platform.oracle.OracleConnector;
import com.platform.utils.Configs;
public class OracleExtractService implements Runnable {
public class OracleExtractService extends Thread implements Runnable {
private OracleConnectorParams ocp;
private java.sql.Connection conn;
@ -15,11 +15,10 @@ public class OracleExtractService implements Runnable {
String url = "";
this.conn=OracleConnector.ConnectionBuilder(url, Configs.GATHER_USER_NAME, Configs.GATHER_USER_PASSWORD);
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
}

View File

@ -0,0 +1,19 @@
package com.platform.utils;
import java.util.List;
public class Constant {
public static String rootUser = "root";
public static String rootPasswd = "root";
public static String hostIp = "192.168.0.116";
public static int port = 22;
public static String glusterPeerStatus = "gluster peer status";
public static String glusterVolumeInfo = "gluster volume info ";
public static String df = "df -k ";
public static String peerincluster_connected="PeerinCluster(Connected)";
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);
}

View File

@ -0,0 +1,9 @@
package com.platform.utils;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateHandle {
public static String getNow() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置æ¥æœŸæ ¼å¼<EFBFBD>
return df.format(new Date());// new Date()为获å<EFBFBD>å½å<EFBFBD>系统æé?
}
}

View File

@ -0,0 +1,265 @@
package com.platform.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import ch.ethz.ssh2.*;
public class GanymedSSH {
public static Logger log = Logger.getLogger(GanymedSSH.class);
Connection conn;
public boolean status = true;// 锟角凤拷锟斤拷锟街达拷锟斤拷锟斤拷锟阶刺<EFBFBD>
public GanymedSSH() {
// TODO Auto-generated constructor stub
}
public GanymedSSH(String host, String username, String password, int port) {
// TODO Auto-generated constructor stub
try {
conn = getOpenedConnection(host, username, password, port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getOpenedConnection(String host, String username, String password, int port)
throws IOException {
conn = new Connection(host, port);
conn.connect(); // make sure the connection is opened
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
return conn;
}
public void execCmdNoWaitAcquiescent(String cmd) {
String host=Constant.hostIp;
String username=Constant.rootUser;
String password=Constant.rootPasswd;
int port=Constant.port;
Session sess = null;
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
// 执锟斤拷cmd
sess.execCommand(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sess.close();
}
}
public void execCmdNoWait(String host, String username, String password, int port, String cmd) {
Session sess = null;
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
// 执锟斤拷cmd
sess.execCommand(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sess.close();
}
}
public List<String> execCmdWaitAcquiescent(String cmd) {
String host=Constant.hostIp;
String username=Constant.rootUser;
String password=Constant.rootPasswd;
int port=Constant.port;
List<String> reStrings = new ArrayList<String>();
Session sess = null;
try {
sess = conn.openSession();
// 执锟斤拷cmd
sess.execCommand(cmd);
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
while (true) {
String line = stdoutReader.readLine();
if (line != null) {
// System.out.println(line);
reStrings.add(line);
} else {
break;
}
}
while (true) {
String line = stderrReader.readLine();
if (line != null) {
// System.out.println(line);
reStrings.add(line);
} else {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sess.close();
}
return reStrings;
}
public List<String> execCmdWait(String host, String username, String password, int port, String cmd) {
List<String> reStrings = new ArrayList<String>();
Session sess = null;
try {
sess = conn.openSession();
// 执锟斤拷cmd
sess.execCommand(cmd);
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
while (true) {
String line = stdoutReader.readLine();
if (line != null) {
// System.out.println(line);
reStrings.add(line);
} else {
break;
}
}
while (true) {
String line = stderrReader.readLine();
if (line != null) {
// System.out.println(line);
reStrings.add(line);
} else {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sess.close();
}
return reStrings;
}
public Map<String, String> execMD5cmd(String host, String username, String password, int port, String cmd,
String prefix) {
Map<String, String> md5 = new HashMap<String, String>();
Session sess = null;
try {
sess = conn.openSession();
// 执锟斤拷cmd
sess.execCommand(cmd);
InputStream stdout = new StreamGobbler(sess.getStdout());
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line != null) {
String[] lines = line.split(" ");
String key = lines[1].replace(prefix, "");
String value = lines[0];
md5.put(key, value);
// System.out.println(key+"\t"+value);
} else {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sess.close();
}
return md5;
}
public String execGetSize(String cmd) {
status = false;
String str_size = "0";
Session sess = null;
try {
// 执锟斤拷cmd
sess = conn.openSession();
sess.execCommand(cmd);
InputStream stdout = new StreamGobbler(sess.getStdout());
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line != null) {
// String[] lines=line.split(" ");
// str_size=lines[0];
str_size = line;
} else {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sess.close();
}
status = true;
return str_size;
}
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
}
}

View File

@ -0,0 +1,60 @@
package com.platform.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.*;
public class RunCommand {
public static Logger log = Logger.getLogger(RunCommand.class);
public List<String> runCommandWait(String command) {
List<String> reStrings = null;
String cmds[] = { "/bin/bash", "-c", command };
try {
Process ps = Runtime.getRuntime().exec(cmds);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
reStrings = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
reStrings.add(line);
// System.out.println(line);
}
br = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
reStrings = new ArrayList<String>();
while ((line = br.readLine()) != null) {
reStrings.add(line);
// System.out.println(line);
}
} catch (Exception e) {
log.error("0001 runCommandWait is error");
e.printStackTrace();
}
return reStrings;
}
public int runCommand(String command) {
List<String> reStrings = null;
String cmds[] = { "/bin/bash", "-c", command };
try {
Process ps = Runtime.getRuntime().exec(cmds);
} catch (Exception e) {
log.error("0002 runCommand execute " + command + " is error");
e.printStackTrace();
return -1;
}
return 1;
}
}

View File

@ -0,0 +1,14 @@
package com.platform.utils;
import java.util.ArrayList;
import java.util.List;
public class TestSupport {
public List<String> strToList(String str) {
List<String> reStrings=new ArrayList<String>();
for(String one:str.split("\n")){
reStrings.add(one);
}
return reStrings;
}
}