Search in sources :

Example 26 with LogRequest

use of com.emc.vipr.model.sys.logging.LogRequest in project coprhd-controller by CoprHD.

the class LogService method getLogs.

/**
 * Get log data from the specified virtual machines that are filtered, merged,
 * and sorted based on the passed request parameters and streams the log
 * messages back to the client as JSON formatted strings.
 *
 * @brief Show logs from all or specified virtual machine
 * @param nodeIds The ids of the virtual machines for which log data is
 *            collected.
 *            Allowed values: standalone,
 *            control nodes: vipr1,vipr2 etc
 *            data services nodes: dataservice-10-111-111-222 (node-ip-address)
 * @param nodeNames The custom names of the vipr nodes for which log data is
 *            collected.
 *            Allowed values: Current values of node_x_name properties
 * @param logNames The names of the log files to process.
 * @param severity The minimum severity level for a logged message.
 *            Allowed values:0-9. Default value: 7
 * @param startTimeStr The start datetime of the desired time window. Value is
 *            inclusive.
 *            Allowed values: "yyyy-MM-dd_HH:mm:ss" formatted date or
 *            datetime in ms.
 *            Default: Set to yesterday same time
 * @param endTimeStr The end datetime of the desired time window. Value is
 *            inclusive.
 *            Allowed values: "yyyy-MM-dd_HH:mm:ss" formatted date or
 *            datetime in ms.
 * @param msgRegex A regular expression to which the log message conforms.
 * @param maxCount Maximum number of log messages to retrieve. This may return
 *            more than max count, if there are more messages with same
 *            timestamp as of the latest message.
 *            Value should be greater than 0.
 * @param dryRun if true, the API will do a dry run for log collection. Instead
 *            of collecting logs from nodes, dry run will check the nodes'
 *            availability for collecting logs. Entity body of the response
 *            will return an error message string indicating which node(s)
 *            not available for collecting logs. If log collection is ok
 *            for all specified nodes, no error message is included in
 *            response.
 *            Default value of this parameter is false.
 * @prereq none
 * @return A reference to the StreamingOutput to which the log data is
 *         written.
 * @throws WebApplicationException When an invalid request is made.
 */
