LoginSignup
4
7

More than 5 years have passed since last update.

OpsのためのIFTTT的なStackStormを始めよう

Last updated at Posted at 2016-08-25

StackStormの概要とインストール、基本操作まで今回は解説します。

StackStormの概要

StackStormについては公式サイトのドキュメントをみると分かりますがとりあえず必要な分だけまとめて解説します。

StackStormの用途

StackStormは以下のような用途を想定して作られたツールです。

  • 障害対応の補助

StackStormはNagios(ネットワーク監視)、Sensu(サーバ監視)、New Relic(パフォーマンス監視サービス)やその他モニタリングシステムによりシステムのダウンをキャプチャし、それをHipChat(チェットツール)やJIRA(プロジェクト管理ツール)などに通知する。

  • 自動修復

OpenStackのコンピュートノードのハードウェア障害を検知し、適切にインスタンスを停止させ、ダウンタイムを管理者にメールする。そしてこれらがうまくいかない時はPagerDuty(アラート集約、通知ツール)で人間に連絡する

  • 継続的なデプロイ

NewRelicのパフォーマンスデータからJenkinsのビルドとテスト、新しいAWSクラスタの構築、あるトラフィックに対するロードバランサーの起動、ロールフォワードやロールバックなどを行う

動作環境

  • Ubuntu14.04、CentOS7、CentOS6

Ubuntu16.04はサポート外みたいです。

Install

今回はCentOS release 6.7 (Final)を使用しています。

とりあえずSELinuxを切ります。/etc/selinux/config.confを編集して再起動。

/etc/selinux/config.conf
SELINUX=disabled

再起動

$ reboot

インストール手順も公開されていますが、一発で入るシェルが用意されているのでこちらを仕様します。
以下のpasswordは変更してください。curl: (35) SSL connect errorと言われた場合は'yum update -y'してから以下を行ってください。

$ curl -sSL https://stackstorm.com/packages/install.sh | bash -s -- --user=st2admin --password=<CHANGEME>

私の場合途中で

mongod を起動中: [失敗]
############### ERROR ###############
# Failed on Install st2 dependencies #
#####################################

と言われてしまいました。
/var/log/mongodb/mongodb.logを見に行くと

/var/log/mongodb/mongodb.log
~
ERROR: Insufficient free space for journal file
~

と言われていたので/etc/mongodb.confに以下を追記

/etc/mongodb.conf
smallfiles=true

もう一度実行

$ curl -sSL https://stackstorm.com/packages/install.sh | bash -s -- --user=st2admin --password=<CHANGEME>

今度は成功したようです。
以下のようなものが表示されました。



███████╗████████╗██████╗      ██████╗ ██╗  ██╗
██╔════╝╚══██╔══╝╚════██╗    ██╔═══██╗██║ ██╔╝
███████╗   ██║    █████╔╝    ██║   ██║█████╔╝ 
╚════██║   ██║   ██╔═══╝     ██║   ██║██╔═██╗ 
███████║   ██║   ███████╗    ╚██████╔╝██║  ██╗
╚══════╝   ╚═╝   ╚══════╝     ╚═════╝ ╚═╝  ╚═╝

  st2 is installed and ready to use.

Head to https://YOUR_HOST_IP/ to access the WebUI

Don't forget to dive into our documentation! Here are some resources
for you:

* Documentation  - https://docs.stackstorm.com
* Knowledge Base - https://stackstorm.reamaze.com

Thanks for installing StackStorm! Come visit us in our Slack Channel
and tell us how it's going. We'd love to hear from you!
http://stackstorm.com/community-signup

動作確認

ST2_AUTH_TOKENを登録します。<CHANGEME>は先ほど登録したものに書き換えてください。

$ export ST2_AUTH_TOKEN=`st2 auth -t -p <CHANGEME> st2admin`

まずは以下のコマンドをうって動作確認します。

  • StackStormのバージョン確認
$ st2 --version
st2 1.5.0

これでインストールが完了しました。

基本操作

StackStormはAction、Trigger、Sensor、Ruleの4つの要素で構成され、これらを組み合わせて実行します。

Action

Actionはローカル・リモートのマシンや他ツールに対し行える操作のことです。
実行可能なActionは以下のように確認できます。

$ st2 action list

+----------------------------+---------+----------------------------+
| ref                        | pack    | description                |
+----------------------------+---------+----------------------------+
| chatops.format_execution_r | chatops | Format an execution result |
| esult                      |         | for chatops                |
| chatops.post_message       | chatops | Post a message to stream   |
|                            |         | for chatops                |
| chatops.post_result        | chatops | Post an execution result   |
|                            |         | to stream for chatops      |
| core.announcement          | core    | Action that broadcasts the |
|                            |         | announcement to all stream |
|                            |         | consumers.                 |
| core.http                  | core    | Action that performs an    |
|                            |         | http request.              |
| core.local                 | core    | Action that executes an    |
|                            |         | arbitrary Linux command on |
|                            |         | the localhost.             |
| core.local_sudo            | core    | Action that executes an    |
|                            |         | arbitrary Linux command on |
|                            |         | the localhost.             
.
.
.

このActionを単独で実行させるには以下のように行います。
ここではローカルのコマンドdate -Rを実行させてみます。

$ st2 run core.local -- date -R
id: 57**********
status: succeeded
parameters: 
  cmd: date -R
result: 
  failed: false
  return_code: 0
  stderr: ''
  stdout: Tue, 05 Jul 2016 09:16:04 +0900
  succeeded: true

