Skip to content

Commit bd6f706

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-5261: added support for Alert publishing via ROOT Admin API
Conflicts: engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateServiceImpl.java engine/storage/volume/src/org/apache/cloudstack/storage/datastore/provider/DefaultHostListener.java engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java plugins/hypervisors/hyperv/src/com/cloud/hypervisor/hyperv/discoverer/HypervServerDiscoverer.java plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareServerDiscoverer.java plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/discoverer/XcpServerDiscoverer.java server/src/com/cloud/alert/AlertManagerImpl.java server/src/com/cloud/alert/ConsoleProxyAlertAdapter.java server/src/com/cloud/alert/SecondaryStorageVmAlertAdapter.java server/src/com/cloud/configuration/ConfigurationManagerImpl.java server/src/com/cloud/ha/HighAvailabilityManagerExtImpl.java server/src/com/cloud/ha/HighAvailabilityManagerImpl.java server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java server/src/com/cloud/vm/UserVmManagerImpl.java usage/src/com/cloud/usage/UsageAlertManagerImpl.java usage/src/com/cloud/usage/UsageManagerImpl.java listAlerts: introduced new parameter "name" to the alertResponse Conflicts: api/src/org/apache/cloudstack/api/command/admin/resource/ListAlertsCmd.java server/src/com/cloud/alert/AlertManagerImpl.java usage/src/com/cloud/usage/UsageAlertManagerImpl.java Added new Admin API - generateAlert. Available to ROOT admin only Conflicts: api/src/org/apache/cloudstack/alert/AlertService.java api/src/org/apache/cloudstack/api/BaseCmd.java usage/src/com/cloud/usage/UsageAlertManagerImpl.java listAlerts: implemented search by alert name Conflicts: api/src/org/apache/cloudstack/alert/AlertService.java api/src/org/apache/cloudstack/api/command/admin/resource/ListAlertsCmd.java engine/schema/src/com/cloud/alert/AlertVO.java
1 parent 751d8d1 commit bd6f706

36 files changed

Lines changed: 465 additions & 249 deletions

File tree

api/src/com/cloud/alert/Alert.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ public interface Alert extends Identity, InternalIdentity {
3939
Date getResolved();
4040

4141
boolean getArchived();
42+
String getName();
4243
}

