Search in sources :

Example 1 with NetstatFunctionArgument

use of org.apache.geode.management.internal.cli.functions.NetstatFunction.NetstatFunctionArgument 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)

Aggregations

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 DeflaterInflaterData (org.apache.geode.management.internal.cli.CliUtil.DeflaterInflaterData)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 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