-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_text2kuma.py
executable file
·702 lines (647 loc) · 21.1 KB
/
sync_text2kuma.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#!/usr/bin/env python3
import os
import time
import json
import re
import sys
#from dotenv import load_dotenv
import configparser
from uptime_kuma_api import UptimeKumaApi, MonitorType
# default values
interval_default = 120
retryInterval_default = 120
resendInterval_default = 60
maxretries_default = 3
timeout_default = 10 # seconds after the monitor is considered as down
expiryNotification_default = False
#
server_tags_queried = False
#
api_timeout=5 # seconds Default is 10
wait_events=2 # seconds Default is 0.2
def edit_monitor_with_retry(func, id, **kwargs):
global api, username, password
success = False
while not success:
try:
if func == "add":
if verbose:
print(f" Add Monitor: '{kwargs['name']}'")
print(json.dumps(kwargs, indent=2))
result = api.add_monitor(**kwargs)
elif func == "edit":
if verbose:
print(f" Edit Monitor: '{id}'")
print(json.dumps(kwargs, indent=2))
result = api.edit_monitor(id, **kwargs)
else:
print("Unknown function")
return 0
success = True
return result["monitorID"]
except Exception as e:
#print( " An exception occurred:", type(e).__name__, "–", e)
#print(f" retrying: {id}")
success = False
lsuccess = False
while not lsuccess:
try:
#print(f" Login again: {username}")
api.login(username, password)
lsuccess = True
except Exception:
#print("Login failed")
time.sleep(2)
lsuccess = False
# If we are here, we have an error
return 0
def server_add_tag(**kwargs):
global api, username, password
if verbose:
print(f"Server:Add Tag {kwargs['name']} with color {kwargs['color']}")
success = False
while not success:
try:
result = api.add_tag(
**kwargs
)
success = True
return result
except Exception as e:
if verbose:
print( " An exception occurred:", type(e).__name__, "–", e)
success = False
lsuccess = False
while not lsuccess:
try:
if verbose:
print(f" Login again: {username}")
api.login(username, password)
lsuccess = True
except Exception:
if verbose:
print("Login failed")
time.sleep(2)
lsuccess = False
def remove_tags(monitor_id, tags):
global api, username, password
if verbose:
print(f" Remove unused tags from Monitor {monitor_id}")
success = False
while not success:
try:
result = api.get_monitor(monitor_id)
#print(json.dumps(result, indent=2))
monitor_tags = result["tags"]
# Find all monitor_tags["name"] which are not in the tags array
for monitor_tag in monitor_tags:
if monitor_tag["name"] not in tags:
print(f" Remove Tag {monitor_tag['name']} with id {monitor_tag['tag_id']} from Monitor {monitor_id}")
api.delete_monitor_tag(
tag_id=monitor_tag["tag_id"],
monitor_id=monitor_id
##value=monitor_tag["name"]
)
success = True
except Exception as e:
#print( " An exception occurred:", type(e).__name__, "–", e)
#print(f" retrying: {id}")
success = False
lsuccess = False
while not lsuccess:
try:
#print(f" Login again: {username}")
api.login(username, password)
lsuccess = True
except Exception:
#print("Login failed")
time.sleep(2)
lsuccess = False
def add_tag(monitor_id, tag_id):
global api, api_timeout, base_url, username, password
if verbose:
print(f" Add Tag {tag_id} to Monitor {monitor_id}")
# Check if the tag is already assigned
success = False
while not success:
try:
result = api.get_monitor(monitor_id)
for monitor_tagid in result["tags"]:
if monitor_tagid["tag_id"] == tag_id:
if verbose:
print(f" Tag {tag_id} already assigned to Monitor {monitor_id}")
return
# If we are here, the tag is not assigned yet
if verbose:
print(f" Add Tags")
result = api.add_monitor_tag(
tag_id=tag_id,
monitor_id=monitor_id
##value="stylite"
)
success = True
except Exception as e:
#print( " An exception occurred:", type(e).__name__, "–", e)
#print(f" retrying: {id}")
success = False
lsuccess = False
while not lsuccess:
try:
#print(f" Login again: {username}")
api.login(username, password)
lsuccess = True
except Exception:
#print("Login failed")
time.sleep(2)
lsuccess = False
# If we are here, we have an error
return 0
def create_group(name):
global api, username, password, groups
if verbose:
print(f" Create Group: {name}")
if name in groups:
if verbose:
print(f" Group {name} already exists with ID {groups[name]}")
return groups[name]
success = False
while not success:
try:
result = api.add_monitor(
type="group",
name=name,
description=f"{name}"
)
if verbose:
print(json.dumps(result, indent=2))
print(f" Group {name} created with ID {result['monitorID']}")
success = True
except Exception as e:
print( " An exception occurred:", type(e).__name__, "–", e)
#print(f" retrying: {id}")
success = False
#api.disconnect()
#api = UptimeKumaApi(base_url ,timeout=api_timeout)
lsuccess = False
while not lsuccess:
try:
#print(f" Login again: {username}")
api.login(username, password)
lsuccess = True
except Exception:
#print("Login failed")
time.sleep(2)
lsuccess = False
monitor_id = result["monitorID"]
groups[name] = monitor_id
return monitor_id
if "-h" in sys.argv:
print(f"Usage: {sys.argv[0]} [-u] [-f input_file] [-c config_file]")
print(" -h: show this help")
print(" -u: update the existing monitors")
print(" -r: remove unused tags")
print(" -c config_file: use the config file (default: SCRIPTNAME.ini)")
print(" -f input_file: use the file input_file as input (default: urls.txt)")
print(" -v: verbose output")
print(" -n: no updates, just show what would be done (dry run)")
exit(0)
verbose = False
if "-v" in sys.argv:
verbose = True
do_updates = False
if "-u" in sys.argv:
do_updates = True
remove_unused_tags = False
if "-r" in sys.argv:
remove_unused_tags = True
try_only = False
if "-n" in sys.argv:
try_only = True
input_file_name = "urls.txt"
if "-f" in sys.argv:
input_file_name = sys.argv[sys.argv.index("-f")+1]
# Create a config_filename wich is the path iand the name of this script without the extension and with .ini
config_file_name = os.path.splitext(sys.argv[0])[0]+".ini"
#print(f"Config File: {config_file_name}")
if "-c" in sys.argv:
config_file_name = sys.argv[sys.argv.index("-c")+1]
if not os.path.exists(config_file_name):
print(f"Config File {config_file_name} not found")
exit(1)
# Read the config file
config = configparser.ConfigParser()
config.read(config_file_name)
# get baseurl form config
base_url = config.get("uptimekuma", "base_url")
username = config.get("uptimekuma", "username")
password = config.get("uptimekuma", "password")
group = config.get("uptimekuma", "default_group", fallback="AutoCheck")
if base_url is None:
print("No BASE_URL given")
exit(1)
if username is None:
print("No USERNAME given")
exit(1)
if password is None:
print("No PASSWORD given")
exit(1)
if verbose:
print(f"Base URL: {base_url}")
print(f"Username: {username}")
#print(f"Password: {password}")
api = UptimeKumaApi(base_url ,timeout=api_timeout, wait_events=wait_events)
#if verbose:
# print(f"API: {api}")
api.login(username, password)
if verbose:
print(f"API: {api}")
monitors = api.get_monitors()
#print(json.dumps(monitors, indent=2))
# Get all Mointors IDs from pages
monitor_id = []
monitor_name = []
monitor_pathname = []
monitor_type = []
for monitor in monitors:
monitor_id.append(monitor["id"])
monitor_name.append(monitor["name"])
monitor_pathname.append(monitor["pathName"])
monitor_type.append(monitor["type"])
if verbose:
print(f"Monitor ID: {monitor['id']}, Name: {monitor['name']}, Type: {monitor['type']}, PathName: {monitor['pathName']}")
# Find all groups in the Instance
groups = {}
for i in range(len(monitor_id)):
if monitor_type[i] == "group":
groups[monitor_name[i]] = monitor_id[i]
if verbose:
print(f" found Group: {monitor_name[i]} with ID {monitor_id[i]}")
#######################################################################################################################
# Read the input file
name = "Undefined"
url = "Undefined"
check_mk_warn_default = 0
check_for_default = ""
check_for = ""
url_suffix = ""
url_suffix_default = ""
#do_special = False
name1 = ""
name2 = ""
warntimes = {}
keywords = {}
keyword_urls = {}
intervals = {}
retryIntervals = {}
resendIntervals = {}
maxretriess = {}
timeouts = {}
expiryNotifications = {}
tags = []
tag_id = {}
with open(input_file_name, 'r') as file:
# Read a line from the ffile until the end of the file
for line in file:
check_mk_hint = "null" # This is the default which gets reported
line = line.strip()
#print(f"Line: {line}")
# If the line starts with a #, then it is a comment
if line.startswith("#"):
continue
# If the line is empty, then skip
if len(line) == 0:
continue
if line.startswith("!"):
# This is a command
# Remove the !
line = line[1:]
parts = line.split(" ")
keyword = parts[0]
rest = " ".join(parts[1:])
if verbose:
print(f"Command: {keyword}, Rest: {rest}")
print(json.dumps(parts, indent=2))
# Check the commands
if keyword == "group":
group = rest
if verbose:
print(f" Group: {group}")
continue
if keyword == "prefix":
prefix = rest
if verbose:
print(f" Prefix: {prefix}")
continue
if keyword == "suffix":
suffix = rest
if verbose:
print(f" Suffix: {suffix}")
continue
if keyword == "keyword_default":
check_for_default = rest
continue
#url_suffix = url_suffix_default
if keyword == "url_suffix_default":
url_suffix_default = rest
continue
#check_for = check_for_default
if keyword == "warn_default":
# Set a new default for the check_mk_warn for the rest of the file
check_mk_warn_default = int(rest)
continue
#check_mk_warn = check_mk_warn_default
if keyword == "interval_default":
interval_default = int(rest)
continue
if keyword == "retryInterval_default":
retryInterval_default = int(rest)
continue
if keyword == "resendInterval_default":
resendInterval_default = int(rest)
continue
if keyword == "maxretries_default":
maxretries_default = int(rest)
continue
if keyword == "timeout_default":
timeout_default = int(rest)
continue
if keyword == "expiryNotification_default":
expiryNotification_default1 = int(rest)
if expiryNotification_default1 == 1:
expiryNotification_default = True
else:
expiryNotification_default = False
continue
if keyword == "tag":
tag = rest
if len(tag) > 0:
tags = []
# Split the tag into parts
parts = tag.split(" ")
for part in parts:
tags.append(part)
else:
# If empty Clear the tag Array
tags = []
continue
# check if we have two argument
if len(parts) <= 2:
print(f"WARNING:Not enough arguments for {keyword}, ignoring line '!{line}'")
#print(json.dumps(parts, indent=2))
continue
else:
# All this keywords have two arguments
# The first one is the search string in name of the check
# The second one is the value we want to set
p1 = parts[1]
p2 = parts[2]
if keyword == "warn":
search = p1
warntime = int(p2)
warntimes[search] = warntime
continue
if keyword == "keyword":
search = p1
keyword = p2
keywords[search] = keyword
continue
if keyword == "keyword_url":
search = p1
keyword = p2
keyword_urls[search] = keyword
continue
if keyword == "interval":
search = p1
interval = int(p2)
intervals[search] = interval
continue
if keyword == "retryInterval":
search = p1
retryInterval = int(p2)
retryIntervals[search] = retryInterval
continue
if keyword == "resendInterval":
search = p1
resendInterval = int(p2)
resendIntervals[search] = resendInterval
continue
if keyword == "maxretries":
search = p1
maxretries = int(p2)
maxretriess[search] = maxretries
continue
if keyword == "timeout":
search = p1
timeout = int(p2)
timeouts[search] = timeout
continue
if keyword == "expiryNotification":
search = p1
expiryNotification1 = int(p2)
if expiryNotification1 == 1:
expiryNotification = True
else:
expiryNotification = False
expiryNotifications[search] = expiryNotification
continue
print(f"Unknown Command: {line}")
if ":" not in line:
#name = line
#do_special = False
name1 = ""
name2 = ""
prefix = line
suffix = ""
# When ein - drin ist verändere den Namen/reihenfolge
#print(f"Group: {line}")
if "-" in line:
# Verstausche die beiden Teile und setze den suffix
parts = line.split("-",1)
if len(parts) > 0:
#print(f" parts: {parts}")
name1 = parts[0].strip()
name2 = parts[1].strip()
prefix = name2
suffix = name1
#do_special = True
#print(f" name1: {name1}, name2: {name2}")
continue
else:
# Wenn ein : enthalten ist, dann ist es ein URL zum checkem
parts = line.split(":",1)
# Get the name and the url
check = parts[0].strip()
rest = parts[1].strip()
# Split the rest into URL and keyword
parts = rest.split(" ",1)
if len(parts) > 1:
url = parts[0].strip()
check_for = parts[1].strip()
else:
url = rest.strip()
check_for = ""
# Does check_for contain string like "(299ms)"? check with a regex
matches = re.findall(r'\((\d+)ms\)', check+check_for)
if matches:
check_mk_warn = int(matches[0])
pattern = r'\s*\(\d+ms\)'
check_for = re.sub(pattern, '', check_for).strip()
check = re.sub(pattern, '', check).strip()
if not url.startswith("http"):
continue
if url.startswith("https:"):
expiryNotification = True
else:
expiryNotification = False
#if check_for == "":
check_mk_warn = check_mk_warn_default
check_for_temp = ""
check_for = check_for_default
url_suffix = url_suffix_default
interval = interval_default
retryInterval = retryInterval_default
resendInterval = resendInterval_default
maxretries = maxretries_default
timeout = timeout_default
expiryNotification = expiryNotification_default
# Loop over all dictionaries
for dictionary in [warntimes, keywords, keyword_urls, intervals, retryIntervals, resendIntervals, maxretriess, timeouts, expiryNotifications]:
for search in dictionary:
if search in check: # if check.contains(search)
if dictionary is warntimes:
check_mk_warn = int(dictionary[search])
elif dictionary is keywords:
check_for_temp = dictionary[search]
elif dictionary is keyword_urls:
url_suffix = dictionary[search]
elif dictionary is intervals:
interval = int(dictionary[search])
elif dictionary is retryIntervals:
retryInterval = int(dictionary[search])
elif dictionary is resendIntervals:
resendInterval = int(dictionary[search])
elif dictionary is maxretriess:
maxretries = int(dictionary[search])
elif dictionary is timeouts:
timeout = int(dictionary[search])
elif dictionary is expiryNotifications:
expiryNotification = expiryNotifications[search]
break
if check_for == "":
# Kein keyword speziell für diesen check gefunden
check_for = check_for_temp
# End of loop over all dictionaries
if verbose:
print(f" URL: '{url}', Check for: '{check_for}', URL Suffix: '{url_suffix}' (default: '{url_suffix_default}')")
if url_suffix == "":
url_suffix = url_suffix_default
if url_suffix != "":
# Strip a trailing / from the url
#if url.endswith("/"):
# url = url[:-1]
# strip a leading / from the url_suffix
if url_suffix.startswith("/"):
url_suffix = url_suffix[1:]
url = url + "/" + url_suffix
if verbose:
print(f" -> URL: {url}")
check_mk_hint = ""
if check_mk_warn > 0:
check_mk_hint = f"({check_mk_warn}ms)"
#print(f"Name: {check}, URL: {url}, Check for: {check_for}, Interval: {interval}, retryInterval: {retryInterval}, resendInterval: {resendInterval}, maxretries: {maxretries}, timeout: {timeout}, expiryNotification: {expiryNotification}, check_mk_hint: {check_mk_hint}")
#print(f"name1: {name1}, name2: {name2}")
myname = check
if prefix != "":
myname = prefix + " - " + myname
if suffix != "":
myname = myname + " - " + suffix
#myname = f"{name} - {check}"
#if do_special:
# myname = f"{check} - {name2} - {name1}"
# print(f" do_special: {myname}")
check_type=MonitorType.HTTP
if check_for != "":
check_type=MonitorType.KEYWORD
# Find the group ID
group_id = 0
if group in groups:
group_id = groups[group]
else:
if verbose:
print(f"Group {group} not found")
if not try_only:
group_id = create_group(group)
if group_id == 0:
print(f"Group {group} could not be created")
exit(1)
# Find the tag IDs
if server_tags_queried == False:
server_tags = api.get_tags()
for server_tag in server_tags:
if verbose:
print(f" Server Tag: {server_tag['name']} with ID {server_tag['id']}")
tag_id[server_tag["name"]] = server_tag["id"]
server_tags_queried = True
# Add the tags if the tags array which does not exist yet
for tag in tags:
if tag not in tag_id:
result = server_add_tag(
name=tag,
color="#900000"
)
tag_id[tag] = result["id"]
if verbose:
print(f" Tag {tag} added with ID {tag_id[tag]}")
# Check if the Monitor already exists
monID = 0
if myname in monitor_name:
#id=monitor_name.index(myname)
id=monitor_id[monitor_name.index(myname)]
#continue
print(f" Monitor edit: '{myname}' already exists")
if not do_updates:
print(f" Not updating '{myname}'")
continue
if not try_only:
monID = edit_monitor_with_retry("edit", id,
type=check_type,
name=myname,
url=url,
parent=group_id,
keyword=check_for,
interval=interval,
retryInterval=retryInterval,
resendInterval=resendInterval,
maxretries=maxretries,
expiryNotification=expiryNotification,
timeout=timeout,
description=f"Changed on {time.strftime('%Y-%m-%d %H:%M:%S')} by sync_stylite.py",
hostname=check_mk_hint # This is visible in the metrics api, we use it to give some hints to check_mk/omd
)
else:
print(f" Monitor add: '{myname}'")
if not try_only:
monID = edit_monitor_with_retry("add", 0,
type=check_type,
name=myname,
url=url,
parent=group_id,
keyword=check_for,
interval=interval,
retryInterval=retryInterval,
resendInterval=resendInterval,
maxretries=maxretries,
expiryNotification=expiryNotification,
timeout=timeout,
description=f"Changed on {time.strftime('%Y-%m-%d %H:%M:%S')} by sync_stylite.py",
hostname=check_mk_hint # This is visible in the metrics api, we use it to give some hints to check_mk/omd
)
if verbose:
print(f" Monitor ID: {monID}")
if monID > 0:
# Add the tags
for tag in tags:
add_tag(monID, tag_id[tag])
if remove_unused_tags:
remove_tags(monID, tags)
api.disconnect()