Skip to content

Commit 59027ec

Browse files
author
Emmanuel Riviere
committed
feat: create alarm resource
1 parent 5467cb6 commit 59027ec

File tree

6 files changed

+1416
-0
lines changed

6 files changed

+1416
-0
lines changed

docs/resources/alarm.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
subcategory: "Inventory"
3+
page_title: "VMware vSphere: vsphere_alarm"
4+
sidebar_current: "docs-vsphere-resource-inventory-alarm"
5+
description: |-
6+
Provides a VMware vSphere alarm resource. This can be used deployed on all kinds of vSphere inventory objects.
7+
---
8+
9+
# vsphere_alarm
10+
11+
Provides a VMware vSphere alarm resource. This can be used deployed on all kinds of vSphere inventory objects.
12+
13+
## Example Usages
14+
15+
**Create warning alarm with an email action on a datacenter when on a host is disconnected:**
16+
17+
```hcl
18+
resource "vsphere_alarm" "host_disconnected_warning" {
19+
name = "Host disconnected"
20+
description = "Triggers a warning when a host is disconnected"
21+
entity_type = "Datacenter"
22+
entity_id = data.vsphere_datacenter.datacenter.id
23+
24+
state_expression {
25+
operator = "isEqual"
26+
state_path = "runtime.connectionState"
27+
object_type = "HostSystem"
28+
yellow = "disconnected"
29+
}
30+
31+
email_action {
32+
to = "sample@vmware.com"
33+
subject = "Host disconnected"
34+
start_state = "green"
35+
final_state = "yellow"
36+
}
37+
}
38+
```
39+
40+
**Create critical alarm when a host CPU usage is above 95% for 5min:**
41+
42+
```hcl
43+
resource "vsphere_alarm" "host_disconnected_warning" {
44+
name = "Host CPU usage"
45+
description = "Triggers a critical when a host CPU usage is too high"
46+
entity_type = "Datacenter"
47+
entity_id = data.vsphere_datacenter.datacenter.id
48+
49+
metric_expression {
50+
metric_counter_id = 2 # hosted counter ID for CPU usage
51+
operator = "isAbove"
52+
object_type = "HostSystem"
53+
red = 9500
54+
red_interval = 300
55+
}
56+
}
57+
```
58+
59+
**Create alarm to set a host in maintenance when a lacp down event occurs, and remove the maintenance when the lacp is up again:**
60+
61+
```hcl
62+
resource "vsphere_alarm" "lacp_down_on_host" {
63+
name = "LACP down on host"
64+
description = "Set the host is maintenance if lacp goes down"
65+
entity_type = "Datacenter"
66+
entity_id = data.vsphere_datacenter.datacenter.id
67+
68+
# alert
69+
event_expression {
70+
event_type_id = "esx.problem.net.lacp.lag.transition.down"
71+
object_type = "HostSystem"
72+
status = "red"
73+
}
74+
75+
# recovery
76+
event_expression {
77+
event_type_id = "esx.problem.net.lacp.lag.transition.up"
78+
object_type = "HostSystem"
79+
status = "green"
80+
}
81+
82+
# enter maintenance mode if alert is triggered
83+
advanced_action {
84+
start_state = "green"
85+
final_state = "red"
86+
name = "EnterMaintenanceMode_Task"
87+
}
88+
89+
# remove maintenance mode on alert² recovery
90+
advanced_action {
91+
start_state = "red"
92+
final_state = "green"
93+
name = "ExitMaintenanceMode_Task"
94+
}
95+
96+
}
97+
```
98+
99+
## Argument Reference
100+
101+
The following arguments are supported:
102+
103+
* `name` - (Required) The name of the alarm. This name needs to be unique
104+
within the vCenter. Forces a new resource if changed.
105+
* `description` - (Optional) The alarm description.
106+
* `enabled` - (Optional) Whether or not the alarm is enabled.
107+
* `entity_id` - (Required) The ID of the entity the alarm will be created in.
108+
* `entity_type` - (Required) The type of the entity the alarm will be created in.
109+
* `entity_id` - (Required) The ID of the entity the alarm will be created in.
110+
* `expression_operator` - (Optional) The logical link between expressions.
111+
* `event_expression` - (Optional) Alarm trigger expressions based on events.
112+
* `metric_expression` - (Optional) Alarm trigger expressions based on metric values.
113+
* `state_expression` - (Optional) Alarm trigger expressions based on object state changes.
114+
* `snmp_action` - (Optional) Snmp alarm action to trigger depending on the alarm state.
115+
* `email_action` - (Optional) Email alarm action to trigger depending on the alarm state.
116+
* `advanced_action` - (Optional) Advanced alarm action to trigger depending on the alarm state, such as entering maintenance mode.
117+
118+
## Attribute Reference
119+
120+
* `id` - The ID of the alarm.
121+
122+
123+
## Importing
124+
125+
Importing vSphere alarm is not managed.

vsphere/helper_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/vmware/govmomi/vim25/mo"
2727
"github.com/vmware/govmomi/vim25/types"
2828
"github.com/vmware/govmomi/vsan"
29+
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/alarm"
2930
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/clustercomputeresource"
3031
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/contentlibrary"
3132
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/datacenter"
@@ -1401,3 +1402,31 @@ func testGetVsphereRole(s *terraform.State, resourceName string) (string, error)
14011402
}
14021403
return role.Name, nil
14031404
}
1405+
1406+
func testGetVsphereAlarm(s *terraform.State, resourceName string) (string, error) {
1407+
tVars, err := testClientVariablesForResource(s, fmt.Sprintf("vsphere_alarm.%s", resourceName))
1408+
if err != nil {
1409+
return "", err
1410+
}
1411+
id, ok := tVars.resourceAttributes["id"]
1412+
if !ok {
1413+
return "", fmt.Errorf("resource %q has no id", resourceName)
1414+
}
1415+
entityID, ok := tVars.resourceAttributes["entity_id"]
1416+
if !ok {
1417+
return "", fmt.Errorf("resource %q has no id", resourceName)
1418+
}
1419+
entityType, ok := tVars.resourceAttributes["entity_type"]
1420+
if !ok {
1421+
return "", fmt.Errorf("resource %q has no entity_type", resourceName)
1422+
}
1423+
entityMor := types.ManagedObjectReference{
1424+
Type: entityType,
1425+
Value: entityID,
1426+
}
1427+
al, err := alarm.FromID(tVars.client, id, entityMor)
1428+
if err != nil {
1429+
return "", err
1430+
}
1431+
return al.Info.Name, nil
1432+
}

0 commit comments

Comments
 (0)