別のマシン(192.168.0.7)にusernameがaliceでパスワードがpass123でsshできるなら以下のようにしてリモートでコマンドを実行させられます。

$ st2 run core.remote hosts='192.168.0.7' username='alice' password='pass123' -- whoami

id: 577b*****
status: succeeded
parameters: 
  cmd: whoami
  hosts: 192.168.0.7
  password: '********'
  username: alice
result: 
  192.168.0.7:
    failed: false
    return_code: 0
    stderr: ''
    stdout: alice
    succeeded: true

Trigger

何らかの操作やエラーなどをTriggerとして受け取ることができます。
受け取ることができるTriggerの一覧は以下のように確認できます。

$ st2 trigger list
+----------------------------+-------+----------------------------+
| ref                        | pack  | description                |
+----------------------------+-------+----------------------------+
| core.st2.generic.actiontri | core  | Trigger encapsulating the  |
| gger                       |       | completion of an action    |
|                            |       | execution.                 |
| core.st2.generic.notifytri | core  | Notification trigger.      |
| gger                       |       |                            |
| core.st2.action.file_write | core  | Trigger encapsulating      |
| n                          |       | action file being written  |
|                            |       | on disk.                   |
| core.st2.key_value_pair.cr | core  | Trigger encapsulating      |
| eate                       |       | datastore item creation.   |
| core.st2.key_value_pair.up | core  | Trigger encapsulating      |
| date                       |       | datastore set action.      |
| core.st2.key_value_pair.va | core  | Trigger encapsulating a    |
| lue_change                 |       | change of datastore item   |
|                            |       | value.                     |
| core.st2.key_value_pair.de | core  | Trigger encapsulating      |
| lete                       |       | datastore item deletion.   |
| core.st2.sensor.process_sp | core  | Trigger indicating sensor  |
| awn                        |       | process is started up.     |
| core.st2.sensor.process_ex | core  | Trigger indicating sensor  |
| it                         |       | process is stopped.        |
| core.st2.webhook           | core  | Trigger type for           |
|                            |       | registering webhooks that  |
|                            |       | can consume arbitrary      |
|                            |       | payload.                   |
| core.st2.IntervalTimer     | core  | Triggers on specified      |
|                            |       | intervals. e.g. every 30s, |
|                            |       | 1week etc.                 |
| core.st2.DateTimer         | core  | Triggers exactly once when |
|                            |       | the current time matches   |
|                            |       | the specified time. e.g.   |
|                            |       | timezone:UTC               |
|                            |       | date:2014-12-31 23:59:59.  |
| core.st2.CronTimer         | core  | Triggers whenever current  |
|                            |       | time matches the specified |
|                            |       | time constaints like a     |
|                            |       | UNIX cron scheduler.       |
| linux.file_watch.line      | linux | Trigger which indicates a  |
|                            |       | new line has been detected |
+----------------------------+-------+----------------------------+

Sensor

上記のTriggerを受け取るためにはあらかじめSensorを設定しておく必要がある物もあります。

$ st2 sensor list
+----------------------+-------+----------------------+---------+
| ref                  | pack  | description          | enabled |
+----------------------+-------+----------------------+---------+
| linux.FileWatchSenso | linux | Sensor which         | True    |
| r                    |       | monitors files for   |         |
|                      |       | new lines            |         |
+----------------------+-------+----------------------+---------+

Rule

TriggerとActionの組みを設定した物をRuleと呼び、Ruleを設定しておくことで様々な処理を自動化できます。

以下はドキュメントにあるwebhookを利用する例です。

---
    name: "sample_rule_with_webhook"
    pack: "examples"
    description: "Sample rule dumping webhook payload to a file."
    enabled: true

    trigger:
        type: "core.st2.webhook"
        parameters:
            url: "sample"

    criteria:
        trigger.body.name:
            pattern: "st2"
            type: "equals"

    action:
        ref: "core.local"
        parameters:
            cmd: "echo \"{{trigger.body}}\" >> ~/st2.webhook_sample.out"

trigger:でTriggerの種類とそのオプションを指定し、criteria:でTriggerが受け取った内容で条件を指定し、action:で実行する内容を記述しています。

ファイルの変更を監視してみる

\test.txtを監視して\test2.txtに書き込むようなRuleを作成してみます。この二つのファイルはStackStormをインストールした時に作られたstanleyユーザからアクセスできる必要があります。

Sensorを起動する

linux.FileWatchSensorを起動します。/opt/stackstorm/packs/linux/config.yamlfile_paths:に監視対象を追加します。

/opt/stackstorm/packs/linux/config.yaml
file_watch_sensor:
  file_paths: "/test.txt"

Sensorを変更したらStackStormをリロードします。

$ st2ctl reload

これでSensorを起動できました。

Ruleを作成する

Ruleを記述したtest.yamlを作成します。

test.yaml
---
    name: "test_rule"
    pack: "examples"
    description: "test rule"
    enabled: true

    trigger:
        type: "linux.file_watch.line"

    criteria:
        trigger.line:
            pattern: "OK??"
            type: "equals"

    action:
        ref: "core.local"
        parameters:
            cmd: "echo \"OK!!\" >> /test2.txt"

これで\test.txtOK??と書き込まれると\test2.txtOK!!と自動で書き込まれます。

このRuleを登録します。

$ st2 rule create test.yaml

あとは\test.txtOK??と書き込むと\test2.txtOK!!と書き込まれるようになりました。

その他

webhookの使い方
http://qiita.com/ko-he-8/items/22d0b2efd766be6bdbc7

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7