@GET
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.SYSTEM_MONITOR, Role.SECURITY_ADMIN })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
public Response getLogs(@QueryParam(LogRequestParam.NODE_ID) List<String> nodeIds, @QueryParam(LogRequestParam.NODE_NAME) List<String> nodeNames, @QueryParam(LogRequestParam.LOG_NAME) List<String> logNames, @DefaultValue(LogSeverity.DEFAULT_VALUE_AS_STR) @QueryParam(LogRequestParam.SEVERITY) int severity, @QueryParam(LogRequestParam.START_TIME) String startTimeStr, @QueryParam(LogRequestParam.END_TIME) String endTimeStr, @QueryParam(LogRequestParam.MSG_REGEX) String msgRegex, @QueryParam(LogRequestParam.MAX_COUNT) int maxCount, @QueryParam(LogRequestParam.DRY_RUN) @DefaultValue("false") boolean dryRun) throws Exception {
    _log.info("Received getlogs request");
    enforceRunningRequestLimit();
    final MediaType mediaType = getMediaType();
    _log.info("Logs request media type {}", mediaType);
    nodeIds = _coordinatorClientExt.combineNodeNamesWithNodeIds(nodeNames, nodeIds);
    // Validate the passed node ids.
    validateNodeIds(nodeIds);
    _log.debug("Validated requested nodes");
    // Validate the passed severity is valid.
    validateLogSeverity(severity);
    _log.debug("Validated requested severity");
    // Validate the passed start and end times are valid.
    Date startTime = TimeUtils.getDateTimestamp(startTimeStr);
    Date endTime = TimeUtils.getDateTimestamp(endTimeStr);
    TimeUtils.validateTimestamps(startTime, endTime);
    _log.debug("Validated requested time window");
    // Setting default start time to yesterday
    if (startTime == null) {
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DATE, -1);
        startTime = yesterday.getTime();
        _log.info("Setting start time to yesterday {} ", startTime);
    }
    // Validate regular message
    validateMsgRegex(msgRegex);
    _log.debug("Validated regex");
    // Validate max count
    if (maxCount < 0) {
        throw APIException.badRequests.parameterIsNotValid("maxCount");
    }
    // validate log names
    Set<String> allLogNames = getValidLogNames();
    _log.debug("valid log names {}", allLogNames);
    boolean invalidLogName = false;
    for (String logName : logNames) {
        if (!allLogNames.contains(logName)) {
            invalidLogName = true;
            break;
        }
    }
    if (invalidLogName) {
        throw APIException.badRequests.parameterIsNotValid("log names");
    }
    if (dryRun) {
        List<NodeInfo> clusterNodesInfo = ClusterNodesUtil.getClusterNodeInfo();
        if (clusterNodesInfo.isEmpty()) {
            _log.error("No nodes available for collecting logs");
            throw APIException.internalServerErrors.noNodeAvailableError("no nodes available for collecting logs");
        }
        List<NodeInfo> matchingNodes = null;
        if (nodeIds.isEmpty()) {
            matchingNodes = clusterNodesInfo;
        } else {
            matchingNodes = new ArrayList<NodeInfo>();
            for (NodeInfo node : clusterNodesInfo) {
                if (nodeIds.contains(node.getId())) {
                    matchingNodes.add(node);
                }
            }
        }
        // find the unavailable nodes
        List<String> failedNodes = null;
        if (matchingNodes.size() == 1 && matchingNodes.get(0).getId().equals("standalone")) {
            failedNodes = new ArrayList<String>();
        } else {
            // find the unavailable nodes
            failedNodes = _coordinatorClientExt.getUnavailableControllerNodes();
        }
        if (!nodeIds.isEmpty()) {
            failedNodes.retainAll(nodeIds);
        }
        String baseNodeURL;
        SysClientFactory.SysClient sysClient;
        for (final NodeInfo node : matchingNodes) {
            baseNodeURL = String.format(SysClientFactory.BASE_URL_FORMAT, node.getIpAddress(), node.getPort());
            _log.debug("getting log names from node: " + baseNodeURL);
            sysClient = SysClientFactory.getSysClient(URI.create(baseNodeURL), _logSvcPropertiesLoader.getNodeLogCollectorTimeout() * 1000, _logSvcPropertiesLoader.getNodeLogConnectionTimeout() * 1000);
            LogRequest logReq = new LogRequest.Builder().nodeIds(nodeIds).baseNames(getLogNamesFromAlias(logNames)).logLevel(severity).startTime(startTime).endTime(endTime).regex(msgRegex).maxCont(maxCount).build();
            logReq.setDryRun(true);
            try {
                sysClient.post(SysClientFactory.URI_NODE_LOGS, null, logReq);
            } catch (Exception e) {
                _log.error("Exception accessing node {}: {}", baseNodeURL, e);
                failedNodes.add(node.getId());
            }
        }
        if (_coordinatorClientExt.getNodeCount() == failedNodes.size()) {
            throw APIException.internalServerErrors.noNodeAvailableError("All nodes are unavailable for collecting logs");
        }
        return Response.ok().build();
    }
    LogRequest logReqInfo = new LogRequest.Builder().nodeIds(nodeIds).baseNames(getLogNamesFromAlias(logNames)).logLevel(severity).startTime(startTime).endTime(endTime).regex(msgRegex).maxCont(maxCount).build();
    _log.info("log request info is {}", logReqInfo.toString());
    final LogNetworkStreamMerger logRequestMgr = new LogNetworkStreamMerger(logReqInfo, mediaType, _logSvcPropertiesLoader);
    StreamingOutput logMsgStream = new StreamingOutput() {

        @Override
        public void write(OutputStream outputStream) {
            try {
                runningRequests.incrementAndGet();
                logRequestMgr.streamLogs(outputStream);
            } finally {
                runningRequests.decrementAndGet();
            }
        }
    };
    return Response.ok(logMsgStream).build();
}
Also used : Calendar(java.util.Calendar) OutputStream(java.io.OutputStream) SysClientFactory(com.emc.storageos.systemservices.impl.client.SysClientFactory) StreamingOutput(javax.ws.rs.core.StreamingOutput) Date(java.util.Date) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) LogRequest(com.emc.vipr.model.sys.logging.LogRequest) NodeInfo(com.emc.storageos.systemservices.impl.resource.util.NodeInfo) MediaType(javax.ws.rs.core.MediaType) LogNetworkStreamMerger(com.emc.storageos.systemservices.impl.logsvc.merger.LogNetworkStreamMerger) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 27 with LogRequest

