• 夜莺n9ev5,categraf自定义采集数据


    一、下载categraf

    GitHub - flashcatcloud/categraf: one-stop telemetry collector for nightingale

    go环境自己搭建

    用mtail采集日志参考:

    夜莺日志采集mtail_小青蛙呱呱跳的博客-CSDN博客

    二、新建inputs/mtail文件

     mtail文件内容如下:本例只返回:999,小伙伴可以重写gatherOnce方法,返回自己想要的内容

    1. package mtail
    2. import (
    3. "sync"
    4. "sync/atomic"
    5. "flashcat.cloud/categraf/config"
    6. "flashcat.cloud/categraf/inputs"
    7. "flashcat.cloud/categraf/types"
    8. "github.com/toolkits/pkg/container/list"
    9. )
    10. const inputName = "mtail"
    11. type Mtail struct {
    12. config.Interval
    13. counter uint64
    14. waitgrp sync.WaitGroup
    15. Instances []*Instance `toml:"instances"`
    16. }
    17. func init() {
    18. inputs.Add(inputName, func() inputs.Input {
    19. return &Mtail{}
    20. })
    21. }
    22. func (r *Mtail) Prefix() string {
    23. return inputName
    24. }
    25. func (r *Mtail) Init() error {
    26. if len(r.Instances) == 0 {
    27. return types.ErrInstancesEmpty
    28. }
    29. for i := 0; i < len(r.Instances); i++ {
    30. if err := r.Instances[i].Init(); err != nil {
    31. return err
    32. }
    33. }
    34. return nil
    35. }
    36. func (r *Mtail) Drop() {}
    37. func (r *Mtail) Gather(slist *list.SafeList) {
    38. atomic.AddUint64(&r.counter, 1)
    39. for i := range r.Instances {
    40. ins := r.Instances[i]
    41. r.waitgrp.Add(1)
    42. go func(slist *list.SafeList, ins *Instance) {
    43. defer r.waitgrp.Done()
    44. if ins.IntervalTimes > 0 {
    45. counter := atomic.LoadUint64(&r.counter)
    46. if counter%uint64(ins.IntervalTimes) != 0 {
    47. return
    48. }
    49. }
    50. ins.gatherOnce(ins.TargetUrl, slist)
    51. }(slist, ins)
    52. }
    53. r.waitgrp.Wait()
    54. }
    55. type Instance struct {
    56. Labels map[string]string `toml:"labels"`
    57. IntervalTimes int64 `toml:"interval_times"`
    58. TargetUrl string `toml:"target_url"`
    59. }
    60. func (ins *Instance) Init() error {
    61. return nil
    62. }
    63. func (ins *Instance) gatherOnce(TargetUrl string, slist *list.SafeList) {
    64. fields := map[string]interface{}{
    65. "send_error": "999",
    66. }
    67. inputs.PushSamples(slist, fields)
    68. }

    三、新建conf/input.mtail/mtail.toml文件

    mtail.toml文件自己定义内容

    1. [[instances]]
    2. labels = "hlw"
    3. interval_times = 1
    4. target_url = "http://192.168.1.44:3903/metrics"

    四、注册

    在agent/agent.go注册

     

    五、打包测试

    1. # export GO111MODULE=on
    2. # export GOPROXY=https://goproxy.cn
    3. go build

    如果打包成linux下运行的程序

    $Env:GOARCH="amd64";$Env:GOOS="linux" 

    go build 

    1. PS D:\workspace_go\categraf> .\categraf --test --inputs mtail
    2. 2022/08/13 09:38:09 main.go:110: I! runner.binarydir: D:\workspace_go\categraf
    3. 2022/08/13 09:38:09 main.go:111: I! runner.hostname: LAPTOP-4O62U4N3
    4. 2022/08/13 09:38:09 main.go:112: I! runner.fd_limits: N/A
    5. 2022/08/13 09:38:09 main.go:113: I! runner.vm_limits: N/A
    6. 2022/08/13 09:38:09 config.go:33: I! tracing disabled
    7. 2022/08/13 09:38:09 agent.go:62: I! agent starting
    8. 2022/08/13 09:38:09 metrics_agent.go:64: I! input: mtail started
    9. 2022/08/13 09:38:09 agent.go:67: I! agent started
    10. 23291
    11. 2022/08/13 09:38:38 main.go:90: I! received signal: interrupt
    12. 2022/08/13 09:38:38 agent.go:71: I! agent stopping
    13. 2022/08/13 09:38:38 agent.go:76: I! agent stopped
    14. 2022/08/13 09:38:38 main.go:105: I! exited
    15. PS D:\workspace_go\categraf> go build
    16. PS D:\workspace_go\categraf> .\categraf.exe --test --inputs mtail
    17. 2022/08/13 09:39:38 main.go:110: I! runner.binarydir: D:\workspace_go\categraf
    18. 2022/08/13 09:39:38 main.go:111: I! runner.hostname: LAPTOP-4O62U4N3
    19. 2022/08/13 09:39:38 main.go:112: I! runner.fd_limits: N/A
    20. 2022/08/13 09:39:38 main.go:113: I! runner.vm_limits: N/A
    21. 2022/08/13 09:39:38 config.go:33: I! tracing disabled
    22. 2022/08/13 09:39:38 agent.go:62: I! agent starting
    23. 2022/08/13 09:39:38 metrics_agent.go:64: I! input: mtail started
    24. 2022/08/13 09:39:38 agent.go:67: I! agent started
    25. 09:39:53 mtail_send_error agent_hostname=LAPTOP-4O62U4N3 999
    26. 09:40:08 mtail_send_error agent_hostname=LAPTOP-4O62U4N3 999
    27. 09:40:23 mtail_send_error agent_hostname=LAPTOP-4O62U4N3 999
    28. 2022/08/13 09:40:25 main.go:90: I! received signal: interrupt
    29. 2022/08/13 09:40:25 agent.go:71: I! agent stopping
    30. 2022/08/13 09:40:25 agent.go:76: I! agent stopped
    31. 2022/08/13 09:40:25 main.go:105: I! exited

    六、部署

    1、选择在linux上生成docker镜像

    2、 将categraf和conf文件赋值到docker文件夹中

    1. [root@localhost docker]# ll
    2. -rwxrwxrwx 1 root root 131276085 Aug 12 17:50 categraf
    3. drwxrwxrwx 39 root root 4096 Aug 12 17:21 conf
    4. -rwxrwxrwx 1 root root 247 Aug 12 17:17 Dockerfile
    5. -rwxrwxrwx 1 root root 279 Aug 12 17:17 Dockerfile.goreleaser
    6. -rwxrwxrwx 1 root root 277 Aug 12 17:40 entrypoint.sh
    7. #打包镜像
    8. [root@localhost docker]# docker build -t categraf:v1 .
    9. Sending build context to Docker daemon 131.4MB
    10. Step 1/7 : FROM ubuntu:22.10
    11. ---> 15a38249db7a
    12. Step 2/7 : RUN echo 'hosts: files dns' >> /etc/nsswitch.conf
    13. ---> Using cache
    14. ---> 6b369085d262
    15. Step 3/7 : RUN set -ex && mkdir -p /usr/bin /etc/categraf
    16. ---> Using cache
    17. ---> 76630541bbf9
    18. Step 4/7 : COPY categraf /usr/bin/categraf
    19. ---> Using cache
    20. ---> 7ac37851e65f
    21. Step 5/7 : COPY conf /etc/categraf/conf
    22. ---> Using cache
    23. ---> 326db080659e
    24. Step 6/7 : COPY entrypoint.sh /entrypoint.sh
    25. ---> Using cache
    26. ---> 3f66ddcb98a8
    27. Step 7/7 : CMD ["/entrypoint.sh"]
    28. ---> Using cache
    29. ---> 6549372b42a3
    30. Successfully built 6549372b42a3
    31. Successfully tagged categraf:v1
    32. [root@localhost docker]# docker images
    33. REPOSITORY TAG IMAGE ID CREATED SIZE
    34. categraf v1 6549372b42a3 16 hours ago 202MB

    3、修改夜莺docker-compose.yaml

    categraf:
      #image: "flashcatcloud/categraf:latest"
      image: "categraf:v1"

    注意:将自己写的toml文件,赋值到 nightingale-main\docker\categraf

    因为categraf容器的conf映射到这里。

     

     4、重新启动categraf容器

    七、夜莺查看

    1、在即时查询

    输入  mtail_send_error

     

     2、快捷试图

    剩余的操作可自行定制告警了

  • 相关阅读:
    为什么C++中的继承比Java设计的要复杂,原因竟出在这儿?
    Linux三剑客:awk的基本用法
    pulsar简介
    18、Flink的SQL 支持的操作和语法
    基于nodejs的疫情大数据展示与政策查询系统
    数据结构c语言版第二版(严蔚敏)第二章笔记
    Spark 安装与启动
    来自中科院的一次java技术面经历
    MYSQL中LIKE(模糊查询)
    3D激光slam:ALOAM---后端lasermapping最终篇地图更新及消息发布
  • 原文地址:https://blog.csdn.net/xiaohanshasha/article/details/126314902