当前位置: 首页 > 工具软件 > Kapacitor > 使用案例 >

Kapacitor安装及使用

乐正翰
2023-12-01

1 安装

1.1 Tar包安装

  (1)下载

wget https://dl.influxdata.com/kapacitor/releases/kapacitor-1.5.5_linux_amd64.tar.gz

  (2)安装

$ tar xvfz /opt/package/kapacitor-1.5.5-static_linux_amd64.tar.gz -C /home/tigk/.local/
$ mv /home/tigk/.local/kapacitor-1.5.5-1/ /home/tigk/.local/kapacitor

  (3)可执行文件路径为:{influxdb目录}/kapacitor

  默认配置文件所在路径:{influxdb目录}/kapacitor.conf

$ mkdir /data/tigk/kapacitor/conf
$ cp /home/tigk/.local/kapacitor/kapacitor.conf  /data/tigk/kapacitor/conf/
$ cd /data/tigk/kapacitor/
$ mkdir data
$ mkdir logs

  然后进行修改配置文件

  (4)编辑配置文件

  /data/tigk/kapacitor/conf/kapacitor.conf,定位到[[influxdb]]

$ vim /data/tigk/kapacitor/conf/kapacitor.conf
data_dir = "/data/tigk/kapacitor/data"
#  https-certificate = "/etc/ssl/kapacitor.pem"
[logging]
    file = "/data/tigk/kapacitor/logs/kapacitor.log"
[load]
    dir = "/data/tigk/kapacitor/load"
[replay]
    dir = "/data/tigk/kapacitor/replay"
[task]
  # Where to store the tasks database
  # DEPRECATED: This option is not needed for new installations.
  # It is only used to determine the location of the task.db file
  # for migrating to the new `storage` service.
  dir = "/data/tigk/kapacitor/tasks"
[storage]
  # Where to store the Kapacitor boltdb database
  boltdb = "/data/tigk/kapacitor/boltdb/kapacitor.db"
[[influxdb]]
  name = "fbi-local-02"
  urls = ["http://localhost:8085"]

  (4)启动服务

$ cd /home/tigk/.local/kapacitor/
$ ./kapacitord -config /data/tigk/kapacitor/conf/kapacitor.conf 

  或者后台启动

$ nohup ./kapacitord -config /data/tigk/kapacitor/conf/kapacitor.conf &

1.2 RPM包安装

  (1)下载

wget https://dl.influxdata.com/kapacitor/releases/kapacitor-1.5.5-1.x86_64.rpm

  (2)安装

sudo yum localinstall kapacitor-1.5.5-1.x86_64.rpm

  (3)修改配置文件

/etc/kapacitor/kapacitor.conf

  (4)启动服务

sudo systemctl start kapacitor

2 使用

2.1 Kapacitor 常用命令

  Kapacitor 是数据处理引擎,主要用来作为报警使用。使用TICKscript来定义任务,脚本定义了kapacitor处理的数据来源及处理过程。
  kapacitor常用命令

kapacitor list tasks –显示所有当前kapacitor加载的脚本
kapacitor define -tick -dbrp -type –安装脚本到kapacitor
kapacitor enable –脚本监控开启
kapacitor disable –脚本监控关闭
kapacitor show –显示某一个脚本的执行状态
kapacitor record -task -duration –记录某一时间段内某脚本运行状态(测试用)
kapacitor replay kapacitor replay -recording -task –重复执行测试结果

  如果Kapacitor启动并运行,您将看到一个空的任务列表,如下所示:

$ ./kapacitor list tasks
ID Type      Status    Executing Databases and Retention Policies

2.2 CPU告警案例

  (1)编写tick文件,当cpu闲置率小于70%则告警
  vi cpu_alert.tick

stream
	|from()
		.measurement('cpu')
	|alert()
		.crit(lambda:"usage_idle" < 70)
		.log('/data/tigk/kapacitor/alerts/cpu_alert.log')

  (2)Kapacitor任务定义,到Kapacitor的安装目录下

./kapacitor define cpu_alert -type stream -tick /data/tigk/kapacitor/tick/cpu_alert.tick -dbrp telegraf.autogen

  define:定义任务名,-tpye:指定任务类型,-tick:指定脚本名,-dprp:指定数据保留策略
  (3)查询已定义任务

$ ./kapacitor list tasks
ID        Type      Status    Executing Databases and Retention Policies
cpu_alert stream    disabled  false     ["telegraf"."autogen"]

  (4)查询任务详细信息

$ ./kapacitor show cpu_alert
ID: cpu_alert
Error: 
Template: 
Type: stream
Status: disabled
Executing: false
Created: 05 Jul 20 20:33 CST
Modified: 05 Jul 20 20:33 CST
LastEnabled: 01 Jan 01 00:00 UTC
Databases Retention Policies: ["telegraf"."autogen"]
TICKscript:
stream
    |from()
        .measurement('cpu')
    |alert()
        .crit(lambda: "usage_idle" < 70)
        .log('/data/tigk/kapacitor/alerts/cpu_alert.log')

DOT:
digraph cpu_alert {
stream0 -> from1;
from1 -> alert2;
}


  (5)脚本监控生效

$ ./kapacitor enable cpu_alert

  (6)脚本监控关闭

$ ./kapacitor disable cpu_alert

 类似资料: