Search in sources :

Example 26 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class JmxRrdMigratorOffline method fixJmxConfigurationFile.

/**
 * Fixes a JMX configuration file.
 *
 * @param jmxConfigFile the JMX configuration file
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
private void fixJmxConfigurationFile(File jmxConfigFile) throws OnmsUpgradeException {
    try {
        log("Updating JMX metric definitions on %s\n", jmxConfigFile);
        zipFile(jmxConfigFile);
        backupFiles.add(new File(jmxConfigFile.getAbsolutePath() + ZIP_EXT));
        File outputFile = new File(jmxConfigFile.getCanonicalFile() + ".temp");
        FileWriter w = new FileWriter(outputFile);
        Pattern extRegex = Pattern.compile("import-mbeans[>](.+)[<]");
        Pattern aliasRegex = Pattern.compile("alias=\"([^\"]+\\.[^\"]+)\"");
        List<File> externalFiles = new ArrayList<>();
        LineIterator it = FileUtils.lineIterator(jmxConfigFile);
        while (it.hasNext()) {
            String line = it.next();
            Matcher m = extRegex.matcher(line);
            if (m.find()) {
                externalFiles.add(new File(jmxConfigFile.getParentFile(), m.group(1)));
            }
            m = aliasRegex.matcher(line);
            if (m.find()) {
                String badDs = m.group(1);
                String fixedDs = getFixedDsName(badDs);
                log("  Replacing bad alias %s with %s on %s\n", badDs, fixedDs, line.trim());
                line = line.replaceAll(badDs, fixedDs);
                if (badMetrics.contains(badDs) == false) {
                    badMetrics.add(badDs);
                }
            }
            w.write(line + "\n");
        }
        LineIterator.closeQuietly(it);
        w.close();
        FileUtils.deleteQuietly(jmxConfigFile);
        FileUtils.moveFile(outputFile, jmxConfigFile);
        if (!externalFiles.isEmpty()) {
            for (File configFile : externalFiles) {
                fixJmxConfigurationFile(configFile);
            }
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix " + jmxConfigFile + " because " + e.getMessage(), e);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) File(java.io.File) LineIterator(org.apache.commons.io.LineIterator) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 27 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class JmxRrdMigratorOffline method fixJmxGraphTemplateFile.

/**
 * Fixes a JMX graph template file.
 *
 * @param jmxTemplateFile the JMX template file
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
private void fixJmxGraphTemplateFile(File jmxTemplateFile) throws OnmsUpgradeException {
    try {
        log("Updating JMX graph templates on %s\n", jmxTemplateFile);
        zipFile(jmxTemplateFile);
        backupFiles.add(new File(jmxTemplateFile.getAbsolutePath() + ZIP_EXT));
        File outputFile = new File(jmxTemplateFile.getCanonicalFile() + ".temp");
        FileWriter w = new FileWriter(outputFile);
        Pattern defRegex = Pattern.compile("DEF:.+:(.+\\..+):");
        Pattern colRegex = Pattern.compile("\\.columns=(.+)$");
        Pattern incRegex = Pattern.compile("^include.directory=(.+)$");
        List<File> externalFiles = new ArrayList<>();
        boolean override = false;
        LineIterator it = FileUtils.lineIterator(jmxTemplateFile);
        while (it.hasNext()) {
            String line = it.next();
            Matcher m = incRegex.matcher(line);
            if (m.find()) {
                File includeDirectory = new File(jmxTemplateFile.getParentFile(), m.group(1));
                if (includeDirectory.isDirectory()) {
                    FilenameFilter propertyFilesFilter = new FilenameFilter() {

                        @Override
                        public boolean accept(File dir, String name) {
                            return (name.endsWith(".properties"));
                        }
                    };
                    for (File file : includeDirectory.listFiles(propertyFilesFilter)) {
                        externalFiles.add(file);
                    }
                }
            }
            m = colRegex.matcher(line);
            if (m.find()) {
                String[] badColumns = m.group(1).split(",(\\s)?");
                for (String badDs : badColumns) {
                    String fixedDs = getFixedDsName(badDs);
                    if (fixedDs.equals(badDs)) {
                        continue;
                    }
                    if (badMetrics.contains(badDs)) {
                        override = true;
                        log("  Replacing bad data source %s with %s on %s\n", badDs, fixedDs, line);
                        line = line.replaceAll(badDs, fixedDs);
                    } else {
                        log("  Warning: a bad data source not related with JMX has been found: %s (this won't be updated)\n", badDs);
                    }
                }
            }
            m = defRegex.matcher(line);
            if (m.find()) {
                String badDs = m.group(1);
                if (badMetrics.contains(badDs)) {
                    override = true;
                    String fixedDs = getFixedDsName(badDs);
                    log("  Replacing bad data source %s with %s on %s\n", badDs, fixedDs, line);
                    line = line.replaceAll(badDs, fixedDs);
                } else {
                    log("  Warning: a bad data source not related with JMX has been found: %s (this won't be updated)\n", badDs);
                }
            }
            w.write(line + "\n");
        }
        LineIterator.closeQuietly(it);
        w.close();
        if (override) {
            FileUtils.deleteQuietly(jmxTemplateFile);
            FileUtils.moveFile(outputFile, jmxTemplateFile);
        } else {
            FileUtils.deleteQuietly(outputFile);
        }
        if (!externalFiles.isEmpty()) {
            for (File configFile : externalFiles) {
                fixJmxGraphTemplateFile(configFile);
            }
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix " + jmxTemplateFile + " because " + e.getMessage(), e);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) LineIterator(org.apache.commons.io.LineIterator) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 28 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class JmxRrdMigratorOffline method getJmxResourceDirectories.

/**
 * Gets the JMX resource directories.
 *
 * @return the JMX resource directories
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
private List<File> getJmxResourceDirectories() throws OnmsUpgradeException {
    if (jmxResourceDirectories == null) {
        jmxResourceDirectories = new ArrayList<>();
        CollectdConfiguration config;
        try {
            config = new CollectdConfigFactory().getCollectdConfig();
        } catch (Exception e) {
            throw new OnmsUpgradeException("Can't upgrade the JRBs because " + e.getMessage(), e);
        }
        List<String> services = getJmxServices(config);
        log("JMX services found: %s\n", services);
        List<String> jmxFriendlyNames = new ArrayList<>();
        for (String service : services) {
            Service svc = getServiceObject(config, service);
            if (svc != null) {
                String friendlyName = getSvcPropertyValue(svc, "friendly-name");
                if (friendlyName == null) {
                    // According with JMXCollector, port will be used if there is no friendly-name.
                    friendlyName = getSvcPropertyValue(svc, "port");
                }
                if (friendlyName == null) {
                    log("Warning: there is no friendly-name or port parameter for service %s. The JRBs/RRDs for this service are not going to be updated.", service);
                } else {
                    jmxFriendlyNames.add(friendlyName);
                }
            } else {
                log("Warning: JMX service %s is defined but not used in any package definition. Skipping migration.\n", service);
            }
        }
        log("JMX friendly names found: %s\n", jmxFriendlyNames);
        File rrdDir = new File(jmxDataCollectionConfigDao.getRrdPath());
        findJmxDirectories(rrdDir, jmxFriendlyNames, jmxResourceDirectories);
        if (jmxResourceDirectories.isEmpty()) {
            log("Warning: no JMX directories found on %s\n", rrdDir);
        }
    }
    return jmxResourceDirectories;
}
Also used : CollectdConfiguration(org.opennms.netmgt.config.collectd.CollectdConfiguration) ArrayList(java.util.ArrayList) Service(org.opennms.netmgt.config.collectd.Service) CollectdConfigFactory(org.opennms.netmgt.config.CollectdConfigFactory) File(java.io.File) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 29 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class MagicUsersMigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    if (!canRun()) {
        log("Error: ignoring the execution of the task because the file magic-users.properties was not found. Maybe the task was already successfully executed before.\n");
        return;
    }
    // Parse read-only attributes
    final List<String> readOnlyUsers = new ArrayList<>();
    try {
        boolean readOnly = false;
        for (String line : Files.readAllLines(usersFile.toPath())) {
            if (line.contains("read-only")) {
                Matcher m = Pattern.compile("read-only=\"(.+)\"").matcher(line);
                if (m.find()) {
                    readOnly = Boolean.parseBoolean(m.group(1));
                }
            }
            if (line.contains("user-id")) {
                if (readOnly) {
                    Matcher m = Pattern.compile("user-id[>](.+)[<][/]user-id").matcher(line);
                    if (m.find()) {
                        log("Warning: User %s has read-only flag\n", m.group(1));
                        readOnlyUsers.add(m.group(1));
                    }
                }
                readOnly = false;
            }
        }
        if (!readOnlyUsers.isEmpty()) {
            log("Removing the read-only flags from users.xml\n");
            String content = new String(Files.readAllBytes(usersFile.toPath()), StandardCharsets.UTF_8);
            content = content.replaceAll(" read-only=\".+\"", "");
            Files.write(usersFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix configuration because " + e.getMessage(), e);
    }
    log("Moving security roles into users.xml...\n");
    try {
        UserFactory.init();
        UserManager userManager = UserFactory.getInstance();
        // Retrieve all the currently configured users.
        final List<OnmsUser> users = new ArrayList<>();
        for (final String userName : userManager.getUserNames()) {
            log("Loading configured user: %s...\n", userName);
            users.add(userManager.getOnmsUser(userName));
        }
        // Parse magic-users.properties
        Properties properties = new Properties();
        if (magicUsersFile.exists()) {
            properties.load(new FileInputStream(magicUsersFile));
        } else if (magicUsersFileRPM.exists()) {
            properties.load(new FileInputStream(magicUsersFileRPM));
        } else if (magicUsersFileDEB.exists()) {
            properties.load(new FileInputStream(magicUsersFileDEB));
        } else {
            throw new IllegalArgumentException("Can't find magic-users.properties, or any RPM/DEB backup of it");
        }
        // Look up for custom users and their passwords
        String[] configuredUsers = BundleLists.parseBundleList(properties.getProperty("users"));
        for (String user : configuredUsers) {
            String username = properties.getProperty("user." + user + ".username");
            String password = properties.getProperty("user." + user + ".password");
            OnmsUser newUser = new OnmsUser();
            newUser.setUsername(username);
            newUser.setFullName(user);
            newUser.setComments("This is a system user, do not delete");
            newUser.setPassword(userManager.encryptedPassword(password, true));
            newUser.setPasswordSalted(true);
            users.add(0, newUser);
        }
        // Configure security roles
        String[] configuredRoles = BundleLists.parseBundleList(properties.getProperty("roles"));
        for (final String role : configuredRoles) {
            String userList = properties.getProperty("role." + role + ".users");
            if (userList == null) {
                log("Warning: Role configuration for '%s' does not have 'users' parameter.  Expecting a 'role.%s.users' property. The role will not be usable.\n", role, role);
                continue;
            }
            String[] authUsers = BundleLists.parseBundleList(userList);
            boolean notInDefaultGroup = "true".equals(properties.getProperty("role." + role + ".notInDefaultGroup"));
            String securityRole = "ROLE_" + role.toUpperCase();
            List<String> customRoles = new ArrayList<>();
            for (final String username : authUsers) {
                OnmsUser onmsUser = getUser(users, username);
                if (onmsUser == null) {
                    log("Warning: User %s doesn't exist on users.xml, Ignoring.\n", username);
                } else {
                    addRole(onmsUser, securityRole);
                    if (!notInDefaultGroup && !securityRole.equals(Authentication.ROLE_ADMIN)) {
                        addRole(onmsUser, Authentication.ROLE_USER);
                    }
                    if (!Authentication.isValidRole(securityRole)) {
                        log("Warning: %s is a custom role.\n", securityRole);
                        customRoles.add(role);
                    }
                }
            }
            if (!customRoles.isEmpty()) {
                String roleList = StringUtils.join(customRoles, ',');
                log("Creating %s with roles: %s\n", Authentication.ROLE_CONFIGURATION_FILE, roleList);
                Properties p = new Properties();
                p.put("roles", roleList);
                File configFile = new File(ConfigFileConstants.getHome(), "etc" + File.separator + Authentication.ROLE_CONFIGURATION_FILE);
                p.store(new FileWriter(configFile), "Custom Roles");
            }
        }
        // Update users.xml
        for (final OnmsUser user : users) {
            if (readOnlyUsers.contains(user.getUsername())) {
                addRole(user, Authentication.ROLE_READONLY);
                if (!user.getRoles().contains(Authentication.ROLE_USER)) {
                    addRole(user, Authentication.ROLE_USER);
                }
            }
            userManager.save(user);
        }
    } catch (Throwable e) {
        throw new OnmsUpgradeException("Can't fix configuration because " + e.getMessage(), e);
    }
}
Also used : Matcher(java.util.regex.Matcher) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Properties(java.util.Properties) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) FileInputStream(java.io.FileInputStream) UserManager(org.opennms.netmgt.config.UserManager) OnmsUser(org.opennms.netmgt.model.OnmsUser) File(java.io.File)

Example 30 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class ServiceConfig1701MigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    try {
        ServiceConfiguration currentCfg = JaxbUtils.unmarshal(ServiceConfiguration.class, configFile);
        log("Current configuration: " + currentCfg.getServices().size() + " services.\n");
        for (int i = currentCfg.getServices().size() - 1; i >= 0; i--) {
            final Service localSvc = (Service) currentCfg.getServices().get(i);
            final String name = localSvc.getName();
            if (oldServices.contains(name)) {
                log("Removing old service %s\n", name);
                currentCfg.getServices().remove(i);
            }
        }
        log("New configuration: " + currentCfg.getServices().size() + " services.\n");
        // now remove
        final StringWriter sw = new StringWriter();
        sw.write("<?xml version=\"1.0\"?>\n");
        sw.write("<!-- NOTE!!!!!!!!!!!!!!!!!!!\n");
        sw.write("The order in which these services are specified is important - for example, Eventd\n");
        sw.write("will need to come up last so that none of the event topic subcribers loose any event.\n");
        sw.write("\nWhen splitting services to run on mutiple VMs, the order of the services should be\n");
        sw.write("maintained\n");
        sw.write("-->\n");
        JaxbUtils.marshal(currentCfg, sw);
        final FileWriter fw = new FileWriter(configFile);
        fw.write(sw.toString());
        fw.close();
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
    }
}
Also used : ServiceConfiguration(org.opennms.netmgt.config.service.ServiceConfiguration) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Service(org.opennms.netmgt.config.service.Service) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Aggregations

OnmsUpgradeException (org.opennms.upgrade.api.OnmsUpgradeException)31 IOException (java.io.IOException)23 File (java.io.File)14 FileWriter (java.io.FileWriter)8 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 Map (java.util.Map)3 Pattern (java.util.regex.Pattern)3 LineIterator (org.apache.commons.io.LineIterator)3 DefaultDataCollectionConfigDao (org.opennms.netmgt.config.DefaultDataCollectionConfigDao)3 Service (org.opennms.netmgt.config.service.Service)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Writer (java.io.Writer)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2