use of com.emc.vipr.model.sys.logging.LogRequest in project coprhd-controller by CoprHD.

the class LogReaderTest method testMultipleLinesInfoNoFilterSVCParser.

/**
 * Test if readMessage() can read multiple lines INFO service log from file and parse it correctly
 * Test log whose first line's message field is null.
 */
@Test
public void testMultipleLinesInfoNoFilterSVCParser() throws Exception {
    LogStatusInfo status = new LogStatusInfo();
    LogRequest req = new LogRequest.Builder().build();
    LogReader reader = new LogReader(multipleLineINFOLogPath, req, status, null);
    LogMessage l = reader.readNextLogMessage();
    Calendar calendar = Calendar.getInstance();
    // month starts from 0;
    calendar.set(2014, 0, 16, 18, 58, 24);
    calendar.set(Calendar.MILLISECOND, 25);
    Date date = calendar.getTime();
    long time = date.getTime();
    assertEquals("Time is wrong", l.getTime(), time);
    assertTrue("Thread name is wrong", Arrays.equals(LogUtil.stringToBytes("pool-10-thread-1"), l.getThreadName()));
    assertEquals("Log level is wrong", new String(l.getLevel()), "INFO");
    assertTrue("File name is wrong", Arrays.equals(LogUtil.stringToBytes("ProcessMonitor"), l.getFileName()));
    assertTrue("Line number is wrong", Arrays.equals(l.getLineNumber(), LogUtil.stringToBytes("34")));
    assertTrue("Log message content is wrong", Arrays.equals(LogUtil.stringToBytes("" + '\n' + "Memory Usage Metrics " + '\n' + "Total  Memory: 379MB; " + '\n' + "Available Free Memory: 146MB; " + '\n' + "Available Maximum Memory : 910MB; " + '\n' + "Used Memory: 233MB; " + '\n' + "Max used Memory : 366MB at 2014-01-01 23:03:24.025 UTC; "), l.getLogContent()));
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) Calendar(java.util.Calendar) LogReader(com.emc.storageos.systemservices.impl.logsvc.stream.LogReader) Date(java.util.Date) Test(org.junit.Test)

Example 28 with LogRequest

use of com.emc.vipr.model.sys.logging.LogRequest in project coprhd-controller by CoprHD.

the class LogReaderTest method testSuperLongLogNoFilterSVCParser.

/**
 * Test if readMessage() can read super long service log from file and parse it correctly
 */
