Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions hetrixtools_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Set PATH/Locale
export LC_NUMERIC="C"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
ScriptPath=$(dirname "${BASH_SOURCE[0]}")

# Agent Version (do not change)
Expand Down Expand Up @@ -152,7 +152,11 @@ if [ -n "$ConnectionPorts" ]
then
IFS=',' read -r -a ConnectionPortsArray <<< "$ConnectionPorts"
declare -A Connections
netstat=$(ss -ntu | awk '{print $5}')
if [ $SYNOLOGY -gt 0 ]; then
netstat=$(netstat -ntuW | grep ESTABLISHED | awk '{ print $4}' | sed 's/^\([^\.]*\):\([0-9]\+\)$/\[\1\]:\2/g')
else
netstat=$(ss -ntu | awk '{print $5}')
fi
for cPort in "${ConnectionPortsArray[@]}"
do
Connections[$cPort]=$(echo "$netstat" | grep -c ":$cPort$")
Expand Down Expand Up @@ -197,11 +201,18 @@ done
RunTimes=$(echo | awk "{print 60 / $CollectEveryXSeconds}")
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) Collecting data for $RunTimes loops" >> "$ScriptPath"/debug.log; fi

SYNOLOGY=0
if [[ "$(uname -a)" =~ "synology" ]]; then
SYNOLOGY=1
fi

# Collect data loop
for X in $(seq "$RunTimes")
do
# Get vmstat
if [ $SYNOLOGY -gt 0 ]; then VMSTAT=$(dool --proc --cpu --mem-adv -s --integer --nocolor --ascii --noupdate 5 2 | sed 's/\x1B\[[0-9;]*[a-zA-Z]//g' | sed 's/|/ /g' | tail -1 | awk '{print 0,0,$17,$11,$12,$13,0,0,0,0,0,0,$4,$5,$6,$7,$8}' | numfmt --field 3-6 --from iec --to-unit 1000); else
VMSTAT=$(vmstat "$CollectEveryXSeconds" 2 | tail -1)
fi
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) $VMSTAT" >> "$ScriptPath"/debug.log; fi

# CPU usage
Expand Down Expand Up @@ -442,6 +453,9 @@ then
RequiresReboot=1
fi
fi
elif [ $SYNOLOGY -gt 0 ]
then
OS="Linux Synology $(cat /etc/VERSION | grep os_name | sed 's/.*\"\(.*\)\"/\1/g') $(cat /etc/VERSION | grep productversion | sed 's/.*\"\(.*\)\"/\1/g')"
# If all else fails
else
OS="$(uname -s)"
Expand Down Expand Up @@ -633,9 +647,28 @@ mdstat=$(cat /proc/mdstat 2>/dev/null)
declare -A zpooldiskusage
if [ "$CheckSoftRAID" -gt 0 ]
then
for i in $(echo -ne "$dfPB1" | awk '$1 ~ /\// {print}' | awk '{print $1}')
for i in $(echo -ne "$dfPB1" | awk '$1 ~ /\// {print}' | awk '{print $1}' | uniq)
do
mdadm=$(mdadm -D "$i" 2>/dev/null)

if [ $SYNOLOGY -gt 0 ]; then
mnt=$(echo -ne "$dfPB1" | grep "$i " | awk '{print $(NF)}')
if [ "$i" == "/dev/md0" ]; then
mdadm=$(echo "$mdadm" | sed 's/, degraded//g')
else
MDDEVICE=$(lsblk -Js "$i" | jq -r '.. | objects | select(any(contains("raid")?)) .name')
if [ -n "$MDDEVICE" ]; then
mdadm=$(mdadm -D "/dev/$MDDEVICE" 2>/dev/null)
fi
fi
if [ -n "$mdadm" ]
then
mnt=$(echo -ne "$dfPB1" | grep "$i " | awk '{print $(NF)}')
RAID="$RAID$mnt,$i,$mdadm;"
fi
continue
fi

# DEBUG
if [ "$DEBUG" -eq 1 ]; then echo -e "$ScriptStartTime-$(date +%T]) mdadm -D $i:\n$mdadm" >> "$ScriptPath"/debug.log; fi
if [ -n "$mdadm" ]
Expand Down
89 changes: 61 additions & 28 deletions hetrixtools_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
#

# Set PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin

INSTALL_PATH=/etc/hetrixtools

SYNOLOGY=0
if [[ "$(uname -a)" =~ "synology" ]]; then
SYNOLOGY=1
INSTALL_PATH="/opt/hetrixtools"
fi

# Branch
BRANCH="master"
Expand Down Expand Up @@ -49,49 +57,63 @@ if [ -z "$2" ]
exit
fi

EXEC_USER="hetrixtools"
[[ "$2" -eq "1" ]] && EXEC_USER="root"
[[ "$2" -eq "0" && $SYNOLOGY -gt 0 ]] && EXEC_USER="root"

