Git Product home page Git Product logo

nacos_check's Introduction

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

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

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

  • 支持Nacos v1 v2版本,支持集群模式
  • 快速查找注册服务,支持匹配名称,命名空间,端口,ip,多个服务模糊匹配
  • 支持指定命名空间,默认全部
  • 支持命令行导出json
  • 支持自定义Prometheus label
  • 支持Prometheus自动发现,file_sdhttp_sd_configs
  • 查看集群状态,以及v1升级v2接口详情
  • 支持注册本身到Nacos集群
  • 支持本地配置文件使用
  • 支持Nacos用户名密码鉴权
  • 支持自定义contextpath
  • 支持Table和Markdown格式输出
  • 支持实时同步nacos配置信息到本地文件

安装

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      查看本地配置文件路径
  config-sync 实时同步nacos配置到本地
  help        Help about any command
  register    注册本实例到Nacos并开启webapi
  version     查看版本
  web         开启web api Prometheus http_sd_configs

Flags:
      --cluster                全集群查找
      --config string          指定配置文件路径
      --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 

image

查看Nacos集群状态

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

image

查找注册服务

./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
  • 支持查找服务名,ip,端口,命名空间

查找注册服务,每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 快速查找服务,支持以下👇匹配

  • 匹配命名空间
  • 匹配服务名
  • 匹配IP端口
# 模糊匹配命名空间
./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

image

加载本地配置

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

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

/root/.nacos_conf.toml 示例

# nacos url地址
url = "http://nacos-0:8848,http://nacos-1: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"
nacos_sync = [
    {namespace = "dc7bca41-5xxx",dataId = "java1.yml",group = "DEFAULT_GROUP",dest = "ymlconfig/java1.yml"},
    {namespace = "dc7bca41-5xxx",dataId = "java2.yml",group = "DEFAULT_GROUP",dest = "ymlconfig/java2.yml"},
    {namespace = "dc7bca41-5xxx",dataId = "java3.yml",group = "DEFAULT_GROUP",dest = "ymlconfig/java3.yml"}
]

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

config-sync

此功能可以将nacos上面的配置信息实时同步到本地文件(试验阶段可能不稳定)

首先在配置文件加入配置信息

# nacos-sync配置使用
nacos_sync = [
    {namespace = "dc7bca41-5xxx",dataId = "java1.yml",group = "DEFAULT_GROUP",dest = "ymlconfig/java1.yml"},
    {namespace = "dc7bca41-5xxx",dataId = "java2.yml",group = "DEFAULT_GROUP",dest = "ymlconfig/java2.yml"},
    {namespace = "dc7bca41-5xxx",dataId = "java3.yml",group = "DEFAULT_GROUP",dest = "ymlconfig/java3.yml"}
]
  • namespace 命名空间
  • dest 为要写入的本地文件

开启实时同步

./nacos-check config-sync

建议使用systemctl开启进程守护

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注册问题。

  • -i 指定注册到Nacos的IP地址
  • --namespace 指定命名空间
  • -g 指定组
  • -p 指定开启端口
  • --url 指定Nacos服务地址,多个地址,号分开
  • -n 指定注册到Nacos的服务名称

image image

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

主机名解析

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

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

效果

image image

对接机器人执行命令查询

image

grafana 展示出图

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

image

nacos_check's People

Contributors

typ431127 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nacos_check's Issues

-f 不支持多组模糊查询 ,也不支持指定namespaceid 查询

老兄,这工具挺不错的。在使用中遇到这2个问题。

  1. -f 不支持多组模糊查询 ,如:
    ./nacos_check web --url http://10.0.5.157:8848 -f service,domain,gateway
  2. 不支持指定namespaceid查询,主要因为我们的nacos 有namespace隔离,所以就会出现多个的情况,但真实网络却是隔离的,所以需要指定一个命名空间查询,并生成json,或web
    如: ./nacos_check web --url http://10.0.5.157:8848 -a "3b19f3ca-6f5f-4d96-b7ff-a9116fa06f63" -b “DEMO环境”
func (d *Nacos) GetNameSpace() {
	if d.DefaultNamespace != "" {
		//fmt.Println(d.DefaultNamespace)
		arrNamespace := make([]NamespaceServer, 0)
		arrNamespace = append(arrNamespace, NamespaceServer{
			Namespace:         d.DefaultNamespace,
			NamespaceShowName: d.DefaultSpaceNamd,
			ConfigCount:       500,
		})

		d.Namespaces = &Namespaces{
			Code: 200,
			// Message: types.Nil{},
			Data: arrNamespace,
		}
	} else {
		_url := fmt.Sprintf("%s/nacos/v1/console/namespaces", d.DefaultUlr)
		res := d.HttpReq(_url)
		err := json.Unmarshal(res, &d.Namespaces)
		if err != nil {
			fmt.Println("获取命名空间json异常")
		}
	}
	fmt.Println(len(d.Namespaces.Data))
	// fmt.Println(cap(d.Namespaces.Data))
	for _, v := range d.Namespaces.Data {
		fmt.Println(fmt.Sprintf("ns:%s ,name=%s ,count: %d,quota=%d, type=%d", v.Namespace, v.NamespaceShowName, v.ConfigCount, v.Quota, v.Type))
	}

}







func (d *Nacos) TableRender() {
	nacos_server := d.Clusterdata[d.Host]
	tabletitle := []string{"命名空间", "服务名称", "实例", "健康状态", "主机名", "权重", "PID", "容器"}
	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader(tabletitle)
	for _, v := range nacos_server.HealthInstance {
		tabledata := v[0:8]
		if FIND == "" {
			table.Append(tabledata)
		} else {
			if strings.Contains(FIND, ",") {
				array2 := strings.Split(FIND, ",")
				for _, v2 := range array2 {
					if strings.Contains(v[0], v2) {
						table.Append(tabledata)
					}
					if strings.Contains(v[1], v2) {
						table.Append(tabledata)
					}
					if strings.Contains(v[2], v2) {
						table.Append(tabledata)
					}
				}
			} else {
				if strings.Contains(v[0], FIND) {
					table.Append(tabledata)
				}
				if strings.Contains(v[1], FIND) {
					table.Append(tabledata)
				}
				if strings.Contains(v[2], FIND) {
					table.Append(tabledata)
				}
			}
		}
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.