さて、前回StackStormのインストールを行ったので、今回は実際に何らかの処理を行わせてみる。
StackStormにはブラウザ経由での管理が行えるWebUI管理画面があるのだが、今回はCUIでの設定・操作をする。

まず、StackStormには以下の4つの要素がある。

  • Action → トリガーを受けて、対象のホストに対して処理を行う
  • Trigger → 何かしらの操作やエラー、フラグをトリガーとし、アクションを実行する
  • Sensor → トリガーを検知するセンサー
  • Rule → トリガーとアクションを組み合わせたもの

つまり、TriggerとActionをうまく組み合わせて処理を自動化させればいいということか。
これらはPack(プラグイン)として提供されているので、必要なTriggerやActionについては随時導入してやればいい。

なお、ActionとTriggerはインストール時からいくつか用意されている。
以下のコマンドで確認できる。

bash
st2 action list # Actionの一覧 st2 trigger list # Triggerの一覧
shell
[root@BS-PUB-STACKSTORM ~]# st2 action list +---------------------------------+---------+---------------------------------+ | ref | pack | description | +---------------------------------+---------+---------------------------------+ | chatops.format_execution_result | chatops | Format an execution result 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. | | core.noop | core | Action that does nothing | | core.remote | core | Action to execute arbitrary | | | | linux command remotely. | | core.remote_sudo | core | Action to execute arbitrary | | | | linux command remotely. | | core.sendmail | core | This sends an email | | core.windows_cmd | core | Action to execute arbitrary | | | | Windows command remotely. | | linux.check_loadavg | linux | Check CPU Load Average on a | | | | Host | | linux.check_processes | linux | Check Interesting Processes | | linux.cp | linux | Copy file(s) | | linux.diag_loadavg | linux | Diagnostic workflow for high | | | | load alert | | linux.dig | linux | Dig action | | linux.file_touch | linux | Touches a file | | linux.lsof | linux | Run lsof | | linux.lsof_pids | linux | Run lsof for a group of PIDs | | linux.mv | linux | Move file(s) | | linux.netstat | linux | Run netstat | | linux.netstat_grep | linux | Grep netstat results | | linux.pkill | linux | Kill processes using pkill | | linux.rm | linux | Remove file(s) | | linux.rsync | linux | Copy file(s) from one place to | | | | another w/ rsync | | linux.scp | linux | Secure copy file(s) | | linux.service | linux | Stops, Starts, or Restarts a | | | | service | | linux.traceroute | linux | Traceroute a Host | | linux.vmstat | linux | Run vmstat | | linux.wait_for_ssh | linux | Action which waits for a SSH | | | | server to become accessible. By | | | | default, if no credentials are | | | | provided, this action will try | | | | to authenticate using the | | | | system user username and key | | | | file. | | packs.check_auto_deploy_repo | packs | Check if a given branch in a | | | | ST2 Pack's Git repository | | | | should be auto deployed | | packs.delete | packs | Deletes the pack from local | | | | content repository. | | packs.deploy | packs | Deploy StackStorm Pack(s) | | packs.download | packs | Downloads packs and places it | | | | in the local content | | | | repository. | | packs.expand_repo_name | packs | For supplied ST2 pack Repo | | | | return the Git URL and if it | | | | has a subtree. | | packs.info | packs | Get currently deployed pack | | | | information | | packs.install | packs | Installs packs from st2-contrib | | | | into local content repository. | | | | Will download pack, load the | | | | actions, sensors and rules from | | | | the pack. Note that install | | | | require reboot of some st2 | | | | services. | | packs.load | packs | Action that reloads all st2 | | | | content. | | packs.restart_component | packs | Action that restarts st2 | | | | service. | | packs.setup_virtualenv | packs | Set up virtual environment for | | | | the provided packs | | packs.uninstall | packs | Uninstalls packs from local | | | | content repository. Removes | | | | pack and content from st2. Note | | | | that uninstall require reboot | | | | of some st2 services. | | packs.unload | packs | Unregisters all content from a | | | | pack. | | packs.update_virtualenv | packs | Update / reinstall Python | | | | dependencies listed in | | | | requirements.txt inside the | | | | pack virtual environment | | packs.virtualenv_prerun | packs | Transformation step to conver | | | | packs_status to list of packs. | +---------------------------------+---------+---------------------------------+ [root@BS-PUB-STACKSTORM ~]# st2 trigger list +---------------------------------+-------+---------------------------------+ | ref | pack | description | +---------------------------------+-------+---------------------------------+ | linux.file_watch.line | linux | Trigger which indicates a new | | | | line has been detected | | core.st2.generic.actiontrigger | core | Trigger encapsulating the | | | | completion of an action | | | | execution. | | core.st2.generic.notifytrigger | core | Notification trigger. | | core.st2.action.file_writen | core | Trigger encapsulating action | | | | file being written on disk. | | core.st2.key_value_pair.create | core | Trigger encapsulating datastore | | | | item creation. | | core.st2.key_value_pair.update | core | Trigger encapsulating datastore | | | | set action. | | core.st2.key_value_pair.value_c | core | Trigger encapsulating a change | | hange | | of datastore item value. | | core.st2.key_value_pair.delete | core | Trigger encapsulating datastore | | | | item deletion. | | core.st2.sensor.process_spawn | core | Trigger indicating sensor | | | | process is started up. | | core.st2.sensor.process_exit | core | Trigger indicating sensor | | | | process is stopped. | | 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. | | core.st2.webhook | core | Trigger type for registering | | | | webhooks that can consume | | | | arbitrary payload. | +---------------------------------+-------+---------------------------------+

ローカルサーバでアクションを単体で実行する

とりあえず、アクションを単体で実行してみよう。
ローカルで任意のコマンドを実行するアクションは「core.local(root権限での実行はcore.local_sudo)」になるので、このコマンドを実行する。

bash
st2 run core.local -- コマンド
shell
[root@BS-PUB-STACKSTORM ~]# st2 run core.local -- hostname . id: 5802bd8f2e5b192da9fa8f31 status: succeeded parameters: cmd: hostname result: failed: false return_code: 0 stderr: '' stdout: BS-PUB-STACKSTORM.BLACKNON.LOCAL succeeded: true

リモートサーバでアクションを実行する

当然、リモートサーバに対してもアクションを実行できる。

bash
st2 run core.remote hosts='IPアドレスorホスト名' username='ユーザ名' @private_key='/home/stanley/.ssh/stanley_rsa' -- コマンド
shell
[root@BS-PUB-STACKSTORM ~]# st2 run core.remote hosts='172.XXX.XXX.XXX' username='root' @private _key='/home/stanley/.ssh/stanley_rsa' -- hostname .. id: 5802c2d62e5b192da9fa8f37 status: succeeded parameters: cmd: hostname hosts: 172.XXX.XXX.XXX private_key: '********' username: root result: 172.XXX.XXX.XXX: failed: false return_code: 0 stderr: '' stdout: BS-PUB-CENT7-01.blacknon.local succeeded: true

なお、実行時は以下のようにパスワード認証でも実行させることができる。

bash
st2 run core.remote hosts='IPアドレスorホスト名' username='ユーザ名' password='パスワード' -- コマンド
shell
[root@BS-PUB-STACKSTORM ~]# st2 run core.remote hosts='172.XXX.XXX.XXX' username='root' password='password' -- uname -a .. id: 5802c52b2e5b192da9fa8f40 status: succeeded parameters: cmd: uname -a hosts: 172.XXX.XXX.XXX password: '********' username: root result: 172.XXX.XXX.XXX: failed: false return_code: 0 stderr: '' stdout: 'Linux BS-PUB-CENT7-02.blacknon.local 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux' succeeded: true

トリガーとアクションを関連づけてルールを作成し自動化してみる

センサーの設定

さて、それではStackStormの本来の使い方といってもいい、特定の事象をトリガーとしたアクションの自動実行処理を設定してみよう。
デフォルトでは「linux.FileWatchSensor(ファイルの変動を検知するセンサー)」が入っているので、今回はこれを使ってみよう。

shell
[root@BS-PUB-STACKSTORM ~]# st2 sensor list +-----------------------+-------+-------------------------+---------+ | ref | pack | description | enabled | +-----------------------+-------+-------------------------+---------+ | linux.FileWatchSensor | linux | Sensor which monitors | True | | | | files for new lines | | +-----------------------+-------+-------------------------+---------+ [root@BS-PUB-STACKSTORM ~]# cat /opt/stackstorm/packs/linux/config.yaml file_watch_sensor: file_paths:

「/opt/stackstorm/packs/linux/config.yaml」へ監視対象となるファイルPATHを以下のように記述し、reloadする。
今回は、テスト用に作った「/work」というディレクトリ配下のファイルを対象にする。

yaml
file_watch_sensor: file_paths: "/work/st2_test.txt"

設定ファイルを編集したら、以下のコマンドでreloadする。

bash
st2ctl reload

ルールの作成

センサーの設定が完了したら、ルールを作成する。
ルールの作成は、内容を記述したyamlファイルを読み込ませることで行える。

そこで、以下のようなyamlファイルを作成した。

●test.yaml

yaml
--- name: "st2_testrule" description: "StackStorm Test Rule" enabled: true trigger: ref: "linux.file_watch.line" type: "linux.file_watch.line" criteria: trigger.line: type: "matchregex" pattern: "abc" action: ref: "core.local" parameters: cmd: "echo \"$(date +%Y%m%d) test line\" >> /work/test_output2.txt"

上記yamlファイルを読み込んでRuleを作成する。

bash
st2 rule create test.yaml
shell
[root@BS-PUB-STACKSTORM ~]# st2 rule create test.yaml +-------------+--------------------------------------------------------------+ | Property | Value | +-------------+--------------------------------------------------------------+ | id | 58030da32e5b190deeeb7bd7 | | name | st2_testrule | | pack | default | | description | StackStorm Test Rule | | action | { | | | "ref": "core.local", | | | "parameters": { | | | "cmd": "echo "$(date +%Y%m%d) test line" >> | | | /work/test_output2.txt" | | | } | | | } | | criteria | { | | | "trigger.line": { | | | "pattern": "abc", | | | "type": "matchregex" | | | } | | | } | | enabled | True | | ref | default.st2_testrule | | tags | | | trigger | { | | | "type": "linux.file_watch.line", | | | "ref": "linux.file_watch.line", | | | "parameters": {} | | | } | | type | { | | | "ref": "standard", | | | "parameters": {} | | | } | | uid | rule:default:st2_testrule | +-------------+--------------------------------------------------------------+

これで、「/work/st2_test.txt」というファイルに「abc」というキーワードを含む行が挿入されたら、「/work/test_output.txt」ファイルに書き込みが行われるようになった。
うまく動かない場合は、Sensorの動きがおかしい可能性があるので、以下のコマンドで再度Sensorのみリロードしてみるといいだろう。

bash
st2ctl reload --register-sensors