# Check if system has crontab and wget
echo "Checking for crontab and wget..."
command -v crontab >/dev/null 2>&1 || { echo "ERROR: Crontab is required to run this agent." >&2; exit 1; }
command -v crontab >/dev/null 2>&1 || [[ $SYNOLOGY -gt 0 ]] || { echo "ERROR: Crontab is required to run this agent." >&2; exit 1; }
command -v wget >/dev/null 2>&1 || { echo "ERROR: wget is required to run this agent." >&2; exit 1; }
echo "... done."

if [ $SYNOLOGY -gt 0 ]; then
echo "Checking dool installation..."
if ! command -v dool >/dev/null 2>&1; then
echo "No dool installation foud... Installing one..."
wget -t 1 -T 30 -qO "/opt/bin/dool" https://raw.githubusercontent.com/scottchiefbaker/dool/refs/tags/v1.3.3/dool
chmod +x /opt/bin/dool
fi
echo "... done"
fi

# Remove old agent (if exists)
echo "Checking if there's any old hetrixtools agent already installed..."
if [ -d /etc/hetrixtools ]
if [ -d "$INSTALL_PATH" ]
then
echo "Old hetrixtools agent found, deleting it..."
rm -rf /etc/hetrixtools
rm -rf "$INSTALL_PATH"
else
echo "No old hetrixtools agent found..."
fi
echo "... done."

# Creating agent folder
echo "Creating the hetrixtools agent folder..."
mkdir -p /etc/hetrixtools
mkdir -p "$INSTALL_PATH"
echo "... done."

# Fetching the agent
echo "Fetching the agent..."
wget -t 1 -T 30 -qO /etc/hetrixtools/hetrixtools_agent.sh https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools_agent.sh
wget -t 1 -T 30 -qO "$INSTALL_PATH/hetrixtools_agent.sh" https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools_agent.sh
echo "... done."

# Fetching the config file
echo "Fetching the config file..."
wget -t 1 -T 30 -qO /etc/hetrixtools/hetrixtools.cfg https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools.cfg
wget -t 1 -T 30 -qO "$INSTALL_PATH/hetrixtools.cfg" https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools.cfg
echo "... done."

# Inserting Server ID (SID) into the agent config
echo "Inserting Server ID (SID) into agent config..."
sed -i "s/SID=\"\"/SID=\"$SID\"/" /etc/hetrixtools/hetrixtools.cfg
sed -i "s/SID=\"\"/SID=\"$SID\"/" "$INSTALL_PATH/hetrixtools.cfg"
echo "... done."

# Check if any services are to be monitored
echo "Checking if any services should be monitored..."
if [ "$3" != "0" ]
then
echo "Services found, inserting them into the agent config..."
sed -i "s/CheckServices=\"\"/CheckServices=\"$3\"/" /etc/hetrixtools/hetrixtools.cfg
sed -i "s/CheckServices=\"\"/CheckServices=\"$3\"/" "$INSTALL_PATH/hetrixtools.cfg"
fi
echo "... done."

Expand All @@ -100,16 +122,16 @@ echo "Checking if software RAID should be monitored..."
if [ "$4" -eq "1" ]
then
echo "Enabling software RAID monitoring in the agent config..."
sed -i "s/CheckSoftRAID=0/CheckSoftRAID=1/" /etc/hetrixtools/hetrixtools.cfg
sed -i "s/CheckSoftRAID=0/CheckSoftRAID=1/" "$INSTALL_PATH/hetrixtools.cfg"
fi
echo "... done."

# Check if Drive Health should be monitored
echo "Checking if Drive Health should be monitored..."
if [ "$5" -eq "1" ]
if [ "$5" -eq "1" ]
then
echo "Enabling Drive Health monitoring in the agent config..."
sed -i "s/CheckDriveHealth=0/CheckDriveHealth=1/" /etc/hetrixtools/hetrixtools.cfg
sed -i "s/CheckDriveHealth=0/CheckDriveHealth=1/" "$INSTALL_PATH/hetrixtools.cfg"
fi
echo "... done."

Expand All @@ -118,7 +140,7 @@ echo "Checking if 'View running processes' should be enabled..."
if [ "$6" -eq "1" ]
then
echo "Enabling 'View running processes' in the agent config..."
sed -i "s/RunningProcesses=0/RunningProcesses=1/" /etc/hetrixtools/hetrixtools.cfg
sed -i "s/RunningProcesses=0/RunningProcesses=1/" "$INSTALL_PATH/hetrixtools.cfg"
fi
echo "... done."

Expand All @@ -127,7 +149,7 @@ echo "Checking if any ports to monitor number of connections on..."
if [ "$7" != "0" ]
then
echo "Ports found, inserting them into the agent config..."
sed -i "s/ConnectionPorts=\"\"/ConnectionPorts=\"$7\"/" /etc/hetrixtools/hetrixtools.cfg
sed -i "s/ConnectionPorts=\"\"/ConnectionPorts=\"$7\"/" "$INSTALL_PATH/hetrixtools.cfg"
fi
echo "... done."

Expand All @@ -137,6 +159,7 @@ ps aux | grep -ie hetrixtools_agent.sh | awk '{print $2}' | xargs kill -9
echo "... done."

