- code refactor
- security fixes
This commit is contained in:
parent
efaa7610ad
commit
d6528c247a
1 changed files with 38 additions and 46 deletions
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
get_tmux_option() {
|
get_tmux_option() {
|
||||||
local option_value=$(tmux show-option -gqv "$1");
|
local option_value
|
||||||
echo ${option_value:-$2}
|
option_value=$(tmux show-option -gqv "$1")
|
||||||
|
echo "${option_value:-$2}"
|
||||||
}
|
}
|
||||||
|
|
||||||
######
|
######
|
||||||
|
@ -24,44 +25,40 @@ API_URL="http://$HOST:$PORT/api"
|
||||||
# * https://github.com/tmux/tmux/wiki/Advanced-Use#user-content-getting-information
|
# * https://github.com/tmux/tmux/wiki/Advanced-Use#user-content-getting-information
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
### FUNCTIONS
|
### FUNCTIONS
|
||||||
|
|
||||||
DEBUG=0
|
DEBUG=0
|
||||||
TMP_FILE=$(mktemp)
|
TMP_FILE=$(mktemp)
|
||||||
echo $TMP_FILE
|
echo "$TMP_FILE"
|
||||||
|
|
||||||
init_bucket() {
|
init_bucket() {
|
||||||
HTTP_CODE=$(curl -X GET "${API_URL}/0/buckets/$BUCKET_ID" -H "accept: application/json" -s -o /dev/null -w %{http_code})
|
HTTP_CODE=$(curl -X GET "${API_URL}/0/buckets/$BUCKET_ID" -H "accept: application/json" -s -o /dev/null -w "%{http_code}")
|
||||||
if (( $HTTP_CODE == 404 )) # not found
|
if ((HTTP_CODE == 404)); then # not found
|
||||||
then
|
JSON="{\"client\":\"$BUCKET_ID\",\"type\":\"tmux.sessions\",\"hostname\":\"$(uname -n)\"}"
|
||||||
JSON="{\"client\":\"$BUCKET_ID\",\"type\":\"tmux.sessions\",\"hostname\":\"$(hostname)\"}"
|
HTTP_CODE=$(curl -X POST "${API_URL}/0/buckets/$BUCKET_ID" -H "accept: application/json" -H "Content-Type: application/json" -d "$JSON" -s -o /dev/null -w "%{http_code}")
|
||||||
HTTP_CODE=$(curl -X POST "${API_URL}/0/buckets/$BUCKET_ID" -H "accept: application/json" -H "Content-Type: application/json" -d "$JSON" -s -o /dev/null -w %{http_code})
|
if ((HTTP_CODE != 200)); then
|
||||||
if (( $HTTP_CODE != 200 ))
|
|
||||||
then
|
|
||||||
echo "ERROR creating bucket"
|
echo "ERROR creating bucket"
|
||||||
exit -1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
log_to_bucket() {
|
log_to_bucket() {
|
||||||
sess=$1
|
sess=$1
|
||||||
DATA=$(tmux display -t $sess -p "{\"title\":\"#{session_name}\",\"session_name\":\"#{session_name}\",\"window_name\":\"#{window_name}\",\"pane_title\":\"#{pane_title}\",\"pane_current_command\":\"#{pane_current_command}\",\"pane_current_path\":\"#{pane_current_path}\"}");
|
DATA=$(tmux display -t "$sess" -p "{\"title\":\"#{session_name}\",\"session_name\":\"#{session_name}\",\"window_name\":\"#{window_name}\",\"pane_title\":\"#{pane_title}\",\"pane_current_command\":\"#{pane_current_command}\",\"pane_current_path\":\"#{pane_current_path}\"}")
|
||||||
PAYLOAD="{\"timestamp\":\"$(date -Is)\",\"duration\":0,\"data\":$DATA}"
|
PAYLOAD="{\"timestamp\":\"$(date -Is)\",\"duration\":0,\"data\":$DATA}"
|
||||||
echo "$PAYLOAD"
|
echo "$PAYLOAD"
|
||||||
HTTP_CODE=$(curl -X POST "${API_URL}/0/buckets/$BUCKET_ID/heartbeat?pulsetime=$PULSETIME" -H "accept: application/json" -H "Content-Type: application/json" -d "$PAYLOAD" -s -o $TMP_FILE -w %{http_code})
|
HTTP_CODE=$(curl -X POST "${API_URL}/0/buckets/$BUCKET_ID/heartbeat?pulsetime=$PULSETIME" -H "accept: application/json" -H "Content-Type: application/json" -d "$PAYLOAD" -s -o "$TMP_FILE" -w "%{http_code}")
|
||||||
if (( $HTTP_CODE != 200 )); then
|
if ((HTTP_CODE != 200)); then
|
||||||
echo "Request failed"
|
echo "Request failed"
|
||||||
cat $TMP_FILE
|
cat "$TMP_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$DEBUG" -eq "1" ]]; then
|
if [[ "$DEBUG" -eq "1" ]]; then
|
||||||
cat $TMP_FILE
|
cat "$TMP_FILE"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
### MAIN POLL LOOP
|
### MAIN POLL LOOP
|
||||||
|
|
||||||
declare -A act_last
|
declare -A act_last
|
||||||
|
@ -69,36 +66,31 @@ declare -A act_current
|
||||||
|
|
||||||
init_bucket
|
init_bucket
|
||||||
|
|
||||||
while [ true ]
|
while true; do
|
||||||
do
|
|
||||||
#clear
|
#clear
|
||||||
sessions=$(tmux list-sessions | awk '{print $1}')
|
sessions=$(tmux list-sessions | awk '{print $1}')
|
||||||
if (( $? != 0 )); then
|
if ! tmux list-sessions &>/dev/null; then
|
||||||
echo "tmux list-sessions ERROR: $?"
|
echo "tmux list-sessions ERROR: $?"
|
||||||
fi
|
fi
|
||||||
if (( $? == 0 )); then
|
|
||||||
LAST_IFS=$IFS
|
LAST_IFS=$IFS
|
||||||
IFS='
|
IFS=$'\n'
|
||||||
'
|
|
||||||
for sess in ${sessions}; do
|
readarray -t session_list <<<"$sessions"
|
||||||
act_time=$(tmux display -t $sess -p '#{session_activity}')
|
for sess in "${session_list[@]}"; do
|
||||||
|
act_time=$(tmux display -t "$sess" -p '#{session_activity}')
|
||||||
if [[ ! -v "act_last[$sess]" ]]; then
|
if [[ ! -v "act_last[$sess]" ]]; then
|
||||||
act_last[$sess]='0'
|
act_last[$sess]='0'
|
||||||
fi
|
fi
|
||||||
if (( $act_time > ${act_last[$sess]} )); then
|
if ((act_time > ${act_last[$sess]})); then
|
||||||
# echo "###> "$sess' '$(date -Iseconds)' '$act_time' '$act_last[$sess] ## >> tmux-sess-act.log
|
# echo "###> "$sess' '$(date -Iseconds)' '$act_time' '$act_last[$sess] ## >> tmux-sess-act.log
|
||||||
log_to_bucket $sess
|
log_to_bucket "$sess"
|
||||||
fi
|
fi
|
||||||
act_current[$sess]=$act_time
|
act_current[$sess]=$act_time
|
||||||
done
|
done
|
||||||
IFS=$LAST_IFS
|
IFS=$LAST_IFS
|
||||||
# copy arrays
|
|
||||||
unset R
|
|
||||||
declare -A act_last
|
|
||||||
for sess in "${!act_current[@]}"; do
|
for sess in "${!act_current[@]}"; do
|
||||||
act_last[$sess]=${act_current[$sess]}
|
act_last[$sess]=${act_current[$sess]}
|
||||||
done
|
done
|
||||||
fi
|
|
||||||
|
|
||||||
sleep $POLL_INTERVAL
|
sleep "$POLL_INTERVAL"
|
||||||
done
|
done
|
||||||
|
|
Loading…
Add table
Reference in a new issue