cat <<EOF | curl --data-binary @- http://pushgateway-host:9091/metrics/job/data_monitor # HELP data_file_num Total files in data directory # TYPE data_file_num gauge data_file_num{instance="server01",path="/data"} 158 data_file_num{instance="server02",path="/backup"} 72 EOF
# 获取系统指标 read disk mem cpu total <<< $(get_system_metrics)
# 添加系统指标到文件 echo"# HELP cssx_disk_usage Data disk usage" >> "$metrics_file" echo"# TYPE cssx_disk_usage gauge" >> "$metrics_file" echo"cssx_disk_usage $disk" >> "$metrics_file"
echo"# HELP cssx_memory_usage Memory usage" >> "$metrics_file" echo"# TYPE cssx_memory_usage gauge" >> "$metrics_file" echo"cssx_memory_usage $mem" >> "$metrics_file"
echo"# HELP cssx_cpu_usage CPU usage" >> "$metrics_file" echo"# TYPE cssx_cpu_usage gauge" >> "$metrics_file" echo"cssx_cpu_usage $cpu" >> "$metrics_file"
echo"# HELP cssx_total_memory Total system memory in MB" >> "$metrics_file" echo"# TYPE cssx_total_memory gauge" >> "$metrics_file" echo"cssx_total_memory $total" >> "$metrics_file"
# 添加进程状态到文件 echo"# HELP cssx_process_status Process running status" >> "$metrics_file" echo"# TYPE cssx_process_status gauge" >> "$metrics_file"
# 获取内网IP int_ip=$(ip -4 addr show scope global | grep -Po 'inet \K[\d.]+' | grep -v '^127\.' | head -n1) if [[ -z "$int_ip" ]]; then int_ip="unknown" fi
# 获取外网IP ext_ip=$(cat /data/ip 2>/dev/null) if [[ -z "$ext_ip" ]]; then ext_ip=$(curl -s https://api.ipify.org/ 2>/dev/null) if [[ $? -ne 0 || -z "$ext_ip" ]]; then ext_ip="unknown" fi fi
# 添加IP指标 echo"# HELP cssx_internal_ip Internal IP address" >> "$metrics_file" echo"# TYPE cssx_internal_ip gauge" >> "$metrics_file" echo"cssx_internal_ip{ip=\"$int_ip\"} 1" >> "$metrics_file"
echo"# HELP cssx_external_ip External IP address" >> "$metrics_file" echo"# TYPE cssx_external_ip gauge" >> "$metrics_file" echo"cssx_external_ip{ip=\"$ext_ip\"} 1" >> "$metrics_file"
# 处理进程监控 for dir_name in $(get_process_dirs); do # 从目录名提取进程名 process_name=$(echo"$dir_name" | grep -oP 'cssx_\K.+?(?=_s\d+)') count=$(check_process_count "$process_name")