-
Notifications
You must be signed in to change notification settings - Fork 2
/
imon-configure
executable file
·277 lines (254 loc) · 11.2 KB
/
imon-configure
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/python3
#
# Build configuration file for imon
#
version="V1.0"
import datetime
import ipaddress
import json
import os
import readline
import re
def perrorexit(emsg):
"""
Print the message and exit the program
"""
raise SystemExit(emsg)
def getinput(prompt):
try:
str = input(prompt).lower()
except (EOFError, KeyboardInterrupt):
print("")
exit(0)
except:
print("")
str = ""
return str
def askyn(question, dfans=""):
"""
Prompt for an answer with the given question.
Returns True if yes, False if anything else
"""
yni = getinput(question)
if yni == "" and dfans != "": yni = dfans.lower()
if yni != "" and "yes".startswith(yni): return True
return False
def askinput(question, dfans=""):
prompt = "{}: ".format(question) if dfans == "" else "{} [{}]: ".format(question, dfans)
str = getinput(prompt)
if str == "": str = dfans
return str
def isdnsname(domain):
return re.match('''
(?=^.{,253}$) # max. length 253 chars
(?!^.+\.\d+$) # TLD is not fully numerical
(?=^[^-.].+[^-.]$) # doesn't start/end with '-' or '.'
(?!^.+(\.-|-\.).+$) # levels don't start/end with '-'
(?:[a-z\d-] # uses only allowed chars
{1,63}(\.|$)) # max. level length 63 chars
{2,127} # max. 127 levels
''', domain, re.X | re.I)
def isipaddr(address):
try:
ip = ipaddress.ip_address(address)
return True
except ValueError:
return False
def getfile(prompt, dfans=""):
while True:
str = askinput(prompt)
if str != "":
if os.path.exists(str):
if os.access(str, os.X_OK): break
print("% File '{}' is not executable".format(str))
else:
print("% File '{}' does not exist".format(str))
else:
break
return str
def getint(prompt, dfans=""):
while True:
str = askinput(prompt, dfans)
try:
n = int(str)
if n > 0: break
print("% Must be greater than 0")
except:
print("% Enter an integer")
return n
def getfloat(prompt, dfans=""):
while True:
str = askinput(prompt, dfans)
try:
n = float(str)
if n > 0.0: break
print("% Must be greater than 0.0")
except:
print("% Enter an integer or floating point number")
return n
def getyn(prompt, dfans=""):
while True:
yni = askinput(prompt, dfans)
return "yes".startswith(yni)
def getstring(prompt, dfans="", optional=False):
while True:
str = askinput(prompt, dfans)
if optional or str != "": break
print("% Value required")
return str
def getipaddr(prompt, dfans="", optional=False):
while True:
ip = askinput(prompt, dfans)
if optional and ip == "": break
if ip != "" and isipaddr(ip): break
print("% IP Address required")
return ip
def isiplist(str):
le = str.split(",")
for hn in le:
if not (isdnsname(hn) or isipaddr(hn)):
return False
return True
def getiplist(prompt, dfans="", optional=False):
while True:
ip = askinput(prompt, dfans)
if ip != "" and isiplist(ip): break
if optional and ip == "": break
print("% A DNS Name/IP Address is ill-formed")
return ip
def getdnsname(prompt, dfans="", optional=False):
while True:
dn = askinput(prompt, dfans)
if dn != "" and (isdnsname(dn) or isipaddr(dn)): break
if optional and dn == "": break
print("% A DNS name or IP address is required")
return dn
def ckinterval(pd, section):
try:
f = float(pd.db[section]['interval'])
except:
f = 0.0
if f == 0.0: perrorexit("? Invalid value '{}' for {}".format(pd.db[section][iname], iname))
pd.db[section]['interval'] = f
return
def doconfigure(mon, pd):
for item in pd.db[mon]:
if item != "enable":
thisdefault = pd.db[mon][item]
thistype = pd.pdtype[mon][item][0]
thisprompt = "{} ({})".format(pd.pdtype[mon][item][1], item)
if thistype == "tf":
pd.db[mon][item] = getyn(thisprompt, "N")
elif thistype == "int":
pd.db[mon][item] = getint(thisprompt, thisdefault)
elif thistype == "float":
pd.db[mon][item] = getfloat(thisprompt, thisdefault)
elif thistype == "required-str":
pd.db[mon][item] = getstring(thisprompt, thisdefault, optional=False)
elif thistype == "optional-str":
pd.db[mon][item] = getstring(thisprompt, thisdefault, optional=True)
elif thistype == "required-ipaddr":
pd.db[mon][item] = getipaddr(thisprompt, optional=False)
elif thistype == "optional-ipaddr":
pd.db[mon][item] = getipaddr(thisprompt, optional=True)
elif thistype == "optional-dns":
pd.db[mon][item] = getdnsname(thisprompt, optional=True)
elif thistype == "required-dns":
pd.db[mon][item] = getdnsname(thisprompt, optional=False)
elif thistype == "iplist":
pd.db[mon][item] = getiplist(thisprompt, optional=True)
elif thistype == "file":
pd.db[mon][item] = getfile(thisprompt)
else:
pd.db[mon][item] = True
return
def runconfigure(pd):
for mon in ['ddns', 'updown', 'failover', 'ping']:
if askyn("* Enable the '{}' monitor? [y/N]: ".format(mon)):
doconfigure(mon, pd)
if pd.db[mon]['enable']:
ckinterval(pd, mon)
if mon == "ddns":
pass
#if pd.db['global']['action'] == "":
#print("? ddns monitor requires action script")
#return False
elif mon == "updown":
if pd.db['updown']['monitorip'] == "":
print("? updown monitor requires an IP Address to monitor")
return False
pass
elif mon == "failover":
missing = ""
for s in ['primary-if', 'primary-gw', 'standby-if', 'standby-gw']:
if not s in pd.db['failover']:
missing = "{} {}".format(missing, s)
else:
if pd.db['failover'][s] == "": missing = "{} {}".format(missing, s)
if missing != "":
print("? Missing settings for failover monitor: {}".format(missing))
return False
elif mon == "ping":
#if pd.db['global']['action'] == "":
#print("? ping monitor requires action script")
#return False
if pd.db['ping']['monitorip'] == "":
print("? ping monitor requires an IP Address to monitor")
return False
return True
class pdat:
def __init__(self):
self.db = {}
def main():
# This gets written to the instance configuration file
pd = pdat()
pd.db = { "global":{"action":"", "datefmt":"%Y-%m-%d %H:%M:%S"},\
"ddns":{"enable":False, "interval":10, "pingcount":2, "pingwait":1},\
"updown":{"enable":False, "interval":10, "pingcount":2, "pingwait":1, "monitorip":"", "addping":"", "dnsserver":"", "dnslookup":""},\
"failover":{"enable":False, "interval":10, "pingcount":2, "pingwait":1, "prefer-primary":False, "metric":201,\
"primary-if":"", "primary-name":"", "primary-gw":"", "primary-ping":"", "primary-postping":"",\
"standby-if":"", "standby-name":"", "standby-gw":"", "standby-ping":"", "standby-postping":""},\
"ping":{"enable":False, "interval":10, "pingcount":2, "pingwait":1, "monitorip":""} }
# This parallels pd, but has additional info for each item for processing
pd.pdtype = {"global":{"action":["file", "Full path to action script"], "datefmt":["none",""]},\
"ddns":{"enable":["tf","SNH"], "interval":["float","Interval between checks"],\
"pingcount":["int","Ping count"], "pingwait":["int","Ping wait time"] },\
"updown":{"enable":["tf","SNH"], "interval":["float","Interval between checks"],\
"pingcount":["int","Ping count"], "pingwait":["int","Ping wait time"],\
"monitorip":["required-dns","Name/IP address to monitor"], "addping":["iplist","Additional IP addresses to ping"],\
"dnsserver":["optional-dns","Name/IP address of DNS server"],"dnslookup":["optional-dns","DNS name to lookup"]},\
"failover":{"enable":["tf","SNH"], "interval":["float","Interval between checks"], "prefer-primary":["tf","Prefer primary network"],\
"pingcount":["float","Ping count"], "pingwait":["float","Ping wait time"], "metric":["int", "Network default route metric"],\
"primary-if":["required-str","Primary network interface name"], "primary-name":["required-str", "Primary network name"],\
"primary-gw":["required-ipaddr","Primary network gateway IP Address"],\
"primary-ping":["optional-dns", "Name/IP Address to check if Primary network is up"],\
"primary-postping":["optional-dns","Name/IP Address to ping after failover to Primary"],\
"standby-if":["required-str","Standby network interface name"], "standby-name":["required-str","Standby network name"],\
"standby-gw":["required-ipaddr","Standby network gateway IP Address"],\
"standby-ping":["optional-dns", "Name/IP Address to check if Standby network is up"],\
"standby-postping":["optional-dns","Name/IP Address to ping after failover to Standby"]},\
"ping":{"enable":["tf","SNH"], "interval":["float","Interval between pings"], "monitorip":["required-dns","Name/IP Address to ping"],\
"pingcount":["int","Ping count"], "pingwait":["int","Ping wait time"] } }
print("\nBuild imon configuration file")
print("See https://github.com/gitbls/imon for configuration details\n")
instance = askinput("Instance name",dfans="main")
cfnote = askinput("Note to add to config file", dfans="")
doconfigure("global", pd)
runconfigure(pd)
pd.db['global']['instance'] = instance
pd.db['global']['note'] = cfnote
pd.db['global']['imon-configure-version'] = version
pd.db['global']['imon-configure-created'] = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
#
# All done!
#
cfile = "/etc/default/imon-{}".format(instance)
if not askyn("\nWrite configuration file '{}'? [Y/n] ".format(cfile), dfans="Y"): return
with open(cfile, "w") as jf:
json.dump(pd.db, jf, indent=4, sort_keys=False)
jf.write("\n") #Add final eol lol
print("Wrote '{}'".format(cfile))
return
if __name__ == "__main__":
main()
exit(0)