use of org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData in project geode by apache.
the class MiscellaneousCommands method netstat.
@CliCommand(value = CliStrings.NETSTAT, help = CliStrings.NETSTAT__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DEBUG_UTIL })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public // TODO : Verify the auto-completion for multiple values.
Result netstat(@CliOption(key = CliStrings.NETSTAT__MEMBER, mandatory = false, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.NETSTAT__MEMBER__HELP) String[] members, @CliOption(key = CliStrings.NETSTAT__GROUP, mandatory = false, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.NETSTAT__GROUP__HELP) String group, @CliOption(key = CliStrings.NETSTAT__FILE, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.NETSTAT__FILE__HELP) String saveAs, @CliOption(key = CliStrings.NETSTAT__WITHLSOF, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.NETSTAT__WITHLSOF__HELP) boolean withlsof) {
Result result = null;
Map<String, DistributedMember> hostMemberMap = new HashMap<String, DistributedMember>();
Map<String, List<String>> hostMemberListMap = new HashMap<String, List<String>>();
try {
if (members != null && members.length > 0 && group != null) {
throw new IllegalArgumentException(CliStrings.NETSTAT__MSG__ONLY_ONE_OF_MEMBER_OR_GROUP_SHOULD_BE_SPECIFIED);
}
StringBuilder resultInfo = new StringBuilder();
// Execute for remote members whose id or name matches
InternalDistributedSystem system = InternalDistributedSystem.getConnectedInstance();
if (members != null) {
Set<String> notFoundMembers = new HashSet<String>();
for (String memberIdOrName : members) {
Set<DistributedMember> membersToExecuteOn = CliUtil.getAllMembers(system);
boolean memberFound = false;
for (DistributedMember distributedMember : membersToExecuteOn) {
String memberName = distributedMember.getName();
String memberId = distributedMember.getId();
if (memberName.equals(memberIdOrName) || memberId.equals(memberIdOrName)) {
buildMaps(hostMemberMap, hostMemberListMap, memberIdOrName, distributedMember);
memberFound = true;
break;
}
}
if (!memberFound) {
notFoundMembers.add(memberIdOrName);
}
}
// if there are not found members, it's probably unknown member or member has departed
if (!notFoundMembers.isEmpty()) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.NETSTAT__MSG__COULD_NOT_FIND_MEMBERS_0, new Object[] { CliUtil.collectionToString(notFoundMembers, -1) }));
}
} else {
Set<DistributedMember> membersToExecuteOn = null;
if (group != null) {
membersToExecuteOn = system.getGroupMembers(group);
} else {
// consider all members
membersToExecuteOn = CliUtil.getAllMembers(system);
}
for (DistributedMember distributedMember : membersToExecuteOn) {
String memberName = distributedMember.getName();
String memberId = distributedMember.getId();
String memberIdOrName = memberName != null && !memberName.isEmpty() ? memberName : memberId;
buildMaps(hostMemberMap, hostMemberListMap, memberIdOrName, distributedMember);
}
}
String lineSeparatorToUse = null;
lineSeparatorToUse = CommandExecutionContext.getShellLineSeparator();
if (lineSeparatorToUse == null) {
lineSeparatorToUse = GfshParser.LINE_SEPARATOR;
}
NetstatFunctionArgument nfa = new NetstatFunctionArgument(lineSeparatorToUse, withlsof);
if (!hostMemberMap.isEmpty()) {
Set<DistributedMember> membersToExecuteOn = new HashSet<DistributedMember>(hostMemberMap.values());
ResultCollector<?, ?> netstatResult = CliUtil.executeFunction(NetstatFunction.INSTANCE, nfa, membersToExecuteOn);
List<?> resultList = (List<?>) netstatResult.getResult();
for (int i = 0; i < resultList.size(); i++) {
NetstatFunctionResult netstatFunctionResult = (NetstatFunctionResult) resultList.get(i);
DeflaterInflaterData deflaterInflaterData = netstatFunctionResult.getCompressedBytes();
try {
String remoteHost = netstatFunctionResult.getHost();
List<String> membersList = hostMemberListMap.get(remoteHost);
resultInfo.append(MessageFormat.format(netstatFunctionResult.getHeaderInfo(), CliUtil.collectionToString(membersList, 120)));
DeflaterInflaterData uncompressedBytes = CliUtil.uncompressBytes(deflaterInflaterData.getData(), deflaterInflaterData.getDataLength());
resultInfo.append(new String(uncompressedBytes.getData()));
} catch (DataFormatException e) {
resultInfo.append("Error in some data. Reason : " + e.getMessage());
}
}
}
InfoResultData resultData = ResultBuilder.createInfoResultData();
if (saveAs != null && !saveAs.isEmpty()) {
String saveToFile = saveAs;
if (!saveAs.endsWith(NETSTAT_FILE_REQUIRED_EXTENSION)) {
saveToFile = saveAs + NETSTAT_FILE_REQUIRED_EXTENSION;
}
resultData.addAsFile(saveToFile, resultInfo.toString(), CliStrings.NETSTAT__MSG__SAVED_OUTPUT_IN_0, // Note: substitution for {0} will
false);
// happen on client side.
} else {
resultData.addLine(resultInfo.toString());
}
result = ResultBuilder.buildResult(resultData);
} catch (IllegalArgumentException e) {
LogWrapper.getInstance().info(CliStrings.format(CliStrings.NETSTAT__MSG__ERROR_OCCURRED_WHILE_EXECUTING_NETSTAT_ON_0, new Object[] { Arrays.toString(members) }));
result = ResultBuilder.createUserErrorResult(e.getMessage());
} catch (RuntimeException e) {
LogWrapper.getInstance().info(CliStrings.format(CliStrings.NETSTAT__MSG__ERROR_OCCURRED_WHILE_EXECUTING_NETSTAT_ON_0, new Object[] { Arrays.toString(members) }), e);
result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.NETSTAT__MSG__ERROR_OCCURRED_WHILE_EXECUTING_NETSTAT_ON_0, new Object[] { Arrays.toString(members) }));
} finally {
hostMemberMap.clear();
hostMemberListMap.clear();
}
return result;
}
use of org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData in project geode by apache.
the class AbstractResultData method addAsFile.
private ResultData addAsFile(String fileName, byte[] data, int fileType, String message) {
if (fileType != FILE_TYPE_BINARY && fileType != FILE_TYPE_TEXT) {
throw new IllegalArgumentException("Unsupported file type is specified.");
}
GfJsonObject sectionData = new GfJsonObject();
try {
GfJsonArray fileDataArray = contentObject.getJSONArray(BYTE_DATA_ACCESSOR);
if (fileDataArray == null) {
fileDataArray = new GfJsonArray();
contentObject.put(BYTE_DATA_ACCESSOR, fileDataArray);
}
fileDataArray.put(sectionData);
sectionData.put(FILE_NAME_FIELD, fileName);
sectionData.put(FILE_TYPE_FIELD, fileType);
sectionData.put(FILE_MESSAGE, message);
DeflaterInflaterData deflaterInflaterData = CliUtil.compressBytes(data);
sectionData.put(FILE_DATA_FIELD, Base64.getEncoder().encodeToString(deflaterInflaterData.getData()));
sectionData.put(DATA_LENGTH_FIELD, deflaterInflaterData.getDataLength());
} catch (GfJsonException e) {
throw new ResultDataException(e.getMessage());
}
return this;
}
Aggregations