File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/beacons/__pycache__/wtmp.cpython-310.pyc
o
�N�g') � @ s� d Z ddlZddlZddlZddlZddlZddlZddlZdZ dZ
dZg d�Ze�
e�ZdZdZd Zd
Ze�e�Zz
ddlmZ dZW n eyO dZY nw d
d� Zdd� Zdd� Zdd� Zdd� Zdd� Z dd� Z!dS )a�
Beacon to fire events at login of users as registered in the wtmp file
.. versionadded:: 2015.5.0
Example Configuration
=====================
.. code-block:: yaml
# Fire events on all logins
beacons:
wtmp: []
# Matching on user name, using a default time range
beacons:
wtmp:
- users:
gareth:
- defaults:
time_range:
start: '8am'
end: '4pm'
# Matching on user name, overriding the default time range
beacons:
wtmp:
- users:
gareth:
time_range:
start: '7am'
end: '3pm'
- defaults:
time_range:
start: '8am'
end: '4pm'
# Matching on group name, overriding the default time range
beacons:
wtmp:
- groups:
users:
time_range:
start: '7am'
end: '3pm'
- defaults:
time_range:
start: '8am'
end: '4pm'
How to Tell What An Event Means
===============================
In the events that this beacon fires, a type of ``7`` denotes a login, while a
type of ``8`` denotes a logout. These values correspond to the ``ut_type``
value from a wtmp/utmp event (see the ``wtmp`` manpage for more information).
In the extremely unlikely case that your platform uses different values, they
can be overridden using a ``ut_type`` key in the beacon configuration:
.. code-block:: yaml
beacons:
wtmp:
- ut_type:
login: 9
logout: 10
This beacon's events include an ``action`` key which will be either ``login``
or ``logout`` depending on the event type.
.. versionchanged:: 2019.2.0
``action`` key added to beacon event, and ``ut_type`` config parameter
added.
Use Case: Posting Login/Logout Events to Slack
==============================================
This can be done using the following reactor SLS:
.. code-block:: jinja
report-wtmp:
runner.salt.cmd:
- args:
- fun: slack.post_message
- channel: mychannel # Slack channel
- from_name: someuser # Slack user
- message: "{{ data.get('action', 'Unknown event') | capitalize }} from `{{ data.get('user', '') or 'unknown user' }}` on `{{ data['id'] }}`"
Match the event like so in the master config file:
.. code-block:: yaml
reactor:
- 'salt/beacon/*/wtmp/':
- salt://reactor/wtmp.sls
.. note::
This approach uses the :py:mod:`slack execution module
<salt.modules.slack_notify>` directly on the master, and therefore requires
that the master has a slack API key in its configuration:
.. code-block:: yaml
slack:
api_key: xoxb-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX
See the :py:mod:`slack execution module <salt.modules.slack_notify>`
documentation for more information. While you can use an individual user's
API key to post to Slack, a bot user is likely better suited for this. The
:py:mod:`slack engine <salt.engines.slack>` documentation has information
on how to set up a bot user.
� NZwtmpz
/var/log/wtmps hi32s4s32s256shhiii4i20x)
�typeZPID�lineZinittab�user�hostname�exit_status�session�time�addrzwtmp.locz wtmp.tty.� � TFc C s0 t j�t�rtS t� d�} t�dt| � d| fS )Nz does not exist.zUnable to load %s beacon: %sF)�os�path�isfile�WTMP�__virtualname__�log�error)�err_msg� r �E/opt/saltstack/salt/lib/python3.10/site-packages/salt/beacons/wtmp.py�__virtual__� s
r c sD � s||fS t � t�sd}d}t� fdd�dD ��sd}d}||fS )�
Check time range
Fz>The time_range parameter for wtmp beacon must be a dictionary.c 3 s � | ]}|� v V qd S )Nr )�.0�k��tranger r � <genexpr>� s � z'_validate_time_range.<locals>.<genexpr>)�start�endzJThe time_range parameter for wtmp beacon must contain start & end options.)�
isinstance�dict�all)r �status�msgr r r �_validate_time_range� s
�r$ c C sF t d | �}|st�d| � dS |d D ]}||vr || ||<