Last active
March 24, 2017 03:38
-
-
Save hemebond/56320892bf21d3ff76ae3fbb7bc747de to your computer and use it in GitHub Desktop.
NSCA Saltstack Returner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Return data to an NSCA server | |
This returner requires the following individual files | |
from the https://github.com/Yelp/send_nsca repo | |
* send_nsca/nagios.py | |
* send_nsca/nsca.py | |
Place those files, and this `__init__.py` file into your | |
state returners directory, e.g.: | |
.. code-block | |
_returners/ | |
nsca/ | |
__init__.py | |
nagios.py | |
nsca.py | |
The minion will need to be configured with the NSCA server hostname: | |
.. code-block:: yaml | |
nsca: | |
nsca_server: 'hostname.domain.com' | |
To override individual configuration items, append --return_kwargs '{"key:": "value"}' to the salt command. | |
.. versionadded:: 2016.3.0 | |
.. code-block:: bash | |
salt '*' test.ping --return nsca --return_kwargs '{"nsca_server": "hostname.domain.com"}' | |
Service checks can be scheduled via Pillar data: | |
.. code-block:: yaml | |
check_tomcat7: | |
function: nagios.run_all | |
name: check_tomcat7 | |
args: | |
- check_procs | |
- 1:1 1: -a org.apache.catalina.startup.Bootstrap -u tomcat7 | |
return_kwargs: | |
nsca_server: db9_icinga | |
service: tomcat7 | |
return_job: false | |
returner: nsca | |
minutes: 1 | |
''' | |
from .nsca import NscaSender | |
import logging | |
import salt.returners | |
log = logging.getLogger(__name__) | |
# Define the module's virtual name | |
__virtualname__ = 'nsca' | |
def __virtual__(): | |
return __virtualname__ | |
def _get_options(ret=None): | |
''' | |
Get the nsca options from salt. | |
''' | |
attrs = { | |
'nsca_server': 'nsca_server' | |
} | |
_options = salt.returners.get_returner_options(__virtualname__, | |
ret, | |
attrs, | |
__salt__=__salt__, | |
__opts__=__opts__) | |
log.debug(_options) | |
return _options | |
def returner(ret): | |
''' | |
Return information via NSCA. Must use cmd.run_all or nagios.run_all | |
so that the exit code is included in the return data. | |
''' | |
log.debug(ret) | |
_options = _get_options(ret) | |
if 'return' in ret: | |
r = ret.get('return', '') | |
if type(r) is dict: | |
# This is the result of a .run_all | |
# ret['return'] is a dict | |
retcode = r.get('retcode', 3) | |
output = r.get('stdout') | |
stderr = r.get('stderr', '') | |
if stderr != '': | |
log.error(stderr) | |
output = stderr | |
else: | |
# This is the result of a .run | |
# ret['return'] is a string | |
retcode = ret.get('retcode', 3) | |
output = r | |
else: | |
output = str(ret) | |
retcode = 3 | |
output = output.replace('\n', '\\n') | |
# The service name should have been passed in via return_kwargs | |
# otherwise a host check will be sent | |
if 'service' in _options: | |
service_name = _options.get('service') | |
else: | |
service_name = None | |
# The minion ID | |
host_name = __opts__.get('id') | |
# The NSCA server hostname should | |
remote_host = _options.get('nsca_server') | |
send_nsca(retcode, host_name, service_name, output, remote_host) | |
return ret | |
def send_nsca(status, host_name, service_name, text_output, remote_host, **kwargs): | |
''' | |
Helper function to easily send a NSCA message (wraps .nsca.NscaSender) | |
status | |
Integer describing the status | |
host_name | |
Host name to report as | |
service_name | |
Service to report as | |
text_output | |
Freeform text, should be under 512b | |
remote_host | |
Host name to send to | |
All other arguments are passed to the NscaSender constructor | |
''' | |
try: | |
n = NscaSender(remote_host=remote_host, **kwargs) | |
if service_name is not None: | |
n.send_service(host_name, service_name, status, text_output) | |
else: | |
n.send_host(host_name, status, text_output) | |
n.disconnect() | |
except Exception as e: | |
log.error("Unable to send NSCA packet to %s for %s:%s (%s)", remote_host, host_name, service_name, str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment