Skip to content

Commit

Permalink
MON-129 remove deleting host feature
Browse files Browse the repository at this point in the history
  • Loading branch information
xp-1000 committed Jun 2, 2018
1 parent 3826a25 commit 61ed9e6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 87 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Zabbix Deregister for AWS AutoScaling

Disable or Delete host from zabbix automatically when AWS autoscaling group terminates its corresponding instance:
Cloudwatch event rule catchs the scale down event from autoscaling group then run a lambda function which will connect to Zabbix API to update host.
A lambda function which disable host from zabbix automatically when AWS autoscaling group terminates its corresponding instance.
The host is renamed to avoid collision with new host from same address and its description is updated to allow later purge.

Here are steps of the workflow:

1. Cloudwatch event rule on the source account catchs the scale down event from autoscaling group and push to event bus of the destination account.

2. Similar cloudwatch event rule on the destination account catchs the event and push it to a dedicated SNS topic.

3. The SNS topic will trigger the lambda function which will connect to Zabbix API to update host.

## Zabbix configuration

Expand Down Expand Up @@ -142,7 +150,6 @@ Environment variables:
ZABBIX_URL: https://zabbix.host.tld/api_jsonrpc.php
ZABBIX_USER: previously created user from "Zabbix administration" step (aws-autoScaling-deregister)
ZABBIX_PASS: previously created password from "Zabbix administration" step
DELETING_HOST: true / false
DEBUG: true / false
Encryption configuration:
Enable helpers for encryption in transit: [x]
Expand All @@ -154,8 +161,6 @@ Execution role: set the previously created role

Then, click on `encrypt` button for both `ZABBIX_USER` and `ZABBIX_PASS` environment variables.

Notice: if `DELETING_HOST` is set to `false` so zabbix hosts are not deleted, only disabled.

6/ Create an sns topic and subscribe [AWS console > SNS > Topics > Create topic]

7/ Add subscription on sns topic to the previously created lambda
Expand Down
127 changes: 45 additions & 82 deletions zabbix-aws-deregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Configuration struct {
URL string
User string
Password string
Deleting bool
}

// ZabbixHostDisable value corresponding to zabbix host disabled
Expand Down Expand Up @@ -79,7 +78,6 @@ func decrypt(encrypted string, variable string) string {
// init function to setup environment
func init() {
var ok bool
var err error
var encryptedUser string
var encryptedPass string

Expand Down Expand Up @@ -113,24 +111,6 @@ func init() {
"variable": "ZABBIX_URL",
}).Panic("Environment variable not set")
}
deletingHost := os.Getenv("DELETING_HOST")
if deletingHost != "" {
Config.Deleting, err = strconv.ParseBool(deletingHost)
if err != nil {
log.WithFields(log.Fields{
"stage": "config",
"error": err,
"variable": "DELETING_HOST",
}).Error("Failed to parse boolean")
}
} else {
Config.Deleting = false
}
log.WithFields(log.Fields{
"stage": "config",
"variable": "DELETING_HOST",
"value": Config.Deleting,
}).Debug("Configuration set")

encryptedUser, ok = os.LookupEnv("ZABBIX_USER")
if !ok {
Expand Down Expand Up @@ -271,49 +251,49 @@ func HandleRequest(snsEvents events.SNSEvent) (string, error) {
}).Error("More than one zabbix host found")
return "", fmt.Errorf("more than one hosts found")
} else {
if Config.Deleting {
type description struct {
Time time.Time
Action string
Pending []string
Name string
}

name := strings.Join([]string{"ZDTP", res[0].Host}, "_")

descriptionStruct := description{
Time: time.Now(),
Action: "deregistered by zabbix aws deregister",
Pending: []string{"purge"},
Name: res[0].Host,
}
descriptionJSON, err := json.Marshal(descriptionStruct)
if err != nil {
log.WithFields(log.Fields{
"stage": "delete",
"instance": autoscalingEvent.InstanceID,
"host": res[0].HostId,
}).Debug("Deleting zabbix host")
_, err := api.CallWithError("host.delete", []string{res[0].HostId})
if err != nil {
log.WithFields(log.Fields{
"stage": "delete",
"instance": autoscalingEvent.InstanceID,
"host": res[0].HostId,
"error": err,
}).Error("Deleting zabbix host")
return "", err
}
} else {
type description struct {
Time time.Time
Action string
Pending []string
Name string
}

name := strings.Join([]string{"ZDTP", res[0].Host}, "_")

descriptionStruct := description{
Time: time.Now(),
Action: "deregistered by zabbix aws deregister",
Pending: []string{"purge"},
Name: res[0].Host,
}
descriptionJSON, err := json.Marshal(descriptionStruct)
if err != nil {
log.WithFields(log.Fields{
"stage": "update",
"description": descriptionStruct,
"host": res[0].HostId,
"error": err,
}).Error("Failed marshal json from description struct")
return "", err
}
"stage": "update",
"description": descriptionStruct,
"host": res[0].HostId,
"error": err,
}).Error("Failed marshal json from description struct")
return "", err
}

log.WithFields(log.Fields{
"stage": "update",
"instance": autoscalingEvent.InstanceID,
"host": res[0].HostId,
"description": string(descriptionJSON),
"cur_name": res[0].Host,
"new_name": name,
"enabled": false,
}).Debug("Updating zabbix host")
_, err = api.CallWithError("host.update", zabbix.Params{
"hostid": res[0].HostId,
"host": name,
"name": name,
"description": string(descriptionJSON),
"status": ZabbixHostDisable,
})
if err != nil {
log.WithFields(log.Fields{
"stage": "update",
"instance": autoscalingEvent.InstanceID,
Expand All @@ -322,28 +302,11 @@ func HandleRequest(snsEvents events.SNSEvent) (string, error) {
"cur_name": res[0].Host,
"new_name": name,
"enabled": false,
}).Debug("Updating zabbix host")
_, err = api.CallWithError("host.update", zabbix.Params{
"hostid": res[0].HostId,
"host": name,
"name": name,
"description": string(descriptionJSON),
"status": ZabbixHostDisable,
})
if err != nil {
log.WithFields(log.Fields{
"stage": "update",
"instance": autoscalingEvent.InstanceID,
"host": res[0].HostId,
"description": string(descriptionJSON),
"cur_name": res[0].Host,
"new_name": name,
"enabled": false,
"error": err,
}).Error("Updating zabbix host")
return "", err
}
"error": err,
}).Error("Updating zabbix host")
return "", err
}

}

log.WithFields(log.Fields{
Expand Down

0 comments on commit 61ed9e6

Please sign in to comment.