#!/bin/bash

# 本脚本用于生成 process_exporter 需要的配置文件，会固定带上 mysqld，会动态生成当前 skynet 进程的信息
# 直接运行此脚本，会在当前目录生成一份 config.yaml 文件
# 将它替换到 /usr/local/process_exporter/ ，并重启 process_exporter 即生效

fp="/usr/local/process_exporter/config.yaml"

echo -n "" > $fp

echo "process_names:" >> $fp
echo "   - name: mysqld" >> $fp
echo "     cmdline:" >> $fp
echo "     - '/usr/local/mysql/bin/mysqld'" >> $fp

allskynet=`ps aux | grep skynet | grep -v "grep" | awk '{print $12}'`
for s in ${allskynet[@]}; do
    name=`echo $s | awk -F '/' '{split($10,stype,"_");printf("'\''%s-%s-%s'\''", $3, $4,stype[1])}'`
    echo "   - name: $name" >> $fp
    echo "     cmdline:" >> $fp
    echo "     - '$s'" >> $fp
done
