Directadmin has the ability to manage basic system service using a service monitor feature. The service monitor lets you control a few basic operations like start, stop or restart the service. In addition to that, you are also able to view the process ID (PID) and memory usage of the service. The very cool thing about service monitor is, when your service status is listed in the service monitor went offline, it will then notify you via a system ticket or email so that you can take necessary actions. This is how the service monitor looks like in the screenshot below:
In this article, I’m going to show you how you can add a custom system service to the service monitor so that it will be monitored by Directadmin if the service goes down. Let’s assume that you want to add an SSH Daemon process (sshd) in the list which is not included by default:
Step 1: Open the following file:
nano /usr/local/directadmin/data/admin/services.status
Step 2: Look for an entry sshd= if it does not exist execute the following command:
echo "sshd=ON' >> /usr/local/directadmin/data/admin/services.status
Step 3: If the sshd= already exists in Step 2 but it is turned off (sshd=OFF), then you can enable it using the following command:
sed -i "s|^sshd\=.*|$sshd=ON|" /usr/local/directadmin/data/admin/services.status
After that, you can check the service monitor page in Directadmin whether the sshd process is running like the following screenshot:
Now, if your sshd process crashes or stopped running out of sudden, DirectAdmin will automatically notify you via ticket or email.
Extra solutions with automation script:
You might want to add a few more custom services and you are lazy to copy-paste the above commands. If that the case, use the following bash script to automate this operation:
Step 1: Create a new file called add_service.bash with the following content:
#!/bin/bash
# Author: Arafat Ali
# Website: codegix.com
# Add more services here automatically in array list (make sure this service is available when you execute systemctl status <service_name> in your BSD/linux terminal)
da_service_status="/usr/local/directadmin/data/admin/services.status"
service_names=("rsyslog" "csf" "suricata" "rspamd" "sshd" "mysqld")
echo "Adding and enabling extra services: ${service_names} ..."
echo ""
for service_name in "${service_names[@]}"; do
echo "Checking service status for ${service_name} ..."
grep_find=$(grep -m1 "^${service_name}=" ${da_service_status})
if [ -n "${grep_find}" ]; then
echo "Turning on service ${service_name} ..."
sed -i "s|^${service_name}\=.*|${service_name}=ON|" ${da_service_status}
else
echo "Adding new extra service ${service_name} ..."
echo "${service_name}=ON" >>${da_service_status}
fi
echo ""
done
Step 2: Execute the script to add extra services:
chmod +x add_service.bash
./add_service.bash
After you have executed the above script, all the services that were listed in the variable service_names will be added to the service monitor page.
Directadmin custom hook script for service_down_notice.sh
Directadmin has a feature to add a custom script to be executed after your listed service in the service monitor goes down. This will let you add extra custom checks when the service is offline. This feature is also mentioned here https://www.directadmin.com/features.php?id=2101