#!/bin/bash # # [TESTING] # # Purpose: # This script collects status of the host system and sends it to a remote endpoint. # # Specifications: # - Take first argument for status to collect and send. # - disk: disk usage, using df # - memory: memory usage, using free # - The base URL to the API endpoint is set in a file named ".api-endpoint" located in the script's directory. # - If the file does not exist, the script returns an error and terminates, do not exit to prevent breaking SSH session. # - The path to the API is /host-report # - The script sends a POST request with the collected data in form-data format to the specified endpoint. # - The form-data contains these fields: # - machine: the current hostname # - type: the type of status being reported (disk or memory) # - data: the collected status data if [ ! -f ".api-endpoint" ]; then echo "Error: .api-endpoint file not found." exit 1 fi API_ENDPOINT=$(cat .api-endpoint) if [ "$#" -ne 1 ] || { [ "$1" != "disk" ] && [ "$1" != "memory" ]; }; then echo "Usage: $0 disk|memory" exit 1 fi STATUS_TYPE=$1 MACHINE=$(hostname) if [ "$STATUS_TYPE" == "disk" ]; then DATA=$(df -h) elif [ "$STATUS_TYPE" == "memory" ]; then DATA=$(free -h) fi curl -X POST "$API_ENDPOINT/host-report" -d "machine=$MACHINE&type=$STATUS_TYPE&data=$DATA"