站点图标 星露谷值班站长

Nacos便携运维命令行工具,支持Prometheus自动发现

Nacos 运维便携命令行检查工具

Github: https://github.com/typ431127/Nacos_check

如果您觉得此工具对你的工作有帮助,可以点击上面的链接地址点一个小星星⭐⭐

方便运维查看 nacos 注册服务,快速查找服务,同时生成 prometheus 自动发现所需要的 json 文件。
golang 运维萌新,学习项目 … 😊

首先我并不是一个专业的开发人员,我只是一个小运维,此工具只为方便工作开发,代码水平一般,大佬勿喷 ……….

安装

curl  -L https://github.com/typ431127/Nacos_check/releases/download/0.7.2/nacos-check_Linux_x86_64.tar.gz -o nacos-check_Linux_x86_64.tar.gz
tar xvf nacos-check_Linux_x86_64.tar.gz
chmod +x nacos-check
./nacos-check --url https://nacos 地址 

可以把文件放入 /usr/bin 或者 windows path 环境变量里面,随时使用 cmd 调用命令

使用帮助

Nacos

Usage:
  nacos-check [flags]
  nacos-check [command]

Available Commands:
  cluster     集群状态
  completion  Generate the autocompletion script for the specified shell
  config      查看本地配置文件路径
  help        Help about any command
  register    注册本实例到 Nacos 并开启 webapi
  version     查看版本
  web         开启 web api Prometheus http_sd_configs

Flags:
      --cluster                全集群查找
      --contextpath string     server.servlet.contextPath (default "/nacos")
  -f, --find string            查找服务
      --group string           指定分组 多个分组 group1,group2 (default "DEFAULT_GROUP")
  -h, --help                   help for nacos-check
  -i, --ipfile string          ip 解析文件 (default "salt_ip.json")
      --json                   输出 json
  -l, --lable stringToString   添加标签 -l env=dev,pro=java (default [])
      --namespace string       指定命名空间 ID 多个: id1,id2,id3
      --password string        密码
  -s, --second duration        监控服务间隔刷新时间 (default 5s)
      --stdout string          输出类型 table / markdown (default "table")
  -u, --url string             Nacos 地址 (default "http://dev-k8s-nacos:8848")
      --username string        账户 (default "nacos")
  -w, --watch                  监控服务
  -o, --write string           导出 json 文件, prometheus 自动发现文件路径

Use "nacos-check [command] --help" for more information about a command.

显示所有实例注册信息

./nacos_check-linux-amd64 --url http://nacos-0:8848

查看 Nacos 集群状态

./nacos_check-linux-amd64 --url http://nacos-0:8848cluster --v2upgrade

查找注册服务

./nacos_check-linux-amd64 --url http://nacos-0:8848-f gateway 
./nacos_check-linux-amd64 --url http://nacos-0:8848-f 8080
./nacos_check-linux-amd64 --url http://nacos-0:8848-f 172.30
# 多个服务匹配, 分割
./nacos_check-linux-amd64 --url http://nacos-0:8848-f gateway,user,order
# 指定命名空间和 group
./nacos_check-linux-amd64 --url http://nacos-0:8848--namespace df7bee71-33ff-49ae-9adc-d9412b3d2ddb,dc7bca41-5aeb-417e-9876-488dcfb5b911 --group ddn,DEFAULT_GROUP -f xxx

查找注册服务, 每 10 秒刷新一次

./nacos_check-linux-amd64 --url http://nacos-0:8848-f gateway  -w -s 10s

Prometheus 自动发现支持

写入自动发现 json 文件
./nacos_check-linux-amd64 --url http://nacos-0:8848-o discovery.json
控制台输出 json
./nacos_check-linux-amd64 --url http://nacos-0:8848--json
# 添加自定义 label
./nacos_check-linux-amd64 --url http://nacos-0:8848 -l env=dev,pro=test-pro,k8s=true --json
prometheus 可以结合 blackbox_exporter 使用
file_sd_configs:
  - files:
      - '/data/work/prometheus/discovery/*.json'
      refresh_interval: 3m
