Product docs and API reference are now on Akamai TechDocs.
Search product docs.
Search for “” in product docs.
Search API reference.
Search for “” in API reference.
Search Results
 results matching 
 results
No Results
Filters
Monitoring Salt Minions with Beacons
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
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.
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:
- File: /srv/salt/nginx_conf.sls
1 2 3 4 5
/etc/nginx/nginx.conf: file.managed: - source: - salt://files/nginx.conf - makedirs: True
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, undersource
and prefixed withsalt://
, 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
:- File: /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 thepyinotify
package on your minions:- File: /srv/salt/packages.sls
1 2 3 4 5 6 7 8
python-pip: pkg.installed pyinotify: pip.installed: - require: - pkg: python-pip
Note The inotify beacon only works on OSes that have inotify kernel support. Currently this excludes FreeBSD, macOS, and Windows.On the Salt master create a
minion.d
directory to store the beacon configuration file:mkdir /srv/salt/files/minion.d
Now create a beacon that will emit an event every time the
nginx.conf
file changes on your minion. Create the/etc/salt/minion.d/beacons.conf
file and add the following lines:- File: /etc/salt/minion.d/beacons.conf
1 2 3 4 5 6 7
beacons: inotify: - files: /etc/nginx/nginx.conf: mask: - modify - disable_during_state_run: True
To apply this beacon to your minions, create a new
file.managed
Salt state:- File: /srv/salt/beacons.sls
1 2 3 4 5 6
/etc/salt/minion.d/beacons.conf: file.managed: - source: - salt://files/minion.d/beacons.conf - makedirs: True
Add the new
packages
andbeacons
states to your Salt master’s top file:- File: /srv/salt/top.sls
1 2 3 4 5
base: '*': - nginx_conf - packages - beacons
Apply a highstate from your Salt master to implement these changes on your minions:
salt '*' state.apply
Open another shell to your Salt master and start the Salt event runner. You will use this to monitor for file change events from your beacon.
salt-run state.event pretty=True
On your Salt minion, make a change to your
nginx.conf
file, and then check out your Salt event runner shell. You should see an event like the following:salt/beacon/salt-minion/inotify//etc/nginx/nginx.conf { "_stamp": "2018-10-10T13:53:47.163499", "change": "IN_MODIFY", "id": "salt-minion", "path": "/etc/nginx/nginx.conf" }
Note that the first line is the name of the event, and it includes your Salt minion name and the path to your managed file. We will use this event name in the next section.
To revert the
nginx.conf
file to it’s initial state, you can apply a highstate from your Salt master.salt '*' state.apply nginx_conf
Open your managed file on your Salt minion and notice that the change has been reverted. We will automate this last step in the next section.
Create a Reactor
On your Salt master, create the
/srv/reactor
directory:mkdir /srv/reactor
Then create a reactor state file in the
/srv/reactor
directory and include the following:- File: /srv/reactor/nginx_conf_reactor.sls
1 2 3 4 5
/etc/nginx/nginx.conf: local.state.apply: - tgt: {{ data['id'] }} - arg: - nginx_conf
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 thedata
dictionary. Thearg
, or argument, is the name of the Salt state file that was created to manage thenginx.conf
file.On your Salt master, create a
reactor.conf
file and include the new reactor state file:- File: /etc/salt/master.d/reactor.conf
1 2 3
reactor: - 'salt/beacon/*/inotify//etc/nginx/nginx.conf': - /srv/reactor/nginx_conf_reactor.sls
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 anginx.conf
on 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 thereactor.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 yournginx.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
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 abeacons.conf
file from the previous example, leave out thebeacons:
line, but ensure that rest of the configuration is indented two spaces:- File: /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:
salt/beacon/salt-minion/memusage/ { "_stamp": "2018-10-10T15:48:53.165368", "id": "salt-minion", "memusage": 20.7 }
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
, andfrom_name
keys to reflect your desired values. Theapi_key
is the OAuth token you copied down in step 3 of the Configure Your Slack App section:- File: /srv/reactor/memusage.sls
1 2 3 4 5 6 7 8
Send memusage to Slack: local.slack.post_message: - tgt: {{ data['id'] }} - kwarg: channel: "#general" api_key: "xoxp-451607817121-453578458246..." message: "{{ data['id'] }} has hit a memory usage threshold: {{ data['memusage'] }}%." from_name: "Memusage Bot"
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 areactor.conf
file from the previous example, leave out thereactor:
line, but ensure that rest of the configuration is indented two spaces:- File: /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 thereactor.conf
:systemctl restart salt-master
In your event-runner shell, after a few seconds, you should see an event like the following:
salt/job/20181010161053393111/ret/salt-minion { "_stamp": "2018-10-10T16:10:53.571956", "cmd": "_return", "fun": "slack.post_message", "fun_args": [ { "api_key": "xoxp-451607817121-453578458246-452348335312-2328ce145e5c0c724c3a8bc2afafee17", "channel": "#general", "from_name": "Memusage Bot", "message": "salt-minion has hit a memory usage threshold: 20.7." } ], "id": "salt-minion", "jid": "20181010161053393111", "retcode": 0, "return": true, "success": true }
Open Slack and you should see that your app has notified the room.
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.
This page was originally published on