MySQL 查询

这个插件提供了收集 MySQL 查询结果的功能。

插件文件地址
mysql.query
插件类型
接受参数,重复执行

插件参数

参数 功能
host MySQL 服务器地址 [1]
port MySQL 服务器端口
database 希望执行查询的数据库名
user MySQL 用户
password MySQL 用户对应的密码
name 这个监控的名字,用来区分其他的 MySQL 查询监控
sql 执行的 SQL 语句 [2]
json-file 可选 ,从这个文件中读取上述参数 [3]
[1]同时这个值也会作为监控的 host 上报,如果 host 是 localhost 则会使用本机 hostname 上报。
[2]SQL 语句必须返回数值类型的 scalar (1行1列)。
[3]因为是插件在读取这个文件,所以这个文件需要在插件调度的机器上。

上报的监控值

mysql.query. name
意义:执行指定的 SQL 的结果
取值:浮点数,与指定的 SQL 有关
Tags:

监控规则样例

(def mysql-query-rules
  (sdo
    (where (host "mysql-host")
      (plugin "mysql.query" 60
        {:name "bad-user-count"
         :host "localhost"
         :port 3306
         :user "user_for_monitor"
         :password "Secret!IMeanIt!"
         :database "awesome_app"
         :sql "SELECT count(*) FROM user WHERE bad = 1"})

    (where (service "mysql.query.bad-user-count")
      (judge (> 50)
        (runs 2 :state
          (alarm-every 5 :min
            (! {:note "坏用户太多了!"
                :level 3
                :groups [:operation]}))))))))