Skip to content

Instantly share code, notes, and snippets.

@broekema41
Last active March 13, 2019 10:19
Show Gist options
  • Select an option

  • Save broekema41/26b23d5816395989a65c5ac4eb18d869 to your computer and use it in GitHub Desktop.

Select an option

Save broekema41/26b23d5816395989a65c5ac4eb18d869 to your computer and use it in GitHub Desktop.
The most minimalistic version of bulk pushing docker stats to elasticsearch
#!/bin/bash
# Count throttles
# The docker stats command maintaines a stream at an interval,
# we are depending on this interval with counters so that we
# do not need a cron or date detection / manipulation.
max=20
lineNumber=0
cleanMax=25
cleanNumber=0
bulkstore=""
# on startup only
create_index () {
curl -s -X PUT "localhost:9200/stats" -H 'Content-Type: application/json' -d'
{
"mappings" : {
"_doc" : {
"properties": {
"epoch_millis": {
"type": "date",
"format": "epoch_millis"
},
"container": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"cpu": {
"type": "short"
},
"memory": {
"properties": {
"percent": {
"type": "short"
}
}
}
}
}
}
}
' > /dev/null
}
clean_index () {
cleanNumber=$((cleanNumber+1))
echo "cleanNumber"
if [ "$cleanNumber" -gt "$cleanMax" ]; then
echo "cleaning"
cleanNumber=0
curl -X POST "localhost:9200/stats/_doc/_delete_by_query?conflicts=proceed" -H 'Content-Type: application/json' -d'
{
"query" : {
"range" : {
"utcTimeStamp" : {
"lte": "now-48h",
"time_zone": "+00:00"
}
}
}
}
'
fi
}
create_index
docker stats --format "{\"container\": \"{{.Container}}\", \"name\":\"{{.Name}}\", \"memory\": { \"raw\": \"{{.MemUsage}}\", \"percent\": \"{{.MemPerc}}\"}, \"cpu\": \"{{.CPUPerc}}\"}" | while read line; do
lineNumber=$((lineNumber+1))
mydate=`date +%s%3N`
formattedline=`echo $line | tr -dc '[[:print:]]' | sed -e "s/\[2J\[H//g" | sed -e "s/%//g" | sed -e "s/\"container\":/\"epoch_millis\": \"${mydate}\",\"container\":/g"`
bulkstore="${bulkstore}{\"index\":{\"_index\":\"stats\",\"_type\":\"_doc\"}}\n"
bulkstore="${bulkstore}${formattedline}\n"
if [ "$lineNumber" -gt "$max" ]; then
json=$bulkstore
bulkstore=""
lineNumber=0
echo "$json" > statdocs.json
curl -s -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @statdocs.json > /dev/null
clean_index
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment