Search in sources :

Example 1 with SmbFilenameFilter

use of jcifs.smb.SmbFilenameFilter in project opennms by OpenNMS.

the class JCifsMonitorTest method setUp.

@Before
public void setUp() throws Exception {
    mockSmbFileValidPath = createNiceMock(SmbFile.class);
    expect(mockSmbFileValidPath.exists()).andReturn(true).anyTimes();
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://10.123.123.123/validPath"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFileValidPath).anyTimes();
    mockSmbFileInvalidPath = createNiceMock(SmbFile.class);
    expect(mockSmbFileInvalidPath.exists()).andReturn(false).anyTimes();
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://10.123.123.123/invalidPath"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFileInvalidPath).anyTimes();
    mockSmbFolderEmpty = createNiceMock(SmbFile.class);
    expect(mockSmbFolderEmpty.exists()).andReturn(true).anyTimes();
    expect(mockSmbFolderEmpty.list((SmbFilenameFilter) anyObject())).andReturn(new String[] {}).anyTimes();
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://10.123.123.123/folderEmpty"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFolderEmpty).anyTimes();
    mockSmbFolderNotEmpty = createNiceMock(SmbFile.class);
    expect(mockSmbFolderNotEmpty.exists()).andReturn(true).anyTimes();
    expect(mockSmbFolderNotEmpty.list((SmbFilenameFilter) anyObject())).andReturn(new String[] { "ABCD", "ACBD", "DCBA", "DABC" }).anyTimes();
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://10.123.123.123/folderNotEmpty"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFolderNotEmpty).anyTimes();
    mockSmbFileSmbException = createNiceMock(SmbFile.class);
    expect(mockSmbFileSmbException.exists()).andThrow(new SmbException(SmbException.ERROR_ACCESS_DENIED, true));
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://10.123.123.123/smbException"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFileSmbException).anyTimes();
    mockSmbFileMalformedUrlException = createNiceMock(SmbFile.class);
    expect(mockSmbFileMalformedUrlException.exists()).andThrow(new SmbException(SmbException.ERROR_ACCESS_DENIED, true));
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://10.123.123.123/malformedUrlException"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFileMalformedUrlException).anyTimes();
    mockSmbFileSmbHost = createNiceMock(SmbFile.class);
    expect(mockSmbFileSmbHost.exists()).andThrow(new SmbException(SmbException.ERROR_ACCESS_DENIED, true));
    expectNew(SmbFile.class, new Class<?>[] { String.class, NtlmPasswordAuthentication.class }, eq("smb://192.168.0.123/smbException"), isA(NtlmPasswordAuthentication.class)).andReturn(mockSmbFileSmbHost).anyTimes();
}
Also used : SmbException(jcifs.smb.SmbException) NtlmPasswordAuthentication(jcifs.smb.NtlmPasswordAuthentication) SmbFilenameFilter(jcifs.smb.SmbFilenameFilter) SmbFile(jcifs.smb.SmbFile) Before(org.junit.Before)

Example 2 with SmbFilenameFilter

use of jcifs.smb.SmbFilenameFilter in project opennms by OpenNMS.

the class JCifsMonitor method poll.

