使用 shell 批量生成 Prometheus 的自动发现文件。
文件 a
ly@Fuck:/tmp$ cat file_a
192.168.96.229:39100
192.168.96.119:39100
文件 b
ly@Fuck:/tmp$ cat file_b
k8s主节点1
k8s主节点2
期望生成的文件:
{
"targets": [ "192.168.96.229:39100"],
"labels": {
"job": "k8s主节点1"
}
},
{
"targets": [ "192.168.96.119:39100"],
"labels": {
"job": "k8s主节点2"
}
},
方法一:
#!/bin/bash
exec 3<>file_a
exec 4<>file_b
while read -u3 line1;read -u4 line2;do
cat <<EOF>> nimad
{
"targets": [ "${line1}"],
"labels": {
"job": "${line2}"
}
},
EOF
done
方法2
#!/bin/bash
a=`cat file_a`
b=`cat file_b`
set -- $b
for i in $a;do
cat <<EOF>> nimad
{
"targets": [ "$i"],
"labels": {
"job": "$1"
}
},
EOF
shift
done
标签:shell,变量,192.168,cat,file,单个,k8s,39100,targets
From: https://www.cnblogs.com/fsckzy/p/17105421.html