start_agent.py
2.43 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
98
99
100
101
102
#!/usr/bin/env python
import os
import sys
from django.conf import settings as djangosettings
AGENTS = {
"agentX" : "AgentX",
"webserver" : "webserver",
"monitoring" : "monitoring",
"majordome" : "majordome",
"scheduler" : "scheduler",
"alert_manager" : "alert_manager"
}
configfile = None
if len(sys.argv) < 2:
print("Error: you must give an agent name to start")
exit(1)
# arg 1 : agent name
agent_name = sys.argv[1]
# arg 2 : config file
if len(sys.argv) == 3:
configfile = sys.argv[2]
# Conseil sur le net:
#https://stackoverflow.com/questions/16853649/how-to-execute-a-python-script-from-the-django-shell
"""
import sys, os
sys.path.append('/path/to/your/django/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings
"""
"""
TODO:
- communiquer avec BD
- lire config XML
- gestion des logs, fich texte, 1/jour, mode 30 last lines, + BD, stdout/stderr
- thread de run
- thread de wrapper
- gestion des commandes dans le run
- table dediee à l agent, infos dynamiques volatiles
"""
# To avoid a "ModuleNotFoundError: No module named 'dashboard'"... (not even 1 app found) :
##sys.path.insert(0, os.path.abspath(".."))
##sys.path.insert(0, os.path.abspath("src"))
##sys.path.insert(0, "../src")
##sys.path.insert(0, "src")
# To avoid a "ModuleNotFoundError: No module named 'src'"
##sys.path.append("..")
sys.path.append("src")
print("Starting with this sys.path", sys.path)
# DJANGO setup
# print("file is", __file__)
# mypath = os.getcwd()
# Go into src/
##os.chdir("..")
##os.chdir("src")
print("Current directory : " + str(os.getcwd()))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.pyros.settings")
# os.environ['SECRET_KEY'] = 'abc'
# os.environ['ENVIRONMENT'] = 'production'
import django
django.setup()
print("DB2 used is:", djangosettings.DATABASES["default"]["NAME"])
print()
if agent_name not in AGENTS:
print("Error: this agent does not exists")
sys.exit(1)
if agent_name == "majordome":
from src.majordome.tasks import Majordome
Majordome().run(FOR_REAL=True)
sys.exit(0)
if agent_name == "monitoring":
from src.monitoring.tasks import Monitoring
Monitoring().run()
sys.exit(0)
# Default agent is AgentX
from src.agent.AgentX import AgentX
# AgentX().run(FOR_REAL=False)
agentx = AgentX(name="agentX", config_filename=configfile)
# Run agent without actual commands sent to devices (FOR_REAL=False)
agentx.run(FOR_REAL=True)