/**
 * This method queries the CIFS share.
 *
 * @param svc        the monitored service
 * @param parameters the parameter map
 * @return the poll status for this system
 */
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    final String domain = parameters.containsKey("domain") ? (String) parameters.get("domain") : "";
    final String username = parameters.containsKey("username") ? (String) parameters.get("username") : "";
    final String password = parameters.containsKey("password") ? (String) parameters.get("password") : "";
    String mode = parameters.containsKey("mode") ? ((String) parameters.get("mode")).toUpperCase() : "PATH_EXIST";
    String path = parameters.containsKey("path") ? (String) parameters.get("path") : "";
    String smbHost = parameters.containsKey("smbHost") ? (String) parameters.get("smbHost") : "";
    final String folderIgnoreFiles = parameters.containsKey("folderIgnoreFiles") ? (String) parameters.get("folderIgnoreFiles") : "";
    // changing to Ip address of MonitoredService if no smbHost is given
    if ("".equals(smbHost)) {
        smbHost = svc.getIpAddr();
    }
    // Filename filter to give user the possibility to ignore specific files in folder for the folder check.
    SmbFilenameFilter smbFilenameFilter = new SmbFilenameFilter() {

        @Override
        public boolean accept(SmbFile smbFile, String s) throws SmbException {
            return !s.matches(folderIgnoreFiles);
        }
    };
    // Initialize mode with default as PATH_EXIST
    Mode enumMode = Mode.PATH_EXIST;
    try {
        enumMode = Mode.valueOf(mode);
    } catch (IllegalArgumentException exception) {
        logger.error("Mode '{}‘ does not exists. Valid candidates are {}", mode, modeCandidates);
        return PollStatus.unknown("Mode " + mode + " does not exists. Valid candidates are " + modeCandidates);
    }
    // Checking path parameter
    if (!path.startsWith("/")) {
        path = "/" + path;
        logger.debug("Added leading / to path.");
    }
    // Build authentication string for NtlmPasswordAuthentication: syntax: domain;username:password
    String authString = "";
    // Setting up authenticationString...
    if (domain != null && !"".equals(domain)) {
        authString += domain + ";";
    }
    authString += username + ":" + password;
    // ... and path
    String fullUrl = "smb://" + smbHost + path;
    logger.debug("Domain: [{}], Username: [{}], Password: [{}], Mode: [{}], Path: [{}], Authentication: [{}], Full Url: [{}]", new Object[] { domain, username, password, mode, path, authString, fullUrl });
    // Initializing TimeoutTracker with default values
    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    // Setting default PollStatus
    PollStatus serviceStatus = PollStatus.unknown();
    for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
        NtlmPasswordAuthentication ntlmPasswordAuthentication = new NtlmPasswordAuthentication(authString);
        try {
            // Creating SmbFile object
            SmbFile smbFile = new SmbFile(fullUrl, ntlmPasswordAuthentication);
            // Setting the defined timeout
            smbFile.setConnectTimeout(tracker.getConnectionTimeout());
            // Does the file exists?
            boolean smbFileExists = smbFile.exists();
            switch(enumMode) {
                case PATH_EXIST:
                    if (smbFileExists) {
                        serviceStatus = PollStatus.up();
                    } else {
                        serviceStatus = PollStatus.down("File " + fullUrl + " should exists but doesn't!");
                    }
                    break;
                case PATH_NOT_EXIST:
                    if (!smbFileExists) {
                        serviceStatus = PollStatus.up();
                    } else {
                        serviceStatus = PollStatus.down("File " + fullUrl + " should not exists but does!");
                    }
                    break;
                case FOLDER_EMPTY:
                    if (smbFileExists) {
                        if (smbFile.list(smbFilenameFilter).length == 0) {
                            serviceStatus = PollStatus.up();
                        } else {
                            serviceStatus = PollStatus.down("Directory " + fullUrl + " should be empty but isn't!");
                        }
                    } else {
                        serviceStatus = PollStatus.down("Directory " + fullUrl + " should exists but doesn't!");
                    }
                    break;
                case FOLDER_NOT_EMPTY:
                    if (smbFileExists) {
                        if (smbFile.list(smbFilenameFilter).length > 0) {
                            serviceStatus = PollStatus.up();
                        } else {
                            serviceStatus = PollStatus.down("Directory " + fullUrl + " should not be empty but is!");
                        }
                    } else {
                        serviceStatus = PollStatus.down("Directory " + fullUrl + " should exists but doesn't!");
                    }
                    break;
                default:
                    logger.warn("There is no implementation for the specified mode '{}'", mode);
                    break;
            }
        } catch (MalformedURLException exception) {
            logger.error("Malformed URL on '{}' with error: '{}'", smbHost, exception.getMessage());
            serviceStatus = PollStatus.down(exception.getMessage());
        } catch (SmbException exception) {
            logger.error("SMB error on '{}' with error: '{}'", smbHost, exception.getMessage());
            serviceStatus = PollStatus.down(exception.getMessage());
        }
    }
    return serviceStatus;
}
Also used : SmbException(jcifs.smb.SmbException) MalformedURLException(java.net.MalformedURLException) PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) NtlmPasswordAuthentication(jcifs.smb.NtlmPasswordAuthentication) SmbFilenameFilter(jcifs.smb.SmbFilenameFilter) SmbFile(jcifs.smb.SmbFile)

Aggregations

NtlmPasswordAuthentication (jcifs.smb.NtlmPasswordAuthentication)2 SmbException (jcifs.smb.SmbException)2 SmbFile (jcifs.smb.SmbFile)2 SmbFilenameFilter (jcifs.smb.SmbFilenameFilter)2 MalformedURLException (java.net.MalformedURLException)1 Before (org.junit.Before)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1