Last active
September 8, 2015 22:14
-
-
Save ryancurrah/bb129754eebc09a849c2 to your computer and use it in GitHub Desktop.
Sensu Check Salt Master (test.ping)
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
import salt.client | |
from salt.config import apply_minion_config | |
from argparse import ArgumentParser | |
from common.sensu import Sensu | |
sensu = Sensu() | |
def main(): | |
""" | |
Checks if the specified salt-master is responding to salt ping's | |
This check can be ran from any host with a salt-minion installed | |
""" | |
parser = ArgumentParser() | |
parser.add_argument('--master', default='localhost') | |
args = parser.parse_args() | |
# | |
# | |
# Test.ping salt master which is a functional test | |
result = _check_salt_master(args.master) | |
# | |
# | |
# Output check state | |
if result: | |
message = 'Ping salt-master "{0}" succesful OK'.format(args.master) | |
return sensu.output_check(sensu.STATE_OK, message=message) | |
else: | |
message = 'Ping salt-master "{0}" failed CRITICAL'.format(args.master) | |
return sensu.output_check(sensu.STATE_CRITICAL, message=message) | |
def _check_salt_master(master='localhost'): | |
""" | |
Does a salt test.ping on the specified master host | |
:master: ip address or fqdn of salt-master | |
:returns: True if ping successful, False if ping failed | |
""" | |
opts = apply_minion_config() | |
opts['master'] = master | |
opts['auth_timeout'] = 20 | |
opts['auth_tries'] = 2 | |
opts['log_level'] = 'quiet' | |
opts['log_level_logfile'] = 'quiet' | |
try: | |
caller = salt.client.Caller(mopts=opts) | |
result = caller.function('test.ping') | |
except salt.exceptions.SaltClientError: | |
result = False | |
return True if result else False | |
if __name__ == '__main__': | |
main() | |
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
import socket | |
import time | |
import warnings | |
from sys import exit | |
warnings.filterwarnings("ignore", category=DeprecationWarning) | |
class Sensu(object): | |
""" | |
Sensu object for creating checks and metrics | |
""" | |
STATE_OK = 0 | |
STATE_WARNING = 1 | |
STATE_CRITICAL = 2 | |
STATE_UNKNOWN = 3 | |
VALID_STATES = range(0,4) | |
def __init__(self, scheme=None): | |
""" | |
:scheme: base scheme of the metric as an str | |
""" | |
if scheme: | |
self.scheme = scheme.rstrip('.') | |
return | |
def output_metric(self, name, value): | |
""" | |
Output metric to stdout | |
The metric name will be appended to the base scheme | |
:name: name of the metric as an str | |
:value: value of the metric as an int | |
:returns: prints metric to stdout | |
""" | |
print '{0}.{1}\t{2}\t{3}'.format(self.scheme, name, value, int(time.time())) | |
return | |
def output_check(self, state, message=''): | |
""" | |
Output check result to stdout | |
:state: one of the valid sensu states as an int | |
:message: message to show to standard out as an str | |
:returns: exit code and message to stdout | |
""" | |
if not self._valid_state(state): | |
raise BadSensuCheckState("Please enter a valid Sensu check state.") | |
print '{0}'.format(message) | |
exit(state) | |
return | |
def _valid_state(self, state): | |
""" | |
Validates the sensu check state | |
:state: the state as an int | |
""" | |
return True if state in self.VALID_STATES else False | |
class BadSensuCheckState(Exception): | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes the salt-master process is running but minions are having trouble connecting to it. This check tests if the salt-master is actually functioning by doing a salt 'test.ping' using python API. All you need installed on the host that runs the check is a salt-minion.