@ -8,13 +8,14 @@ DOCUMENTATION:
type : notification
type : notification
short_description : Sends events to Logentries
short_description : Sends events to Logentries
description :
description :
- This callback plugin will generate JSON objects and send them to Logentries for auditing / debugging purposes .
- This callback plugin will generate JSON objects and send them to Logentries via TCP for auditing / debugging purposes .
- If you want to use an ini configuration , the file must be placed in the same directory as this plugin and named logentries . ini
- Before 2.4 , if you wanted to use an ini configuration , the file must be placed in the same directory as this plugin and named logentries . ini
- In 2.4 and above you can just put it in the main Ansible configuration file .
version_added : " 2.0 "
version_added : " 2.0 "
requirements :
requirements :
- whitelisting in configuration
- whitelisting in configuration
- certifi ( python library )
- certifi ( python library )
- flatdict ( pytnon library )
- flatdict ( pytnon library ) , if you want to use the ' flatten ' option
options :
options :
api :
api :
description : URI to the Logentries API
description : URI to the Logentries API
@ -22,7 +23,7 @@ DOCUMENTATION:
- name : LOGENTRIES_API
- name : LOGENTRIES_API
default : data . logentries . com
default : data . logentries . com
ini :
ini :
- section : default s
- section : callback_logentrie s
key : api
key : api
port :
port :
description : Http port to use when connecting to the API
description : Http port to use when connecting to the API
@ -30,7 +31,7 @@ DOCUMENTATION:
- name : LOGENTRIES_PORT
- name : LOGENTRIES_PORT
default : 80
default : 80
ini :
ini :
- section : default s
- section : callback_logentrie s
key : port
key : port
tls_port :
tls_port :
description : Port to use when connecting to the API when TLS is enabled
description : Port to use when connecting to the API when TLS is enabled
@ -38,15 +39,15 @@ DOCUMENTATION:
- name : LOGENTRIES_TLS_PORT
- name : LOGENTRIES_TLS_PORT
default : 443
default : 443
ini :
ini :
- section : default s
- section : callback_logentrie s
key : tls_port
key : tls_port
token :
token :
description : the authentication token
description : The logentries " TCP token "
env :
env :
- name : LOGENTRIES_ANSIBLE_TOKEN
- name : LOGENTRIES_ANSIBLE_TOKEN
required : True
required : True
ini :
ini :
- section : default s
- section : callback_logentrie s
key : token
key : token
use_tls :
use_tls :
description :
description :
@ -56,7 +57,7 @@ DOCUMENTATION:
default : False
default : False
type : boolean
type : boolean
ini :
ini :
- section : default s
- section : callback_logentrie s
key : use_tls
key : use_tls
flatten :
flatten :
description : flatten complex data structures into a single dictionary with complex keys
description : flatten complex data structures into a single dictionary with complex keys
@ -65,7 +66,7 @@ DOCUMENTATION:
env :
env :
- name : LOGENTRIES_FLATTEN
- name : LOGENTRIES_FLATTEN
ini :
ini :
- section : default s
- section : callback_logentrie s
key : flatten
key : flatten
EXAMPLES : >
EXAMPLES : >
To enable , add this to your ansible . cfg file in the defaults block
To enable , add this to your ansible . cfg file in the defaults block
@ -78,8 +79,8 @@ EXAMPLES: >
export LOGENTRIES_PORT = 10000
export LOGENTRIES_PORT = 10000
export LOGENTRIES_ANSIBLE_TOKEN = dd21fc88 - f00a - 43 ff - b977 - e3a4233c53af
export LOGENTRIES_ANSIBLE_TOKEN = dd21fc88 - f00a - 43 ff - b977 - e3a4233c53af
Or create a logentries . ini config file that sites next to the plugin with the following contents
Or in the main Ansible config file
[ logentries]
[ callback_ logentries]
api = data . logentries . com
api = data . logentries . com
port = 10000
port = 10000
tls_port = 20000
tls_port = 20000
@ -108,8 +109,8 @@ try:
except ImportError :
except ImportError :
HAS_FLATDICT = False
HAS_FLATDICT = False
from ansible . module_utils. six . moves import configparse r
from ansible . errors import AnsibleErro r
from ansible . module_utils . _text import to_bytes , to_text
from ansible . module_utils . _text import to_bytes , to_text , to_native
from ansible . plugins . callback import CallbackBase
from ansible . plugins . callback import CallbackBase
"""
"""
Todo :
Todo :
@ -118,11 +119,7 @@ Todo:
class PlainTextSocketAppender ( object ) :
class PlainTextSocketAppender ( object ) :
def __init__ ( self ,
def __init__ ( self , display , LE_API = ' data.logentries.com ' , LE_PORT = 80 , LE_TLS_PORT = 443 ) :
verbose = True ,
LE_API = ' data.logentries.com ' ,
LE_PORT = 80 ,
LE_TLS_PORT = 443 ) :
self . LE_API = LE_API
self . LE_API = LE_API
self . LE_PORT = LE_PORT
self . LE_PORT = LE_PORT
@ -134,7 +131,7 @@ class PlainTextSocketAppender(object):
# Unicode Line separator character \u2028
# Unicode Line separator character \u2028
self . LINE_SEP = u ' \u2028 '
self . LINE_SEP = u ' \u2028 '
self . verbose = verbose
self . _display = display
self . _conn = None
self . _conn = None
def open_connection ( self ) :
def open_connection ( self ) :
@ -149,9 +146,8 @@ class PlainTextSocketAppender(object):
try :
try :
self . open_connection ( )
self . open_connection ( )
return
return
except Exception :
except Exception as e :
if self . verbose :
self . _display . vvvv ( " Unable to connect to Logentries: %s " % str ( e ) )
self . _display . warning ( " Unable to connect to Logentries " )
root_delay * = 2
root_delay * = 2
if ( root_delay > self . MAX_DELAY ) :
if ( root_delay > self . MAX_DELAY ) :
@ -160,6 +156,7 @@ class PlainTextSocketAppender(object):
wait_for = root_delay + random . uniform ( 0 , root_delay )
wait_for = root_delay + random . uniform ( 0 , root_delay )
try :
try :
self . _display . vvvv ( " sleeping %s before retry " % wait_for )
time . sleep ( wait_for )
time . sleep ( wait_for )
except KeyboardInterrupt :
except KeyboardInterrupt :
raise
raise
@ -221,90 +218,75 @@ class CallbackModule(CallbackBase):
CALLBACK_NEEDS_WHITELIST = True
CALLBACK_NEEDS_WHITELIST = True
def __init__ ( self ) :
def __init__ ( self ) :
# TODO: allow for alternate posting methods (REST/UDP/agent/etc)
super ( CallbackModule , self ) . __init__ ( )
super ( CallbackModule , self ) . __init__ ( )
# verify dependencies
if not HAS_SSL :
if not HAS_SSL :
self . _display . warning ( " Unable to import ssl module. Will send over port 80. " )
self . _display . warning ( " Unable to import ssl module. Will send over port 80. " )
warn = ' '
if not HAS_CERTIFI :
if not HAS_CERTIFI :
self . disabled = True
self . disabled = True
warn + = ' The `certifi` python module is not installed. '
self . _display . warning ( ' The `certifi` python module is not installed. \n Disabling the Logentries callback plugin. ' )
if not HAS_FLATDICT :
self . le_jobid = str ( uuid . uuid4 ( ) )
self . disabled = True
warn + = ' The `flatdict` python module is not installed. '
if warn :
# FIXME: remove when done testing
self . _display . warning ( ' %s \n Disabling the Logentries callback plugin. ' % warn )
# initialize configurable
self . api_url = ' data.logentries.com '
self . api_port = 80
self . api_tls_port = 443
self . use_tls = False
self . flatten = False
self . token = None
config_path = os . path . abspath ( os . path . dirname ( __file__ ) )
# FIXME: make configurable, move to options
config = configparser . ConfigParser ( )
self . timeout = 10
try :
config . readfp ( open ( os . path . join ( config_path , ' logentries.ini ' ) ) )
if config . has_option ( ' logentries ' , ' api ' ) :
self . api_uri = config . get ( ' logentries ' , ' api ' )
if config . has_option ( ' logentries ' , ' port ' ) :
self . api_port = config . getint ( ' logentries ' , ' port ' )
if config . has_option ( ' logentries ' , ' tls_port ' ) :
self . api_tls_port = config . getint ( ' logentries ' , ' tls_port ' )
if config . has_option ( ' logentries ' , ' use_tls ' ) :
self . use_tls = config . getboolean ( ' logentries ' , ' use_tls ' )
if config . has_option ( ' logentries ' , ' token ' ) :
self . token = config . get ( ' logentries ' , ' token ' )
if config . has_option ( ' logentries ' , ' flatten ' ) :
self . flatten = config . getboolean ( ' logentries ' , ' flatten ' )
except :
self . api_uri = os . getenv ( ' LOGENTRIES_API ' )
if self . api_uri is None :
self . api_uri = ' data.logentries.com '
try :
# FIXME: remove testing
self . api_port = int ( os . getenv ( ' LOGENTRIES_PORT ' ) )
# self.set_options({'api': 'data.logentries.com', 'port': 80,
if self . api_port is None :
# 'tls_port': 10000, 'use_tls': True, 'flatten': False, 'token': 'ae693734-4c5b-4a44-8814-1d2feb5c8241'})
self . api_port = 80
except TypeError :
def set_option ( self , name , value ) :
self . api_port = 80
raise AnsibleError ( " The Logentries callabck plugin does not suport setting individual options. " )
def set_options ( self , options ) :
super ( CallbackModule , self ) . set_options ( options )
# get options
try :
try :
self . api_tls_port = int ( os . getenv ( ' LOGENTRIES_TLS_PORT ' ) )
self . api_url = self . _plugin_options [ ' api ' ]
if self . api_tls_port is None :
self . api_port = self . _plugin_options [ ' port ' ]
self . api_tls_port = 443
self . api_tls_port = self . _plugin_options [ ' tls_port ' ]
except TypeError :
self . use_tls = self . _plugin_options [ ' use_tls ' ]
self . api_tls_port = 443
self . flatten = self . _plugin_options [ ' flatten ' ]
except KeyError as e :
self . _display . warning ( " Missing option for Logentries callback plugin: %s " % to_native ( e ) )
self . disabled = True
# this just needs to be set to use TLS
try :
self . use_tls = os . getenv ( ' LOGENTRIES_USE_TLS ' )
self . token = self . _plugin_options [ ' token ' ]
if self . use_tls is None :
except KeyError as e :
self . use_tls = False
self . _display . warning ( ' Logentries token was not provided, this is required for this callback to operate, disabling ' )
elif self . use_tls . lower ( ) in [ ' yes ' , ' true ' ] :
self . disabled = True
self . use_tls = True
self . token = os . getenv ( ' LOGENTRIES_ANSIBLE_TOKEN ' )
if self . flatten and not HAS_FLATDICT :
if self . token is None :
self . disabled = True
self . disabled = True
self . _display . warning ( ' Logentries token could not be loaded. The logentries token can be provided using the `LOGENTRIES_TOKEN` environment '
self . _display . warning ( ' You have chosen to flatten and the `flatdict` python module is not installed. \n Disabling the Logentries callback plugin. ' )
' variable ' )
self . flatten = os . getenv ( ' LOGENTRIES_FLATTEN ' )
self . _initialize_connections ( )
if self . flatten is None :
self . flatten = False
elif self . flatten . lower ( ) in [ ' yes ' , ' true ' ] :
self . flatten = True
self . verbose = False
def _initialize_connections ( self ) :
self . timeout = 10
self . le_jobid = str ( uuid . uuid4 ( ) )
if not self . disabled :
if self . use_tls :
if self . use_tls :
self . _appender = TLSSocketAppender ( verbose = self . verbose ,
self . _display . vvvv ( " Connecting to %s : %s with TLS " % ( self . api_url , self . api_tls_port ) )
LE_API = self . api_uri ,
self . _appender = TLSSocketAppender ( display = self . _display , LE_API = self . api_url , LE_TLS_PORT = self . api_tls_port )
LE_TLS_PORT = self . api_tls_port )
else :
else :
self . _appender = PlainTextSocketAppender ( verbose = self . verbose ,
self . _display . vvvv ( " Connecting to %s : %s " % ( self . api_url , self . api_port ) )
LE_API = self . api_uri ,
self . _appender = PlainTextSocketAppender ( display = self . _display , LE_API = self . api_url , LE_PORT = self . api_port )
LE_PORT = self . api_port )
self . _appender . reopen_connection ( )
self . _appender . reopen_connection ( )
def emit_formatted ( self , record ) :
def emit_formatted ( self , record ) :
@ -318,43 +300,34 @@ class CallbackModule(CallbackBase):
msg = record . rstrip ( ' \n ' )
msg = record . rstrip ( ' \n ' )
msg = " {} {} " . format ( self . token , msg )
msg = " {} {} " . format ( self . token , msg )
self . _appender . put ( msg )
self . _appender . put ( msg )
self . _display . vvvv ( " Sent event to logentries " )
def _set_info ( self , host , res ) :
return { ' le_jobid ' : self . le_jobid , ' hostname ' : host , ' results ' : res }
def runner_on_ok ( self , host , res ) :
def runner_on_ok ( self , host , res ) :
results = { }
results = self . _set_info ( host , res )
results [ ' le_jobid ' ] = self . le_jobid
results [ ' hostname ' ] = host
results [ ' results ' ] = res
results [ ' status ' ] = ' OK '
results [ ' status ' ] = ' OK '
self . emit_formatted ( results )
self . emit_formatted ( results )
def runner_on_failed ( self , host , res , ignore_errors = False ) :
def runner_on_failed ( self , host , res , ignore_errors = False ) :
results = { }
results = self . _set_info ( host , res )
results [ ' le_jobid ' ] = self . le_jobid
results [ ' hostname ' ] = host
results [ ' results ' ] = res
results [ ' status ' ] = ' FAILED '
results [ ' status ' ] = ' FAILED '
self . emit_formatted ( results )
self . emit_formatted ( results )
def runner_on_skipped ( self , host , item = None ) :
def runner_on_skipped ( self , host , item = None ) :
results = { }
results = self . _set_info ( host , item )
results [ ' le_jobid ' ] = self . le_jobid
del results [ ' results ' ]
results [ ' hostname ' ] = host
results [ ' status ' ] = ' SKIPPED '
results [ ' status ' ] = ' SKIPPED '
self . emit_formatted ( results )
self . emit_formatted ( results )
def runner_on_unreachable ( self , host , res ) :
def runner_on_unreachable ( self , host , res ) :
results = { }
results = self . _set_info ( host , res )
results [ ' le_jobid ' ] = self . le_jobid
results [ ' hostname ' ] = host
results [ ' results ' ] = res
results [ ' status ' ] = ' UNREACHABLE '
results [ ' status ' ] = ' UNREACHABLE '
self . emit_formatted ( results )
self . emit_formatted ( results )
def runner_on_async_failed ( self , host , res , jid ) :
def runner_on_async_failed ( self , host , res , jid ) :
results = { }
results = self . _set_info ( host , res )
results [ ' le_jobid ' ] = self . le_jobid
results [ ' hostname ' ] = host
results [ ' results ' ] = res
results [ ' jid ' ] = jid
results [ ' jid ' ] = jid
results [ ' status ' ] = ' ASYNC_FAILED '
results [ ' status ' ] = ' ASYNC_FAILED '
self . emit_formatted ( results )
self . emit_formatted ( results )