Monitor Syncthing Synchronization Status with Conky: Bash Script for Local and Remote Devices on Linux

Hello everyone,

I’m excited to share my first post on the Syncthing forum! I’ve been working on a script for Ubuntu 24.04 (my machine) to monitor the synchronization status of local and remote devices using Syncthing’s API, and I thought it might be helpful to others in the community.

Overview:

Purpose: Display real-time synchronization status for local and remote devices in a Conky widget on your desktop.

Features:

  • Shows local folder synchronization percentages.
  • Shows remote devices’ synchronization percentages for each shared folder.
  • Uses color coding to indicate synchronization progress.
  • Automatically starts with your system.

Prerequisites:

  • Syncthing installed and running on your system.
  • Conky installed for displaying system information on your desktop.
  • Basic knowledge of Bash scripting.

1_ Install Conky:

If you don’t have Conky installed, you can install it using your package manager.

For Ubuntu/Debian-based systems:

bash

sudo apt update
sudo apt install conky-all

For Fedora:

bash

sudo dnf install conky

For Arch Linux:

bash

sudo pacman -S conky

2_ Create the Bash Script:

Create a new Bash script file, for example syncthing_monitor.sh, and make it executable.

bash

touch ~/syncthing_monitor.sh
chmod +x ~/syncthing_monitor.sh

Open the script in your favorite text editor and paste the following code:

bash

#!/bin/bash

# Use environment variables or default values
API_KEY="${API_KEY:-YOUR_API_KEY}"
LOCAL_DEVICE_ID="${LOCAL_DEVICE_ID:-YOUR_LOCAL_DEVICE_ID}"

# Ensure consistent decimal separator
export LC_NUMERIC="C"

