%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/engines/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/engines/junos_syslog.pyc

�
���^c@@s~dZddlmZmZmZddlZddlZddlZy|ddlm	Z	ddl
mZmZddl
mZmZmZmZmZmZmZmZmZmZmZeZWn-ek
r�eZdefd��YZ	nXddljj Z dd	l!m"Z"dd
l#m$Z$ej%e&�Z'dZ(d�Z)d
efd��YZ*de	fd��YZ+dd�Z,dS(u@
Junos Syslog Engine
==========================

.. versionadded:: 2017.7.0


:depends: pyparsing, twisted


An engine that listens to syslog message from Junos devices,
extract event information and generate message on SaltStack bus.

The event topic sent to salt is dynamically generated according to the topic title
specified by the user. The incoming event data (from the junos device) consists
of the following fields:

1.   hostname
2.   hostip
3.   daemon
4.   event
5.   severity
6.   priority
7.   timestamp
8.   message
9.   pid
10.   raw (the raw event data forwarded from the device)

The topic title can consist of any of the combination of above fields, but the
topic has to start with ``jnpr/syslog``. Here are a couple example
combinations:

- jnpr/syslog/hostip/daemon/event
- jnpr/syslog/daemon/severity

The corresponding dynamic topic sent on salt event bus would look something like:

- jnpr/syslog/1.1.1.1/mgd/UI_COMMIT_COMPLETED
- jnpr/syslog/sshd/7

The default topic title is ``jnpr/syslog/hostname/event``.

One can choose the type of data they want from the event bus. For example, if
one wants only events pertaining to a particular daemon, this can be specified
in the configuration file:

.. code-block:: yaml

    daemon: mgd

One can even have a list of daemons:

.. code-block:: yaml

    daemon:
      - mgd
      - sshd

Example configuration (to be written in master config file)

.. code-block:: yaml

    engines:
      - junos_syslog:
          port: 9999
          topic: jnpr/syslog/hostip/daemon/event
          daemon:
            - mgd
            - sshd

For junos_syslog engine to receive events, syslog must be set on the junos device.
This can be done via following configuration:

.. code-block:: text

    set system syslog host <ip-of-the-salt-device> port 516 any any

Below is a sample syslog event which is received from the junos device:

.. code-block:: text

    <30>May 29 05:18:12 bng-ui-vm-9 mspd[1492]: No chassis configuration found

The source for parsing the syslog messages is taken from:
https://gist.github.com/leandrosilva/3651640#file-xlog-py
i(tabsolute_importtprint_functiontunicode_literalsN(tDatagramProtocol(treactortthreads(tWordtalphastSuppresstCombinetnumststringtOptionaltRegextLineEndt	StringEndt
delimitedListRcB@seZRS((t__name__t
__module__(((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyRhs(tsix(trangeujunos_syslogcC@ststdfStS(u>
    Load only if twisted and pyparsing libs are present.
    u\junos_syslog could not be loaded. Make sure you have twisted and pyparsing python libraries.(tHAS_TWISTED_AND_PYPARSINGtFalsetTrue(((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyt__virtual__ws
t_ParsercB@seZd�Zd�ZRS(cC@sett�}t�j�}tt|ddt�td��}td�|td�}ttj	tj
dd�}|}t|d|d|�}|||}tttdd	d�}	tttd
d	dd�ttd�|td��td�}
t
d
�}||||	|
|t�|B|_||||	|t�|B|_dS(Nu.tcombineu:u<u>texactiu_u-u/u[u]u.*(RR
RtsuppressRRRRRt	uppercaset	lowercaseR	RR
Rt_Parser__patternt_Parser__pattern_without_daemon(tselftintstEOLt	ipAddresstprioritytmonthtdaythourt	timestampthostnametdaemontmessage((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyt__init__�s$	
H(cC@s�y|jj|�}Wn<tk
rTy|jj|�}WqUtk
rPdSXnXt|�dkr�i}t|d�|d<|dd@|d<|dd?|d<tjd�|d	<|d
|d<d|d
<|d|d<d|d<||d<|St|�dkr�i}t|d�|d<|dd@|d<|dd?|d<tjd�|d	<|d
|d<|d|d
<|d|d<d|d<tj	d|d�}|r�|j
d�|d<n||d<|St|�dkr�i}t|d�|d<|dd@|d<|dd?|d<tjd�|d	<|d
|d<|d|d
<|d|d<|d|d<d|d<tj	d|d�}|r�|j
d�|d<|j
d�|d<n||d<|St|�dkr�i}|d|d<t|d�|d<|dd@|d<|dd?|d<tjd�|d	<|d|d<|d|d
<|d|d<|d|d<d|d<tj	d|d�}|r�|j
d�|d<|j
d�|d<n||d<|SdS(Niiupriorityiuseverityiufacilityu%Y-%m-%d %H:%M:%Su	timestampiuhostnameuunknownudaemoniumessageuSYSTEMueventurawu(\w+): (.*)iiupidii	uhostip(RtparseStringt	ExceptionR tlentintttimetstrftimetretmatchtgroup(R!tlinetparsedtpayloadtobj((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pytparse�s�

	








(RRR-R;(((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyR�s	't_SyslogServerFactorycB@s5eZd�Zd�Zd�Zd�Zd�ZRS(c
C@sQ||_t�|_ddddddddd	d
g
}d|jkr8|djd�|jd<|djd�}||_t|�d
ks�|ddks�|ddkr�tjd�dddd
g|_nVxSt	d
t|��D]<}|||kr�tjd�dddd
g|_Pq�q�W|jd=ndddd
g|_dS(Nuhostipupriorityuseverityufacilityu	timestampuhostnameudaemonupidumessageueventutopicu/iiujnpriusyslogurThe topic specified in configuration should start with                     "jnpr/syslog". Using the default topic.uKPlease check the topic specified.                               Only the following keywords can be specified                                in the topic: hostip, priority, severity,                                 facility, timestamp, hostname, daemon, pid,                                  message, event. Using the default topic.(
toptionsRR:tstriptsplitttitleR0tlogtdebugR(R!R=tdatattopicsti((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyR-�s8			2
cC@s�|jj|�}||d<tjd|||�t}x�|D]�}||krt||tjtf�r�tj	||�tj	||�krt
}Pqqt||t�r�xN||D],}tj	|�tj	||�kr�Pq�q�Wt
}Pqtd��q?td��q?W|r�d|kr�d}xUt
dt|j��D];}	|dtj	||j|	�7}tjd	||�qHWitd
6|d6|d6}
|
Std
��nit
d
6}
|
SdS(u�
        This function will parse the raw syslog data, dynamically create the
        topic according to the topic specified by the user (if specified) and
        decide whether to send the syslog data as an event on the master bus,
        based on the constraints given by the user.

        :param data: The raw syslog event data which is to be parsed.
        :param host: The IP of the host from where syslog is forwarded.
        :param port: Port of the junos device from which the data is sent
        :param options: kwargs provided by the user in the configuration file.
        :return: The result dictionary which contains the data and the topic,
                 if the event is to be sent on the bus.

        uhostipu5Junos Syslog - received %s from %s, sent from port %su*Arguments in config not specified properlyu^Please check the arguments given to junos engine in the                     configuration fileueventujnpr/syslogiu/u8Junos Syslog - sending this event on the bus: %s from %susendudatautopicu5The incoming event data could not be parsed properly.N(R:R;RARBRt
isinstanceRtstring_typesR1t	text_typeRtlistR/RR0R@(R!RCthosttportR=tsend_this_eventtkeytoptttopicREtresult((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyt	parseData!sH


&"
"
cC@so|drk|d}|d}tddkrQtjttd�j||�qktdd|d	|�nd
S(u�
        This function identifies whether the engine is running on the master
        or the minion and sends the data to the master event bus accordingly.

        :param result: It's a dictionary which has the final data and topic.

        usendudatautopicu__roleumasterusock_diruevent.fire_masterRCttagN(t__opts__teventtget_master_eventt
fire_eventt__salt__(R!RPRCRO((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pytsend_event_to_salt`s


	

cC@stj|j�dS(u)
        Log the error messages.
        N(RAterrortgetErrorMessage(R!terr_msg((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pythandle_errorvscC@sG|\}}tj|j||||j�}|j|j|j�dS(N(Rt
deferToThreadRQR=taddCallbacksRXR\(R!RCtconnection_detailsRJRKtd((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pytdatagramReceived|s(RRR-RQRXR\Ra(((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyR<�s
	-	?		icK@s4tjd|�tj|t|��tj�dS(Nu&Starting junos syslog engine (port %s)(RAtinfoRt	listenUDPR<trun(RKtkwargs((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pytstart�s(-t__doc__t
__future__RRRR4tloggingR2ttwisted.internet.protocolRttwisted.internetRRt	pyparsingRRRR	R
RRR
RRRRRtImportErrorRtobjecttsalt.utils.eventtutilsRTtsalt.extRtsalt.ext.six.movesRt	getLoggerRRAt__virtualname__RRR<Rf(((s=/usr/lib/python2.7/site-packages/salt/engines/junos_syslog.pyt<module>Ws*L

	
q�

Zerion Mini Shell 1.0