woodywuuu / blogs

7 stars 1 forks source link

StackStorm介绍&实践 #4

Open woodywuuu opened 6 years ago

woodywuuu commented 6 years ago

简介

概述

StackStorm(以下简称ST2)是一个时下非常流行的事件驱动型自动化引擎,擅长处理自动化流程,支持各种服务及工具的自动化和集成。可以轻松实现故障诊断和自动修复,利用st2来实现故障自动诊断以及修复是不错的选择。

优势

以上算是ST2最为简单的流程应用了,其中可定制化的东西非常多,在数据量足够大时,可以考虑使用机器学习等方法进行进一步的智能化。

尝试

前期准备

rule.yaml如下:

---
name: "redis_restart"
pack: "test"
description: "just a test"
enabled: true

trigger: #必选
    type: "core.st2.webhook"
    parameters:
        url: "test" #此处配url

criteria: #可选
    trigger.body.name:
          pattern: "st2"
          type: "equals"

action: #必选
    ref: "core.remote"
    parameters:
        cmd: "redis-server&"
        hosts: "XXXXXXX"
        password: "XXXXXXXX"
        username: "root"
  1. 使用命令st2 rule create /path/to/rule.yaml 注册规则。
  2. 定义了一个钩子,url是“https://($ip)/api/v1/webhooks/test”,带着token,post到这个接口就可以调用这个规则
  3. 其中criteria(匹配规则)支持非常多的自定义,最方便应用的应该就是正则了。
  4. action可以填写某个指令,也可以由不同动作组成,更好的是写成动作链(ActionChain),非常灵活&健壮。(以上详见下文Further)
    • 介于对zabbix的变量传递还不熟悉,原本的告警脚本中拼接url的部分被我简化,直接写了个拼url的脚本,将数据直接传输过去。流程就是:申请token,拼url,Post到指定接口。

非常简单的监控脚本:

#!/usr/bin/env python

import requests
import simplejson
#头部包括类型&认证token
headers = {
    'x-auth-token': 'Token_Here',
    'Content-Type': 'application/json',
}
#传递数据
payload = {
    "you": "too",
    "name": "st2"
}
payload = simplejson.dumps(payload)
requests.post("https://($ip)/api/v1/webhooks/test",data=payload,headers=headers,verify=False)

Further

关于Webhook

Webhook与Sensors都可以接收信息,不同之处在于:

校验的所有类型在st2/st2common/st2common/operators.py中定义,节选部分如下表:

操作符 描述
equals 触发器值与所给值相等(任意类型)
nequals 触发器值与所给值不相等(任意类型)
lessthan 触发器值小于所给值
regex 正则匹配(基本是PY的re库实现)
contains 触发器值包含所给值(触发器值可为string或是list)
startswith 字符类型触发器值的开头匹配所给值
exists Key存在于Payload中

动作

动作是ST2执行的最小单元,可以使用任意编程语言撰写。每个动作基本由两个文件组成:动作描述文件(yaml),执行文件(类型不定)。

动作描述文件:

找了个ST2内置的发邮件的动作,其描述文件如下:

---
name: sendmail
description: This sends an email
entry_point: send_mail/send_mail
runner_type: "local-shell-script"
enabled: true
parameters:
  from:
    description: Sender email address.
    position: 0
    required: false
    type: string
    default: "stanley"
  to:
    description: Recipient email address.
    position: 1
    required: true
    type: string
  subject:
    description: Subject of the email.
    position: 2
    required: true
    type: string
  send_empty_body:
    description: Send a message even if the body is empty.
    position: 3
    required: false
    type: boolean
    default: True
  content_type:
    type: string
    description: Content type of message to be sent
    default: "text/html"
    position: 4
  body:
    description: Body of the email.
    position: 5
    required: true
    type: string
  sudo:
    immutable: true
  attachments:
    description: Array of attachment file paths, comma-delimited.
    position: 6
    required: false
    type: "string"

由以下几部分组成:

一个动作链需要两个文件组成,描述文件&执行文件,均采用yaml语法。描述文件定义了名称、入口文件、参数和通知。而执行文件列了一系列的动作,并且提供了每一步成功或是失败的回调。简单执行文件如下:

---
chain:
    -
        name: "c1"
        ref: "core.local"
        parameters:
            cmd: "echo c1"
        on-success: "c2"
        on-failure: "c4"
    -
        name: "c2"
        ref: "core.local"
        parameters:
            cmd: "echo \"c2: parent exec is {{action_context.parent.execution_id}}.\""
        on-success: "c3"
        on-failure: "c4"
    -
        name: "c3"
        ref: "core.local"
        parameters:
            cmd: "echo c3"
        on-failure: "c4"
    -
        name: "c4"
        ref: "core.local"
        parameters:
            cmd: "echo fail c4"
default: "c1"
Mistral

feat

只是做了些简单的了解,其中能自定义的部分非常多,我个人有几点想法:

参考:

https://sdk.cn/news/7548

http://os.51cto.com/art/201801/563769.htm

https://github.com/StackStorm-Exchange/stackstorm-zabbix

tongtie commented 3 years ago

willhope commented 3 years ago

图看不到了