# Retrieve system configuration
if ! SYSTEM_CONFIG=$(curl -s -H "X-API-Key: $API_KEY" http://127.0.0.1:8384/rest/system/config); then
    echo "Failed to retrieve system configuration."
    exit 1
fi

# Retrieve connection data
if ! CONNECTIONS=$(curl -s -H "X-API-Key: $API_KEY" http://127.0.0.1:8384/rest/system/connections); then
    echo "Failed to retrieve connection data."
    exit 1
fi

# Display local synchronization status
echo "Local Sync:"
echo "$SYSTEM_CONFIG" | jq -r '.folders[] | "\(.id)|\(.label)"' | while IFS='|' read -r FOLDER_ID FOLDER_LABEL; do
    # Retrieve local folder sync data
    if ! STATUS=$(curl -s -H "X-API-Key: $API_KEY" "http://127.0.0.1:8384/rest/db/status?folder=$FOLDER_ID"); then
        echo "Failed to retrieve status for folder $FOLDER_ID."
        continue
    fi

    # Extract local and global bytes
    LOCAL_BYTES=$(echo "$STATUS" | jq '.localBytes')
    GLOBAL_BYTES=$(echo "$STATUS" | jq '.globalBytes')

    # Calculate synchronization percentage
    if [ "$GLOBAL_BYTES" -ne 0 ]; then
        PERCENTAGE=$(printf "%.2f" "$(echo "$LOCAL_BYTES $GLOBAL_BYTES" | awk '{print ($1/$2)*100}')")
    else
        PERCENTAGE="0.00"
    fi

    # Determine color based on synchronization percentage
    PERCENTAGE_ROUNDED=$(printf "%.0f" "$PERCENTAGE")
    if [ "$PERCENTAGE_ROUNDED" -eq 100 ]; then
        COLOR="#29b74e"  # Green for 100%
    else
        COLOR="#3498db"  # Blue for less than 100%
    fi

    # Display the result for the local folder with color applied only to the percentage
    echo -e "$FOLDER_LABEL ($FOLDER_ID): \${color $COLOR}$PERCENTAGE%\${color}"
done

echo ""

# Display remote devices sync status
echo "Remote devices sync status:"
echo "$SYSTEM_CONFIG" | jq -r '.devices[] | "\(.deviceID)|\(.name)"' | while IFS='|' read -r DEVICE_ID DEVICE_NAME; do
    # Skip local device
    if [ "$DEVICE_ID" = "$LOCAL_DEVICE_ID" ]; then
        continue
    fi

    # Check connection status
    CONNECTED=$(echo "$CONNECTIONS" | jq -r --arg DEVICE_ID "$DEVICE_ID" '.connections[$DEVICE_ID].connected')

    if [ "$CONNECTED" = "true" ]; then
        echo -n "$DEVICE_NAME: "
        echo "$SYSTEM_CONFIG" | jq -r --arg DEVICE_ID "$DEVICE_ID" '.folders[] | select(.devices[].deviceID == $DEVICE_ID) | "\(.id)|\(.label)"' | while IFS='|' read -r FOLDER_ID FOLDER_LABEL; do
            if ! COMPLETION_DATA=$(curl -s -H "X-API-Key: $API_KEY" "http://127.0.0.1:8384/rest/db/completion?folder=$FOLDER_ID&device=$DEVICE_ID"); then
                echo "Failed to retrieve completion data for folder $FOLDER_ID."
                continue
            fi

            COMPLETION=$(echo "$COMPLETION_DATA" | jq -r '.completion // "0"')
            COMPLETION=$(printf "%.2f" "$(echo "scale=2; $COMPLETION / 1" | bc)")

            # Determine color based on completion percentage
            COMPLETION_ROUNDED=$(printf "%.0f" "$COMPLETION")
            if [ "$COMPLETION_ROUNDED" -eq 100 ]; then
                COLOR="#29b74e"  # Green for 100%
            else
                COLOR="#3498db"  # Blue for less than 100%
            fi

            # Display the result with color applied only to the percentage
            echo -n "$FOLDER_LABEL ($FOLDER_ID): \${color $COLOR}$COMPLETION%\${color}  "
        done
        echo ""
    else
        echo "$DEVICE_NAME: \${color #9a6dc0} disconnected \${color}"
    fi
done

Important: Replace YOUR_API_KEY with your actual Syncthing API key and YOUR_LOCAL_DEVICE_ID with your local device ID. You can find these in Syncthing’s GUI under Settings > Actions > Show ID and Settings > GUI.


3_ Install jq and bc (if not already installed):

The script uses jq for parsing JSON and bc for calculations.

For Ubuntu/Debian-based systems:

bash

sudo apt install jq bc

4_ Configure Conky:

Create or edit your Conky configuration file, usually located at ~/.conkyrc or ~/.config/conky/conky.conf.

Here’s a basic Conky configuration that runs the script:

For Conky 1.10 and later (lua syntax):

Create a file ~/.config/conky/conky.conf with the following content:


conky.config = {
    alignment = 'bottom_right',
    gap_x = 20,
    gap_y = 20,
    use_xft = true,
    font = 'Ubuntu:size=11',
    update_interval = 1.0,
    double_buffer = true,
    own_window = true,
    own_window_type = 'desktop',
    own_window_transparent = true,
    own_window_argb_visual = true,
    own_window_argb_value = 0,
    default_color = 'white',
};

conky.text = [[
${execpi 1 ~/syncthing_status.sh}
]];

Note: Replace /path/to/your/syncthing_monitor.sh with the actual path to your script.


5_ Test Conky:

Run Conky to test the setup:

bash

conky

You should see the synchronization status displayed on your desktop. The percentages will be color-coded:

Blue (#3498db): Synchronization is in progress (less than 100%).

Green (#29b74e): Synchronization is complete (100%).

Purple (#9a6dc0): Device is disconnected.


6_ Configure Conky to Start at Boot:

To have Conky start automatically when you log in:

  • Open Startup Applications or Autostart settings.
  • Add a new startup item with the command conky.

7_ Enjoy Your New Syncthing Monitor!

Your Conky widget should now display the synchronization status every 10 seconds. Feel free to customize the Conky configuration to match your desktop theme and preferences.


Conclusion:

I hope this script helps you monitor your Syncthing synchronization status more conveniently. As this is my first post, I’d appreciate any feedback or suggestions for improvements.

Feel free to ask any questions or share how you’ve customized the script for your setup!

Schermata del 2024-10-14 14-50-16

3 Likes