At XpresServers, we constantly strive to deliver total customer satisfaction with all our hosting services. That’s why we offer fast, reliable and secure service that’s backed by our friendly, knowledgeable support team, 24/7.

Every action performed by Salt, such as applying a highstate or restarting a minion, generates an event. Beacons emit events for non-salt processes, such as system state changes or file changes. This guide will use Salt beacons to notify the Salt master of changes to minions, and Salt reactors to react to those changes.

Before You Begin

If you don’t already have a Salt master and minion, follow the first steps in our Getting Started with Salt – Basic Installation and Setup guide.

Note

The steps in this guide require root privileges. Be sure to run the steps below as root or with the sudo prefix. For more information on privileges, see our Users and Groups guide.

Example 1: Preventing Configuration Drift

Configuration drift occurs when there are untracked changes to a system configuration file. Salt can help prevent configuration drift by ensuring that a file is immediately reverted to a safe state upon change. In order to do this, we first have to let Salt manage the file. This section will use an NGINX configuration file as an example, but you can choose any file.

Manage Your File

On your Salt master, create a directory for your managed files in /srv/salt/files:

mkdir /srv/salt/files

On your Salt master, place your nginx.conf, or whichever file you would like to manage, in the /srv/salt/files folder.

On your Salt master, create a state file to manage the NGINX configuration file:

There are two file paths in this .sls file. The first file path is the path to your managed file on your minion. The second, under source and prefixed with salt://, points to the file path on your master. salt:// is a convenience file path that maps to /srv/salt.

On your Salt master, create a top file if it does not already exist and add your nginx_conf.sls:

/srv/salt/top.sls

1
2
3

base:'*':-nginx_conf

Apply a highstate from your Salt master to run the nginx_conf.sls state on your minions.

salt '*' state.apply

Create a Beacon

In order to be notified when a file changes, you will need the Python pyinotify package. Create a Salt state that will handle installing the pyinotify package on your minions:

The file path in the first line is simply the name of the reactor, and can be whatever you choose. The tgt, or target, is the Salt minion that will receive the highstate. In this case, the information passed to the reactor from the beacon event is used to programmatically choose the right Salt minion ID. This information is available as the data dictionary. The arg, or argument, is the name of the Salt state file that was created to manage the nginx.conf file.

On your Salt master, create a reactor.conf file and include the new reactor state file:

This reactor.conf file is essentially a list of event names matched to reactor state files. In this example we’ve used a glob (*) in the event name instead of specifying a specific minion ID, (which means that any change to a nginx.confon any minion will trigger the reactor), but you might find a specific minion ID better suits your needs.

Restart the salt-master service to apply the reactor.conf file:

systemctl restart salt-master

On your Salt minion, make a change to the nginx.conf file. Then check out your event runner shell and you should see a number of events. Then, check your nginx.conf file. The changes you made should have automatically been reverted.

Congratulations, you now know how to manage configuration drift with Salt. All future updates to nginx.conf should be made on the Salt master and applied using state.apply.

Example 2: Monitoring Minion Memory Usage with Slack

Salt comes with a number of system monitoring beacons. In this example we will monitor a minion’s memory usage and send a Slack notification when the memory usage has passed a certain threshold. For this section you will need to create a Slack bot, obtain an OAuth token, and configure the bot to be able to send Slack messages on your behalf.

Configure Your Slack App

Create a Slack app.

From the Slack app settings page, navigate to OAuth & Permissions.

Copy down the OAuth Access Token.

Under Scopes, select Send Messages As < your app name >.

Create a Beacon

On your Salt master, open or create the /srv/salt/files/minion.d/beacons.conf file and add the following lines. If you already have a beacons.conf file from the previous example, leave out the beacons: line, but ensure that rest of the configuration is indented two spaces:

/srv/salt/files/minion.d/beacons.conf

1
2
3
4
5

beacons:memusage:beacon.present:-percent:15%-interval:15

In this example we’ve left the memory usage percentage low to ensure the beacon event will fire, and the event interval set to 15 seconds. In a production environment you should change these to more sane values.

Apply a highstate from your Salt master to add the beacon to your minions:

salt '*' state.apply

If you haven’t already, open another shell into your Salt master and start the event runner:

salt-run state.event pretty=True

After a few seconds, assuming you’ve set the memory percentage low enough, you should see an event like the following:

Note that the first line is the name of the event, and contains the minion name. We will use this event name in the next section.

Create a Reactor

On your Salt master, create the /srv/reactor directory if you have not already done so:

mkdir /srv/reactor

Then create a reactor state file and add the following lines, making sure to change the channel, api_key, and from_name keys to reflect your desired values. The api_key is the OAuth token you copied down in step 3 of the Configure Your Slack App section:

We’re using the data dictionary provided to the reactor from the memusage event to populate the minion ID and the memory usage.

Open or create the reactor.conf file. If you already have a reactor.conf file from the previous example, leave out the reactor: line, but ensure that rest of the configuration is indented two spaces:

/etc/salt/master.d/reactor.conf

1
2
3

reactor:-'salt/beacon/*/memusage/':-'/srv/reactor/memusage.sls'

In this example we’ve used a glob (*) in the event name instead of specifying a specific minion ID, (which means that any memusage event will trigger the reactor), but you might find a specific minion ID better suits your needs.

Restart salt-master to apply the reactor.conf:

systemctl restart salt-master

In your event-runner shell, after a few seconds, you should see an event like the following:

Congratulations, you now know how to monitor your Salt minion’s memory usage with Slack integration. Salt can also monitor CPU load, disk usage, and a number of other things. Refer to the More Information section below for additional resources.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.