Last active
October 7, 2018 12:18
-
-
Save suzukiyuzs/c96bd5b9103c9d2cbc1152b4ddc64867 to your computer and use it in GitHub Desktop.
技術書典5: Githubを使ったMackerelの運用管理 / Githubを使用した実例(2) / インシデント管理システムの構築 / Lambdaファンクション
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/base64" | |
"encoding/json" | |
"fmt" | |
"os" | |
"time" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/kms" | |
"github.com/google/go-github/github" | |
"golang.org/x/oauth2" | |
) | |
type Roles struct { | |
Fullname string `json:"fullname"` | |
ServiceName string `json:"serviceName"` | |
ServiceUrl string `json:"serviceUrl"` | |
RoleName string `json:"roleName"` | |
RoleUrl string `json:"roleUrl"` | |
} | |
type Host struct { | |
Id string `json:"id"` | |
Name string `json:"name"` | |
Url string `json:"url"` | |
Type string `json:"type"` | |
Status string `json:"status"` | |
Memo string `json:"memo"` | |
IsRetired bool `json:"isRetired"` | |
Roles []Roles `json:"roles"` | |
} | |
type Alert struct { | |
CreatedAt int64 `json:"createdAt"` | |
WarningThreshold float64 `json:"warningThreshold"` | |
CriticalThreshold float64 `json:"criticalThreshold"` | |
Duration int `json:"duration"` | |
IsOpen bool `json:"isOpen"` | |
MetricLabel string `json:"metricLabel"` | |
MetricValue float64 `json:"metricValue"` | |
MonitorName string `json:"monitorName"` | |
MonitorOperator string `json:"monitorOperator"` | |
Status string `json:"status"` | |
Trigger string `json:"trigger"` | |
Url string `json:"url"` | |
} | |
type Notification struct { | |
OrgName string `json:"orgName"` | |
Event string `json:"event"` | |
Host Host `json:"host"` | |
Alert Alert `json:"alert"` | |
} | |
const ( | |
layout = "2006-01-02 15:04:05 MST" | |
timezone = "Asia/Tokyo" | |
) | |
var owner string = os.Getenv("OWNER") | |
var repository string = os.Getenv("REPOSITORY") | |
var encToken string = os.Getenv("TOKEN") | |
var decToken string | |
func init() { | |
kmsClient := kms.New(session.New()) | |
decodedBytes, err := base64.StdEncoding.DecodeString(encToken) | |
if err != nil { | |
panic(err) | |
} | |
input := &kms.DecryptInput{ | |
CiphertextBlob: decodedBytes, | |
} | |
response, err := kmsClient.Decrypt(input) | |
if err != nil { | |
panic(err) | |
} | |
decToken = string(response.Plaintext[:]) | |
} | |
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
var notification Notification | |
json.Unmarshal([]byte(request.Body), ¬ification) | |
// Ignore notification of recovery | |
if notification.Alert.Status == "ok" { | |
return events.APIGatewayProxyResponse{Body: "ok", StatusCode: 200}, nil | |
} | |
/* debug | |
for key, value := range request.Headers { | |
fmt.Printf(" %s: %s\n", key, value) | |
} | |
fmt.Println(request.Body) | |
*/ | |
gctx := context.Background() | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: decToken}, | |
) | |
tc := oauth2.NewClient(ctx, ts) | |
client := github.NewClient(tc) | |
status := notification.Alert.Status | |
host := notification.Host.Name | |
monitor := notification.Alert.MonitorName | |
url := notification.Alert.Url | |
utc := time.Unix(notification.Alert.CreatedAt/1000, 0) | |
location, _ := time.LoadLocation(timezone) | |
jst := utc.In(location) | |
creatAt := fmt.Sprintf("%s", jst.Format(layout)) | |
title := fmt.Sprintf("[%s] Host:%s, Monitor:%s", status, host, monitor) | |
body := fmt.Sprintf("Time: %s\nUrl: %s", creatAt, url) | |
opt := &github.IssueRequest{ | |
Title: &title, | |
Body: &body, | |
} | |
client.Issues.Create(gctx, owner, repository, opt) | |
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil | |
} | |
func main() { | |
lambda.Start(handleRequest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment