# 应用配置参数解析

我们在配置里面内置支持一些灵活的语法让你能够获取每台机器的一些独特属性,你可以针对每台机器的特性来设置一些自适应的属性。

比如,你可能会看到有些配置的值是这样的:

$(#6986816630836953366.stat:network.in_addr@^(192|172).speed>9999.0.in_addr)

这条配置的逻辑解析是:

  1. 找到 nodeId 为 6986816630836953366 的节点
  2. 从该节点的网络统计信息中找到符合要求的 IP 地址
  3. 过滤规则为:IP 地址是 192 或者 172 开头的,且速度大于 9999Mb/s网卡(万兆卡)的 IP 地址

有时候你可能有 2 张万兆卡,一张 IP 为 192.168.1.100 另一张 IP 为 192.168.2.100 这时如果你需要获取 1.0 网段的网卡 IP,你可以这样写:

$(#6986816630836953366.stat:network.in_addr@^(192..168..1..).speed>9999.0.in_addr)

接下来我们把参数的解析语法详细讲一遍。整个配置的语法格式为 : 选择器 -> 数据源 -> 过滤器

# 节点选择器

节点选择过滤器目前支持 2 种:#nodeIdthis。分别表示选择其他节点和本节点。

选择 NodeID 为 6986816630836953366 的节点名称:

$(#6986816630836953366.data.name)

选择当前节点的节点名称:

$(this.data.name)

提示

点号 . 表示 access,也就是获取(数组的)下一级数据,如果下一级还是数组的话,则可以通过 . 来继续操作。.0 表示获取第一个匹配项。

# 数据源

数据源目前也支持 2 种:statdata

  1. stat 是节点的状态统计信息集合,它是一个很大的数组,里面包含了机器所有的硬件信息和进程信息。 具体详情可以参考 stat 数据结构。你可以通过 . 去获取它的任何一级的数据内容。

  2. data 是节点存储在数据库的信息,目前只开放了获取 name 属性(节点名称)的权限。

# 函数过滤器

函数过滤器通过 # 调用。目前支持的函数有以下几个:

  1. max: 取最大的那个,比如获取网速最快的那张网卡的 IP 地址:
    $(this.stat:network.speed#max.in_addr)
    # 获取第一个GPU中最大占用内存最大的进程id
    $(this.stat:gpu.0.process.mem#max.id)
    
  2. min: 取最小的那个,如获取最小的容量的磁盘:
    $(this.stat:disk.size#min.size)
    
  3. sub: 截取字符串,如截取掉节点名称的第一个字符:
    $(this.data:name#sub(1))	# 长度不填写,默认是整个字符串的长度
    $(this.data:name#sub(1,-1))
    
  4. replace: 替换,如把节点名称中的 p1- 替换成 p2-
    $(this.data:name#replace(p1-,p2-).0#sub(2).0#sub(1))
    
  5. len: 获取长度,如获 CPU 的核数:
    $(this.stat:cpu.list#len)
    

# 正则过滤器

我们支持标注的正则表达式来做匹配和过滤,使用 @ 触发正则匹配。比如开头我们看到的那个获取网卡的表达式使用的就是正则匹配:

$(#6986816630836953366.stat:network.in_addr@^(192..168..1..).speed>9999.0.in_addr)

# 操作符过滤

支持以下操作符:

  • =: 等于
  • !: 不等于
  • >: 大于
  • <: 小于
  • ^: 以 xxx 开始
  • $: 以 xxx 结束
  • %: 包含 xxx

例如:

# 通过=获取网卡的ip地址
$(this.stat:network.name=enp3s0.in_addr
# 通过!获取第一个非lo网卡的ip地址
$(this.stat:network.name!lo.in_addr)
# 通过%操作获取sshd的进程id
$(this.stat:process.cmd%ssh.pid)
# 通过.操作获取主板序列号
$(this.stat:hd_board.serial_no)
# 通过>操作获取size>'1073741824'的硬盘的wwn
$(this.stat:disk.size>1099511627776.wwn)
# 通过>操作获取size<'120040980481'的硬盘的wwn
$(this.stat:disk.size<120040980481.wwn)

# stat 数据结构

{
    "version": 1.13,
    "node": {
        "no": 1752,
        "id": "60efa2c6-a691c040-b327-cc01-90e2ba8a002d",
        "created_at": 1626317510,
        "last_boot_at": 1626864263,
        "interval": 30,
        "last_stat_begin": 1626920880,
        "last_stat_end": 1626920911
    },
    "hd_bios": {
        "vendor": "American Megatrends Inc.",
        "version": "2.1",
        "r_date": "02/21/2020",
        "run_size": 65536,
        "rom_size": 33554432
    },
    "hd_sys": {
        "manufacturer": "Supermicro",
        "pro_name": "Super Server",
        "version": "0123456789",
        "serial_no": "0123456789",
        "uuid": "00000000-0000-0000-0000-ac1f6be6408a"
    },
    "hd_board": {
        "manufacturer": "Supermicro",
        "pro_name": "H11DSi",
        "version": "2.00",
        "serial_no": "zm207s600210"
    },
    "system": {
        "pre_def": "Linux",
        "sys_name": "ubuntu",
        "sys_info": "ubuntu 18.04.5 lts",
        "node_name": "daemon-yimai",
        "release": "4.15.0-147-generic",
        "version": "#151-Ubuntu SMP Fri Jun 18 19:21:19 UTC 2021",
        "machine": "x86_64"
    },
    "numa": [
        {
            "cpu_list": "0-15,32-47",
            "mem": {
                "total": 32863472,
                "free": 220268,
                "active": 15622564,
                "inactive": 15105960,
                "mlocked": 0,
                "mapped": 14872216,
                "shmem": 872
            },
            "stat": {
                "numa_hit": 5125883924,
                "numa_miss": 497094309,
                "numa_foreign": 841245478,
                "interleave_hit": 39567,
                "local_node": 5125974776,
                "other_node": 497003457
            }
        }
    ],
    "cpu": {
        "list": [
            {
                "vendor_id": "AuthenticAMD",
                "model": 49,
                "model_name": "AMD EPYC 7302 16-Core Processor",
                "mhz": 1496.9720458984375,
                "cache_size": 512,
                "physical_id": 0,
                "core_id": 0,
                "cores": 16
            }
          
        ],
        "stat": {
            "round_1": {
                "name": "cpu",
                "user": 72161840,
                "nice": 485,
                "system": 20444552,
                "idle": 246776663
            },
            "round_2": {
                "name": "cpu",
                "user": 72171315,
                "nice": 485,
                "system": 20445096,
                "idle": 246962947
            },
            "load_avg": 5.10384464263916,
            "sys_loadavg": {
                "t_1m": 3.4200000762939453,
                "t_5m": 1.8899999856948853,
                "t_15m": 3.4600000381469727
            }
        }
    },
    "gpu": [
        {
            "desc": "VGA compatible controller",
            "product": "ASPEED Graphics Family",
            "vendor": "ASPEED Technology, Inc.",
            "bus": "pci@0000:42:00.0",
            "config": "driver=ast latency=0",
            "width": 32,
            "clock": 33,
            "query": false
        },
        {
            "desc": "VGA compatible controller",
            "product": "NVIDIA Corporation",
            "vendor": "NVIDIA Corporation",
            "bus": "pci@0000:81:00.0",
            "config": "driver=nvidia latency=0",
            "width": 64,
            "clock": 33,
            "query": true,
            "driver_version": "460.80",
            "cuda_version": "11.2",
            "name": "GeForce RTX 3070",
            "uuid": "GPU-c782beb7-31bb-393b-09d9-7881b5f8b265",
            "fan_speed": 30,
            "mem_fb_total": 7982,
            "mem_fb_used": 2,
            "mem_fb_free": 7980,
            "util_gpu": 0,
            "util_mem": 0,
            "util_dec": 0,
            "util_enc": 0,
            "temp_current": 37,
            "temp_shutdown": 98,
            "temp_slowdown": 95,
            "temp_maxopt": 93,
            "power_draw": 8.479999542236328,
            "power_limit": 220,
            "power_limit_min": 100,
            "power_limit_max": 240,
            "clock_graphics": 210,
            "clock_sm": 210,
            "clock_mem": 405,
            "clock_video": 555,
            "clock_graphics_max": 2100,
            "clock_sm_max": 2100,
            "clock_mem_max": 7001,
            "clock_video_max": 1950,
            "process": []
        },
    ],
    "ram": {
        "list": [
            {
                "total_width": 72,
                "data_width": 64,
                "size": 16384,
                "speed": 3200,
                "voltage": 1.2000000476837158,
                "type": "DDR4",
                "manufacturer": "Samsung",
                "serial_no": "cc700000"
            }
        ],
        "stat": {
            "total": 65885012,
            "free": 383152,
            "available": 37509420,
            "buffers": 10852,
            "cached": 36737380,
            "active": 33712260,
            "inactive": 27529608,
            "mlocked": 0,
            "swapTotal": 8388604,
            "swapFree": 4384300,
            "mapped": 28372812,
            "shmem": 1804
        }
    },
    "disk": [
        {
            "name": "/dev/nvme0n1",
            "wwn": "0x0000000000000000",
            "serial_no": "0x00000000",
            "model": "Unknow",
            "blocks": 15002931888,
            "size": 7681501126656,
            "stat": {
                "num_r": 0,
                "num_mr": 0,
                "num_sr": 0,
                "num_r_ms": 0,
                "num_w": 0,
                "num_mw": 0,
                "num_sw": 0,
                "num_w_ms": 0,
                "num_io": 0,
                "num_io_ms": 0,
                "num_mio_ms": 0,
                "num_io_util": "",
                "num_rs_byte": 0,
                "num_ws_byte": 0
            }
        }
    ],
    "vg": [
        {
            "name": "ubuntu-vg",
            "vg_uuid": "aYxTrE-peP2-B8rY-RClA-2dft-HKcN-Zmct7X",
            "format": "lvm2",
            "pe_size": 4,
            "total_pe": 60664,
            "alloc_pe": 58395,
            "num_cur_lv": 1,
            "num_open_lv": 1,
            "num_cur_pv": 1,
            "num_act_pv": 1,
            "lv": [
                {
                    "blocks": 478371840,
                    "size": 244926382080,
                    "lv_uuid": "TT3R3J-sdYv-Hwyq-9yZG-3iiC-a2dV-v2Kfo9",
                    "name": "ubuntu--lv",
                    "path": "/dev/mapper/ubuntu--vg-ubuntu--lv",
                    "pt_uuid": "b8bb3c21-5926-4af0-bb38-68d4e623993a",
                    "format": "ext4",
                    "mount": "/",
                    "f_flag": 4096,
                    "f_bsize": 4096,
                    "f_blocks": 58726908,
                    "f_bfree": 55036939,
                    "f_bavail": 52330784,
                    "f_files": 14950400,
                    "f_ffree": 14813093,
                    "f_favail": 14813093,
                    "total": 240545415168,
                    "free": 225431302144,
                    "stat": {
                        "num_r": 11275147,
                        "num_mr": 0,
                        "num_sr": 105768282,
                        "num_r_ms": 1278612,
                        "num_w": 14784183,
                        "num_mw": 0,
                        "num_sw": 118583808,
                        "num_w_ms": 50790652,
                        "num_io": 0,
                        "num_io_ms": 8074564,
                        "num_mio_ms": 52092520
                    }
                }
            ]
        }
    ],
    "network": [
        {
            "name": "enp68s0f1",
            "speed": 10000,
            "hd_addr": "90:e2:ba:8a:00:2d",
            "in_addr": "172.17.1.154",
            "round_1": {
                "receive": 45447440267,
                "r_packets": 124978102,
                "transmit": 75515877514,
                "t_packets": 158956926
            },
            "round_2": {
                "receive": 45455811184,
                "r_packets": 124992907,
                "transmit": 75676976045,
                "t_packets": 159074316
            },
            "incoming": 272312.5,
            "outgoing": 5240661.5
        },
        {
            "name": "lo",
            "speed": 0,
            "hd_addr": "00:00:00:00:00:00",
            "in_addr": "127.0.0.1",
            "round_1": {
                "receive": 7186738795,
                "r_packets": 1797667,
                "transmit": 7186738795,
                "t_packets": 1797667
            },
            "round_2": {
                "receive": 7186742395,
                "r_packets": 1797715,
                "transmit": 7186742395,
                "t_packets": 1797715
            },
            "incoming": 117.11082458496094,
            "outgoing": 117.11082458496094
        }
    ],
    "process": [
        {
            "user": "root",
            "pid": 2596,
            "pri": 19,
            "psr": 55,
            "cpu": 1633,
            "mem": 79.9000015258789,
            "vsz": 2310216668,
            "rss": 52687696,
            "gpu_id": 0,
            "gpu_mem": 0,
            "tty": "?",
            "stat": "Sl",
            "start": "Jul21",
            "time": "15380:25",
            "cmd": "/opt/QarkClient/lotus/lotus daemon"
        }
    ]
}
上次更新: 2021/10/9 20:34:35