mirror of
https://github.com/netweak/agent.git
synced 2025-01-13 22:40:28 -05:00
Add endpoint and branch parameters
This commit is contained in:
parent
eb48df0f85
commit
64e3a406ae
3 changed files with 246 additions and 161 deletions
132
agent.sh
132
agent.sh
|
@ -3,42 +3,51 @@
|
|||
# Set env
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
|
||||
# Get the directory name
|
||||
NETWEAK=$(basename $(dirname $0))
|
||||
|
||||
# Agent version
|
||||
version="1.2.0"
|
||||
if [ -f "/etc/$NETWEAK/version" ]; then
|
||||
version=$(cat "/etc/$NETWEAK/version")
|
||||
else
|
||||
echo "Error: File /etc/$NETWEAK/version is missing."
|
||||
fi
|
||||
|
||||
# Get endpoint
|
||||
if [ -f "/etc/$NETWEAK/endpoint.conf" ]; then
|
||||
ENDPOINT=$(cat "/etc/$NETWEAK/endpoint.conf")
|
||||
else
|
||||
ENDPOINT="https://api.netweak.com"
|
||||
fi
|
||||
|
||||
# API Token
|
||||
if [ -f /etc/netweak/token.conf ]
|
||||
then
|
||||
auth=($(cat /etc/netweak/token.conf))
|
||||
if [ -f "/etc/$NETWEAK/token.conf" ]; then
|
||||
auth=($(cat "/etc/$NETWEAK/token.conf"))
|
||||
else
|
||||
echo "Error: File /etc/netweak/token.conf is missing."
|
||||
echo "Error: File /etc/$NETWEAK/token.conf is missing."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Prepare values
|
||||
function prep ()
|
||||
{
|
||||
function prep() {
|
||||
echo "$1" | sed -e 's/^ *//g' -e 's/ *$//g' | sed -n '1 p'
|
||||
}
|
||||
|
||||
# Base64 values
|
||||
function base ()
|
||||
{
|
||||
function base() {
|
||||
echo "$1" | tr -d '\n' | base64 | tr -d '=' | tr -d '\n' | sed 's/\//%2F/g' | sed 's/\+/%2B/g'
|
||||
}
|
||||
|
||||
# Integer values
|
||||
function int ()
|
||||
{
|
||||
echo ${1/\.*}
|
||||
function int() {
|
||||
echo ${1/\.*/}
|
||||
}
|
||||
|
||||
# Filter numeric
|
||||
function num ()
|
||||
{
|
||||
function num() {
|
||||
case $1 in
|
||||
''|*[!0-9\.]*) echo 0 ;;
|
||||
*) echo $1 ;;
|
||||
'' | *[!0-9\.]*) echo 0 ;;
|
||||
*) echo $1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
@ -65,23 +74,18 @@ file_handles_limit=$(prep $(num "$(cat /proc/sys/fs/file-nr | awk '{ print $3 }'
|
|||
# OS details
|
||||
os_kernel=$(prep "$(uname -r)")
|
||||
|
||||
if ls /etc/*release > /dev/null 2>&1
|
||||
then
|
||||
if ls /etc/*release >/dev/null 2>&1; then
|
||||
os_name=$(prep "$(cat /etc/*release | grep '^PRETTY_NAME=\|^NAME=\|^DISTRIB_ID=' | awk -F\= '{ print $2 }' | tr -d '"' | tac)")
|
||||
fi
|
||||
|
||||
if [ -z "$os_name" ]
|
||||
then
|
||||
if [ -e /etc/redhat-release ]
|
||||
then
|
||||
if [ -z "$os_name" ]; then
|
||||
if [ -e /etc/redhat-release ]; then
|
||||
os_name=$(prep "$(cat /etc/redhat-release)")
|
||||
elif [ -e /etc/debian_version ]
|
||||
then
|
||||
elif [ -e /etc/debian_version ]; then
|
||||
os_name=$(prep "Debian $(cat /etc/debian_version)")
|
||||
fi
|
||||
|
||||
if [ -z "$os_name" ]
|
||||
then
|
||||
if [ -z "$os_name" ]; then
|
||||
os_name=$(prep "$(uname -s)")
|
||||
fi
|
||||
fi
|
||||
|
@ -100,18 +104,16 @@ esac
|
|||
|
||||
# CPU details
|
||||
cpu_name=$(prep "$(cat /proc/cpuinfo | grep 'model name' | awk -F\: '{ print $2 }')")
|
||||
cpu_cores=$(prep "$(($(cat /proc/cpuinfo | grep 'model name' | awk -F\: '{ print $2 }' | sed -e :a -e '$!N;s/\n/\|/;ta' | tr -cd \| | wc -c)+1))")
|
||||
cpu_cores=$(prep "$(($(cat /proc/cpuinfo | grep 'model name' | awk -F\: '{ print $2 }' | sed -e :a -e '$!N;s/\n/\|/;ta' | tr -cd \| | wc -c) + 1))")
|
||||
|
||||
if [ -z "$cpu_name" ]
|
||||
then
|
||||
if [ -z "$cpu_name" ]; then
|
||||
cpu_name=$(prep "$(cat /proc/cpuinfo | grep 'vendor_id' | awk -F\: '{ print $2 } END { if (!NR) print "N/A" }')")
|
||||
cpu_cores=$(prep "$(($(cat /proc/cpuinfo | grep 'vendor_id' | awk -F\: '{ print $2 }' | sed -e :a -e '$!N;s/\n/\|/;ta' | tr -cd \| | wc -c)+1))")
|
||||
cpu_cores=$(prep "$(($(cat /proc/cpuinfo | grep 'vendor_id' | awk -F\: '{ print $2 }' | sed -e :a -e '$!N;s/\n/\|/;ta' | tr -cd \| | wc -c) + 1))")
|
||||
fi
|
||||
|
||||
cpu_freq=$(prep "$(cat /proc/cpuinfo | grep 'cpu MHz' | awk -F\: '{ print $2 }')")
|
||||
|
||||
if [ -z "$cpu_freq" ]
|
||||
then
|
||||
if [ -z "$cpu_freq" ]; then
|
||||
cpu_freq=$(prep $(num "$(lscpu | grep 'CPU MHz' | awk -F\: '{ print $2 }' | sed -e 's/^ *//g' -e 's/ *$//g')"))
|
||||
fi
|
||||
|
||||
|
@ -120,13 +122,13 @@ ram_total=$(prep $(num "$(cat /proc/meminfo | grep ^MemTotal: | awk '{ print $2
|
|||
ram_free=$(prep $(num "$(cat /proc/meminfo | grep ^MemFree: | awk '{ print $2 }')"))
|
||||
ram_cached=$(prep $(num "$(cat /proc/meminfo | grep ^Cached: | awk '{ print $2 }')"))
|
||||
ram_buffers=$(prep $(num "$(cat /proc/meminfo | grep ^Buffers: | awk '{ print $2 }')"))
|
||||
ram_usage=$((($ram_total-($ram_free+$ram_cached+$ram_buffers)) * 1024))
|
||||
ram_usage=$((($ram_total - ($ram_free + $ram_cached + $ram_buffers)) * 1024))
|
||||
ram_total=$(($ram_total * 1024))
|
||||
|
||||
# Swap usage (in bytes)
|
||||
swap_total=$(prep $(num "$(cat /proc/meminfo | grep ^SwapTotal: | awk '{ print $2 }')"))
|
||||
swap_free=$(prep $(num "$(cat /proc/meminfo | grep ^SwapFree: | awk '{ print $2 }')"))
|
||||
swap_usage=$((($swap_total-$swap_free) * 1024))
|
||||
swap_usage=$((($swap_total - $swap_free) * 1024))
|
||||
swap_total=$(($swap_total * 1024))
|
||||
|
||||
# Disk usage (in bytes)
|
||||
|
@ -137,8 +139,7 @@ disk_usage=$(prep $(num "$(($(df --output=used,source -B 1 | grep ' /' | awk '{
|
|||
disk_array=$(prep "$(df -P -B 1 | grep '^/' | awk '{ print $1" "$2" "$3";" }' | sed -e :a -e '$!N;s/\n/ /;ta' | awk '{ print $0 } END { if (!NR) print "N/A" }')")
|
||||
|
||||
# Active connections
|
||||
if [ -n "$(command -v ss)" ]
|
||||
then
|
||||
if [ -n "$(command -v ss)" ]; then
|
||||
connections=$(prep $(num "$(ss -tun | tail -n +2 | wc -l)"))
|
||||
else
|
||||
connections=$(prep $(num "$(netstat -tun | tail -n +3 | wc -l)"))
|
||||
|
@ -147,8 +148,7 @@ fi
|
|||
# Network interface
|
||||
nic=$(prep "$(ip route get 1.1.1.1 | grep dev | awk -F'dev' '{ print $2 }' | awk '{ print $1 }')")
|
||||
|
||||
if [ -z $nic ]
|
||||
then
|
||||
if [ -z $nic ]; then
|
||||
nic=$(prep "$(ip link show | grep 'eth[0-9]' | awk '{ print $2 }' | tr -d ':')")
|
||||
fi
|
||||
|
||||
|
@ -156,8 +156,7 @@ fi
|
|||
ipv4=$(prep "$(ip addr show $nic | grep 'inet ' | awk '{ print $2 }' | awk -F\/ '{ print $1 }' | grep -v '^127' | awk '{ print $0 } END { if (!NR) print "N/A" }')")
|
||||
ipv6=$(prep "$(ip addr show $nic | grep 'inet6 ' | awk '{ print $2 }' | awk -F\/ '{ print $1 }' | grep -v '^::' | grep -v '^0000:' | grep -v '^fe80:' | awk '{ print $0 } END { if (!NR) print "N/A" }')")
|
||||
|
||||
if [ -d /sys/class/net/$nic/statistics ]
|
||||
then
|
||||
if [ -d /sys/class/net/$nic/statistics ]; then
|
||||
rx=$(prep $(num "$(cat /sys/class/net/$nic/statistics/rx_bytes)"))
|
||||
tx=$(prep $(num "$(cat /sys/class/net/$nic/statistics/tx_bytes)"))
|
||||
else
|
||||
|
@ -171,41 +170,36 @@ load=$(prep "$(cat /proc/loadavg | awk '{ print $1" "$2" "$3 }')")
|
|||
# Detailed system load calculation
|
||||
time=$(date +%s)
|
||||
stat=($(cat /proc/stat | head -n1 | sed 's/[^0-9 ]*//g' | sed 's/^ *//'))
|
||||
cpu=$((${stat[0]}+${stat[1]}+${stat[2]}+${stat[3]}))
|
||||
io=$((${stat[3]}+${stat[4]}))
|
||||
cpu=$((${stat[0]} + ${stat[1]} + ${stat[2]} + ${stat[3]}))
|
||||
io=$((${stat[3]} + ${stat[4]}))
|
||||
idle=${stat[3]}
|
||||
|
||||
if [ -e /etc/netweak/cache ]
|
||||
then
|
||||
data=($(cat /etc/netweak/cache))
|
||||
interval=$(($time-${data[0]}))
|
||||
cpu_gap=$(($cpu-${data[1]}))
|
||||
io_gap=$(($io-${data[2]}))
|
||||
idle_gap=$(($idle-${data[3]}))
|
||||
if [ -e "/etc/$NETWEAK/cache" ]; then
|
||||
data=($(cat "/etc/$NETWEAK/cache"))
|
||||
interval=$(($time - ${data[0]}))
|
||||
cpu_gap=$(($cpu - ${data[1]}))
|
||||
io_gap=$(($io - ${data[2]}))
|
||||
idle_gap=$(($idle - ${data[3]}))
|
||||
|
||||
if [[ $cpu_gap > "0" ]]
|
||||
then
|
||||
load_cpu=$(((1000*($cpu_gap-$idle_gap)/$cpu_gap+5)/10))
|
||||
if [[ $cpu_gap > "0" ]]; then
|
||||
load_cpu=$(((1000 * ($cpu_gap - $idle_gap) / $cpu_gap + 5) / 10))
|
||||
fi
|
||||
|
||||
if [[ $io_gap > "0" ]]
|
||||
then
|
||||
load_io=$(((1000*($io_gap-$idle_gap)/$io_gap+5)/10))
|
||||
if [[ $io_gap > "0" ]]; then
|
||||
load_io=$(((1000 * ($io_gap - $idle_gap) / $io_gap + 5) / 10))
|
||||
fi
|
||||
|
||||
if [[ $rx > ${data[4]} ]]
|
||||
then
|
||||
rx_gap=$(($rx-${data[4]}))
|
||||
if [[ $rx > ${data[4]} ]]; then
|
||||
rx_gap=$(($rx - ${data[4]}))
|
||||
fi
|
||||
|
||||
if [[ $tx > ${data[5]} ]]
|
||||
then
|
||||
tx_gap=$(($tx-${data[5]}))
|
||||
if [[ $tx > ${data[5]} ]]; then
|
||||
tx_gap=$(($tx - ${data[5]}))
|
||||
fi
|
||||
fi
|
||||
|
||||
# System load cache
|
||||
echo "$time $cpu $io $idle $rx $tx" > /etc/netweak/cache
|
||||
echo "$time $cpu $io $idle $rx $tx" >"/etc/$NETWEAK/cache"
|
||||
|
||||
# Prepare load variables
|
||||
rx_gap=$(prep $(num "$rx_gap"))
|
||||
|
@ -222,19 +216,17 @@ ping_as=$(prep $(num "$(ping -c 2 -w 2 ping-as.netweak.com | grep rtt | cut -d'/
|
|||
data_post="token=${auth[0]}&data=$(base "$version") $(base "$uptime") $(base "$sessions") $(base "$processes") $(base "$processes_array") $(base "$file_handles") $(base "$file_handles_limit") $(base "$os_kernel") $(base "$os_name") $(base "$os_arch") $(base "$cpu_name") $(base "$cpu_cores") $(base "$cpu_freq") $(base "$ram_total") $(base "$ram_usage") $(base "$swap_total") $(base "$swap_usage") $(base "$disk_array") $(base "$disk_total") $(base "$disk_usage") $(base "$connections") $(base "$nic") $(base "$ipv4") $(base "$ipv6") $(base "$rx") $(base "$tx") $(base "$rx_gap") $(base "$tx_gap") $(base "$load") $(base "$load_cpu") $(base "$load_io") $(base "$ping_eu") $(base "$ping_us") $(base "$ping_as")"
|
||||
|
||||
# API request with automatic termination
|
||||
if [ -n "$(command -v timeout)" ]
|
||||
then
|
||||
timeout -s SIGKILL 30 wget -q -o /dev/null -O /etc/netweak/log/agent.log -T 25 --post-data "$data_post" --no-check-certificate "https://api.netweak.com/agent/report"
|
||||
if [ -n "$(command -v timeout)" ]; then
|
||||
timeout -s SIGKILL 30 wget -q -o /dev/null -O "/etc/$NETWEAK/log/agent.log" -T 25 --post-data "$data_post" --no-check-certificate "$ENDPOINT/agent/report"
|
||||
else
|
||||
wget -q -o /dev/null -O /etc/netweak/log/agent.log -T 25 --post-data "$data_post" --no-check-certificate "https://api.netweak.com/agent/report"
|
||||
wget -q -o /dev/null -O "/etc/$NETWEAK/log/agent.log" -T 25 --post-data "$data_post" --no-check-certificate "$ENDPOINT/agent/report"
|
||||
wget_pid=$!
|
||||
wget_counter=0
|
||||
wget_timeout=30
|
||||
|
||||
while kill -0 "$wget_pid" && (( wget_counter < wget_timeout ))
|
||||
do
|
||||
sleep 1
|
||||
(( wget_counter++ ))
|
||||
while kill -0 "$wget_pid" && ((wget_counter < wget_timeout)); do
|
||||
sleep 1
|
||||
((wget_counter++))
|
||||
done
|
||||
|
||||
kill -0 "$wget_pid" && kill -s SIGKILL "$wget_pid"
|
||||
|
|
31
heartbeat.sh
31
heartbeat.sh
|
@ -3,32 +3,39 @@
|
|||
# Set env
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
|
||||
# Get the directory name
|
||||
NETWEAK=$(basename $(dirname $0))
|
||||
|
||||
# API Token
|
||||
if [ -f /etc/netweak/token.conf ]
|
||||
then
|
||||
auth=($(cat /etc/netweak/token.conf))
|
||||
if [ -f "/etc/$NETWEAK/token.conf" ]; then
|
||||
auth=($(cat "/etc/$NETWEAK/token.conf"))
|
||||
else
|
||||
echo "Error: File /etc/netweak/token.conf is missing."
|
||||
echo "Error: File /etc/$NETWEAK/token.conf is missing."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get endpoint
|
||||
if [ -f "/etc/$NETWEAK/endpoint.conf" ]; then
|
||||
ENDPOINT=$(cat "/etc/$NETWEAK/endpoint.conf")
|
||||
else
|
||||
ENDPOINT="https://api.netweak.com"
|
||||
fi
|
||||
|
||||
# Build data for post
|
||||
data_post="token=${auth[0]}"
|
||||
|
||||
# API request with automatic termination
|
||||
if [ -n "$(command -v timeout)" ]
|
||||
then
|
||||
timeout -s SIGKILL 30 wget -q -o /dev/null -O /etc/netweak/log/agent.log -T 25 --post-data "$data_post" --no-check-certificate "https://api.netweak.com/agent/heartbeat"
|
||||
if [ -n "$(command -v timeout)" ]; then
|
||||
timeout -s SIGKILL 30 wget -q -o /dev/null -O "/etc/$NETWEAK/log/agent.log" -T 25 --post-data "$data_post" --no-check-certificate "$ENDPOINT/agent/heartbeat"
|
||||
else
|
||||
wget -q -o /dev/null -O /etc/netweak/log/agent.log -T 25 --post-data "$data_post" --no-check-certificate "https://api.netweak.com/agent/heartbeat"
|
||||
wget -q -o /dev/null -O "/etc/$NETWEAK/log/agent.log" -T 25 --post-data "$data_post" --no-check-certificate "$ENDPOINT/agent/heartbeat"
|
||||
wget_pid=$!
|
||||
wget_counter=0
|
||||
wget_timeout=30
|
||||
|
||||
while kill -0 "$wget_pid" && (( wget_counter < wget_timeout ))
|
||||
do
|
||||
sleep 1
|
||||
(( wget_counter++ ))
|
||||
while kill -0 "$wget_pid" && ((wget_counter < wget_timeout)); do
|
||||
sleep 1
|
||||
((wget_counter++))
|
||||
done
|
||||
|
||||
kill -0 "$wget_pid" && kill -s SIGKILL "$wget_pid"
|
||||
|
|
244
install.sh
244
install.sh
|
@ -3,32 +3,101 @@
|
|||
# Set env
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
|
||||
# Default values
|
||||
VERSION="1.2.0"
|
||||
ENDPOINT="https://api.netweak.com"
|
||||
BRANCH="main"
|
||||
NETWEAK="netweak"
|
||||
|
||||
# Function to display usage information
|
||||
usage() {
|
||||
echo -e "| Usage: bash $0 [options] <token>\n|"
|
||||
echo -e "| Options:"
|
||||
echo -e "| -e, --endpoint <url> API endpoint (default: https://api.netweak.com)"
|
||||
echo -e "| -b, --branch <name> Branch to install from (default: main)\n|"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Parse command line arguments
|
||||
PARSED_ARGS=$(getopt -o e:b: --long endpoint:,branch: -n "$0" -- "$@")
|
||||
if [ $? -ne 0 ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
eval set -- "$PARSED_ARGS"
|
||||
|
||||
# Parse arguments
|
||||
while true; do
|
||||
case "$1" in
|
||||
-e | --endpoint)
|
||||
ENDPOINT="$2"
|
||||
shift 2
|
||||
;;
|
||||
-b | --branch)
|
||||
BRANCH="$2"
|
||||
shift 2
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
echo "Internal error!"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Get token
|
||||
TOKEN="$1"
|
||||
|
||||
# If branch is not main, append it to the folder name
|
||||
if [ "$BRANCH" != "main" ]; then
|
||||
SAFE_BRANCH=$(echo "$BRANCH" | sed 's|/|-|g' | sed 's|[^a-zA-Z0-9_-]||g')
|
||||
NETWEAK="netweak-${SAFE_BRANCH}"
|
||||
fi
|
||||
|
||||
# Prepare output
|
||||
echo -e "|\n| Netweak Installer\n| ===================\n|"
|
||||
|
||||
# Check if user is root
|
||||
if [ $(id -u) != "0" ];
|
||||
then
|
||||
if [ $(id -u) != "0" ]; then
|
||||
echo -e "| Error: You need to be root to install the Netweak agent\n|"
|
||||
echo -e "| The agent itself will NOT be running as root but instead under its own non-privileged user\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Required parameter
|
||||
if [ $# -lt 1 ]
|
||||
then
|
||||
echo -e "| Usage: bash $0 'token'\n|"
|
||||
if [ -z "$TOKEN" ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
# Validate endpoint URL format
|
||||
if ! [[ $ENDPOINT =~ ^https?:// ]]; then
|
||||
echo -e "| Error: Invalid endpoint URL format. Must start with http:// or https://\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate branch name
|
||||
if ! [[ $BRANCH =~ ^[a-zA-Z0-9_.-]+$ ]]; then
|
||||
echo -e "| Error: Invalid branch name. Only alphanumeric characters, dots, hyphens, and underscores are allowed.\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for curl
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
echo -e "|\n| Error: curl is required but not installed\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Exchange JIT token for agent token
|
||||
if [[ $1 == jit-* ]]; then
|
||||
if [[ $TOKEN == jit-* ]]; then
|
||||
# POST request to retrieve token
|
||||
response=$(curl -s -X POST -d "token=$1" https://api.netweak.com/agent/get-token)
|
||||
response=$(curl -s -X POST -d "token=$TOKEN" "$ENDPOINT/agent/get-token")
|
||||
|
||||
# Check if there's an error
|
||||
if [[ $? -ne 0 ]]; then
|
||||
echo -e "|\n| Error: Failed to retrieve token from API. Make sure your installation command is correct.\n|"
|
||||
echo -e "|\n| Error: Failed to retrieve token from API. Make sure your installation command is correct.\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@ -36,79 +105,66 @@ if [[ $1 == jit-* ]]; then
|
|||
token=$(echo "$response" | tr -d '\n')
|
||||
else
|
||||
# Token doesn't start with "jit-", using it as is
|
||||
token=$1
|
||||
token=$TOKEN
|
||||
fi
|
||||
|
||||
# Check if crontab is installed
|
||||
if [ ! -n "$(command -v crontab)" ]
|
||||
then
|
||||
if [ ! -n "$(command -v crontab)" ]; then
|
||||
|
||||
# Confirm crontab installation
|
||||
echo "|" && read -p "| Crontab is required and could not be found. Do you want to install it? [Y/n] " input_variable_install
|
||||
|
||||
# Attempt to install crontab
|
||||
if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
|
||||
then
|
||||
if [ -n "$(command -v apt-get)" ]
|
||||
then
|
||||
if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]; then
|
||||
if [ -n "$(command -v apt-get)" ]; then
|
||||
echo -e "|\n| Notice: Installing required package 'cron' via 'apt-get'"
|
||||
apt-get -y update
|
||||
apt-get -y install cron
|
||||
elif [ -n "$(command -v yum)" ]
|
||||
then
|
||||
apt-get -y update
|
||||
apt-get -y install cron
|
||||
elif [ -n "$(command -v yum)" ]; then
|
||||
echo -e "|\n| Notice: Installing required package 'cronie' via 'yum'"
|
||||
yum -y install cronie
|
||||
|
||||
if [ ! -n "$(command -v crontab)" ]
|
||||
then
|
||||
echo -e "|\n| Notice: Installing required package 'vixie-cron' via 'yum'"
|
||||
yum -y install vixie-cron
|
||||
fi
|
||||
elif [ -n "$(command -v pacman)" ]
|
||||
then
|
||||
yum -y install cronie
|
||||
|
||||
if [ ! -n "$(command -v crontab)" ]; then
|
||||
echo -e "|\n| Notice: Installing required package 'vixie-cron' via 'yum'"
|
||||
yum -y install vixie-cron
|
||||
fi
|
||||
elif [ -n "$(command -v pacman)" ]; then
|
||||
echo -e "|\n| Notice: Installing required package 'cronie' via 'pacman'"
|
||||
pacman -S --noconfirm cronie
|
||||
pacman -S --noconfirm cronie
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -n "$(command -v crontab)" ]
|
||||
then
|
||||
# Show error
|
||||
echo -e "|\n| Error: Crontab is required and could not be installed\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -n "$(command -v crontab)" ]; then
|
||||
# Show error
|
||||
echo -e "|\n| Error: Crontab is required and could not be installed\n|"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check if cron is running
|
||||
if [ -z "$(ps -Al | grep cron | grep -v grep)" ]
|
||||
then
|
||||
|
||||
if [ -z "$(ps -Al | grep cron | grep -v grep)" ]; then
|
||||
|
||||
# Confirm cron service
|
||||
echo "|" && read -p "| Cron is available but not running. Do you want to start it? [Y/n] " input_variable_service
|
||||
|
||||
# Attempt to start cron
|
||||
if [ -z $input_variable_service ] || [ $input_variable_service == "Y" ] || [ $input_variable_service == "y" ]
|
||||
then
|
||||
if [ -n "$(command -v apt-get)" ]
|
||||
then
|
||||
if [ -z $input_variable_service ] || [ $input_variable_service == "Y" ] || [ $input_variable_service == "y" ]; then
|
||||
if [ -n "$(command -v apt-get)" ]; then
|
||||
echo -e "|\n| Notice: Starting 'cron' via 'service'"
|
||||
service cron start
|
||||
elif [ -n "$(command -v yum)" ]
|
||||
then
|
||||
elif [ -n "$(command -v yum)" ]; then
|
||||
echo -e "|\n| Notice: Starting 'crond' via 'service'"
|
||||
chkconfig crond on
|
||||
service crond start
|
||||
elif [ -n "$(command -v pacman)" ]
|
||||
then
|
||||
elif [ -n "$(command -v pacman)" ]; then
|
||||
echo -e "|\n| Notice: Starting 'cronie' via 'systemctl'"
|
||||
systemctl start cronie
|
||||
systemctl enable cronie
|
||||
systemctl start cronie
|
||||
systemctl enable cronie
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# Check if cron was started
|
||||
if [ -z "$(ps -Al | grep cron | grep -v grep)" ]
|
||||
then
|
||||
if [ -z "$(ps -Al | grep cron | grep -v grep)" ]; then
|
||||
# Show error
|
||||
echo -e "|\n| Error: Cron is available but could not be started\n|"
|
||||
exit 1
|
||||
|
@ -116,59 +172,89 @@ then
|
|||
fi
|
||||
|
||||
# Attempt to delete previous agent
|
||||
if [ -f /etc/netweak/agent.sh ]
|
||||
then
|
||||
if [ -f "/etc/$NETWEAK/agent.sh" ]; then
|
||||
# Remove agent dir
|
||||
rm -Rf /etc/netweak
|
||||
rm -Rf "/etc/$NETWEAK"
|
||||
|
||||
# Remove cron entry and user
|
||||
if id -u netweak >/dev/null 2>&1
|
||||
then
|
||||
(crontab -u netweak -l | grep -v "/etc/netweak/agent.sh") | crontab -u netweak - && userdel netweak
|
||||
if id -u "$NETWEAK" > /dev/null 2>&1; then
|
||||
(crontab -u "$NETWEAK" -l | grep -v "/etc/$NETWEAK/agent.sh") | crontab -u "$NETWEAK" - && userdel "$NETWEAK"
|
||||
else
|
||||
(crontab -u root -l | grep -v "/etc/netweak/agent.sh") | crontab -u root -
|
||||
(crontab -u root -l | grep -v "/etc/$NETWEAK/agent.sh") | crontab -u root -
|
||||
fi
|
||||
fi
|
||||
|
||||
# Create agent dir
|
||||
mkdir -p /etc/netweak
|
||||
mkdir -p "/etc/$NETWEAK"
|
||||
|
||||
# Create log dir
|
||||
mkdir -p /etc/netweak/log
|
||||
mkdir -p "/etc/$NETWEAK/log"
|
||||
|
||||
# Download agent
|
||||
echo -e "| Downloading agent.sh to /etc/netweak\n|\n| + $(curl -JLso /etc/netweak/agent.sh https://github.com/netweak/agent/raw/main/agent.sh)"
|
||||
echo -e "| Downloading agent.sh to /etc/$NETWEAK"
|
||||
if ! curl -JLso "/etc/$NETWEAK/agent.sh" "https://github.com/netweak/agent/raw/$BRANCH/agent.sh"; then
|
||||
echo -e "|\n| Error: Failed to download agent.sh\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Download heartbeat
|
||||
echo -e "| Downloading heartbeat.sh to /etc/netweak\n|\n| + $(curl -JLso /etc/netweak/heartbeat.sh https://github.com/netweak/agent/raw/main/heartbeat.sh)"
|
||||
echo -e "| Downloading heartbeat.sh to /etc/$NETWEAK"
|
||||
if ! curl -JLso "/etc/$NETWEAK/heartbeat.sh" "https://github.com/netweak/agent/raw/$BRANCH/heartbeat.sh"; then
|
||||
echo -e "|\n| Error: Failed to download heartbeat.sh\n|"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -f /etc/netweak/agent.sh ]
|
||||
then
|
||||
# Verify downloaded files exist and aren't empty
|
||||
for file in "agent.sh" "heartbeat.sh"; do
|
||||
if [ ! -f "/etc/$NETWEAK/$file" ]; then
|
||||
echo -e "|\n| Error: Failed to download $file\n|"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! -s "/etc/$NETWEAK/$file" ]; then
|
||||
echo -e "|\n| Error: Downloaded $file is empty\n|"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -f "/etc/$NETWEAK/agent.sh" ]; then
|
||||
# Create auth file
|
||||
echo "$token" > /etc/netweak/token.conf
|
||||
|
||||
echo "$token" >"/etc/$NETWEAK/token.conf"
|
||||
|
||||
# Create endpoint file if different from default
|
||||
if [ "$ENDPOINT" != "https://api.netweak.com" ]; then
|
||||
echo "$ENDPOINT" >"/etc/$NETWEAK/endpoint.conf"
|
||||
fi
|
||||
|
||||
# Create version file
|
||||
echo "$VERSION" >"/etc/$NETWEAK/version"
|
||||
|
||||
# Create user
|
||||
useradd netweak -r -d /etc/netweak -s /bin/false
|
||||
|
||||
useradd "$NETWEAK" -r -d "/etc/$NETWEAK" -s /bin/false
|
||||
|
||||
# Modify user permissions
|
||||
chown -R netweak:netweak /etc/netweak && chmod -R 700 /etc/netweak
|
||||
|
||||
chown -R "$NETWEAK":"$NETWEAK" "/etc/$NETWEAK" && chmod -R 700 "/etc/$NETWEAK"
|
||||
|
||||
# Modify ping permissions
|
||||
chmod +s `type -p ping`
|
||||
chmod +s $(type -p ping)
|
||||
|
||||
# Configure cron
|
||||
crontab -u netweak -l 2>/dev/null | { cat; echo "* * * * * bash /etc/netweak/heartbeat.sh > /etc/netweak/log/cron.log 2>&1"; } | crontab -u netweak -
|
||||
crontab -u netweak -l 2>/dev/null | { cat; echo "* * * * * bash /etc/netweak/agent.sh > /etc/netweak/log/cron.log 2>&1"; } | crontab -u netweak -
|
||||
|
||||
crontab -u "$NETWEAK" -l 2>/dev/null | {
|
||||
cat
|
||||
echo "* * * * * bash /etc/$NETWEAK/heartbeat.sh > /etc/$NETWEAK/log/cron.log 2>&1"
|
||||
} | crontab -u "$NETWEAK" -
|
||||
crontab -u "$NETWEAK" -l 2>/dev/null | {
|
||||
cat
|
||||
echo "* * * * * bash /etc/$NETWEAK/agent.sh > /etc/$NETWEAK/log/cron.log 2>&1"
|
||||
} | crontab -u "$NETWEAK" -
|
||||
|
||||
# Initial run of agent
|
||||
bash /etc/netweak/agent.sh
|
||||
bash "/etc/$NETWEAK/agent.sh"
|
||||
|
||||
# Show success
|
||||
echo -e "|\n| Success: The Netweak agent has been installed\n|"
|
||||
|
||||
|
||||
# Attempt to delete installation script
|
||||
if [ -f $0 ]
|
||||
then
|
||||
if [ -f $0 ]; then
|
||||
rm -f $0
|
||||
fi
|
||||
else
|
||||
|
|
Loading…
Add table
Reference in a new issue