HEX
Server: nginx/1.28.3
System: Linux lightweb-s1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64
User: dawonefr-98 (1071)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: //usr/local/bin/ploi-monitor
#! /bin/bash

# *---------------------------------------------------------------------------*
# * Ploi.io Server Monitor Script
# * Website: https://ploi.io
# * E-mail: info@ploi.io
# *
# * Stop the Hassle. Start deploi'ing.
# * Use Ploi.io for easy site deployments.
# * We take all the difficult work out of your hands, so you can focus on doing what you love: developing your application
# *
# * The MIT License (MIT)
# * Copyright (c) 2018-2026 Ploi
# *
# * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ----------------------------------------------------------------------------*

FORCE_MODE=0
if [[ "$1" == "--force" ]]; then
    FORCE_MODE=1
fi

MONITOR_DIR="/home/ploi/.ploi"
mkdir -p "$MONITOR_DIR"

log_error() {
    local log_file="$MONITOR_DIR/monitor.log"
    local max_lines=100

    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$log_file"

    if [ -f "$log_file" ]; then
        local line_count=$(wc -l < "$log_file")
        if [ "$line_count" -gt "$max_lines" ]; then
            tail -n "$max_lines" "$log_file" > "$log_file.tmp" && mv "$log_file.tmp" "$log_file"
        fi
    fi
}

get_cpu_usage() {
    if [ ! -f "$MONITOR_DIR/.cpu" ]; then
        grep 'cpu ' /proc/stat > "$MONITOR_DIR/.cpu" || {
            log_error "Failed to read CPU stats"
            return 1
        }
    fi

    previous=$(cat "$MONITOR_DIR/.cpu")
    current=$(grep 'cpu ' /proc/stat)

    awkscript='NR == 1 {
        owork=($2+$4);
        oidle=$5;
    }
    NR > 1 {
        work=($2+$4)-owork;
        idle=$5-oidle;
        printf "%.1f", 100 * work / (work+idle)
    }'

    echo -e "$previous\n$current" | awk "$awkscript"
}

get_memory_usage() {
    free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }'
}

get_disk_usage() {
    df -h | awk '$NF=="/"{printf "%s", $5}'
}

get_all_disks_usage() {
    local json_output="["
    local first=1

    while IFS= read -r line; do
        if [[ ! "$line" =~ ^/dev/ ]]; then
            continue
        fi

        local device=$(echo "$line" | awk '{print $1}')
        local size=$(echo "$line" | awk '{print $2}')
        local used=$(echo "$line" | awk '{print $3}')
        local avail=$(echo "$line" | awk '{print $4}')
        local usage=$(echo "$line" | awk '{print $5}' | sed 's/%//')
        local mount=$(echo "$line" | awk '{print $6}')

        if [[ "$mount" =~ ^(/boot/efi|/dev|/sys|/proc|/run|/snap)$ ]] || [[ "$mount" == "/" ]] || [[ "$mount" =~ ^/snap/ ]]; then
            continue
        fi

        if [ $first -eq 0 ]; then
            json_output+=","
        fi
        first=0

        json_output+="{\"mount\":\"$mount\",\"device\":\"$device\",\"usage\":$usage,\"used\":\"$used\",\"total\":\"$size\"}"
    done < <(df -h 2>/dev/null)

    json_output+="]"

    echo "$json_output"
}

get_load_average() {
    cut -d ' ' -f1 /proc/loadavg
}

get_network_stats() {
    rx_bytes=$(cat /sys/class/net/[ev]*/statistics/rx_bytes 2>/dev/null | awk '{sum+=$1} END{print sum}')
    tx_bytes=$(cat /sys/class/net/[ev]*/statistics/tx_bytes 2>/dev/null | awk '{sum+=$1} END{print sum}')
    echo "${rx_bytes}:${tx_bytes}"
}

CPU=$(get_cpu_usage || echo "0")
MEMORY=$(get_memory_usage || echo "0")
DISK=$(get_disk_usage || echo "0")
DISKS=$(get_all_disks_usage || echo "[]")
LOADAVG=$(get_load_average || echo "0")
NETWORK=$(get_network_stats || echo "0:0")

if [ $FORCE_MODE -eq 0 ]; then
    sleep $((RANDOM % 20 + 1))
fi

send_metrics() {
    local max_attempts=3
    local retry_delay=10
    local attempt=1
    local last_status="unknown"
    local last_send_file="$MONITOR_DIR/.last_send"
    local current_time=$(date +%s)

    # Deduplication: skip if we successfully sent data less than 60 seconds ago
    if [ -f "$last_send_file" ]; then
        local last_send=$(cat "$last_send_file")
        if [ $((current_time - last_send)) -lt 60 ]; then
            return 0
        fi
    fi

    while [ $attempt -le $max_attempts ]; do
        local http_code
        http_code=$(curl --max-time 15 \
             --connect-timeout 60 \
             --silent \
             --output /dev/null \
             --write-out "%{http_code}" \
             "https://server-monitoring-api.ploi.io/servers/78803/monitor?h=0GJ7SLmB065FMgX4" \
             -H "Accept: application/json" \
             -H "Content-Type:application/json" \
             -H "User-Agent: Ploi/Monitor" \
             --data @<(cat <<EOF
{
    "memory": "$MEMORY",
    "cpu": "$CPU",
    "disk": "$DISK",
    "disks": $DISKS,
    "loadavg": "$LOADAVG",
    "network": "$NETWORK"
}
EOF
) 2>/dev/null)

        local curl_exit=$?
        last_status="$http_code"

        if [ $curl_exit -eq 0 ] && [ "$http_code" -ge 200 ] && [ "$http_code" -lt 300 ]; then
            echo "$current_time" > "$last_send_file"
            return 0
        fi

        if [ $curl_exit -ne 0 ]; then
            last_status="curl_error_$curl_exit"
        fi

        if [ $attempt -lt $max_attempts ]; then
            sleep $retry_delay
        fi

        attempt=$((attempt + 1))
    done

    echo "$last_status"
    return 1
}

result=$(send_metrics)
if [ $? -ne 0 ]; then
    log_error "Failed to send metrics to server after 3 attempts (status: $result)"
fi

grep 'cpu ' /proc/stat > "$MONITOR_DIR/.cpu"