forked from falzonv/container_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainer_exporter.sh
executable file
·54 lines (49 loc) · 1.7 KB
/
container_exporter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#
# container_exporter exposes CPU% and MEM% metrics of all running
# Docker containers to Prometheus
#
# Copyright (C) 2021 Vincent Falzon
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Collect the data from all running Docker containers
inspect_ip=$(docker inspect -f '{{.Config.Hostname}} {{.NetworkSettings.Networks.mysql.IPAddress}} ' $(docker ps -q) 2>/dev/null)
# Extract and prepare the metrics
ips=""
while read -r line
do
read name ip <<< "$line"
if [ "$ip" == "<no value>" ]
then
ips="${ips}container_ip{container=\"$name\"} "0"\n"
else
ips="${ips}container_ip{container=\"$name\",ip=\"$ip\"} "1"\n"
fi
done <<< "$inspect_ip"
# Write the metrics to a temporary file to get the size
tmp=$(mktemp)
echo "# HELP container_ip The IP Address value from docker inspect." > $tmp
echo "# TYPE container_ip gauge" >> $tmp
echo -en "$ips" >> $tmp
# Display the HTTP header
echo "HTTP/1.1 200 OK"
echo "Date: $(date)"
echo "Content-Length: $(stat -c "%s" $tmp)"
echo "Content-Type: text/plain"
echo "Connection: close"
echo
# Display the metrics
cat $tmp
rm $tmp