mackerelのグラフアノテーションをChatOpsに加えてみた
mackerelからリリースされたグラフアノテーションを追加できるコマンドラインツールをgoで作りました。
作ったコマンドをBot経由で実行できるうようにしてチームのChatOpsに加えていきたいのでbot scriptも作りました。
コマンド作成方法やslack経由で実行できるようにする手順などの紹介エントリです。
コマンドラインツールをつくる
それではmackerelにグラフアノテーションを追加するコマンドラインツールを作っていきます。
クライアントにはmackerel-client-goを使います。
cobraはKubernetesやDocker(distribution)などで採用されています。
cobraを使えば簡単にサブコマンドを作れます。
どんなコマンドを作るか
次のようなコマンドを作ります。
$ graph-annotation post -s 'アノテーションのタイトル' MackerelRole1 MackerelRole2
flagの-sでグラフに表示されるアノテーションのタイトルを指定できます。
パラメータにはrole名を複数指定できます。
チャットからメッセージを入力したいのでflagsを少なくします。
ファイル構成
ファイル構成は次のようになりました。
./cmd ├── graph-annotations │ └── main.go ├── post.go └── root.go
次からはcobraでコマンドを作る手順です。
コマンドの概要はroot.goにまとめる
コマンドの概要をroot.goにまとめます。
var RootCmd = &cobra.Command{ Use: "graph-annotations", Short: "Graph-annotations is a very simple tool for mackerel graph annotations API.", Long: "Complete API documentation is available at https://mackerel.io/api-docs/entry/graph-annotations", Run: func(cmd *cobra.Command, args []string) { }, } func init() { cobra.OnInitialize() }
グラフアノテーションの追加はpost.goにまとめる
グラフアノテーションの追加(mackerel-client-goをつかっているところ)はpost.goに実装しています。
func init() { postCmd.Flags().StringVarP(&title, "title", "s", "", "required: annotation title") postCmd.Flags().StringVar(&description, "description", "", "[optional] annotation details") postCmd.Flags().StringVar(&service, "service", os.Getenv("MACKEREL_SERVICE_NAME"), "required: service name, when it's empty value then will use enviroment variable of 'MACKEREL_SERVICE_NAME''") RootCmd.AddCommand(postCmd) } var title string var description string var service string var postCmd = &cobra.Command{ Use: "post", Short: "Creating graph annotations", Long: "Tne post command creates graph annotations via graph-annotations API.", Example: `graph-annotations post -s 'deploy application' ExampleRole1 ExampleRole2 graph-annotations post --service ExampleService -s 'deploy application' ExampleRole1 ExampleRole2`, RunE: func(cmd *cobra.Command, args []string) error { // flagのバリデーションを省略しています time := time.Now().Unix() client := mkr.NewClient(os.Getenv("MACKEREL_API_KEY")) annotation := &mkr.GraphAnnotation{ Service: service, Roles: args, From: time, To: time, Title: title, Description: description, } err := client.CreateGraphAnnotation(annotation) if err != nil { return errors.Wrap(err, "client error.") } fmt.Printf("completed. params title:%s, from:%d to:%d, service:%s, roles:%s", title, time, time, service, args) return nil }, }
このようにグラフの追加はpost.goにして削除が必要であればdelete.goを追加してサブコマンドを分けることができます。
※ 今回のコマンドではアノテーションをつける時刻範囲は現在時刻にしています。
※ mackerelのサービス名の指定がなければ環境変数の'MACKEREL_SERVICE_NAME'を参照しています。
post.goで整理したコマンドのヘルプ
post.goに整理したコマンドのヘルプは次のように参照できます。
$ graph-annotation help post Tne post command creates graph annotations via graph-annotations API. Usage: graph-annotations post [flags] Examples: graph-annotations post -s 'deploy application' ExampleRole1 ExampleRole2 graph-annotations post --service ExampleService -s 'deploy application' ExampleRole1 ExampleRole2 Flags: --description string [optional] annotation details --service string required: service name, when it's empty value then will use enviroment variable of 'MACKEREL_SERVICE_NAME'' (default "local") -s, --title string required: annotation title
hubotを起動してslackに常駐させたBotにコマンドを実行させる
ローカルでhubotを起動してslackを連携させます。
slackで発行したトークンを環境変数に加えます。
$ export HUBOT_SLACK_TOKEN=<your slack token>
hubotを起動します。
$ cd (path-to-hubot) $ ./bin/hubot --adapter slack
すべてローカルで実行することでローカルで起動したhubotがトークンを使いslackと連携できます。
slackでの利用イメージ
次のようなメッセージをBotに送ると先程つくったコマンドが実行されるようにします。
@localbot note -s 'プッシュ通知を送信しました' web
コマンドを実行するscript
コマンドを実行するscriptは次のように'note '以降の文字列をコマンドに渡して実行しています。
child_process = require 'child_process' module.exports = (robot) -> robot.respond /note\s+(.+)$/, (msg) -> option = msg.match[1] child_process.exec "graph-annotation post #{option}", (error, stdout, stderr) -> if !error output = stdout+'' msg.send output else output = stderr+'' msg.send output