Merge pull request 'up' (#1) from dev into main
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
13
README.md
13
README.md
@@ -1,3 +1,14 @@
|
|||||||
# ITSchool-Project--1
|
# ITSchool-Project--1
|
||||||
|
|
||||||
Servers Monitoring
|
Servers Monitoring
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
```c++
|
||||||
|
PHP 8.4
|
||||||
|
- PHP-SQLITE
|
||||||
|
```
|
||||||
|
|
||||||
|
## Install
|
||||||
|
```bash
|
||||||
|
sqlite3 db/monitor.sqlite < sql/schema.sql
|
||||||
|
```
|
||||||
154
install.sh
Normal file
154
install.sh
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
INSTALL_DIR="/opt/server-monitor"
|
||||||
|
AGENT="$INSTALL_DIR/monitor-agent.sh"
|
||||||
|
CONFIG="$INSTALL_DIR/config.env"
|
||||||
|
CRON_CMD="$AGENT >/dev/null 2>&1"
|
||||||
|
API_URL="https://itschool.pbcv.dev/api/report.php"
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# CHECK ROOT
|
||||||
|
# ============================
|
||||||
|
if [[ $EUID -ne 0 ]]; then
|
||||||
|
echo " Run as root (sudo)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo " Installing Server Monitor Agent..."
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# CREATE DIR
|
||||||
|
# ============================
|
||||||
|
mkdir -p "$INSTALL_DIR"
|
||||||
|
chmod 755 "$INSTALL_DIR"
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# CONFIG FILE
|
||||||
|
# ============================
|
||||||
|
cat > "$CONFIG" <<EOF
|
||||||
|
API_URL="$API_URL"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
chmod 600 "$CONFIG"
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# AGENT SCRIPT
|
||||||
|
# ============================
|
||||||
|
cat > "$AGENT" <<'EOF'
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
source /opt/server-monitor/config.env
|
||||||
|
|
||||||
|
HOSTNAME=$(hostname)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# CPU
|
||||||
|
# ============================
|
||||||
|
CPU_LOAD=$(uptime | awk -F'load average:' '{print $2}' | cut -d',' -f1 | xargs)
|
||||||
|
CPU_CORES=$(nproc)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# RAM / SWAP
|
||||||
|
# ============================
|
||||||
|
RAM_TOTAL=$(free -m | awk '/Mem:/ {print $2}')
|
||||||
|
RAM_USED=$(free -m | awk '/Mem:/ {print $3}')
|
||||||
|
|
||||||
|
SWAP_TOTAL=$(free -m | awk '/Swap:/ {print $2}')
|
||||||
|
SWAP_USED=$(free -m | awk '/Swap:/ {print $3}')
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# DISK (KB)
|
||||||
|
# ============================
|
||||||
|
DISK_TOTAL=$(df -k / | awk 'NR==2 {print $2}')
|
||||||
|
DISK_USED=$(df -k / | awk 'NR==2 {print $3}')
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# NETWORK (bytes)
|
||||||
|
# ============================
|
||||||
|
IFACE=$(ip route get 1 | awk '{print $5; exit}')
|
||||||
|
RX_BYTES=$(cat /sys/class/net/$IFACE/statistics/rx_bytes)
|
||||||
|
TX_BYTES=$(cat /sys/class/net/$IFACE/statistics/tx_bytes)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# PROCESSES
|
||||||
|
# ============================
|
||||||
|
PROC_TOTAL=$(ps ax --no-headers | wc -l)
|
||||||
|
PROC_ZOMBIE=$(ps axo stat | grep -c Z || true)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# SYSTEMD
|
||||||
|
# ============================
|
||||||
|
FAILED_SERVICES=$(systemctl --failed --no-legend 2>/dev/null | wc -l)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# NETWORK PORTS
|
||||||
|
# ============================
|
||||||
|
OPEN_PORTS=$(ss -lntu | tail -n +2 | wc -l)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# OS / KERNEL
|
||||||
|
# ============================
|
||||||
|
OS_NAME=$(lsb_release -ds 2>/dev/null || grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"')
|
||||||
|
KERNEL=$(uname -r)
|
||||||
|
ARCH=$(uname -m)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# UPTIME
|
||||||
|
# ============================
|
||||||
|
UPTIME=$(uptime -p)
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# SEND DATA
|
||||||
|
# ============================
|
||||||
|
curl -s -X POST "$API_URL" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d "{
|
||||||
|
\"hostname\": \"$HOSTNAME\",
|
||||||
|
|
||||||
|
\"cpu\": \"$CPU_LOAD\",
|
||||||
|
\"cpu_cores\": $CPU_CORES,
|
||||||
|
|
||||||
|
\"ram_used\": $RAM_USED,
|
||||||
|
\"ram_total\": $RAM_TOTAL,
|
||||||
|
|
||||||
|
\"swap_used\": $SWAP_USED,
|
||||||
|
\"swap_total\": $SWAP_TOTAL,
|
||||||
|
|
||||||
|
\"disk_used\": $DISK_USED,
|
||||||
|
\"disk_total\": $DISK_TOTAL,
|
||||||
|
|
||||||
|
\"rx_bytes\": $RX_BYTES,
|
||||||
|
\"tx_bytes\": $TX_BYTES,
|
||||||
|
|
||||||
|
\"processes\": $PROC_TOTAL,
|
||||||
|
\"zombies\": $PROC_ZOMBIE,
|
||||||
|
|
||||||
|
\"failed_services\": $FAILED_SERVICES,
|
||||||
|
\"open_ports\": $OPEN_PORTS,
|
||||||
|
|
||||||
|
\"os\": \"$OS_NAME\",
|
||||||
|
\"kernel\": \"$KERNEL\",
|
||||||
|
\"arch\": \"$ARCH\",
|
||||||
|
|
||||||
|
\"uptime\": \"$UPTIME\"
|
||||||
|
}"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
chmod +x "$AGENT"
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# CRON (free duplicate)
|
||||||
|
# ============================
|
||||||
|
( crontab -l 2>/dev/null | grep -v "$AGENT" || true
|
||||||
|
echo "* * * * * $CRON_CMD"
|
||||||
|
) | crontab -
|
||||||
|
|
||||||
|
# ============================
|
||||||
|
# FINAL
|
||||||
|
# ============================
|
||||||
|
echo " Server Monitor installed successfully"
|
||||||
|
echo " Reporting to: $API_URL"
|
||||||
|
echo " Interval: every 1 minute"
|
||||||
|
echo " Test manual: $AGENT"
|
||||||
Reference in New Issue
Block a user