prometheus-file-sd 自动发现
./nacos_check-linux-amd64 --url http://nacos-0.xxxxx:8848 -o  discovery.json

http_sd_configs 自动发现
开启 webapi        
./nacos_check-linux-amd64 web --url http://nacos-0.xxxx:8848

开启 webapi 并添加自定义 label
./nacos_check-linux-amd64 web --url http://nacos-0.xxxx:8848 -l env=dev,pro=test-pro,k8s=true

注意鉴权模式下 Token 有过期时间,开启鉴权后默认 1 小时刷新一次 token,nacos 默认配置 token 过期时间为 5 小时,根据需要可调整 web 模式下 –refresh 参数

基于 http_sd_configs 的自动发现

scrape_configs:
  - job_name: 'nacos'
    scrape_interval: 10s
    metrics_path: /probe
    params:
      module: [tcp_connect]
    http_sd_configs:
     - url: http://localhost:8099
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115

find 快速查找服务,支持以下👇匹配

# 模糊匹配命名空间
./nacos_check-linux-amd64 -f registry
# 模糊匹配服务
./nacos_check-linux-amd64 -f gateway
# 匹配端口
./nacos_check-linux-amd64 -f 8080
# 模糊匹配 IP
./nacos_check-linux-amd64 -f 172.30

加载本地配置

每次运行工具都需要指定 url 很麻烦,可以在本地写一个配置文件,这样默认情况下就会加载配置文件里面的 url,就不需要每次都指定了。查看配置文件路径

./nacos_check-linux-amd64 config
本地配置文件路径: /root/.nacos_conf.toml

/root/.nacos_conf.toml  示例

# nacos url 地址
url = "http://nacos-0:8848"

# 定义容器网段
container_network = ["172.30.0.0/16","172.16.0.0/16","192.168.0.0/16"]

# 账号密码  https://nacos.io/zh-cn/docs/auth.html
#username = ""#password =""

# 定义指定的 namespaceid (可选, 默认所有)
# 等同命令行 --namespace id1,id2
namespace = ["df7bee71-33ff-49ae-9adc-d9412b3d2ddb","dc7bca41-5aeb-417e-9876-488dcfb5b911"]

# 定义 group 组 等同命令行 --group ddn1,ddn2, (可选, 默认 DEFAULT_GROUP)
group = ["ddn","ddn","ddn2","DEFAULT_GROUP"]


# 设置默认导出 json 和 web 服务附加标签
label = [{name = "env",value = "dev"},
    {name = "os",value = "linux"}
]

ipfile = "/mnt/cxxxx/ip.json"

默认优先加载本地配置文件

docker 启动 web 服务 Prometheus httpd_sd_config 使用

docker run -itd -e nacos_url=http://nacos-xx.com:8848 -p 8099:8099 typ431127/nacos-check:0.6
访问 http://localhost:8099

工具注册到 Nacos

./nacos_check-linux-amd64 register -i 192.168.1.4 -p ":8048" -n ddn-test1 --url http://192.16
8.100.132:8848
 ./nacos_check-linux-amd64 register -i 192.168.1.4 -p ":8048" -n ddn-test1 --url \
 http://192.168.100.132:8848,http://192.168.100.133:8848,http://192.168.100.134:8848
 
 ./nacos_check-linux-amd64 register -n ops-test  --namespace dc7bca41-5aeb-417e-9876-488dcfb5b911 -g ddn

执行后工具会开启一个 web 服务并注册到 Nacos 上面,同时可指定多个 Nacos 地址,此功能方便运维排查 Nacos 注册问题。

注意: 仅注册功能支持多个 nacos 地址写法

主机名解析

因为默认只获取到主机 ip,获取不到主机名, 可以指定 ipfile 解析主机名,有条件可以二次开发对接自己 cmdb, 文件格式如下 (可选)

{
    "test1": "10.x.x.x",
    "test2": "10.x.x.x",
}
./nacos_check-linux-amd64 -i ../ip.json

效果

对接机器人执行命令查询

grafana 展示出图

grafana 控制台导入 grafana.json  此模板默认匹配 blackbox_exporter

退出移动版