Skip to content

Commit

Permalink
Rework to include Grafana config objects
Browse files Browse the repository at this point in the history
  • Loading branch information
socheatsok78 committed Dec 5, 2024
1 parent 1318b32 commit ac107b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# About
A housekeeping agent for removing unused/out-dated Promstack config objects and Prometheus scrape configs on an interval.
A housekeeping agent for removing unused/out-dated Promstack, Grafana and Prometheus's Docker config objects on an interval.

## Usage

Expand Down
40 changes: 39 additions & 1 deletion rootfs/promstack-housekeeping-agent.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

DOCKER_STACK_NAMESPACE=${DOCKER_STACK_NAMESPACE:-promstack}
HOUSEKEEPING_INTERVAL=${HOUSEKEEPING_INTERVAL:-60}
HOUSEKEEPING_INTERVAL=${HOUSEKEEPING_INTERVAL:-300}

logfmt() {
echo "ts=\"$(date +'%Y-%m-%dT%H:%M:%S%z')\" $*"
Expand All @@ -18,6 +18,8 @@ exec 2>&1
while true; do
logfmt 'msg="Schedule housekeeping on Docker config objects in '${HOUSEKEEPING_INTERVAL}' seconds..."'
sleep ${HOUSEKEEPING_INTERVAL}

# Promstask
for cid in $(docker config ls -q --filter=label=com.docker.stack.namespace=${DOCKER_STACK_NAMESPACE}); do
logfmt_oneline 'msg="Perform housekeeping on Docker config object"' 'filter="label=com.docker.stack.namespace='${DOCKER_STACK_NAMESPACE}'"' 'id="'$cid'"'
if docker config rm $cid > /dev/null 2>&1; then
Expand All @@ -26,6 +28,8 @@ while true; do
echo ' status="skipped"'
fi
done

# Prometheus
for cid in $(docker config ls -q --filter=label=io.prometheus.scrape_config=true); do
logfmt_oneline 'msg="Perform housekeeping on Docker config object"' 'filter="label=io.prometheus.scrape_config=true"' 'id="'$cid'"'
if docker config rm $cid > /dev/null 2>&1; then
Expand All @@ -34,6 +38,40 @@ while true; do
echo ' status="skipped"'
fi
done

# Grafana
for cid in $(docker config ls -q --filter=label=io.grafana.dashboard=true); do
logfmt_oneline 'msg="Perform housekeeping on Docker config object"' 'filter="label=io.grafana.dashboard=true"' 'id="'$cid'"'
if docker config rm $cid > /dev/null 2>&1; then
echo ' status="removed"'
else
echo ' status="skipped"'
fi
done
for cid in $(docker config ls -q --filter=label=io.grafana.provisioning.alerting=true); do
logfmt_oneline 'msg="Perform housekeeping on Docker config object"' 'filter="label=io.grafana.provisioning.alerting=true"' 'id="'$cid'"'
if docker config rm $cid > /dev/null 2>&1; then
echo ' status="removed"'
else
echo ' status="skipped"'
fi
done
for cid in $(docker config ls -q --filter=label=io.grafana.provisioning.dashboard=true); do
logfmt_oneline 'msg="Perform housekeeping on Docker config object"' 'filter="label=io.grafana.provisioning.dashboard=true"' 'id="'$cid'"'
if docker config rm $cid > /dev/null 2>&1; then
echo ' status="removed"'
else
echo ' status="skipped"'
fi
done
for cid in $(docker config ls -q --filter=label=io.grafana.provisioning.datasource=true); do
logfmt_oneline 'msg="Perform housekeeping on Docker config object"' 'filter="label=io.grafana.provisioning.datasource=true"' 'id="'$cid'"'
if docker config rm $cid > /dev/null 2>&1; then
echo ' status="removed"'
else
echo ' status="skipped"'
fi
done
done

exit 0

0 comments on commit ac107b6

Please sign in to comment.