Files
scripts/ioping_scraper_json.sh

100 lines
3.4 KiB
Bash

#!/bin/bash
#ps aux | grep -v ps |
# awk -v OFS=, '{print $2, $3, $11}' |
# jq -R 'split(",") | {process: .[2], pid: .[0]}'
#ps aux | grep -v ps |
METRICS_DATA=''
varC=''
varM=''
varI=''
# CPU %
# Mem MB
#echo $z
# { printf "\"cpu_usage\": %s{\"process\": \"%s\", \"pid\": \"%s\"}",
# ps aux | awk '
#BEGIN { ORS = ""; print " [ "}
#{ printf "%s{\"user\": \"%s\", \"pid\": \"%s\", \"cpu\": \"%s\"}",
# separator, $1, $2, $3
# separator = ", "
#}
#END { print " ] " }';
# varC=$varC$(echo "$z" | awk '$3>10 {print "\"cpu_usage\": "$3",\\n\"tags\": {\"process\": \""$11"\", \"pid\": "$2"}" }');
# echo $p
# varM=$varM$(echo "$z" | awk '$6/1024>50 {print "memory_usage{process=\""$11"\", pid=\""$2"\"}", $6/1024}');
# varI=$varI$(ioping -q -s 8096 -c 3 -J -D /mnt/mfs_01 | sed 's/\\//g' | jq -r '",{\"__name__\": \"ioping_max\", \"__type__\":\"gauge\", \"target\": \"\(.[].target.path)\", \"size\": \"\(.[].stat.size)\", \"value\": \(.[].stat.max)},{\"__name__\": \"ioping_iops\", \"__type__\":\"gauge\", \"target\": \"\(.[].target.path)\", \"size\":\"\(.[].stat.size)\", \"value\": \(.[].stat.iops)}"')
varI=$varI$(ioping -q -i 1 -s 8096 -c 3 -J -D $1 | sed 's/\\//g' | jq -r '"{\"__name__\": \"ioping_max\", \"__type__\":\"gauge\", \"target\": \"\(.[].target.path)\", \"size\": \"\(.[].stat.size)\", \"value\": \(.[].stat.max)},{\"__name__\": \"ioping_iops\", \"__type__\":\"gauge\", \"target\": \"\(.[].target.path)\", \"size\":\"\(.[].stat.size)\", \"value\": \(.[].stat.iops)}"')
#ioping_iops{target=\"\(.[].target.path)\", size=\"\(.[].stat.size)\", iops=\"\(.[].stat.iops)\n
#echo $varC
#echo $varM
METRICS_DATA="[$varC$varM$varI]"
#echo -ne $var
echo -ne "$METRICS_DATA"
#| sed 's/} ,{/}, {/g; s/},{"__/}, {"__/g; s/} , {/}, {/g' | sed 's/}, {/\n/g' | sed 's/^/[{/; s/$/}]/' | sed '1s/^\[{//; $s/}]$//'
#sed 's/} ,{/}, {/\g'
exit
echo
# Send the metrics using curl
#curl -u osa@novikom.ru:ZxosgpRrpd8sOKDP -k https://opob.service.banknt/api/default11/ingest/metrics/_json -d "$METRICS_DATA"
echo
response=$(curl -s -u osa@novikom.ru:ZxosgpRrpd8sOKDP -k https://opob.service.banknt/api/OSA/ingest/metrics/_json -d "$METRICS_DATA" | jq '.code')
echo
#echo $response
#response=$(curl -s -w "%{http_code}" -o /dev/null -X POST "$OPENOBS_ENDPOINT" \
# -H "Content-Type: application/json" \
# -H "Authorization: Bearer $API_KEY" # Uncomment if using API key \
# -d "$METRICS_DATA")
echo
# Check the response
if [ "$response" -eq 200 ]; then
echo "Metrics sent successfully!"
else
echo "Failed to send metrics. HTTP response code: $response"
fi
#echo -ne $var | sed 's/}, {/\n/g' | sed 's/^/[{/; s/$/}]/' | sed '1s/^\[{{/[{/; $s/}}]$/}]/'
#exit
# cont_len=$(echo -ne $var | wc -c)
# { echo -ne "HTTP/1.0 200 OK\nContent-Length: $cont_len\n"; echo -ne "$var\nEOF\n"; } | nc -l -p 9091
# { echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $cont_len\r\n"; echo -ne "$var\n EOF\n"; } | nc -l -p 9091
#while :; do { echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c <some.file)\r\n\r\n"; cat some.file; } | nc -l -p 8080; done
exit
[
{
"__name__": "cpu",
"__type__": "gauge",
"namespace": "ziox",
"container": "zo1",
"_timestamp": 1687260776548485,
"value": 1.33
},
{
"__name__": "cpu",
"__type__": "gauge",
"namespace": "ziox",
"container": "zo2",
"value": 0.33
}
]