use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.
the class DiscoveryConfigurationMigratorOffline method execute.
@Override
public void execute() throws OnmsUpgradeException {
Writer out = null;
try {
final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
final Document doc = docBuilder.parse(m_configFile);
final NodeList found = doc.getElementsByTagName("discovery-configuration");
if (found.getLength() == 1 && found.item(0) instanceof Element) {
final Element el = (Element) found.item(0);
el.removeAttribute("threads");
final Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
tf.setOutputProperty(OutputKeys.INDENT, "yes");
out = new StringWriter();
tf.transform(new DOMSource(doc), new StreamResult(out));
FileUtils.write(m_configFile, out.toString());
} else {
throw new OnmsUpgradeException("Unsure how to handle XML node(s): " + found);
}
} catch (final IOException | SAXException | ParserConfigurationException | TransformerException e) {
throw new OnmsUpgradeException("Failed to upgrade discovery-configuration.xml", e);
} finally {
IOUtils.closeQuietly(out);
}
}
use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.
the class JmxRrdMigratorOffline method preExecute.
/* (non-Javadoc)
* @see org.opennms.upgrade.api.OnmsUpgrade#preExecute()
*/
@Override
public void preExecute() throws OnmsUpgradeException {
printMainSettings();
if (isInstalledVersionGreaterOrEqual(1, 12, 2)) {
try {
jmxDataCollectionConfigDao.getConfig();
} catch (Exception e) {
throw new OnmsUpgradeException("Can't initialize jmx-datacollection-config.xml because " + e.getMessage());
}
for (File jmxResourceDir : getJmxResourceDirectories()) {
log("Backing up %s\n", jmxResourceDir);
zipDir(new File(jmxResourceDir.getAbsolutePath() + ZIP_EXT), jmxResourceDir);
}
} else {
throw new OnmsUpgradeException("This upgrade procedure requires at least OpenNMS 1.12.2; the current version is " + getOpennmsVersion());
}
}
use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.
the class MonitoringLocationsMigratorOffline method execute.
@Override
public void execute() throws OnmsUpgradeException {
if (monitoringLocationsConfig == null)
return;
log("Moving monitoring locations into the database...\n");
long count = 0;
try {
Connection connection = null;
final DBUtils dbUtils = new DBUtils(getClass());
try {
connection = DataSourceFactory.getInstance().getConnection();
dbUtils.watch(connection);
PreparedStatement insertLocation = connection.prepareStatement("INSERT INTO monitoringlocations (id, monitoringarea, geolocation, latitude, longitude, priority) VALUES (?,?,?,?,?,?)");
PreparedStatement insertPollingPackage = connection.prepareStatement("INSERT INTO monitoringlocationspollingpackages (monitoringlocationid, packagename) VALUES (?,?)");
PreparedStatement insertCollectionPackage = connection.prepareStatement("INSERT INTO monitoringlocationscollectionpackages (monitoringlocationid, packagename) VALUES (?,?)");
PreparedStatement insertTag = connection.prepareStatement("INSERT INTO monitoringlocationstags (monitoringlocationid, tag) VALUES (?,?)");
dbUtils.watch(insertLocation);
dbUtils.watch(insertPollingPackage);
dbUtils.watch(insertCollectionPackage);
dbUtils.watch(insertTag);
for (LocationDef location : monitoringLocationsConfig.getLocations()) {
// id
insertLocation.setString(1, location.getLocationName());
// monitoringarea
insertLocation.setString(2, location.getMonitoringArea());
if (location.getGeolocation() != null && !"".equals(location.getGeolocation().trim())) {
// geolocation
insertLocation.setString(3, location.getGeolocation());
} else {
insertLocation.setNull(3, Types.VARCHAR);
}
if (location.getCoordinates() != null && !"".equals(location.getCoordinates())) {
String[] latLong = location.getCoordinates().split(",");
if (latLong.length == 2) {
// latitude
insertLocation.setDouble(4, Double.valueOf(latLong[0]));
// longitude
insertLocation.setDouble(5, Double.valueOf(latLong[1]));
} else {
insertLocation.setNull(4, Types.DOUBLE);
insertLocation.setNull(5, Types.DOUBLE);
}
} else {
insertLocation.setNull(4, Types.DOUBLE);
insertLocation.setNull(5, Types.DOUBLE);
}
if (location.getPriority() == null) {
// priority
insertLocation.setNull(6, Types.INTEGER);
} else {
// priority
insertLocation.setLong(6, location.getPriority());
}
insertLocation.execute();
count++;
if (location.getPollingPackageName() != null && !"".equals(location.getPollingPackageName())) {
// monitoringlocationid
insertPollingPackage.setString(1, location.getLocationName());
// packagename
insertPollingPackage.setString(2, location.getPollingPackageName());
insertPollingPackage.execute();
}
if (location.getCollectionPackageName() != null && !"".equals(location.getCollectionPackageName())) {
// monitoringlocationid
insertCollectionPackage.setString(1, location.getLocationName());
// packagename
insertCollectionPackage.setString(2, location.getCollectionPackageName());
insertCollectionPackage.execute();
}
for (Tag tag : location.getTags()) {
if (tag.getName() != null && !"".equals(tag.getName().trim())) {
// monitoringlocationid
insertTag.setString(1, location.getLocationName());
// tag
insertTag.setString(2, tag.getName());
insertTag.execute();
}
}
}
} finally {
dbUtils.cleanUp();
}
} catch (Throwable e) {
throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
}
log("Moved %d monitoring locations into the database\n", count);
}
use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.
the class MonitoringLocationsMigratorOffline method preExecute.
@Override
public void preExecute() throws OnmsUpgradeException {
if (monitoringLocationsConfig == null)
return;
try {
log("Backing up %s\n", configFile);
zipFile(configFile);
} catch (Exception e) {
throw new OnmsUpgradeException("Can't backup " + configFile + " because " + e.getMessage());
}
}
use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.
the class DataCollectionConfigMigrator17Offline method isConfigValid.
/**
* Checks if is configuration valid.
*
* @return true, if is configuration valid
* @throws OnmsUpgradeException the OpenNMS upgrade exception
*/
private boolean isConfigValid() throws OnmsUpgradeException {
File configDirectory = new File(sourceFile.getParentFile().getAbsolutePath(), "datacollection");
try {
DefaultDataCollectionConfigDao dao = new DefaultDataCollectionConfigDao();
dao.setConfigDirectory(configDirectory.getAbsolutePath());
dao.setConfigResource(new FileSystemResource(sourceFile));
dao.setReloadCheckInterval(new Long(0));
dao.afterPropertiesSet();
} catch (IllegalArgumentException e) {
log("Found a problem: %s\n", e.getMessage());
Matcher m = pattern.matcher(e.getMessage());
if (m.find()) {
try {
Iterator<Path> paths = Files.list(configDirectory.toPath()).filter(f -> f.getFileName().toString().toLowerCase().endsWith(".xml")).iterator();
for (; paths.hasNext(); ) {
String group = getGroupForResourceType(paths.next().toFile(), m.group(1));
if (group != null) {
updateDataCollectionConfig(m.group(2), group);
return false;
}
}
} catch (Exception ex) {
throw new OnmsUpgradeException("Can't get datacollection-group files", ex);
}
}
} catch (Exception e) {
throw new OnmsUpgradeException("Can't process " + sourceFile, e);
}
return true;
}
Aggregations