diff --git a/README.md b/README.md index 0547b66..584c96f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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] @@ -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 diff --git a/zabbix-aws-deregister.go b/zabbix-aws-deregister.go index 31b262b..c4d2da8 100644 --- a/zabbix-aws-deregister.go +++ b/zabbix-aws-deregister.go @@ -36,7 +36,6 @@ type Configuration struct { URL string User string Password string - Deleting bool } // ZabbixHostDisable value corresponding to zabbix host disabled @@ -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 @@ -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 { @@ -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, @@ -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{