-
Notifications
You must be signed in to change notification settings - Fork 16
/
salt.py
executable file
·54 lines (45 loc) · 1.24 KB
/
salt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
import os
import sys
import subprocess
from optparse import OptionParser
import yaml
import re
# Command Line Parsing Arguments
cmd_parser = OptionParser(version="0.1")
cmd_parser.add_option("-e", "--exclude", type="string", action="store", dest="exclude",
help="Exclude regex", metavar="Exclude")
(cmd_options, cmd_args) = cmd_parser.parse_args()
command = ['salt-run', 'manage.status']
down_servers = ""
try:
child = subprocess.Popen(command, stdout=subprocess.PIPE)
output = child.communicate()[0]
except OSError, e:
print "UNKNOWN: salt doesn't appear to be installed"
exit(3)
yaml_out = yaml.load(output)
try:
if not yaml_out['down'] and not yaml_out['up']:
print "UNKNOWN: Server has no minions attached to it"
exit(3)
except:
print "UNKNOWN: Invalid JSON detected"
exit(3)
try:
for server in yaml_out['down']:
if cmd_options.exclude:
match = re.match(cmd_options.exclude, server)
if match is None:
down_servers += server + " "
else:
down_servers += server + " "
except:
print "OK"
exit(0)
if down_servers:
print "CRITICAL: " + down_servers
exit(2)
else:
print "OK"
exit(0)