Search in sources :

Example 1 with ChangeLogLevelFunction

use of org.apache.geode.management.internal.cli.functions.ChangeLogLevelFunction in project geode by apache.

the class MiscellaneousCommands method changeLogLevel.

@CliCommand(value = CliStrings.CHANGE_LOGLEVEL, help = CliStrings.CHANGE_LOGLEVEL__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_LOGS }, interceptor = "org.apache.geode.management.internal.cli.commands.MiscellaneousCommands$ChangeLogLevelInterceptor")
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.WRITE)
public Result changeLogLevel(@CliOption(key = CliStrings.CHANGE_LOGLEVEL__MEMBER, help = CliStrings.CHANGE_LOGLEVEL__MEMBER__HELP) String[] memberIds, @CliOption(key = CliStrings.CHANGE_LOGLEVEL__GROUPS, unspecifiedDefaultValue = "", help = CliStrings.CHANGE_LOGLEVEL__GROUPS__HELP) String[] grps, @CliOption(key = CliStrings.CHANGE_LOGLEVEL__LOGLEVEL, optionContext = ConverterHint.LOG_LEVEL, mandatory = true, unspecifiedDefaultValue = "", help = CliStrings.CHANGE_LOGLEVEL__LOGLEVEL__HELP) String logLevel) {
    try {
        if ((memberIds == null || memberIds.length == 0) && (grps == null || grps.length == 0)) {
            return ResultBuilder.createUserErrorResult(CliStrings.CHANGE_LOGLEVEL__MSG__SPECIFY_GRP_OR_MEMBER);
        }
        InternalCache cache = GemFireCacheImpl.getInstance();
        LogWriter logger = cache.getLogger();
        Set<DistributedMember> dsMembers = new HashSet<DistributedMember>();
        Set<DistributedMember> ds = CliUtil.getAllMembers(cache);
        if (grps != null && grps.length > 0) {
            for (String grp : grps) {
                dsMembers.addAll(cache.getDistributedSystem().getGroupMembers(grp));
            }
        }
        if (memberIds != null && memberIds.length > 0) {
            for (String member : memberIds) {
                Iterator<DistributedMember> it = ds.iterator();
                while (it.hasNext()) {
                    DistributedMember mem = it.next();
                    if (mem.getName() == null ? false : mem.getName().equals(member) || mem.getId().equals(member)) {
                        dsMembers.add(mem);
                        break;
                    }
                }
            }
        }
        if (dsMembers.size() == 0) {
            return ResultBuilder.createGemFireErrorResult(CliStrings.CHANGE_LOGLEVEL__MSG_NO_MEMBERS);
        }
        Function logFunction = new ChangeLogLevelFunction();
        FunctionService.registerFunction(logFunction);
        Object[] functionArgs = new Object[1];
        functionArgs[0] = logLevel;
        CompositeResultData compositeResultData = ResultBuilder.createCompositeResultData();
        SectionResultData section = compositeResultData.addSection("section");
        TabularResultData resultTable = section.addTable("ChangeLogLevel");
        resultTable = resultTable.setHeader("Summary");
        Execution execution = FunctionService.onMembers(dsMembers).setArguments(functionArgs);
        if (execution == null) {
            return ResultBuilder.createUserErrorResult(CliStrings.CHANGE_LOGLEVEL__MSG__CANNOT_EXECUTE);
        }
        List<?> resultList = (List<?>) execution.execute(logFunction).getResult();
        for (Object object : resultList) {
            try {
                if (object instanceof Throwable) {
                    logger.warning("Exception in ChangeLogLevelFunction " + ((Throwable) object).getMessage(), ((Throwable) object));
                    continue;
                }
                if (object != null) {
                    Map<String, String> resultMap = (Map<String, String>) object;
                    Entry<String, String> entry = resultMap.entrySet().iterator().next();
                    if (entry.getValue().contains("ChangeLogLevelFunction exception")) {
                        resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_MEMBER, entry.getKey());
                        resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_STATUS, "false");
                    } else {
                        resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_MEMBER, entry.getKey());
                        resultTable.accumulate(CliStrings.CHANGE_LOGLEVEL__COLUMN_STATUS, "true");
                    }
                }
            } catch (Exception ex) {
                LogWrapper.getInstance().warning("change log level command exception " + ex);
                continue;
            }
        }
        Result result = ResultBuilder.buildResult(compositeResultData);
        logger.info("change log-level command result=" + result);
        return result;
    } catch (Exception ex) {
        GemFireCacheImpl.getInstance().getLogger().error("GFSH Changeloglevel exception: " + ex);
        return ResultBuilder.createUserErrorResult(ex.getMessage());
    }
}
Also used : CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) TimeoutException(java.util.concurrent.TimeoutException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) FunctionException(org.apache.geode.cache.execute.FunctionException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) 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) ChangeLogLevelFunction(org.apache.geode.management.internal.cli.functions.ChangeLogLevelFunction) ChangeLogLevelFunction(org.apache.geode.management.internal.cli.functions.ChangeLogLevelFunction) Function(org.apache.geode.cache.execute.Function) NetstatFunction(org.apache.geode.management.internal.cli.functions.NetstatFunction) ShutDownFunction(org.apache.geode.management.internal.cli.functions.ShutDownFunction) GarbageCollectionFunction(org.apache.geode.management.internal.cli.functions.GarbageCollectionFunction) GetStackTracesFunction(org.apache.geode.management.internal.cli.functions.GetStackTracesFunction) Execution(org.apache.geode.cache.execute.Execution) LogWriter(org.apache.geode.LogWriter) DistributedMember(org.apache.geode.distributed.DistributedMember) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 DataFormatException (java.util.zip.DataFormatException)1 LogWriter (org.apache.geode.LogWriter)1 Execution (org.apache.geode.cache.execute.Execution)1 Function (org.apache.geode.cache.execute.Function)1 FunctionException (org.apache.geode.cache.execute.FunctionException)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 Result (org.apache.geode.management.cli.Result)1 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)1 ChangeLogLevelFunction (org.apache.geode.management.internal.cli.functions.ChangeLogLevelFunction)1 GarbageCollectionFunction (org.apache.geode.management.internal.cli.functions.GarbageCollectionFunction)1