Created
May 10, 2026 19:27
-
-
Save wyne/c22f8922abe4948bfb42dcb7147fc940 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/bin/bash | |
| # ============================================================= | |
| # synology_folder_sizes.sh | |
| # Collects shared folder sizes and writes to InfluxDB v2 | |
| # Schedule in DSM Task Scheduler (e.g. every 24 hours) | |
| # ============================================================= | |
| INFLUX_URL="http://INFLUXDB_HOST_HERE:8086" | |
| INFLUX_ORG="INFLUXDB_ORG_HERE" | |
| INFLUX_BUCKET="INFLUXDB_BUCKET_HERE" | |
| INFLUX_TOKEN="PUT_INFLUXDB_TOKEN_HERE" | |
| HOSTNAME="PUT_HOSTNAME_HERE" | |
| log() { echo "[$(date '+%H:%M:%S')] $*"; } | |
| log "Starting synology_folder_sizes.sh" | |
| # Get current unix timestamp | |
| TIMESTAMP=$(date +%s) | |
| log "Timestamp: $TIMESTAMP" | |
| # Build line protocol payload | |
| PAYLOAD="" | |
| COUNT=0 | |
| SKIPPED=0 | |
| # Get list of shares (skip the header line) | |
| SHARES=$(synoshare --enum ALL 2>/dev/null | tail -n +2) | |
| TOTAL=$(echo "$SHARES" | grep -c .) | |
| log "Found $TOTAL shares" | |
| while IFS= read -r share; do | |
| # Skip empty lines | |
| [ -z "$share" ] && continue | |
| # Get the real path for this share using sed instead of awk | |
| SHARE_PATH=$(synoshare --get "$share" 2>/dev/null | grep "Path" | sed 's/.*\[\(.*\)\]/\1/') | |
| if [ -z "$SHARE_PATH" ]; then | |
| log " SKIP $share — could not get path" | |
| SKIPPED=$(( SKIPPED + 1 )) | |
| continue | |
| fi | |
| if [ ! -d "$SHARE_PATH" ]; then | |
| log " SKIP $share — path does not exist: $SHARE_PATH" | |
| SKIPPED=$(( SKIPPED + 1 )) | |
| continue | |
| fi | |
| # Get volume from path (e.g. /volume1/Media -> volume1) | |
| VOLUME=$(echo "$SHARE_PATH" | cut -d'/' -f2) | |
| # Get size in KB, convert to bytes | |
| SIZE_KB=$(du -xsk "$SHARE_PATH" 2>/dev/null | awk '{print $1}') | |
| if [ -z "$SIZE_KB" ]; then | |
| log " SKIP $share — du failed on $SHARE_PATH" | |
| SKIPPED=$(( SKIPPED + 1 )) | |
| continue | |
| fi | |
| # Escape spaces in share name for line protocol | |
| SAFE_SHARE=$(echo "$share" | tr ' ' '_') | |
| log " OK $share -> $SHARE_PATH ($SIZE_KB kb)" | |
| # Append to payload | |
| PAYLOAD="${PAYLOAD}synology_folder,host=${HOSTNAME},folder=${SAFE_SHARE},volume=${VOLUME} size_kb=${SIZE_KB} ${TIMESTAMP} | |
| " | |
| COUNT=$(( COUNT + 1 )) | |
| done <<< "$SHARES" | |
| log "Collected $COUNT folders, skipped $SKIPPED" | |
| # Write all data points to InfluxDB in one request | |
| if [ -n "$PAYLOAD" ]; then | |
| log "Writing to InfluxDB..." | |
| HTTP_STATUS=$(curl -s -o /tmp/influx_response.txt -w "%{http_code}" \ | |
| -X POST "${INFLUX_URL}/api/v2/write?org=${INFLUX_ORG}&bucket=${INFLUX_BUCKET}&precision=s" \ | |
| -H "Authorization: Token ${INFLUX_TOKEN}" \ | |
| --data-raw "$PAYLOAD") | |
| RESPONSE=$(cat /tmp/influx_response.txt) | |
| if [ "$HTTP_STATUS" = "204" ]; then | |
| log "Success! HTTP $HTTP_STATUS — $COUNT folders written to InfluxDB" | |
| else | |
| log "ERROR: HTTP $HTTP_STATUS — $RESPONSE" | |
| fi | |
| else | |
| log "No data collected, nothing written" | |
| fi | |
| log "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment