sql2slack alash3al 开源的又个轻量级工具

从名称上是sql 到slack 消息的处理,实际上可以支持基本上各类的webhook 处理

特性

  • 小巧
  • 支持多sql 引擎
  • 可以通过underscore.js 自定义消息
  • cron 语法格式的sql 任务调度
  • 使用hcl 做为配置管理语言
  • 对于大量job,可以直接基于文件的配置(启动的时候会扫描job定义文件)

job 格式

job tst {
    // slack-channel webhook url
    channel = "https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxx"
    // which sql driver do you use?
    driver = "mysql"
    // data source name (connection string)
    dsn = "root:root@tcp(127.0.0.1:3306)/dbname"
    // the query this is a multiline example, you can just write the following
    // query = "select * from users"
    query = <<SQL
        SELECT users.* FROM users;
    SQL
    // cron like syntax
    schedule = "* * * * *"
    // here you need to build the text message that will be sent 
    // to the slack channel.
    // ------- 
    // say(....): a function that will append the specified arguments into the main slack text message.
    // log(....): a logger function, for debugging purposes.
    // $rows: a variable that holds the output of the query execution.
    message = <<JS
        if ( $rows.length < 1 ) {
            return
        }
        say("there are (", $rows.length, ") new users!")
        say("users list is:")
        _.chain($rows).pluck('name').each(function(name){
            say("- ", name, " .")
        })
    JS
}

简单说明

上边的channel,可以替换为各类的webhook(比如钉钉webhook),目前消息处理系统提供了内建函数say(我们也可以发送json格式)
按照设计以及代码,我们可以在job 中支持多种数据库链接(使用多job 文件),注意文件格式为<name>..s2s.hcl
目前内置的js context

$rows sql 查询数据
log 记录log的
say 提供数据包装的函数,很简单,这个我们剋用扩展,返回值为string

整个技术栈 go-resty resty 处理,hashicorp/hcl hcl 解析处理,robfig/cron cron 表达式处理,robertkrimen/otto js 引擎,其他的主要是sql 处理的
基于sqlx,整个设计还是比较简介高效的,很不错和sqler代码模式一样

参考资料

https://github.com/alash3al/sql2slack

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/13206184.html