This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nebula.py
executable file
·132 lines (104 loc) · 3.33 KB
/
nebula.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
#!/usr/bin/env python3
import os
import sys
import time
from nxtools import logging, log_traceback, critical_error
if __name__ == "__main__":
print()
from nx.core.common import config
from nx.plugins import get_plugin_path
#
# Start agents only if this script is executed (not imported)
#
if __name__ == "__main__":
from nx.storage_monitor import StorageMonitor
from nx.service_monitor import ServiceMonitor
from nx.system_monitor import SystemMonitor
# Crontab sync
cron_file = "/etc/cron.d/nebula"
def sync_cron():
plugin_path = get_plugin_path("cron")
if not plugin_path:
return False
cron_source = os.path.join(plugin_path, config["host"])
if not os.path.exists(cron_source):
return False
if not os.path.isdir("/etc/cron.d"):
os.makedirs("/etc/cron.d")
if not os.path.exists(cron_file):
pass
elif os.path.getmtime(cron_file) < os.path.getmtime(cron_source):
pass
else:
return True
logging.info("Installing new crontab")
src = open(cron_source).read()
with open(cron_file, "w") as f:
f.write(src)
return True
def del_cron():
if os.path.exists(cron_file):
logging.info("Removing crontab")
os.remove(cron_file)
# Agents
def are_running(agents):
return any([agent.is_running for agent in agents])
def shutdown(agents):
logging.info("Shutting down agents")
for agent in agents:
agent.shutdown()
while are_running(agents):
time.sleep(0.5)
agent_list = {
"storage-monitor": StorageMonitor,
"service-monitor": ServiceMonitor,
"system-monitor": SystemMonitor,
}
agents = []
for agent_name, Agent in agent_list.items():
env_name = f"NEBULA_DISABLE_{agent_name.upper().replace('-', '_')}"
if os.environ.get(env_name) or f"--disable-{agent_name}" in sys.argv:
os.environ[env_name] = "1"
logging.info(f"Agent {agent_name} is disabled")
continue
try:
agents.append(Agent())
except Exception:
log_traceback()
shutdown(agents)
critical_error(f"Unable to start {Agent.__name__}")
# Main loop
while True:
try:
sync_cron() or del_cron()
time.sleep(10)
except KeyboardInterrupt:
break
except Exception:
log_traceback()
time.sleep(10)
# Shutdown (CTRL+C)
print()
try:
logging.warning("Shutting down nebula. Please wait...")
shutdown(agents)
logging.goodnews("Exiting gracefully")
sys.exit(0)
except KeyboardInterrupt:
print()
logging.warning("Immediate shutdown enforced. This may cause problems")
sys.exit(1)
else:
# This is a very dirty hack to keep old plugins working.
import os # noqa
import sys # noqa
import json # noqa
import time # noqa
from nxtools import * # noqa
from nx.db import * # noqa
from nx.plugins import * # noqa
from nx.core.common import * # noqa
from nx.helpers import * # noqa
from nx.objects import * # noqa
from nx.cache import * # noqa
from nx.messaging import * # noqa