forked from RedHatSatellite/satellite-sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satellite-sanity
executable file
·127 lines (111 loc) · 4.07 KB
/
satellite-sanity
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import satellite_sanity
from satellite_sanity.config import logger
import argparse
parser = argparse.ArgumentParser(description="Check Red Hat Satellite sanity")
parser.add_argument("-d", "--debug", help="enable debug messages", action="store_true")
group = parser.add_mutually_exclusive_group()
group.add_argument("--list-tags", help="list available tags", action="store_true")
group.add_argument("--list-rules", help="list available rules", action="store_true")
parser.add_argument("-t", "--tags", help="run rules tagged with one of these tags (comma separated)")
parser.add_argument("-r", "--rules", help="run only these rules (comma separated)")
parser.add_argument("-s", "--save", help="just save all collectable data and quit", action="store_true")
parser.add_argument("-i", "--from_dir", help="run rules on data in given directory")
parser.add_argument("-f", "--force", help="run even if checks for selected tags failed", action="store_true")
parser.add_argument("-n", "--nagios-plugin", help="run as nagios plugin (exit code and output set appropriately)", action="store_true")
args = parser.parse_args()
# Configure logging
if args.debug:
logger.setLevel(satellite_sanity.config.L_DEBUG)
else:
logger.setLevel(satellite_sanity.config.L_ERROR)
# Determina tags we are going to use
if args.tags:
tags = args.tags.split(',')
else:
tags = ['general']
if not args.list_tags and not args.save and not args.nagios_plugin:
print "Selected tag(s): %s" % ', '.join(tags)
# Parse rules we are supposed to run if any
if args.rules:
rules = args.rules.split(',')
print "Selected rule(s): %s" % ', '.join(rules)
else:
rules = []
# Create object to work with rules
r = satellite_sanity.plugins.rules()
# Handle --list-tags funcionality
if args.list_tags:
for t in r.list_tags():
print t
sys.exit(0)
# Handle --list-rules funcionality
if args.list_rules:
for r in r.list_rules(tags):
print "%s (%s); tags: %s" % (r['name'], r['label'], ', '.join(r['tags']))
sys.exit(0)
# Create object to interface with data (either from directory or live system)
if args.from_dir:
data = satellite_sanity.input_data.InputData(args.from_dir)
else:
data = satellite_sanity.input_data.InputData()
# Handle --save funcionality
if args.save:
data_dir = data.save()
print "Saved to %s" % data_dir
sys.exit(0)
# Load checks and check that we can run rules with given tags
c = satellite_sanity.plugins.checks()
passed, failed = c.check(tags, data)
if len(failed) > 0:
for tag, why in failed.iteritems():
print >>sys.stderr, "Prerequisites for rules tagged with %s tag were not met:" % tag
for w in why:
print >>sys.stderr, " %s" % w
if not args.force:
print >>sys.stderr, "ERROR: Some prerequisities were not met. Exiting. Maybe you want to add '--force'?"
sys.exit(1)
# Load rules and run them
failed = 0
skipped = 0
passed = 0
unknown = 0
for r in r.run(tags, rules, data):
if r['status'] == 'FAIL':
result = satellite_sanity.output.color_fail('FAIL')
failed += 1
elif r['status'] == 'SKIP':
result = satellite_sanity.output.color_skip('SKIP')
skipped += 1
elif r['status'] == 'PASS':
result = satellite_sanity.output.color_pass('PASS')
passed += 1
else:
logger.error("Failed to understand to result of %s" % r['status'])
unknown += 1
continue
if not args.nagios_plugin:
print "[ %s ] %s (%s)" % (result, r['name'], r['label'])
if r['result']:
for l in r['text'].split("\n"):
print " %s" % l
if args.nagios_plugin:
output = "Satellite sanity results: passed: %s, skipped: %s, failed: %s, unknown: %s" % (passed, skipped, failed, unknown)
if failed + skipped + unknown == 0 and passed > 0:
print "OK- %s | %s" % (output, output)
sys.exit(0)
elif failed == 0 and skipped + unknown > 0:
print "WARNING- %s | %s" % (output, output)
sys.exit(1)
elif failed > 0:
print "CRITICAL- %s | %s" % (output, output)
sys.exit(2)
else:
print "UNKNOWN- %s | %s" % (output, output)
sys.exit(3)
else:
if failed > 100:
failed = 100
sys.exit(failed)