api/src/com/cloud/event/EventTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ public class EventTypes {
450450
// Object store migration
451451
public static final String EVENT_MIGRATE_PREPARE_SECONDARY_STORAGE = "MIGRATE.PREPARE.SS";
452452

453+
//Alert generation
454+
public static final String ALERT_GENERATE = "ALERT.GENERATE";
455+
456+
453457
static {
454458

455459
// TODO: need a way to force author adding event types to declare the entity details as well, with out braking
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
package org.apache.cloudstack.alert;
19+
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import com.cloud.capacity.Capacity;
24+
import com.cloud.exception.InvalidParameterValueException;
25+
26+
public interface AlertService {
27+
public static class AlertType {
28+
private static Set<AlertType> defaultAlertTypes = new HashSet<AlertType>();
29+
private final String name;
30+
private final short type;
31+
32+
private AlertType(short type, String name, boolean isDefault) {
33+
this.name = name;
34+
this.type = type;
35+
if (isDefault) {
36+
defaultAlertTypes.add(this);
37+
}
38+
}
39+
40+
public static final AlertType ALERT_TYPE_MEMORY = new AlertType(Capacity.CAPACITY_TYPE_MEMORY, "ALERT.MEMORY", true);
41+
public static final AlertType ALERT_TYPE_CPU = new AlertType(Capacity.CAPACITY_TYPE_CPU, "ALERT.CPU", true);
42+
public static final AlertType ALERT_TYPE_STORAGE = new AlertType(Capacity.CAPACITY_TYPE_STORAGE, "ALERT.STORAGE", true);
43+
public static final AlertType ALERT_TYPE_STORAGE_ALLOCATED = new AlertType(Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, "ALERT.STORAGE.ALLOCATED", true);
44+
public static final AlertType ALERT_TYPE_VIRTUAL_NETWORK_PUBLIC_IP = new AlertType(Capacity.CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP, "ALERT.NETWORK.PUBLICIP", true);
45+
public static final AlertType ALERT_TYPE_PRIVATE_IP = new AlertType(Capacity.CAPACITY_TYPE_PRIVATE_IP, "ALERT.NETWORK.PRIVATEIP", true);
46+
public static final AlertType ALERT_TYPE_SECONDARY_STORAGE = new AlertType(Capacity.CAPACITY_TYPE_SECONDARY_STORAGE, "ALERT.STORAGE.SECONDARY", true);
47+
public static final AlertType ALERT_TYPE_HOST = new AlertType((short)7, "ALERT.COMPUTE.HOST", true);
48+
public static final AlertType ALERT_TYPE_USERVM = new AlertType((short)8, "ALERT.USERVM", true);
49+
public static final AlertType ALERT_TYPE_DOMAIN_ROUTER = new AlertType((short)9, "ALERT.SERVICE.DOMAINROUTER", true);
50+
public static final AlertType ALERT_TYPE_CONSOLE_PROXY = new AlertType((short)10, "ALERT.SERVICE.CONSOLEPROXY", true);
51+
public static final AlertType ALERT_TYPE_ROUTING = new AlertType((short)11, "ALERT.NETWORK.ROUTING", true);
52+
public static final AlertType ALERT_TYPE_STORAGE_MISC = new AlertType((short)12, "ALERT.STORAGE.MISC", true);
53+
public static final AlertType ALERT_TYPE_USAGE_SERVER = new AlertType((short)13, "ALERT.USAGE", true);
54+
public static final AlertType ALERT_TYPE_MANAGMENT_NODE = new AlertType((short)14, "ALERT.MANAGEMENT", true);
55+
public static final AlertType ALERT_TYPE_DOMAIN_ROUTER_MIGRATE = new AlertType((short)15, "ALERT.NETWORK.DOMAINROUTERMIGRATE", true);
56+
public static final AlertType ALERT_TYPE_CONSOLE_PROXY_MIGRATE = new AlertType((short)16, "ALERT.SERVICE.CONSOLEPROXYMIGRATE", true);
57+
public static final AlertType ALERT_TYPE_USERVM_MIGRATE = new AlertType((short)17, "ALERT.USERVM.MIGRATE", true);
58+
public static final AlertType ALERT_TYPE_VLAN = new AlertType((short)18, "ALERT.NETWORK.VLAN", true);
59+
public static final AlertType ALERT_TYPE_SSVM = new AlertType((short)19, "ALERT.SERVICE.SSVM", true);
60+
public static final AlertType ALERT_TYPE_USAGE_SERVER_RESULT = new AlertType((short)20, "ALERT.USAGE.RESULT", true);
61+
public static final AlertType ALERT_TYPE_STORAGE_DELETE = new AlertType((short)21, "ALERT.STORAGE.DELETE", true);
62+
public static final AlertType ALERT_TYPE_UPDATE_RESOURCE_COUNT = new AlertType((short)22, "ALERT.RESOURCE.COUNT", true);
63+
public static final AlertType ALERT_TYPE_USAGE_SANITY_RESULT = new AlertType((short)23, "ALERT.USAGE.SANITY", true);
64+
public static final AlertType ALERT_TYPE_DIRECT_ATTACHED_PUBLIC_IP = new AlertType((short)24, "ALERT.NETWORK.DIRECTPUBLICIP", true);
65+
public static final AlertType ALERT_TYPE_LOCAL_STORAGE = new AlertType((short)25, "ALERT.STORAGE.LOCAL", true);
66+
public static final AlertType ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED = new AlertType((short)26, "ALERT.RESOURCE.EXCEED", true);
67+
public static final AlertType ALERT_TYPE_SYNC = new AlertType((short)27, "ALERT.TYPE.SYNC", true);
68+
69+
public short getType() {
70+
return type;
71+
}
72+
73+
public String getName() {
74+
return name;
75+
}
76+
77+
private static AlertType getAlertType(short type) {
78+
for (AlertType alertType : defaultAlertTypes) {
79+
if (alertType.getType() == type) {
80+
return alertType;
81+
}
82+
}
83+
return null;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return String.valueOf(this.getType());
89+
}
90+
91+
public static AlertType generateAlert(short type, String name) {
92+
AlertType defaultAlert = getAlertType(type);
93+
if (defaultAlert != null && !defaultAlert.getName().equalsIgnoreCase(name)) {
94+
throw new InvalidParameterValueException("There is a default alert having type " + type + " and name " + defaultAlert.getName());
95+
} else {
96+
return new AlertType(type, name, false);
97+
}
98+
}
99+
}
100+
101+
boolean generateAlert(AlertType alertType, long dataCenterId, Long podId, String msg);
102+
103+
}

api/src/org/apache/cloudstack/api/BaseCmd.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626

2727
import javax.inject.Inject;
2828

29-
import org.apache.log4j.Logger;
30-
3129
import org.apache.cloudstack.affinity.AffinityGroupService;
30+
import org.apache.cloudstack.alert.AlertService;
3231
import org.apache.cloudstack.network.element.InternalLoadBalancerElementService;
3332
import org.apache.cloudstack.network.lb.ApplicationLoadBalancerService;
3433
import org.apache.cloudstack.network.lb.InternalLoadBalancerVMService;
3534
import org.apache.cloudstack.query.QueryService;
3635
import org.apache.cloudstack.usage.UsageService;
36+
import org.apache.log4j.Logger;
3737

3838
import com.cloud.configuration.ConfigurationService;
3939
import com.cloud.domain.Domain;
@@ -191,9 +191,12 @@ public enum HTTPMethod {
191191
public InternalLoadBalancerVMService _internalLbSvc;
192192
@Inject
193193
public NetworkModel _ntwkModel;
194-
194+
@Inject
195+
public AlertService _alertSvc;
196+
195197
public abstract void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException,
196-
ResourceAllocationException, NetworkRuleConflictException;
198+
ResourceAllocationException, NetworkRuleConflictException;
199+
197200

198201
public void configure() {
199202
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.admin.alert;
18+
19+
import org.apache.cloudstack.alert.AlertService;
20+
import org.apache.cloudstack.alert.AlertService.AlertType;
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.BaseAsyncCmd;
25+
import org.apache.cloudstack.api.Parameter;
26+
import org.apache.cloudstack.api.ServerApiException;
27+
import org.apache.cloudstack.api.response.PodResponse;
28+
import org.apache.cloudstack.api.response.SuccessResponse;
29+
import org.apache.cloudstack.api.response.ZoneResponse;
30+
import org.apache.log4j.Logger;
31+
32+
import com.cloud.event.EventTypes;
33+
34+
@APICommand(name = "generateAlert", description = "Generates an alert", responseObject = SuccessResponse.class, since="4.3")
35+
public class GenerateAlertCmd extends BaseAsyncCmd {
36+
37+
public static final Logger s_logger = Logger.getLogger(GenerateAlertCmd.class.getName());
38+
39+
private static final String s_name = "generatealertresponse";
40+
41+
// ///////////////////////////////////////////////////
42+
// ////////////// API parameters /////////////////////
43+
// ///////////////////////////////////////////////////
44+
45+
@Parameter(name = ApiConstants.TYPE, type = CommandType.SHORT, description = "Type of the alert", required=true)
46+
private Short type;
47+
48+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "Name of the alert", required=true)
49+
private String name;
50+
51+
@Parameter(name = ApiConstants.DESCRIPTION, type = CommandType.STRING, description = "Alert description", required=true)
52+
private String description;
53+
54+
@Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType=ZoneResponse.class, description="Zone id for which alert is generated")
55+
private Long zoneId;
56+
57+
@Parameter(name=ApiConstants.POD_ID, type=CommandType.UUID, entityType=PodResponse.class, description="Pod id for which alert is generated")
58+
private Long podId;
59+
60+
// ///////////////////////////////////////////////////
61+
// ///////////////// Accessors ///////////////////////
62+
// ///////////////////////////////////////////////////
63+
@Override
64+
public String getCommandName() {
65+
return s_name;
66+
}
67+
68+
public Short getType() {
69+
return type;
70+
}
71+
72+
public String getName() {
73+
return name;
74+
}
75+
76+
public String getDescription() {
77+
return description;
78+
}
79+
80+
public long getZoneId() {
81+
if (zoneId == null) {
82+
return 0L;
83+
}
84+
return zoneId;
85+
}
86+
87+
public Long getPodId() {
88+
return podId;
89+
}
90+
91+
92+
// ///////////////////////////////////////////////////
93+
// ///////////// API Implementation///////////////////
94+
// ///////////////////////////////////////////////////
95+
96+
97+
98+
@Override
99+
public void execute() {
100+
AlertType alertType = AlertService.AlertType.generateAlert(getType(), getName());
101+
if (_alertSvc.generateAlert(alertType, getZoneId(), getPodId(), getDescription())) {
102+
SuccessResponse response = new SuccessResponse(getCommandName());
103+
this.setResponseObject(response);
104+
} else {
105+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate an alert");
106+
}
107+
}
108+
109+
@Override
110+
public String getEventType() {
111+
return EventTypes.ALERT_GENERATE;
112+
}
113+
114+
@Override
115+
public String getEventDescription() {
116+
return "Generating alert of type " + type + "; name " + name;
117+
}
118+
119+
@Override
120+
public long getEntityOwnerId() {
121+
return 0;
122+
}
123+
}

api/src/org/apache/cloudstack/api/command/admin/resource/ListAlertsCmd.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22-
import org.apache.log4j.Logger;
23-
2422
import org.apache.cloudstack.api.APICommand;
2523
import org.apache.cloudstack.api.ApiConstants;
2624
import org.apache.cloudstack.api.BaseListCmd;
2725
import org.apache.cloudstack.api.Parameter;
2826
import org.apache.cloudstack.api.response.AlertResponse;
2927
import org.apache.cloudstack.api.response.ListResponse;
28+
import org.apache.log4j.Logger;
3029

3130
import com.cloud.alert.Alert;
3231
import com.cloud.utils.Pair;
@@ -47,6 +46,9 @@ public class ListAlertsCmd extends BaseListCmd {
4746

4847
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, description = "list by alert type")
4948
private String type;
49+
50+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "list by alert name", since="4.3")
51+
private String name;
5052

5153
// ///////////////////////////////////////////////////
5254
// ///////////////// Accessors ///////////////////////
@@ -59,6 +61,10 @@ public Long getId() {
5961
public String getType() {
6062
return type;
6163
}
64+
65+
public String getName() {
66+
return name;
67+
}
6268

6369
// ///////////////////////////////////////////////////
6470
// ///////////// API Implementation///////////////////
@@ -80,6 +86,7 @@ public void execute() {
8086
alertResponse.setAlertType(alert.getType());
8187
alertResponse.setDescription(alert.getSubject());
8288
alertResponse.setLastSent(alert.getLastSent());
89+
alertResponse.setName(alert.getName());
8390

8491
alertResponse.setObjectName("alert");
8592
alertResponseList.add(alertResponse);

api/src/org/apache/cloudstack/api/response/AlertResponse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class AlertResponse extends BaseResponse {
4242
+ "MANAGMENT_NODE = 13: lost connection to default route (to the gateway), "
4343
+ "DOMAIN_ROUTER_MIGRATE = 14, CONSOLE_PROXY_MIGRATE = 15, USERVM_MIGRATE = 16, VLAN = 17, SSVM = 18, " + "USAGE_SERVER_RESULT = 19")
4444
private Short alertType;
45+
46+
@SerializedName(ApiConstants.NAME) @Param(description="the name of the alert", since="4.3")
47+
private String alertName;
4548

4649
@SerializedName(ApiConstants.DESCRIPTION)
4750
@Param(description = "description of the alert")
@@ -66,4 +69,8 @@ public void setDescription(String description) {
6669
public void setLastSent(Date lastSent) {
6770
this.lastSent = lastSent;
6871
}
72+
73+
public void setName(String name) {
74+
this.alertName = name;
75+
}
6976
}

client/tomcatconf/commands.properties.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ deleteEvents=15
246246
listAlerts=3
247247
archiveAlerts=1
248248
deleteAlerts=1
249+
generateAlert=1
249250

250251
#### system capacity commands
251252
listCapacity=3

0 commit comments

Comments
 (0)