Monitor your website using Saltstack and the schedule option
Basically this in an introduction to Saltstack scheduling functionality. I am actually writing a book about this tool, a kind of introduction to use the best feature of Saltstack and some practical examples, so this is one of the idea that I’ve got when writing the book.
If you want to start using the schedule option of Saltstack, the example in this post is quite easy to understand and begin using the schedule module.
One of the important things when running servers is the availability of hosted applications, usually having a web interface as a front running on Apache, nginx or any other HTTP server. We are going to write a “SLS” file that will be run every 1 minute to check if a website is running or not. If the site is down Saltstack will send an email to the administrator.
Note that the web application is hosted where a minion is installed.
If your default pillar root is :
create a directory called sched:
and the create your init.sls file
in the latter add the following code:
schedule: cmd: function: cmd.run args: - /usr/bin/wget "www.eon01.com" --timeout 30 -O - 2>/dev/null | grep "dev & ops" || echo "The site is down" | /usr/bin/mail -v -s "Your site is down" admin@e-mail minutes: 1 maxrunning: 1
You should add other arguments “args” like “maxrunning”.
According to the official documentation:
Specify maxrunning to ensure that there are no more than N copies of a particular routine running. Use this for jobs that may be long-running and could step on each other or otherwise double execute. The default for maxrunning is 1.
This code will execute the following command:
/usr/bin/wget "www.eon01.com" --timeout 30 -O - 2>/dev/null | grep "dev & ops" || echo "The site is down" | /usr/bin/mail -v -s "Your site is down" admin@e-mail
which will use wget (with a timeout of 30 s) to download the home page of the www.eon01.com and grep “dev & ops”, if the site is down or very slow an email will be sent to the administrator.
This bash command will be executed using the function and its arguments:
cmd.run, will execute it every 1 minute and Saltstack will ensure that no more that 1 instance is running at the same time.
Don’t forget to put
at the beginning, this is important.
Of course the last step is to add the SLS to the top.sls file.
base: '*': - sched
This should be added to :
The latter should be configured in the master configuration file.
If you want to check if the new “sched” pillar was added, type:
salt '*' pillar.items
You will see a similar output to this:
eonSpider: ---------- schedule: ---------- cmd: ---------- args: - wget "www.eon01.com" --timeout 30 -O - 2>/dev/null | grep "dev & ops" || echo "The site is down" | mail -s "Your site is down" firstname.lastname@example.org function: cmd.run maxrunning: 1 minutes: 1
Well, this is a kind of hack but it is better to use Salt Returners if you want to schedule more complex things. You can find this in “SaltStack For DevOps” book here.
You can follow this link if you want to have a more complete view about scheduling jobs with Saltstack.
Happy hacking !