%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/modules/telemetry.pyc

�
���^c@@s2dZddlmZmZmZddlZddlZddlZddl	m
Z
eje�Z
yddlZeZWnek
r�eZnXdZd�Zd�Zddd�Zd	�Zdd
�Zdddd�Zdd�Zdd
�Zddd�Zddd�Zddddd�ZdS(uf
Connection module for Telemetry

.. versionadded:: 2016.3.0

https://github.com/mongolab/mongolab-telemetry-api-docs/blob/master/alerts.md

:configuration: This module accepts explicit telemetry credentials or
    can also read api key credentials from a pillar. More Information available
    here__.

.. __: https://github.com/mongolab/mongolab-telemetry-api-docs/blob/master/alerts.md

In the minion's config file:

.. code-block:: yaml

   telemetry.telemetry_api_keys:
     - abc123  # Key 1
     - efg321  # Backup Key 1
   telemetry_api_base_url: https://telemetry-api.mongolab.com/v0

:depends: requests

i(tabsolute_importtunicode_literalstprint_functionN(tsixu	telemetrycC@sts
tStS(N(tHAS_REQUESTStFalset__virtualname__(((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt__virtual__2scC@std|�}|jd�S(Nu
config.optionutelemetry_api_base_url(t__salt__tget(tprofiletconfig((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt_get_telemetry_base9scC@s�|dkr'|dkr'td��n|r�t|tj�rRtd|�}nt|t�rj|}n|r�|jd�d}q�td��ni|d6dd6S(	NuMissing api_key and profileu
config.optionutelemetry_api_keysiuMissing api_keyuTelemetry-API-Keyuapplication/jsonucontent-type(tNonet	Exceptiont
isinstanceRtstring_typesRtdictR	(tapi_keyR
t_profile((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt_auth>s	cC@sLdj|�}|tkr<t|}|||<|t|<ntj|g�S(Nutelemetry.{0}.alerts(tformatt__context__R	(t
deployment_idtmetric_nametalerttkeytalerts((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt
_update_cachePs


c	C@s�d}td|�}|tkr�t|�d}tj|d|�}|jdkr�i}x?t|j��D]+\}}|jdd�||jd�<qnW|t|<q�nt|j|t�S(	u�
    Given an email address, checks the local
    cache if corresponding email address: channel_id
    mapping exists

    email
        Email escalation policy
    profile
        A dict of telemetry config information.
    utelemetry.channelsR
u5/notification-channels?_type=EmailNotificationChanneltheadersi�u_idufalseuemail(	RRRtrequestsR	tstatus_codet	enumeratetjsonR(	temailR
Rtauthtget_urltresponsetcache_resulttindexR((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt_retrieve_channel_id[s#cC@sQtd|�}t}dj|�}|tkr�y2t|�dj|�}tj|d|�}Wn0tjjk
r�}	t	j
tj|	��tSXi}
|j
dkr�x@|j�D]/}||
|jdi�jd�<|
t|<q�Wq�ntj|�sgSt|j�}|r*t|j|�Sg|D]}d|kr1|d^q1S(	u�
    Get all alert definitions associated with a given deployment or if metric_name
    is specified, obtain the specific alert config

    Returns dictionary or list of dictionaries.

    CLI Example:

        salt myminion telemetry.get_alert_config rs-ds033197 currentConnections profile=telemetry
        salt myminion telemetry.get_alert_config rs-ds033197 profile=telemetry
    R
utelemetry.{0}.alertsu/alerts?deployment={0}Ri�u	conditionumetricu_id(RRRRRRR	t
exceptionstRequestExceptiontlogterrorRt	text_typeRR!tvalues(RRRR
R#RRR$R%tethttp_resultR((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pytget_alert_configws*
cC@std|�}t|�}|stjd|�t|�d}idd6||jd� dd6|d	6}tj|d
tj	j
j|�d|�}|jdkr�tjd
|j
d�|�|j
�j
d�}|td|<qtdj|���n|S(u|
    Given an email address, creates a notification-channels
    if one is not found and also returns the corresponding
    notification channel id.

    notify_channel
        Email escalation policy
    profile
        A dict of telemetry config information.

    CLI Example:

        salt myminion telemetry.get_notification_channel_id userx@company.com profile=telemetry
    R
u$%s channel does not exist, creating.u/notification-channelsuEmailNotificationChannelu_typeu@uEscalationPolicyunameuemailtdataRi�uISuccessfully created EscalationPolicy %s with EmailNotificationChannel %su_idutelemetry.channelsu*Failed to created notification channel {0}(RR(R+tinfoRtfindRtposttsalttutilsR!tdumpsRR	RRR(tnotify_channelR
R#tnotification_channel_idtpost_urlR2R%((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pytget_notification_channel_id�s"
*	cC@s�td|�}y,tjt|�dj|�d|�}Wn0tjjk
rm}tjt	j
|��tSX|jdkr�|j
�}t|�dkr�|Sdj|�Si|jd6tjj
j|j�jdd	�d
6SdS(u�
    get all the alarms set up against the current deployment

    Returns dictionary of alarm information

    CLI Example:

        salt myminion telemetry.get_alarms rs-ds033197 profile=telemetry

    R
u/alerts?deployment={0}Ri�iu%No alarms defined for deployment: {0}uerr_codeuerruuerr_msgN(RRR	RRR)R*R+R,RR-RRR!tlenR6R7tloadsttext(RR
R#R%R/talarms((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt
get_alarms�s,
cC@s�t||�}t|�d}dj|�}i|d6|jd�d6t|jd��j�d6i|d6|jd�d6|jd	�d	6d
6}y.tj|dtj	j
j|�d|�}	Wn/tjj
k
r�}
tjtj|
��nX|	jd
kr[|	jdkr[tjd||�tjd|||	j
��t|||	j
��n%tjd||tj	j
j|��|	jd
ko�|	jdk|	j
�fS(u�
    create an telemetry alarms.

    data is a dict of alert configuration data.

    Returns (bool success, str message) tuple.

    CLI Example:

        salt myminion telemetry.create_alarm rs-ds033197 {} profile=telemetry

    u/alertsutelemetry.{0}.alertsu
deploymentufilteruescalate_tounotificationChannelumetricumaxuminu	conditionR2Ri�i,u-Created alarm on metric: %s in deployment: %su1Updating cache for metric %s in deployment %s: %suBFailed to create alarm on metric: %s in deployment %s: payload: %s(RRRR	R<tsplitRR5R6R7R!R8R)R*R+R,RR-RR3tdebugR(RRR2RR
R#trequest_uriRt	post_bodyR%R/((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pytcreate_alarm�s0.	cC@s�t||�}t||||�}|s@tdj||�fSt|�d|d}i|d6|jd�d6t|jd��j�d6i|d6|jd	�d	6|jd
�d
6d6}y.tj	|dt
jjj
|�d
|�}	Wn9tjjk
r(}
tjd|
�ttj|
�fSX|	jdkr�|	jdkr�tjd|||	j��t|||	j��tjd||�t|	j�fStjd�jt
jjj|�t
jjj|�t
jjj
|��}tj|�t|fS(u�
    update an telemetry alarms. data is a dict of alert configuration data.

    Returns (bool success, str message) tuple.

    CLI Example:

        salt myminion telemetry.update_alarm rs-ds033197 {} profile=telemetry

    u6No entity found matching deployment {0} and alarms {1}u/alerts/u_idu
deploymentufilteruescalate_tounotificationChannelumetricumaxuminu	conditionR2RuUpdate failed: %si�i,u1Updating cache for metric %s in deployment %s: %su-Updated alarm on metric: %s in deployment: %suEFailed to create alarm on metric: {0} in deployment: {1} payload: {2}(RR1RRRR	R<RBRtputR6R7R!R8R)R*R+R,RR-RRCRR3tTruetstringutilst
to_unicode(RRR2RR
R#RRDRER%R/terr_msg((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pytupdate_alarm"s>.		
cC@sttd|�}|d
kr6t|d|d|�}n	|g}t|�dkrdtdj|�fSg}x�|D]�}t|�dj|�}	yEtj|	d|�}
|r�t	j
d||�t||d
�nWn)tjj
k
r}t	jd|�nX|
jd	krq|j|�qqqqWt|�dkrUtd
jdj|�|�fStdjdj|�|�fS(udelete an alert specified by alert_id or if not specified blows away all the alerts
         in the current deployment.

    Returns (bool success, str message) tuple.

    CLI Example:

        salt myminion telemetry.delete_alarms rs-ds033197 profile=telemetry

    R
Riu4failed to find alert associated with deployment: {0}u/alerts/{0}Ru(updating cache and delete %s key from %suDelete failed: %si�u.Failed to delete {0} alarms in deployment: {1}u, u2Successfully deleted {0} alerts in deployment: {1}N(RR
R1R=RRRRtdeleteR+RCRR)R*R,RtappendtjoinRH(Rtalert_idRRR
R#t	alert_idstfailed_to_deletetidt
delete_urlR%R/((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt
delete_alarmsZs,	
	
( t__doc__t
__future__RRRtloggingtsalt.utils.jsonR6tsalt.utils.stringutilstsalt.extRt	getLoggert__name__R+RRHRtImportErrorRRRRR
RRR(R1R<RARFRLRU(((s:/usr/lib/python2.7/site-packages/salt/modules/telemetry.pyt<module>s.


			+,58

Zerion Mini Shell 1.0