%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/beacons/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/beacons/wtmp.pyo

�
���^c
@@sfdZddlmZmZddlZddlZddlZddlZddlZ	ddl
Z	ddlmZddl
mZdZdZdZd	d
ddd
dddddg
Zeje�ZdZdZdZdZeje�ZyddljZeZ Wne!k
r"e"Z nXd�Z#d�Z$d�Z%d�Z&d�Z'd�Z(d�Z)dS(u�
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.
i(tabsolute_importtunicode_literalsN(tsix(tmapuwtmpu
/var/log/wtmpthi32s4s32s256shhiii4i20xutypeuPIDulineuinittabuuseruhostnameuexit_statususessionutimeuaddruwtmp.locu	wtmp.tty.iicC@stjjt�rtStS(N(tostpathtisfiletWTMPt__virtualname__tFalse(((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pyt__virtual__�sc@sc�s||fSt�t�s.t}d}nt�fd�dD��sYt}d}n||fS(u
    Check time range
    u>The time_range parameter for wtmp beacon must be a dictionary.c3@s|]}|�kVqdS(N((t.0tk(ttrange(s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pys	<genexpr>�sustartuenduJThe time_range parameter for wtmp beacon must contain start & end options.(ustartuend(t
isinstancetdictR
tall(Rtstatustmsg((Rs5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pyt_validate_time_range�s
		cC@s`td|�}|s*tjd|�dSx/|dD]#}||kr5||||<q5q5WdS(u
    Gather group members
    u
group.infou"Group %s does not exist, ignoring.Numembers(t__salt__tlogtwarning(tgrouptgroupstuserst_grouptmember((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pyt_gather_group_members�scC@satrLtj|d�}tj|d�}t||koF|kn�Stjd�tSdS(u
    Check time range
    ustartuenduDateutil is required.N(t_TIME_SUPPORTEDtdateutil_parsertparsetboolRterrorR
(t
time_rangetnowt_startt_end((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pyt_check_time_range�s 
cC@sttkrttSdS(u)
    return the active file location
    N(tLOC_KEYt__context__(((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pyt_get_loc�scC@s�t}d}t|t�s*t}d}n�i}tt|j|��d|kr�t|dt�stt}d}nGxD|dD]8}|d|jdi�}t|||�\}}qW|s�||fSnd|krVt|dt�s�t}d}nGxD|dD]8}|d|jdi�}t|||�\}}qW|sV||fSnd|kr�t|dt�s�t}d	}n.|djdi�}t|||�\}}|s�||fSn||fS(
u+
    Validate the beacon configuration
    uValid beacon configurationu-Configuration for wtmp beacon must be a list.uusersu8User configuration for wtmp beacon must be a dictionary.u
time_rangeugroupsu9Group configuration for wtmp beacon must be a dictionary.udefaultsu<Defaults configuration for wtmp beacon must be a dictionary.(	tTrueRtlistR
RtupdateRtgetR(tconfigtvstatustvmsgt_configtusert_time_rangeR((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pytvalidate�sP		
	
	
c
C@s�g}i}i}d}t}t}x�|D]�}d|krJ|d}nd|krc|d}nd|kr||d}n|dkr+y|dd}Wntk
r�nXy|dd}Wq�tk
r�q�Xq+q+Wtjjjtd���}t	j
td�}	|	dkr8|jdd	�|j
�t	t<|S|j|	�x�tr�tjj�}
|jt�}t|�tkr�|S|j
�t	t<tjt|�}i}
x�tt�D]}\}}|||
|<t|
|tj�r�t|
|t�rtjjj|
|�|
|<n|
|jd
�|
|<q�q�W|
d|krsd|
d<|
d
t	dj t!|
d�<nX|
d|kr�d|
d<y't	j"dj t!|
d��|
d
<Wq�tk
r�q�Xnx|D]}t#|||�q�W|r�|
d
|kr�||
d
}t|t$�rQd|krQt%|d|
�r�|j&|
�q�q�|r�d|kr�t%|d|
�r�|j&|
�q�q�|j&|
�q�qH|r�d|kr�t%|d|
�r�|j&|
�q�qH|j&|
�qHWWdQX|S(uF
    Read the last wtmp file and return information on the logins
    uusersugroupsudefaultsuut_typeuloginulogouturbiiuutypeuactionuuseru{0}{1}ulineu
time_rangeN('tNonet
LOGIN_TYPEtLOGOUT_TYPEtKeyErrortsalttutilstfilestfopenRR)R.R(tseekttellR+tdatetimeR$treadtSIZEtlentstructtunpacktFMTt	enumeratetFIELDSRRtstring_typestbyteststringutilst
to_unicodetstriptformattTTY_KEY_PREFIXtpopRRR'tappend(R/tretRRtdefaultst
login_typetlogout_typetconfig_itemtfp_tlocR$trawtpackteventtindtfieldRt_user((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pytbeacon&s�






	 
!
'


	(*t__doc__t
__future__RRR@tloggingRRDtsalt.utils.stringutilsR:tsalt.utils.filestsalt.extRtsalt.ext.six.movesRR	RRFRHtcalcsizeRBR(ROR7R8t	getLoggert__name__Rtdateutil.parsertparserRR+RtImportErrorR
RRRR'R*R5R_(((s5/usr/lib/python2.7/site-packages/salt/beacons/wtmp.pyt<module>vsP	


						<

Zerion Mini Shell 1.0