From 5c8734d9b73c1be01e2bbefe1d1c46c551e74427 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Wed, 24 Feb 2010 09:45:35 -0800 Subject: [PATCH 1/2] [HHQ-3745] Clear session after each definition is processed to prevent exponential slowdown. A more general solution to this issue is needed. --- hqu/hqapi1/app/AlertdefinitionController.groovy | 11 +++++++---- .../hq/hqapi1/tools/AlertDefinitionCommand.java | 5 ++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/hqu/hqapi1/app/AlertdefinitionController.groovy b/hqu/hqapi1/app/AlertdefinitionController.groovy index a7fdc16f..9c709d76 100644 --- a/hqu/hqapi1/app/AlertdefinitionController.groovy +++ b/hqu/hqapi1/app/AlertdefinitionController.groovy @@ -366,6 +366,7 @@ public class AlertdefinitionController extends ApiController { def sync(params) { def syncRequest = new XmlParser().parseText(getPostData()) def definitions = [] + def sess = org.hyperic.hq.hibernate.SessionManager.currentSession() for (xmlDef in syncRequest['AlertDefinition']) { def failureXml = null @@ -866,15 +867,17 @@ public class AlertdefinitionController extends ApiController { pojo.unsetEscalation(user) } - // Keep synced defintions for sync return XML - definitions << pojo + // Keep synced definitions for sync return XML + definitions << pojo.id + sess.flush() + sess.clear() } renderXml() { out << AlertDefinitionsResponse() { out << getSuccessXML() - for (alertdef in definitions) { - out << getAlertDefinitionXML(alertdef, false) + for (alertdefid in definitions) { + out << getAlertDefinitionXML(alertHelper.getById(alertdefid), false) } } } diff --git a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java index 23ffd7d2..a5dc64e0 100644 --- a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java @@ -262,7 +262,7 @@ private void sync(String[] args) throws Exception { int numBatches = (int)Math.ceil(definitions.size()/((double)batchSize)); for (int i = 0; i < numBatches; i++) { - System.out.println("Syncing batch " + (i + 1) + " of " + numBatches); + long start = System.currentTimeMillis(); int fromIndex = i * batchSize; int toIndex = (fromIndex + batchSize) > definitions.size() ? definitions.size() : (fromIndex + batchSize); @@ -271,6 +271,9 @@ private void sync(String[] args) throws Exception { toIndex)); checkSuccess(syncResponse); numSynced += (toIndex - fromIndex); + System.out.println("Synced batch " + (i + 1) + " of " + numBatches + " in " + + (System.currentTimeMillis() - start) + " ms"); + } } else { AlertDefinitionsResponse syncResponse = api.syncAlertDefinitions(definitions); From fea2fa5335e794304ea7293afad50e303297a8e9 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Mon, 8 Mar 2010 12:46:33 -0800 Subject: [PATCH 2/2] [HHQ-3785] Add command lines options to alertdefinition sync command to allow for escalations and alert actions to be set and reset. --- ChangeLog | 3 + .../hqapi1/tools/AlertDefinitionCommand.java | 79 ++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2d39561f..58442da1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ Changes in HQApi 2.4 + *) [HHQ-3785] Add command lines options to alertdefinition sync command + to allow for escalations and alert actions to be set and reset. + *) [HHQ-3740] Allow hqapi.sh to be executed from a symlinked directory. *) Add support for --platform argument to command line group syncing. diff --git a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java index a5dc64e0..eb87ade0 100644 --- a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java @@ -30,9 +30,11 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; import org.hyperic.hq.hqapi1.AlertDefinitionApi; +import org.hyperic.hq.hqapi1.AlertDefinitionBuilder; import org.hyperic.hq.hqapi1.HQApi; import org.hyperic.hq.hqapi1.XmlUtil; import org.hyperic.hq.hqapi1.EscalationApi; +import org.hyperic.hq.hqapi1.types.AlertAction; import org.hyperic.hq.hqapi1.types.AlertDefinition; import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse; import org.hyperic.hq.hqapi1.types.StatusResponse; @@ -66,6 +68,13 @@ public class AlertDefinitionCommand extends Command { private static String OPT_COND_INCLUDE = "conditionTypeInclude"; private static String OPT_COND_EXCLUDE = "conditionTypeExclude"; + // Command line syncing options + private static String OPT_ASSIGN_ESC = "assignEscalation"; + private static String OPT_ASSIGN_SCRIPTACTION = "assignScriptAction"; + private static String OPT_ASSIGN_CONTROLACTION = "assignControlAction"; + private static String OPT_CLEAR_ESC = "clearEscalation"; + private static String OPT_CLEAR_ACTIONS = "clearActions"; + private void printUsage() { System.err.println("One of " + Arrays.toString(COMMANDS) + " required"); } @@ -242,10 +251,25 @@ private void sync(String[] args) throws Exception { p.accepts(OPT_BATCH_SIZE, "Process the sync in batches of the given size"). withRequiredArg().ofType(Integer.class); + p.accepts(OPT_ASSIGN_ESC, "If specified, assign the given Escalation " + + "to all alert definitions in this sync"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_ASSIGN_SCRIPTACTION, "If specified, assign the given Escalation " + + "to all alert definitions in this sync"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_ASSIGN_CONTROLACTION, "If specified, assign the given Escalation " + + "to all alert definitions in this sync"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_CLEAR_ESC, "If specified, clear the assigned escalation from " + + "all alert definitions in this sync"); + p.accepts(OPT_CLEAR_ACTIONS, "If specified, clear alert actions from " + + "all alert definitions in this sync"); OptionSet options = getOptions(p, args); - AlertDefinitionApi api = getApi(options).getAlertDefinitionApi(); + HQApi api = getApi(options); + AlertDefinitionApi adApi = api.getAlertDefinitionApi(); + EscalationApi escApi = api.getEscalationApi(); InputStream is = getInputStream(options); @@ -254,6 +278,53 @@ private void sync(String[] args) throws Exception { List definitions = resp.getAlertDefinition(); + if (options.has(OPT_ASSIGN_ESC)) { + String esc = (String)getRequired(options, OPT_ASSIGN_ESC); + EscalationResponse escResponse = escApi.getEscalation(esc); + checkSuccess(escResponse); + System.out.println("Assigning escalation '" + esc + "' to all alert definitions"); + + for (AlertDefinition a : definitions) { + a.setEscalation(escResponse.getEscalation()); + } + } + + if (options.has(OPT_ASSIGN_SCRIPTACTION)) { + String script = (String)getRequired(options, OPT_ASSIGN_SCRIPTACTION); + AlertAction a = AlertDefinitionBuilder.createScriptAction(script); + System.out.println("Assigning script action '" + script + "' to all alert definitions"); + + for (AlertDefinition def : definitions) { + def.getAlertAction().add(a); + } + } + + if (options.has(OPT_ASSIGN_CONTROLACTION)) { + String action = (String)getRequired(options, OPT_ASSIGN_CONTROLACTION); + System.out.println("Assigning control action '" + action + "' to all alert definitions"); + + for (AlertDefinition def : definitions) { + AlertAction a = AlertDefinitionBuilder.createControlAction(def.getResource(), action); + def.getAlertAction().add(a); + } + } + + if (options.has(OPT_CLEAR_ESC)) { + System.out.println("Clearing escalations for all alert definitions"); + + for (AlertDefinition def : definitions) { + def.setEscalation(null); + } + } + + if (options.has(OPT_CLEAR_ACTIONS)) { + System.out.println("Clearing alert actions for all alert definitions"); + + for (AlertDefinition def : definitions) { + def.getAlertAction().clear(); + } + } + System.out.println("Syncing " + definitions.size() + " alert definitions"); int numSynced = 0; @@ -267,8 +338,8 @@ private void sync(String[] args) throws Exception { int toIndex = (fromIndex + batchSize) > definitions.size() ? definitions.size() : (fromIndex + batchSize); AlertDefinitionsResponse syncResponse = - api.syncAlertDefinitions(definitions.subList(fromIndex, - toIndex)); + adApi.syncAlertDefinitions(definitions.subList(fromIndex, + toIndex)); checkSuccess(syncResponse); numSynced += (toIndex - fromIndex); System.out.println("Synced batch " + (i + 1) + " of " + numBatches + " in " + @@ -276,7 +347,7 @@ private void sync(String[] args) throws Exception { } } else { - AlertDefinitionsResponse syncResponse = api.syncAlertDefinitions(definitions); + AlertDefinitionsResponse syncResponse = adApi.syncAlertDefinitions(definitions); checkSuccess(syncResponse); numSynced = definitions.size(); }