# Checking if hetrixtools user exists
if [ $SYNOLOGY -lt 1 ]; then
echo "Checking if hetrixtools user already exists..."
if id -u hetrixtools >/dev/null 2>&1
then
Expand All @@ -145,34 +168,44 @@ then
echo "Deleting hetrixtools user..."
userdel hetrixtools
echo "Creating the new hetrixtools user..."
useradd hetrixtools -r -d /etc/hetrixtools -s /bin/false
useradd hetrixtools -r -d "$INSTALL_PATH" -s /bin/false
echo "Assigning permissions for the hetrixtools user..."
chown -R hetrixtools:hetrixtools /etc/hetrixtools
chmod -R 700 /etc/hetrixtools
chown -R hetrixtools:hetrixtools "$INSTALL_PATH"
chmod -R 700 "$INSTALL_PATH"
else
echo "The hetrixtools user doesn't exist, creating it now..."
useradd hetrixtools -r -d /etc/hetrixtools -s /bin/false
useradd hetrixtools -r -d "$INSTALL_PATH" -s /bin/false
echo "Assigning permissions for the hetrixtools user..."
chown -R hetrixtools:hetrixtools /etc/hetrixtools
chmod -R 700 /etc/hetrixtools
chown -R hetrixtools:hetrixtools "$INSTALL_PATH"
chmod -R 700 "$INSTALL_PATH"
fi
echo "... done."
fi

# Removing old cronjob (if exists)
echo "Removing any old hetrixtools cronjob, if exists..."
crontab -u root -l | grep -v 'hetrixtools_agent.sh' | crontab -u root - >/dev/null 2>&1
crontab -u hetrixtools -l | grep -v 'hetrixtools_agent.sh' | crontab -u hetrixtools - >/dev/null 2>&1
if [ $SYNOLOGY -gt 0 ]; then
sed -i '/hetrixtools_agent.sh/d' "/etc/crontab"
else
crontab -u root -l | grep -v 'hetrixtools_agent.sh' | crontab -u root - >/dev/null 2>&1
crontab -u hetrixtools -l | grep -v 'hetrixtools_agent.sh' | crontab -u hetrixtools - >/dev/null 2>&1
fi
echo "... done."

# Setup the new cronjob to run the agent either as 'root' or as 'hetrixtools' user, depending on client's installation choice.
# Default is running the agent as 'hetrixtools' user, unless chosen otherwise by the client when fetching the installation code from the hetrixtools website.
if [ "$2" -eq "1" ]
if [ "$EXEC_USER" == "root" ]
then
echo "Setting up the new cronjob as 'root' user..."
crontab -u root -l 2>/dev/null | { cat; echo "* * * * * bash /etc/hetrixtools/hetrixtools_agent.sh >> /etc/hetrixtools/hetrixtools_cron.log 2>&1"; } | crontab -u root - >/dev/null 2>&1
if [ $SYNOLOGY -gt 0 ]; then
echo "* * * * * root bash "$INSTALL_PATH/hetrixtools_agent.sh" >> "$INSTALL_PATH/hetrixtools_cron.log" 2>&1" >> /etc/crontab
synosystemctl restart crond
else
crontab -u root -l 2>/dev/null | { cat; echo "* * * * * bash "$INSTALL_PATH/hetrixtools_agent.sh" >> "$INSTALL_PATH/hetrixtools_cron.log" 2>&1"; } | crontab -u root - >/dev/null 2>&1
fi
else
echo "Setting up the new cronjob as 'hetrixtools' user..."
crontab -u hetrixtools -l 2>/dev/null | { cat; echo "* * * * * bash /etc/hetrixtools/hetrixtools_agent.sh >> /etc/hetrixtools/hetrixtools_cron.log 2>&1"; } | crontab -u hetrixtools - >/dev/null 2>&1
crontab -u hetrixtools -l 2>/dev/null | { cat; echo "* * * * * bash "$INSTALL_PATH/hetrixtools_agent.sh" >> "$INSTALL_PATH/hetrixtools_cron.log" 2>&1"; } | crontab -u hetrixtools - >/dev/null 2>&1
fi
echo "... done."

Expand All @@ -191,13 +224,13 @@ wget -t 1 -T 30 -qO- --post-data "$POST" https://sm.hetrixtools.net/ &> /dev/nul
echo "... done."

# Start the agent
if [ "$2" -eq "1" ]
if [ "$EXEC_USER" == "root" ]
then
echo "Starting the agent under the 'root' user..."
bash /etc/hetrixtools/hetrixtools_agent.sh > /dev/null 2>&1 &
bash "$INSTALL_PATH/hetrixtools_agent.sh" > /dev/null 2>&1 &
else
echo "Starting the agent under the 'hetrixtools' user..."
sudo -u hetrixtools bash /etc/hetrixtools/hetrixtools_agent.sh > /dev/null 2>&1 &
sudo -u hetrixtools bash "$INSTALL_PATH/hetrixtools_agent.sh" > /dev/null 2>&1 &
fi
echo "... done."

Expand Down