@Test
public void testSuperLongLogNoFilterSVCParser() throws Exception {
    LogStatusInfo status = new LogStatusInfo();
    LogRequest req = new LogRequest.Builder().build();
    LogReader reader = new LogReader(superLongSvcLogPath, req, status, null);
    LogMessage l = reader.readNextLogMessage();
    Calendar calendar = Calendar.getInstance();
    // month starts from 0;
    calendar.set(2014, 0, 16, 19, 00, 1);
    calendar.set(Calendar.MILLISECOND, 519);
    Date date = calendar.getTime();
    long time = date.getTime();
    assertEquals("Time is wrong", l.getTime(), time);
    assertTrue("Thread name is wrong", Arrays.equals(LogUtil.stringToBytes("pool-35-thread-1"), l.getThreadName()));
    assertEquals("Log level is wrong", new String(l.getLevel()), "INFO");
    assertTrue("File name is wrong", Arrays.equals(LogUtil.stringToBytes("DefaultSingletonBeanRegistry"), l.getFileName()));
    assertTrue("Line number is wrong", Arrays.equals(l.getLineNumber(), LogUtil.stringToBytes("433")));
    assertTrue("Log message contact is wrong", Arrays.equals(LogUtil.stringToBytes("Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25f13769: defining beans [namespaces,scanner,registeredProfiles,reference-profile,profile-prop,profileProcessor,providerVersionSupport,resultClass-softwareIdentity,softwareIdentity-prop,softwareIdentityProcessor,system,resultClass-system,system-prop,scannerProcessor,model,reference-comp,resultClass-chassis,model-prop,modelProcessor,argscreator,smiutility,cimClient,block,commandgenerator,executor,null,bool,bool-true]; root of factory hierarchy"), l.getLogContent()));
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) Calendar(java.util.Calendar) LogReader(com.emc.storageos.systemservices.impl.logsvc.stream.LogReader) Date(java.util.Date) Test(org.junit.Test)

Example 29 with LogRequest

use of com.emc.vipr.model.sys.logging.LogRequest in project coprhd-controller by CoprHD.

the class LogRequestTest method testMarshal.

@Test
public void testMarshal() throws Exception {
    System.out.println("Entering testMatshal()");
    LogRequest req = new LogRequest();
    List<String> baseNames = new ArrayList<String>();
    List<String> nodeIds = new ArrayList<String>();
    baseNames.add("apisvc");
    nodeIds.add("standalone");
    req.setBaseNames(baseNames);
    req.setMaxCount(10);
    req.setNodeIds(nodeIds);
    File file = new File(PATH + File.separator + "file.xml");
    JAXBContext jc = JAXBContext.newInstance(LogRequest.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(req, file);
    marshaller.marshal(req, System.out);
    System.out.println("Done testMastshal()");
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 30 with LogRequest

use of com.emc.vipr.model.sys.logging.LogRequest in project coprhd-controller by CoprHD.

the class LogRequestTest method testUnMarshal.

@Test
public void testUnMarshal() throws Exception {
    System.out.println("Entering testUnMarshal()");
    File file = new File(PATH + File.separator + "file.xml");
    JAXBContext jc = JAXBContext.newInstance(LogRequest.class);
    Unmarshaller jaxbUnmarshaller = jc.createUnmarshaller();
    LogRequest req = (LogRequest) jaxbUnmarshaller.unmarshal(file);
    System.out.println(req);
    System.out.println("Done testUnMarshal()");
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) File(java.io.File) Test(org.junit.Test)

Aggregations

LogRequest (com.emc.vipr.model.sys.logging.LogRequest)38 Test (org.junit.Test)33 ArrayList (java.util.ArrayList)21 File (java.io.File)17 LogMessage (com.emc.storageos.systemservices.impl.logsvc.LogMessage)15 LogFileStream (com.emc.storageos.systemservices.impl.logsvc.stream.LogFileStream)13 Calendar (java.util.Calendar)13 Date (java.util.Date)13 LogStatusInfo (com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo)12 Ignore (org.junit.Ignore)12 LogReader (com.emc.storageos.systemservices.impl.logsvc.stream.LogReader)10 LogStreamMerger (com.emc.storageos.systemservices.impl.logsvc.merger.LogStreamMerger)4 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)2 SysClientFactory (com.emc.storageos.systemservices.impl.client.SysClientFactory)2 LogNetworkWriter (com.emc.storageos.systemservices.impl.logsvc.LogNetworkWriter)2 LogNetworkStreamMerger (com.emc.storageos.systemservices.impl.logsvc.merger.LogNetworkStreamMerger)2 LogNetworkReader (com.emc.storageos.systemservices.impl.logsvc.stream.LogNetworkReader)2 NodeInfo (com.emc.storageos.systemservices.impl.resource.util.NodeInfo)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2