-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck-builder.py
More file actions
executable file
·97 lines (79 loc) · 2.62 KB
/
Check-builder.py
File metadata and controls
executable file
·97 lines (79 loc) · 2.62 KB
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
# Author - Sagi Yosef
# Creation Date 30.07.2017
#
# This script builds sensu check json file by user input.
#
# --- HOW TO RUN ---
# Copy the script to the monitored server and run the script
#
# python Check-builder.py
#
# *** Notice you have to install python ***
import json
import os
# Check if the OS is Windows or Linux
if (os.name == 'nt'):
CHECK_PATH="C:\\etc\\sensu\\conf.d\\checks\\"
else:
CHECK_PATH="/etc/sensu/conf.d/checks/"
CHECK_EXTENSION=".json"
# Check if directory exists and creates it if not
if not os.path.exists(CHECK_PATH):
os.makedirs(CHECK_PATH)
# Creating check definition
jsCheckJson={}
jsCheckJson['checks']={}
# While user don't want to quit new checks will be created
while True:
# Get the checks uniqe name
strCheckName=raw_input("Enter check uniqe name: ")
jsCheckJson['checks'][strCheckName]={}
# Get checks command
print "Enter the check's command"
print "-------------------------"
jsCheckJson['checks'][strCheckName]['command']=raw_input()
jsCheckJson['checks'][strCheckName]['subscribers']=[]
# Get list of subscribers
print "Enter subscribers. \nFor exit press exit"
strSubscribers=raw_input()
# While user didn't enter exit, subscribers will be added
while (strSubscribers != "exit"):
jsCheckJson['checks'][strCheckName]['subscribers'].append(strSubscribers)
strSubscribers=raw_input()
# Getting the checks interval
while True:
try:
# Get checks interval time in seconds
jsCheckJson['checks'][strCheckName]['interval']=int(raw_input("Enter interval time (seconds): "))
except ValueError:
print("Please enter integer")
continue
else:
# Interval was successfully parsed!
break
try:
# Check if check is stand alone
bIsStandalone=raw_input("Is standalone? (true/false) [t/f]: ")
# Check if user entered t or f
if (bIsStandalone == "t"):
bIsStandalone=True
elif(bIsStandalone == "f"):
bIsStandalone=False
jsCheckJson['checks'][strCheckName]['standalone']=bool(bIsStandalone)
except ValueError:
print("Please enter boolean")
try:
# Creating a file and dumping the check's json
fCheck=open(CHECK_PATH + strCheckName + CHECK_EXTENSION, 'w+')
json.dump(jsCheckJson, fCheck, indent=2, sort_keys=True)
fCheck.close()
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
# Print the check final json
print json.dumps(jsCheckJson, indent=2, sort_keys=True)
# Clear the dictonary
jsCheckJson['checks'].clear()
strUserChoice=raw_input('Continue to another check? y/n: ')
# Check if user want to exit
if (strUserChoice == "n"):
break