This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
contrib: integrate with systemd units #61
Open
pothos
wants to merge
1
commit into
main
Choose a base branch
from
kai/trace-systemd-services
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[Unit] | ||
Description=Traceloop | ||
|
||
[Service] | ||
Type=notify | ||
NotifyAccess=all | ||
ExecStartPre=/bin/rm -f /run/traceloop.socket | ||
ExecStart=/bin/sh -c "/home/kai/kinvolk/traceloop/traceloop serve & while ! curl -fsS --unix-socket /run/traceloop.socket 'http://localhost/list' > /dev/null; do sleep 1; echo Waiting for traceloop to start up; done ; systemd-notify --ready; wait" | ||
ExecStopPost=/bin/rm -f /run/traceloop.socket | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/bin/sh | ||
CMD="$1" | ||
ARG1="$2" | ||
ARG2="$3" | ||
set -euo pipefail | ||
|
||
if [ $# -lt 1 ] || [ "$CMD" = "-h" ] || [ "$CMD" = "--help" ]; then | ||
echo "Usage: $0 COMMAND|-h|--help" | ||
echo "Needs to be run with access to /run/traceloop.socket (e.g., with sudo)." | ||
echo "Commands:" | ||
echo " list-all" | ||
echo " dump-id ID" | ||
echo " dump-name NAME" | ||
echo " close-id ID" | ||
echo " close-name NAME" | ||
echo " add-current-cgroup NAME" | ||
echo | ||
echo " list-sd-units" | ||
echo " list-sd-traces SERVICE" | ||
echo " dump-sd SERVICE INVOCATION|-1" | ||
echo " close-sd SERVICE INVOCATION|-1|all" | ||
echo " add-current-cgroup-sd SERVICE INVOCATION" | ||
echo | ||
echo "The *-sd commands assume the trace name format is systemd_UNIT_INVOCATIONID which can be automated with:" | ||
echo ' ExecStartPre=+/…/contrib/traceloopctl add-current-cgroup-sd "%n" "$INVOCATION_ID"' | ||
exit 1 | ||
fi | ||
|
||
SCRIPT_FOLDER="$(dirname "$(readlink -f "$0")")" | ||
CURL="curl -fsS --unix-socket /run/traceloop.socket" | ||
if [ "$CMD" = list-all ]; then | ||
$CURL "http://localhost/list" | ||
elif [ "$CMD" = dump-id ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected ID argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
ID="$ARG1" | ||
$CURL "http://localhost/dump?id=${ID}" | ||
elif [ "$CMD" = dump-name ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected NAME argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
NAME="$ARG1" | ||
$CURL "http://localhost/dump-by-name?name=${NAME}" | ||
elif [ "$CMD" = close-id ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected ID argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
ID="$ARG1" | ||
$CURL "http://localhost/close?id=${ID}" | ||
elif [ "$CMD" = close-name ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected NAME argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
NAME="$ARG1" | ||
$CURL "http://localhost/close-by-name?name=${NAME}" | ||
elif [ "$CMD" = add-current-cgroup ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected NAME argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
NAME="$ARG1" | ||
CURRENT_CGROUP=$("${SCRIPT_FOLDER}"/current-cgroup) | ||
$CURL "http://localhost/add?name=${NAME}&cgrouppath=${CURRENT_CGROUP}" | ||
elif [ "$CMD" = list-sd-units ]; then | ||
echo " Traces Units" | ||
echo "------- -----" | ||
$CURL "http://localhost/list" | grep -o '\[systemd_.*\]' | cut -d _ -f 2 | sort | uniq -c | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The systemd-specific commands *-sd could be added in the Go program, so that the shell script could be simplified. (could be in a follow-up PR) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And internally traceloop with still concatenate a special name or would you introduce a new attributes for Tracelet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess introducing new attributes but I have not given it much thought. Do you have a preference? |
||
elif [ "$CMD" = list-sd-traces ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected SERVICE argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
SERVICE="$ARG1" | ||
$CURL "http://localhost/list" | grep -o "\[systemd_${SERVICE}.*\] " | cut -d _ -f 3- | cut -d ] -f 1 | ||
elif [ "$CMD" = dump-sd ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected SERVICE argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
SERVICE="$ARG1" | ||
if [ "$ARG2" = "" ]; then | ||
echo "Expected INVOCATION argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
INVOCATION="$ARG2" | ||
if [ "$INVOCATION" = "-1" ]; then | ||
INVOCATION=$($CURL "http://localhost/list" | grep -o "\[systemd_${SERVICE}.*\] " | cut -d _ -f 3- | cut -d ] -f 1 | tail -n 1) | ||
fi | ||
$CURL "http://localhost/dump-by-name?name=systemd_${SERVICE}_${INVOCATION}" | ||
elif [ "$CMD" = close-sd ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected SERVICE argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
SERVICE="$ARG1" | ||
if [ "$ARG2" = "" ]; then | ||
echo "Expected INVOCATION argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
if [ "$ARG2" = "-1" ]; then | ||
INVOCATIONS=$($CURL "http://localhost/list" | grep -o "\[systemd_${SERVICE}.*\] " | cut -d _ -f 3- | cut -d ] -f 1 | tail -n 1) | ||
elif [ "$ARG2" = all ]; then | ||
INVOCATIONS=$($CURL "http://localhost/list" | grep -o "\[systemd_${SERVICE}.*\] " | cut -d _ -f 3- | cut -d ] -f 1) | ||
else | ||
INVOCATIONS="$ARG2" | ||
fi | ||
for ID in $INVOCATIONS; do | ||
$CURL "http://localhost/close-by-name?name=systemd_${SERVICE}_${ID}" | ||
done | ||
elif [ "$CMD" = add-current-cgroup-sd ]; then | ||
if [ "$ARG1" = "" ]; then | ||
echo "Expected SERVICE argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
SERVICE="$ARG1" | ||
if [ "$ARG2" = "" ]; then | ||
echo "Expected INVOCATION argument" > /dev/stderr | ||
exit 1 | ||
fi | ||
INVOCATION="$ARG2" | ||
CURRENT_CGROUP=$("${SCRIPT_FOLDER}"/current-cgroup) | ||
$CURL "http://localhost/add?name=systemd_${SERVICE}_${INVOCATION}&cgrouppath=${CURRENT_CGROUP}" | ||
else | ||
echo "Unknown command \"$CMD\"" > /dev/stderr | ||
exit 1 | ||
fi |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of calling
systemd-notify
, you could do it in Golang:https://vincent.bernat.ch/en/blog/2017-systemd-golang
https://github.com/coreos/go-systemd/blob/v22.3.2/daemon/sdnotify.go#L56
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, with ignoring the return code
(false, nil)
it should be fine when used outside of a systemd unit.