Chfagent won’t survive a host reboot so to make sure chfagent is always running, we’ll create a startup config to start the agent when the virtual machine starts. We’ll also create a watchdog to make sure the chfagent is constantly running.
Startup script
PuTTY into the chfagent virtual machine and execute:
sudo nano /etc/init/chfagent.conf
Copy the script below into the editor:
- Replace $api_email with the email provided by Auvik support when your AuvikFlow account was provisioned.
- Replace $api_token with the token provided by Auvik support when your AuvikFlow account was provisioned.
- Replace $interface_IP with the following interface IPs:
- The IP of a single interface for chfagent to listen on
- 0.0.0.0 to listen on all interfaces
description "chfagent for AuvikFlow"
author "Ray Patel / Mark Jasek / Auvik Networks Inc."
start on runlevel [2345]
stop on shutdown
script
echo "Starting chfagent for AuvikFlow..."
echo $$ > /var/run/chfagent.pid
exec chfagent -api_email=$api_email -api_token=$api_token -host=$interface_ip -port 2055
end script
pre-start script
echo "[`date`] Starting chfagent" >> /var/log/chfagent.log
end script
pre-stop script
rm /var/run/chfagent.pid
echo "[`date`] Stopping chfagent" >> /var/log/chfagent.log
end script
Once you copy the script over and save it, run the follow command to test the script:
init-checkconf /etc/init/chfagent.conf
If you receive the output below, the script has run successfully:
File /etc/init/chfagent.conf: syntax ok
Watchdog script
Use your favorite text editor to create a new cron directory and a watchdog script, called watchdog.sh, inside of it.
sudo mkdir /home/root/cron && sudo mkdir /home/root/tmp && sudo vi /home/root/cron/watchdog.sh
Your script should resemble the script below:
- Replace $api_email with the email provided by Auvik support when your AuvikFlow account was provisioned.
- Replace $api_token with the token provided by Auvik support when your AuvikFlow account was provisioned.
- Replace $interface_IP with the following interface IPs:
- The IP of a single interface for chfagent to listen on
- 0.0.0.0 to listen on all interfaces
#!/bin/bash
mkdir -p "$HOME/tmp"
PIDFILE="/var/run/chfagent.pid"
if [ -e "${PIDFILE}" ] && (ps -u $(whoami) -opid= |
grep -P "^\s*$(cat ${PIDFILE})$" &> /dev/null); then
echo "Already running."
exit 99
fi
/usr/bin/chfagent -api_email=$api_email -api_token=$api_token -host=$interface_ip -port 2055 > /var/log/chfagent.log &
echo $! > "${PIDFILE}"
chmod 644 "${PIDFILE}"
Now, make sure the script is executable:
sudo chmod +x /home/root/cron/watchdog.sh
Add your script to cron
Open crontab as root:
sudo crontab -e
Add the following line to it:
* * * * * /home/root/cron/watchdog.sh > /home/root/cron/watchdog.log 2>&1
The above cronjob checks every minute if chfagent is running. If it finds chfagent isn’t running, it starts it.
Chfagent requires super-user permissions to open a socket to the virtual machine’s network interface.
Comments