-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_salt_minions.py
65 lines (54 loc) · 1.54 KB
/
check_salt_minions.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
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
'''
Project : Icinga/Nagios salt-minion down check
Version : 0.1
Author : Will Platnick <[email protected]>
Summary : This program is an icinga/nagios plugin that is run from the salt-master to return a list of down minions
Dependency : Python 2.6, Linux, Icinga/Nagios
Usage :
```````
shell> python check_salt_minions.py
'''
import os
import sys
import subprocess
from optparse import OptionParser
import yaml
import re
# Command Line Parsing Arguements
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)