Last active
September 18, 2023 03:52
-
-
Save borouhin/d658bdbea1bd85e33e8e48ed42ca3819 to your computer and use it in GitHub Desktop.
Seafile Collector for Prometheus Node Exporter
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
#!/bin/bash | |
# Get some useful Seafile metrics to be comsumed by Node Exporter's text collector (so that we don't need to write | |
# a separate exporter for Seafile) | |
# Fill in authorization token and server address (see below) and let this script run via cronjob at desired frequency | |
# | |
# Requires curl and jq to be installed (and prometheus-node-exporter, of course) | |
# Assumes there are no more than 1000 users, libraries and devices (otherwise increase per page limit or implement paging) | |
# | |
# If an error is encountered during API call, previous values are preserved and seafile_collector_result metric | |
# holds error codes as explained in the description. You should monitor this metric to get notified of failures. | |
# | |
# More detailes on the Seafile API used: https://download.seafile.com/published/web-api/home.md | |
# Get it with "curl -d "username=username@example.com&password=123456" https://seafile.example.com/api2/auth-token/" | |
AUTHTOKEN="" | |
# Put your server name here | |
API_ROOT="https://seafile.example.com/api/v2.1/admin" | |
PROMFILE="/var/lib/prometheus/node-exporter/seafile.prom" | |
TMPFILE_PREFIX="/tmp/seafile_" | |
let API_ERROR=0 | |
curl -f -H "Authorization: Token ${AUTHTOKEN}" ${API_ROOT}/libraries/?per_page=1000 >${TMPFILE_PREFIX}libraries.json 2>/dev/null | |
let API_ERROR=$(( $API_ERROR | $? )) | |
curl -f -H "Authorization: Token ${AUTHTOKEN}" ${API_ROOT}/users/?per_page=1000 >${TMPFILE_PREFIX}users.json 2>/dev/null | |
let API_ERROR=$(( $API_ERROR | 0x100 * $? )) | |
curl -f -H "Authorization: Token ${AUTHTOKEN}" ${API_ROOT}/ldap-users/?per_page=1000 >${TMPFILE_PREFIX}ldap-users.json 2>/dev/null | |
let API_ERROR=$(( $API_ERROR | 0x10000 * $? )) | |
curl -f -H "Authorization: Token ${AUTHTOKEN}" ${API_ROOT}/devices/?per_page=1000 >${TMPFILE_PREFIX}devices.json 2>/dev/null | |
let API_ERROR=$(( $API_ERROR | 0x1000000 * $? )) | |
if [[ $API_ERROR == "0" ]] ; then | |
echo "# HELP seafile_collector_result Errors when Running Seafile Collector, error while getting libraries API in lower 2 bytes, users API - next 2 bytes, then LDAP users and devices API" >>${TMPFILE_PREFIX}output | |
echo "# TYPE seafile_collector_result gauge" >>${TMPFILE_PREFIX}output | |
echo "seafile_collector_result 0" >>${TMPFILE_PREFIX}output | |
echo "# HELP seafile_library_size Seafile Library Sizes, bytes" >>${TMPFILE_PREFIX}output | |
echo "# TYPE seafile_library_size gauge" >>${TMPFILE_PREFIX}output | |
cat ${TMPFILE_PREFIX}libraries.json | jq -r '.repos[] | "seafile_library_size{name=\"\(.name)\",owner=\"\(.owner)\",encrypted=\"\(.encrypted)\",status=\"\(.status)\"} \(.size)"' >>${TMPFILE_PREFIX}output | |
echo "# HELP seafile_library_file_count Seafile Library File Counts" >>${TMPFILE_PREFIX}output | |
echo "# TYPE seafile_library_file_count gauge" >>${TMPFILE_PREFIX}output | |
cat ${TMPFILE_PREFIX}libraries.json | jq -r '.repos[] | "seafile_library_file_count{name=\"\(.name)\",owner=\"\(.owner)\",encrypted=\"\(.encrypted)\",status=\"\(.status)\"} \(.file_count)"' >>${TMPFILE_PREFIX}output | |
echo "# HELP seafile_user_quota Seafile User Quota Usage" >>${TMPFILE_PREFIX}output | |
echo "# TYPE seafile_user_quota gauge" >>${TMPFILE_PREFIX}output | |
cat ${TMPFILE_PREFIX}users.json | jq -r '.data[] | "seafile_user_quota{name=\"\(.name)\",email=\"\(.email)\",is_active=\"\(.is_active)\",create_time=\"\(.create_time)\",last_access_time=\"\(.last_access_time)\"} \(.quota_usage)"' >>${TMPFILE_PREFIX}output | |
cat ${TMPFILE_PREFIX}ldap-users.json | jq -r '.ldap_user_list[] | select(.last_login | .!="") | "seafile_user_quota{name=\"\(.email)\",email=\"\(.email)\",is_active=\"true\",create_time=\"\(.create_time)\",last_access_time=\"\(.last_access_time)\"} \(.quota_usage)"' >>${TMPFILE_PREFIX}output | |
echo "# HELP seafile_device Seafile Device Statistics" >>${TMPFILE_PREFIX}output | |
echo "# TYPE seafile_device gauge" >>${TMPFILE_PREFIX}output | |
cat ${TMPFILE_PREFIX}devices.json | jq -r '.devices[] | "seafile_device{device_name=\"\(.device_name)\",client_version=\"\(.client_version)\",last_accessed=\"\(.last_accessed)\",last_login_ip=\"\(.last_login_ip)\",user=\"\(.user)\",platform=\"\(.platform)\",is_desktop_client=\"\(.is_desktop_client)\"} 0"' >>${TMPFILE_PREFIX}output | |
mv ${TMPFILE_PREFIX}output $PROMFILE | |
else | |
sed -i "s/^seafile_collector_result\ [0-9]*$/seafile_collector_result ${API_ERROR}/" $PROMFILE | |
fi | |
rm ${TMPFILE_PREFIX}* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment