|
1 | | -from swagDocker import SwagDocker |
2 | | -from swagUptimeKuma import SwagUptimeKuma |
3 | | -import sys |
4 | | -import argparse |
5 | | -import os |
6 | | - |
7 | | - |
8 | | -def parseCommandLine(): |
9 | | - """ |
10 | | - Different application behavior if executed from CLI |
11 | | - """ |
12 | | - parser = argparse.ArgumentParser() |
13 | | - parser.add_argument('-purge', action='store_true') |
14 | | - args = parser.parse_args() |
15 | | - |
16 | | - if (args.purge == True): |
17 | | - swagUptimeKuma.purgeData() |
18 | | - swagUptimeKuma.disconnect() |
19 | | - sys.exit(0) |
20 | | - |
21 | | - |
22 | | -def addOrUpdateMonitors(domainName, swagContainers): |
23 | | - for swagContainer in swagContainers: |
24 | | - containerConfig = swagDocker.parseContainerLabels( |
25 | | - swagContainer.labels, ".monitor.") |
26 | | - containerName = swagContainer.name |
27 | | - monitorData = swagUptimeKuma.parseMonitorData( |
28 | | - containerName, domainName, containerConfig) |
29 | | - |
30 | | - if (not swagUptimeKuma.monitorExists(containerName)): |
31 | | - swagUptimeKuma.addMonitor(containerName, domainName, monitorData) |
| 1 | +from auto_uptime_kuma.config_service import ConfigService |
| 2 | +from auto_uptime_kuma.uptime_kuma_service import UptimeKumaService |
| 3 | +from auto_uptime_kuma.docker_service import DockerService |
| 4 | +from auto_uptime_kuma.log import Log |
| 5 | +import sys, os |
| 6 | + |
| 7 | + |
| 8 | +def add_or_update_monitors( |
| 9 | + docker_service: DockerService, |
| 10 | + config_service: ConfigService, |
| 11 | + uptime_kuma_service: UptimeKumaService, |
| 12 | +): |
| 13 | + for container in docker_service.get_swag_containers(): |
| 14 | + container_config = docker_service.parse_container_labels( |
| 15 | + container.labels, ".monitor." |
| 16 | + ) |
| 17 | + container_name = container.name |
| 18 | + monitor_data = uptime_kuma_service.build_monitor_data( |
| 19 | + container_name, container_config |
| 20 | + ) |
| 21 | + |
| 22 | + if not uptime_kuma_service.monitor_exists(container_name): |
| 23 | + uptime_kuma_service.create_monitor(container_name, container_config) |
32 | 24 | else: |
33 | | - swagUptimeKuma.updateMonitor( |
34 | | - containerName, domainName, monitorData) |
35 | | - |
36 | | - |
37 | | -def getMonitorsToBeRemoved(swagContainers, apiMonitors): |
38 | | - # Monitors to be removed are those that no longer have an existing container |
39 | | - # Monitor <-> Container link is done by comparing the container name with the monitor swag tag value |
40 | | - existingMonitorNames = [swagUptimeKuma.getMonitorSwagTagValue( |
41 | | - monitor) for monitor in apiMonitors] |
42 | | - existingContainerNames = [container.name for container in swagContainers] |
43 | | - |
44 | | - monitorsToBeRemoved = [ |
45 | | - containerName for containerName in existingMonitorNames if containerName not in existingContainerNames] |
46 | | - return monitorsToBeRemoved |
| 25 | + if not config_service.config_exists(container_name): |
| 26 | + Log.info( |
| 27 | + f"Monitor '{monitor_data['name']}' for container '{container_name}'" |
| 28 | + " exists but no preset config found, generating from scratch" |
| 29 | + ) |
| 30 | + config_service.create_config(container_name, monitor_data) |
| 31 | + uptime_kuma_service.edit_monitor(container_name, monitor_data) |
| 32 | + |
| 33 | + |
| 34 | +def delete_removed_monitors( |
| 35 | + docker_service: DockerService, uptime_kuma_service: UptimeKumaService |
| 36 | +): |
| 37 | + Log.info("Searching for Monitors that should be deleted") |
| 38 | + # Monitors to be deleted are those that no longer have an existing container |
| 39 | + # Monitor <-> Container link is done by comparing the container name |
| 40 | + # with the monitor swag tag value |
| 41 | + existing_monitor_names = [ |
| 42 | + uptime_kuma_service.get_monitor_swag_tag_value(monitor) |
| 43 | + for monitor in uptime_kuma_service.monitors |
| 44 | + ] |
| 45 | + existing_container_names = [ |
| 46 | + container.name for container in docker_service.get_swag_containers() |
| 47 | + ] |
| 48 | + |
| 49 | + monitors_to_be_deleted = [ |
| 50 | + containerName |
| 51 | + for containerName in existing_monitor_names |
| 52 | + if containerName not in existing_container_names |
| 53 | + ] |
| 54 | + |
| 55 | + monitors_to_be_deleted = list(filter(None, monitors_to_be_deleted)) |
| 56 | + |
| 57 | + uptime_kuma_service.delete_monitors(monitors_to_be_deleted) |
| 58 | + |
| 59 | + |
| 60 | +def delete_removed_groups(uptime_kuma_service: UptimeKumaService): |
| 61 | + Log.info("Searching for Groups that should be deleted") |
| 62 | + # Groups to be deleted are those that no longer have any child Monitors |
| 63 | + existing_monitor_group_ids = [ |
| 64 | + monitor["parent"] for monitor in uptime_kuma_service.monitors |
| 65 | + ] |
| 66 | + |
| 67 | + # remove empty values |
| 68 | + existing_monitor_group_ids = list(filter(None, existing_monitor_group_ids)) |
| 69 | + # get unique values |
| 70 | + existing_monitor_group_ids = list(set(existing_monitor_group_ids)) |
| 71 | + |
| 72 | + groups_to_be_deleted = [] |
| 73 | + |
| 74 | + for group in uptime_kuma_service.groups: |
| 75 | + if group["id"] not in existing_monitor_group_ids: |
| 76 | + groups_to_be_deleted.append(group["name"]) |
| 77 | + |
| 78 | + uptime_kuma_service.delete_groups(groups_to_be_deleted) |
| 79 | + |
| 80 | + |
| 81 | +def execute_cli_mode( |
| 82 | + config_service: ConfigService, uptime_kuma_service: UptimeKumaService |
| 83 | +): |
| 84 | + Log.info("Mod was executed from CLI. Running manual tasks.") |
| 85 | + args = config_service.get_cli_args() |
| 86 | + if args.purge: |
| 87 | + uptime_kuma_service.purge_data() |
| 88 | + |
| 89 | + config_service.purge_data() |
| 90 | + if args.monitor: |
| 91 | + Log.info(f"Requesting data for Monitor '{args.monitor}'") |
| 92 | + print(uptime_kuma_service.get_monitor(args.monitor)) |
| 93 | + |
| 94 | + uptime_kuma_service.disconnect() |
47 | 95 |
|
48 | 96 |
|
49 | 97 | if __name__ == "__main__": |
50 | | - url = os.environ['UPTIME_KUMA_URL'] |
51 | | - username = os.environ['UPTIME_KUMA_USERNAME'] |
52 | | - password = os.environ['UPTIME_KUMA_PASSWORD'] |
53 | | - domainName = os.environ['URL'] |
54 | | - |
55 | | - swagDocker = SwagDocker("swag.uptime-kuma") |
56 | | - swagUptimeKuma = SwagUptimeKuma(url, username, password) |
57 | | - |
58 | | - parseCommandLine() |
59 | | - |
60 | | - swagContainers = swagDocker.getSwagContainers() |
61 | | - |
62 | | - addOrUpdateMonitors(domainName, swagContainers) |
63 | | - |
64 | | - monitorsToBeRemoved = getMonitorsToBeRemoved( |
65 | | - swagContainers, swagUptimeKuma.apiMonitors) |
66 | | - swagUptimeKuma.deleteMonitors(monitorsToBeRemoved) |
67 | | - |
68 | | - swagUptimeKuma.disconnect() |
| 98 | + Log.init("mod-auto-uptime-kuma") |
| 99 | + |
| 100 | + url = os.environ["UPTIME_KUMA_URL"] |
| 101 | + username = os.environ["UPTIME_KUMA_USERNAME"] |
| 102 | + password = os.environ["UPTIME_KUMA_PASSWORD"] |
| 103 | + domainName = os.environ["URL"] |
| 104 | + |
| 105 | + configService = ConfigService(domainName) |
| 106 | + uptimeKumaService = UptimeKumaService(configService) |
| 107 | + dockerService = DockerService("swag.uptime-kuma") |
| 108 | + is_connected = uptimeKumaService.connect(url, username, password) |
| 109 | + |
| 110 | + if not is_connected: |
| 111 | + sys.exit() |
| 112 | + |
| 113 | + uptimeKumaService.load_data() |
| 114 | + if uptimeKumaService.default_notifications: |
| 115 | + notification_names = [ |
| 116 | + f"{notification['id']}:{notification['name']}" |
| 117 | + for notification in uptimeKumaService.default_notifications |
| 118 | + ] |
| 119 | + Log.info( |
| 120 | + f"The following notifications are enabled by default: {notification_names}" |
| 121 | + ) |
| 122 | + |
| 123 | + if configService.is_cli_mode(): |
| 124 | + execute_cli_mode(configService, uptimeKumaService) |
| 125 | + sys.exit() |
| 126 | + |
| 127 | + add_or_update_monitors(dockerService, configService, uptimeKumaService) |
| 128 | + |
| 129 | + # reload data after the sync above |
| 130 | + uptimeKumaService.load_data() |
| 131 | + # cleanup |
| 132 | + delete_removed_monitors(dockerService, uptimeKumaService) |
| 133 | + delete_removed_groups(uptimeKumaService) |
| 134 | + |
| 135 | + uptimeKumaService.disconnect() |
0 commit comments