add bricks size

glusterfs-api
lily 9 years ago
parent 150437e97c
commit d3498ac95e

@ -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

@ -14,7 +14,7 @@
<appender name="dailyRollingFile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="info"></param>
<param name="ImmediateFlush" value="true"></param>
<param name="File" value="d:/logs/dailyRollingFile.log"></param>
<param name="File" value="logs/dailyRollingFile.log"></param>
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'"></param>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] {%c:%L}-%m%n"></param>
@ -23,7 +23,7 @@
<!-- 输出日志到文件 文件大小到达指定尺寸的时候产生一个新的文件 -->
<appender name="railyFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/logs/railyFile.log"></param>
<param name="File" value="logs/railyFile.log"></param>
<param name="ImmediateFlush" value="true" />
<param name="Threshold" value="info"></param>
<param name="Append" value="true"></param>

@ -14,6 +14,16 @@ import org.apache.log4j.PropertyConfigurator;
import com.platform.utils.Constant;
/**
*
* nullmapipip
* ipPeerinCluster(Connected)
* ipPeerinCluster(Disconnected)
* @author liliy
* @version [2016912]
* @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<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");
@ -76,7 +81,26 @@ public class ClusterInfo {
}
return peerIps;
}
/**
* ipip
* ipnull
* ipPeerinCluster(Connected)
* ipPeerinCluster(Disconnected)
* @param peerip
* @return
* @see [##]
*/
public String getPeerStatus(String peerip){
Map<String, String> 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) {

@ -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<String, String> 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<String, String> files = showData.showFolderData(name);
List<String> 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() {

@ -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;
}
}

@ -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<string s1,string s2> s1 is data name and s2 is type file or folder
* get the data of volumeName Map<string s1,string s2> s1 is data name and s2 is type file or folder
* <>
* @param volumeName
* @return
* @see [##]
*/
public Map<String,String> showVolumeFiles(String volumeName){
log.info("start show the data");
Map<String,String> data_type=new HashMap<String, String>();

@ -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<String> showAllVolumeName() {
log.info("get volume name");
List<String> volNames = new ArrayList<String>();
@ -30,18 +37,18 @@ public class VolumeInfo {
* 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");
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){
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;
}
/**
* volumevolume
* <>
* @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<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");
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){
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<String> reStrings =
* runCommand.runCommandWait(command); <<<<<<< HEAD
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
Constant.port, Constant.glusterVolumeInfo + "|grep ^Status");
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){
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<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}'");
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){
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<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}'");
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){
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<String> getVolumeMountPoint(String volumeName) {
log.info("get volume mountPoint");
// String mountPoint = "";
public List<String> 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<String> reStrings = runCommand.runCommandWait(command); <<<<<<<
* HEAD
*/
String cmd="gluster volume info "+volumeName+" |grep ^Brick'[0-9]\\+' |awk '{print $2}'";
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(volumeName);
String cmd = "gluster volume info " + volumeName + " |grep ^Brick'[0-9]\\+' |awk '{print $2}'";
List<String> 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<String> reStrings = runCommand.runCommandWait(command); <<<<<<<
* HEAD
*/
public List<String> getVolumeMountPoint(String volumeName) {
log.info("get volume MountPoint");
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);
@ -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<String> mountPoints = new ArrayList<>();
for(String mountPoint:reStrings){
mountPoint=mountPoint.replaceAll(" ", "");
return mountPoint;
mountPoints.add(mountPoint);
}
return mountPoints;
}
public Map<String, Double> getVolumebricksDataSize(String volumeName) {
List<String> bricks = getVolumeBricks(volumeName);
Map<String, Double> 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<String> 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<String, Double> getVolumebricksAvailableSize(String volumeName) {
List<String> bricks = getVolumeBricks(volumeName);
Map<String, Double> 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<String> 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");
}
}

@ -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";
}

@ -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();
}
}

@ -62,7 +62,7 @@ public class GanymedSSH {
Session sess = null;
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
// 执锟斤拷cmd
sess.execCommand(cmd);

Loading…
Cancel
Save