Skip to content

Commit f4101fe

Browse files
committed
2023-06-24 Telegraf - old-menu branch - PR 2 of 2
Issue reported on [Discord](https://discord.com/channels/638610460567928832/638610461109256194/1121820918210121838) led to the discovery of several problems with Telegraf: 1. Default configuration had been amended to: - Add support for both InfluxDB 1.8 and InfluxDB 2; and - Comment-out both InfluxDB outputs. The practical consequences were: - The Dockerfile `sed` command could not find `[outputs.influxdb]`, so the `urls = ["http://influxdb:8086"]` could not be inserted, so telegraf could not write to any database; and - The container went into a restart loop. 2. The default `entrypoint.sh` script had been amended to invoke telegraf via: ``` exec setpriv --reuid telegraf --init-groups "$@" ``` Inside the container, user "telegraf" is userID 999. Outside container space, user 999 is not a member of the `docker` group so it doesn't have access to `/var/run/docker.sock`. The practical consequence was the telegraf process inside the container endlessly complaining: ``` E! [inputs.docker] Error in plugin: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock ``` Problem 1 has been addressed by removing the `sed` logic and adding `outputs.influxdb.conf` to the `auto_include` folder: ``` [[outputs.influxdb]] urls = ["http://influxdb:8086"] ``` This defaults to the `telegraf` database and is sufficient for telegraf to get going. Problem 2 has been addressed by not downgrading privileges. The alternative of changing the documentation to require the user to add userID 999 to the `docker` group is sub-optimal and not really in the spirit of IOTstack where, to the maximum extent possible, containers should "just work". Also, old-menu branch had become out-of-sync with master branch. That has been rectified. Signed-off-by: Phill Kelley <[email protected]>
1 parent fea7968 commit f4101fe

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

.templates/telegraf/Dockerfile

100644100755
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ RUN apt update && apt install -y rsync
66

77
# where IOTstack template files are stored
88
ENV IOTSTACK_DEFAULTS_DIR="iotstack_defaults"
9+
ENV BASELINE_CONFIG=/${IOTSTACK_DEFAULTS_DIR}/telegraf-reference.conf
10+
ENV IOTSTACK_CONFIG=/${IOTSTACK_DEFAULTS_DIR}/telegraf.conf
11+
ENV IOTSTACK_ENTRY_POINT="entrypoint.sh"
12+
ENV BASELINE_ENTRY_POINT="entrypoint-reference.sh"
913

1014
# copy template files to image
1115
COPY ${IOTSTACK_DEFAULTS_DIR} /${IOTSTACK_DEFAULTS_DIR}
@@ -14,22 +18,25 @@ COPY ${IOTSTACK_DEFAULTS_DIR} /${IOTSTACK_DEFAULTS_DIR}
1418
# a baseline reference for the user, and make it read-only.
1519
# 2. strip comment lines and blank lines from the baseline reference to
1620
# use as the starting point for the IOTstack default configuration.
17-
# 3. edit the IOTstack default configuration to insert an appropriate
18-
# URL for influxdb running in another container in the same stack.
19-
ENV BASELINE_CONFIG=/${IOTSTACK_DEFAULTS_DIR}/telegraf-reference.conf
20-
ENV IOTSTACK_CONFIG=/${IOTSTACK_DEFAULTS_DIR}/telegraf.conf
21+
# 3. append auto-inclusions which, among other things, sets up the
22+
# the appropriate URL for influxdb running in another container in
23+
# the same stack.
2124
RUN cp /etc/telegraf/telegraf.conf ${BASELINE_CONFIG} && \
25+
cat /${IOTSTACK_DEFAULTS_DIR}/auto_include/*.conf >> ${BASELINE_CONFIG} && \
26+
rm -r /${IOTSTACK_DEFAULTS_DIR}/auto_include && \
2227
chmod 444 ${BASELINE_CONFIG} && \
23-
grep -v -e "^[ ]*#" -e "^[ ]*$" ${BASELINE_CONFIG} >${IOTSTACK_CONFIG} && \
24-
sed -i '/^\[\[outputs.influxdb\]\]/a\ \ urls = ["http://influxdb:8086"]' ${IOTSTACK_CONFIG}
25-
ENV BASELINE_CONFIG=
26-
ENV IOTSTACK_CONFIG=
28+
grep -v -e "^[ ]*#" -e "^[ ]*$" ${BASELINE_CONFIG} >${IOTSTACK_CONFIG}
2729

2830
# replace the docker entry-point script with a self-repairing version
29-
ENV IOTSTACK_ENTRY_POINT="entrypoint.sh"
31+
RUN cp /${IOTSTACK_ENTRY_POINT} /${BASELINE_ENTRY_POINT}
3032
COPY ${IOTSTACK_ENTRY_POINT} /${IOTSTACK_ENTRY_POINT}
3133
RUN chmod 755 /${IOTSTACK_ENTRY_POINT}
34+
35+
# undefine variables not needed at runtime
36+
ENV BASELINE_CONFIG=
37+
ENV IOTSTACK_CONFIG=
3238
ENV IOTSTACK_ENTRY_POINT=
39+
ENV BASELINE_ENTRY_POINT=
3340

3441
# IOTstack declares this path for persistent storage
3542
VOLUME ["/etc/telegraf"]

.templates/telegraf/entrypoint.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,28 @@ fi
99
U="$(id -u)"
1010
T="/etc/telegraf"
1111
if [ "$U" = '0' -a -d "$T" ]; then
12+
echo "Performing IOTstack self repair"
1213
rsync -arp --ignore-existing /${IOTSTACK_DEFAULTS_DIR}/ "$T"
1314
chown -R "$U:$U" "$T"
1415
fi
1516

16-
exec "$@"
17+
if [ $EUID -eq 0 ]; then
18+
19+
# Allow telegraf to send ICMP packets and bind to privliged ports
20+
setcap cap_net_raw,cap_net_bind_service+ep /usr/bin/telegraf || echo "Failed to set additional capabilities on /usr/bin/telegraf"
1721

22+
# note: at this point, the default version of this file runs:
23+
#
24+
# exec setpriv --reuid telegraf --init-groups "$@"
25+
#
26+
# Inside the container, user "telegraf" is userID 999, which
27+
# isn't a member of the "docker" group outside container-space
28+
# so the practical effect of downgrading privileges in this
29+
# way is to deny access to /var/run/docker.sock, and then you
30+
# get a mess. It's not clear whether the setcap is necessary
31+
# on a Raspberry Pi but it has been left in place in case it
32+
# turns out to be useful in other Docker environments.
1833

34+
fi
35+
36+
exec "$@"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[inputs.file]]
2+
files = ["/sys/class/thermal/thermal_zone0/temp"]
3+
name_override = "cpu_temperature"
4+
data_format = "value"
5+
data_type = "integer"

.templates/telegraf/iotstack_defaults/additions/inputs.docker.conf renamed to .templates/telegraf/iotstack_defaults/auto_include/inputs.docker.conf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
[[inputs.docker]]
44
endpoint = "unix:///var/run/docker.sock"
55
gather_services = false
6-
container_names = []
76
source_tag = false
87
container_name_include = []
98
container_name_exclude = []
109
timeout = "5s"
1110
perdevice = false
12-
total = true
11+
total_include = ["cpu", "blkio", "network"]
1312
docker_label_include = []
1413
docker_label_exclude = []
1514
tag_env = ["HEAP_SIZE"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[outputs.influxdb]]
2+
urls = ["http://influxdb:8086"]
3+

0 commit comments

Comments
 (0)