Search in sources :

Example 1 with DeflaterInflaterData

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;
}
Also used : DeflaterInflaterData(org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData) HashMap(java.util.HashMap) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) NetstatFunctionResult(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult) List(java.util.List) ArrayList(java.util.ArrayList) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) HashSet(java.util.HashSet) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) ConverterHint(org.apache.geode.management.cli.ConverterHint) DataFormatException(java.util.zip.DataFormatException) DistributedMember(org.apache.geode.distributed.DistributedMember) NetstatFunctionArgument(org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionArgument) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 2 with DeflaterInflaterData

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;
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) DeflaterInflaterData(org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Aggregations

DeflaterInflaterData (org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 DataFormatException (java.util.zip.DataFormatException)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 ConverterHint (org.apache.geode.management.cli.ConverterHint)1 Result (org.apache.geode.management.cli.Result)1 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)1 NetstatFunctionArgument (org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionArgument)1 NetstatFunctionResult (org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionResult)1 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)1 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)1 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)1 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)1 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1