/
AlertDefinitionCommand.java
264 lines (221 loc) · 10.8 KB
/
AlertDefinitionCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/
package org.hyperic.hq.hqapi1.tools;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.hyperic.hq.hqapi1.AlertDefinitionApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.XmlUtil;
import org.hyperic.hq.hqapi1.EscalationApi;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.EscalationResponse;
import org.hyperic.hq.hqapi1.types.Escalation;
import org.hyperic.hq.hqapi1.types.AlertCondition;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
public class AlertDefinitionCommand extends Command {
private static String CMD_LIST = "list";
private static String CMD_SYNC = "sync";
private static String CMD_DELETE = "delete";
private static String[] COMMANDS = { CMD_LIST, CMD_DELETE, CMD_SYNC };
private static String OPT_TYPEALERTS = "typeAlerts";
private static String OPT_EXCLUDE_TYPEALERTS = "excludeTypeAlerts";
private static String OPT_EXCLUDE_IDS = "excludeTypeIds";
private static String OPT_GROUP = "group";
private static String OPT_RESOURCE_NAME = "resourceName";
private static String OPT_ALERT_NAME = "alertName";
private static String OPT_ID = "id";
private static String OPT_BATCH_SIZE = "batchSize";
private static String OPT_ESCLATION = "escalation";
private static String OPT_COND_COUNT = "conditionCount";
private static String OPT_COND_TYPE = "conditionType";
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
}
protected void handleCommand(String[] args) throws Exception {
if (args.length == 0) {
printUsage();
System.exit(-1);
}
if (args[0].equals(CMD_LIST)) {
list(trim(args));
} else if (args[0].equals(CMD_DELETE)) {
delete(trim(args));
} else if (args[0].equals(CMD_SYNC)) {
sync(trim(args));
} else {
printUsage();
System.exit(-1);
}
}
private void list(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_TYPEALERTS, "If specified, only parent resource type " +
"alerts will be returned.");
p.accepts(OPT_EXCLUDE_TYPEALERTS, "If specified, individual alerts " +
"based on resource type alerts will " +
"be excluded.");
p.accepts(OPT_EXCLUDE_IDS, "If specified, parent alert definitions will " +
"not include alert ids.");
p.accepts(OPT_GROUP, "If specified, only show alert definitions for " +
"resources that belong to the specified group.").
withRequiredArg().ofType(String.class);
p.accepts(OPT_RESOURCE_NAME, "If specified, only show alert definitions " +
"belonging to a resource with the given " +
"resource name regex.").
withRequiredArg().ofType(String.class);
p.accepts(OPT_ALERT_NAME, "If specified, only show alert definitions " +
"with names that match the given regex.").
withRequiredArg().ofType(String.class);
p.accepts(OPT_ESCLATION, "If specified, only show alert definitions " +
"that are tied to the named escalation.").
withRequiredArg().ofType(String.class);
p.accepts(OPT_COND_COUNT, "If specified, only show alert definitions " +
"which have the number of conditions " +
"specified")
.withRequiredArg().ofType(Integer.class);
p.accepts(OPT_COND_TYPE, "If specified, only show alert definitions " +
"which have at least one condition of the " +
"given type")
.withRequiredArg().ofType(Integer.class);
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
AlertDefinitionApi definitionApi = api.getAlertDefinitionApi();
EscalationApi escalationApi = api.getEscalationApi();
AlertDefinitionsResponse alertDefs;
if (options.has(OPT_TYPEALERTS)) {
boolean excludeIds = false;
if (options.has(OPT_EXCLUDE_IDS)) {
excludeIds = true;
}
alertDefs = definitionApi.getTypeAlertDefinitions(excludeIds);
} else {
boolean excludeTypeAlerts = false;
if (options.has(OPT_EXCLUDE_TYPEALERTS)) {
excludeTypeAlerts = true;
}
if (options.has(OPT_EXCLUDE_IDS)) {
System.err.println("Option " + OPT_EXCLUDE_IDS + " only valid " +
" when " + OPT_TYPEALERTS + " is specified.");
System.exit(-1);
}
String alertNameFilter = (String)options.valueOf(OPT_ALERT_NAME);
String resourceNameFilter = (String)options.valueOf(OPT_RESOURCE_NAME);
String groupNameFilter = (String)options.valueOf(OPT_GROUP);
Escalation escalation = null;
if (options.has(OPT_ESCLATION)) {
EscalationResponse escalationResponse = escalationApi.
getEscalation((String)getRequired(options, OPT_ESCLATION));
checkSuccess(escalationResponse);
escalation = escalationResponse.getEscalation();
}
alertDefs = definitionApi.getAlertDefinitions(excludeTypeAlerts,
escalation,
alertNameFilter,
resourceNameFilter,
groupNameFilter);
}
if (options.has(OPT_COND_COUNT)) {
Integer count = (Integer)getRequired(options, OPT_COND_COUNT);
for (Iterator<AlertDefinition> i =
alertDefs.getAlertDefinition().iterator(); i.hasNext(); ) {
AlertDefinition d = i.next();
if (d.getAlertCondition().size() != count) {
i.remove();
}
}
}
if (options.has(OPT_COND_TYPE)) {
Integer type = (Integer)getRequired(options, OPT_COND_TYPE);
for (Iterator<AlertDefinition> i =
alertDefs.getAlertDefinition().iterator(); i.hasNext(); ) {
AlertDefinition d = i.next();
boolean hasType = false;
for (AlertCondition c : d.getAlertCondition()) {
if (c.getType() == type) {
hasType = true;
}
}
if (!hasType) {
i.remove();
}
}
}
checkSuccess(alertDefs);
XmlUtil.serialize(alertDefs, System.out, Boolean.TRUE);
}
private void delete(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_ID, "The alert definition id to delete").
withRequiredArg().ofType(Integer.class);
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
AlertDefinitionApi definitionApi = api.getAlertDefinitionApi();
Integer id = (Integer)getRequired(options, OPT_ID);
StatusResponse deleteResponse =
definitionApi.deleteAlertDefinition(id);
checkSuccess(deleteResponse);
System.out.println("Successfully deleted alert definition id " + id);
}
private void sync(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_BATCH_SIZE, "Process the sync in batches of the given size").
withRequiredArg().ofType(Integer.class);
OptionSet options = getOptions(p, args);
AlertDefinitionApi api = getApi(options).getAlertDefinitionApi();
InputStream is = getInputStream(options);
AlertDefinitionsResponse resp =
XmlUtil.deserialize(AlertDefinitionsResponse.class, is);
List<AlertDefinition> definitions = resp.getAlertDefinition();
System.out.println("Syncing " + definitions.size() + " alert definitions");
int numSynced = 0;
if (options.has(OPT_BATCH_SIZE)) {
int batchSize = (Integer)options.valueOf(OPT_BATCH_SIZE);
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);
int fromIndex = i * batchSize;
int toIndex = (fromIndex + batchSize) > definitions.size() ?
definitions.size() : (fromIndex + batchSize);
AlertDefinitionsResponse syncResponse =
api.syncAlertDefinitions(definitions.subList(fromIndex,
toIndex));
checkSuccess(syncResponse);
numSynced += (toIndex - fromIndex);
}
} else {
AlertDefinitionsResponse syncResponse = api.syncAlertDefinitions(definitions);
checkSuccess(syncResponse);
numSynced = definitions.size();
}
System.out.println("Successfully synced " + numSynced + " alert definitions.");
}
}