Skip to content

Instantly share code, notes, and snippets.

@ikrauchanka
Last active August 17, 2016 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikrauchanka/a423e1f64d577f1ea83f44a037816fb9 to your computer and use it in GitHub Desktop.
Save ikrauchanka/a423e1f64d577f1ea83f44a037816fb9 to your computer and use it in GitHub Desktop.
golang show scedulled events for EC2 instances
// thanks to https://github.com/rhartkopf/check_ec2_events/blob/master/check_ec2_events.go
// This program will query Amazon API
// and show scedulled events for EC2 instances
// When you have a slack, you would like to see messages in the room:
// 0 6 * * * root /usr/local/bin/instances_event | /usr/local/bin/slacktee.sh --title "upcoming instances events" > /dev/null
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func connectToAWS() *ec2.EC2 {
screds := credentials.NewSharedCredentials("/root/.aws/credentials", "default")
config := aws.Config{Region: aws.String("us-east-1"), Credentials: screds}
sess := session.New(&config)
if sess == nil {
fmt.Println("problems")
}
svc := ec2.New(sess)
return svc
}
func main() {
svc := connectToAWS()
var params *ec2.DescribeInstanceStatusInput
resp, err := svc.DescribeInstanceStatus(params)
if err != nil {
fmt.Println("Unable to DescribeInstanceStatus", err)
}
for item := range resp.InstanceStatuses {
instance_id := *resp.InstanceStatuses[item].InstanceId
for _, event := range resp.InstanceStatuses[item].Events {
switch code := event.Code; *code {
case "instance-reboot":
fmt.Println("WARNING - instance", instance_id, "will be rebooted at", event.NotBefore, "- Description:", *event.Description)
case "system-reboot":
fmt.Println("WARNING - physical host will be rebooted at", event.NotBefore, "- Description:", *event.Description)
case "system-maintenance":
fmt.Println("WARNING - physical host maintenance will be performed at", event.NotBefore, "- Description:", *event.Description)
case "instance-retirement", "instance-stop":
fmt.Println("CRITICAL - instance", instance_id, "will be retired at", event.NotBefore, "- Description:", *event.Description)
default:
fmt.Println("CRITICAL - unknown event type", *event.Code, "scheduled for", event.NotBefore, "- Description:", *event.Description)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment