Last active
September 16, 2015 16:58
-
-
Save ryancurrah/104ec30997fe11a40df2 to your computer and use it in GitHub Desktop.
Check Salt Configuration
This file contains hidden or 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 ast | |
from salt.config import minion_config, master_config | |
from argparse import ArgumentParser | |
from common.sensu import Sensu | |
sensu = Sensu() | |
def main(): | |
""" | |
Check if the specified salt configuration key has | |
the expected value | |
""" | |
parser = ArgumentParser() | |
parser.add_argument('--conf-file', default='/etc/salt/master') | |
parser.add_argument('--conf-type', default='master', | |
help='Configuration type of "master" or "minion"') | |
parser.add_argument('--key', required=True, | |
help='Configuration key name to compare value to') | |
parser.add_argument('--value', default='/etc/salt/master', | |
help='Expected configuration value') | |
args = parser.parse_args() | |
# | |
# Check salt configuration | |
result, value = _check_salt_conf(args.conf_file, args.conf_type, | |
args.key, args.value) | |
# | |
# Output check state | |
if result: | |
message = 'Salt configuration "{0}" ' \ | |
'has correct value of "{1}" OK'.format(args.key, | |
value) | |
return sensu.output_check(sensu.STATE_OK, message=message) | |
else: | |
message = 'Salt configuration "{0}" has invalid value of ' \ | |
'"{1}", expected value "{2}" CRITICAL'.format(args.key, | |
value, | |
args.value) | |
return sensu.output_check(sensu.STATE_CRITICAL, message=message) | |
def _check_salt_conf(conf, conf_type, key, value): | |
""" | |
Validates the salt configuration | |
:conf: path to the salt configuration file | |
:conf_type: configuration type of 'master' or 'minion' | |
:key: key to get in the salt configuration | |
:value: value to compare in the salt configuration | |
:returns: Tuple of True if configuration valid or False if | |
configuration invalid, and the opts[key] value | |
""" | |
try: | |
value = ast.literal_eval(value) | |
except ValueError: | |
pass | |
opts = master_config(conf) if 'master' in conf_type else minion_config(conf) | |
return (True, opts[key]) if opts[key] == value else (False, opts[key]) | |
if __name__ == '__main__': | |
main() | |
This file contains hidden or 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
Validates that the specified master or minion configuration is set as expected.