-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
312 lines (238 loc) · 8.38 KB
/
agent.py
File metadata and controls
312 lines (238 loc) · 8.38 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
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
#!/usr/bin/env python3
# Libraries
##############################################################################
import json
import os
import logging
import schedule
import requests
import time
from Modules.system_info import get_system_info
from Modules.user_info import get_user_info
from Modules.installed_apps import get_installed_programs
from Modules.npm_info import get_npm_packages
from Modules.service_info import get_service_info
from Modules.pip_info import get_pip_packages
from Modules.docker_info import get_docker_info
from Modules.last_logon import get_last_logons
from Modules.disk_usage_info import get_disk_usage_info
from Modules.memory_usage_info import get_memory_usage_info
from Modules.cpu_usage_info import get_cpu_usage_info
##############################################################################
# Configs
##############################################################################
def load_config(file_path):
with open(file_path, 'r') as file:
config_data = json.load(file)
return config_data
# Specify the path to your JSON configuration file
config_file_path = 'config.json'
# Load configuration from the JSON file
config = load_config(config_file_path)
# Extract information from the configuration
base_url = config['api']['base_url']
endpoints = config['api']['endpoints']
agent_token = config['api']['agent_token']
home_dir = config['dirs']['home_dir']
logs_dir = os.path.join(home_dir,'logs')
logfile_relative_path = config['dirs']['logfile']
logfile_path = os.path.join(home_dir, logfile_relative_path)
# Check if path not exist, create new one
if not os.path.exists(logs_dir):
os.makedirs(logs_dir)
FORMAT = '%(asctime)s :: %(levelname)-6s :: %(name)s :: [%(filename)s:%(lineno)s - %(funcName)s()] :: %(message)s'
# Configure logging to write logs to a log file
logging.basicConfig(
filename=logfile_path,
level=logging.INFO,
format=FORMAT,
encoding='utf-8'
)
# Get Scheduled Jobs from Config
scheduled_jobs = config['scheduled_jobs']
##############################################################################
# Functions
##############################################################################
# System Info Sender Function
def send_system_info():
try:
url = str(base_url) + endpoints["system_info"]
payload = json.dumps(get_system_info(), indent=4)
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Users Info Sender Function
def send_user_info():
try:
url = str(base_url) + endpoints["user_info"]
payload = json.dumps({"users":get_user_info()})
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Installed Apps Info Sender Function
def send_installed_programs():
try:
url = str(base_url) + endpoints["installed_programs"]
payload = json.dumps({"apps":get_installed_programs()})
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Services Info Sender Function
def send_service_info():
try:
url = str(base_url) + endpoints["service_info"]
payload = json.dumps({"services":get_service_info()})
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Last Logons Info Sender Function
def send_last_logons():
try:
url = str(base_url) + endpoints["last_logons"]
payload = json.dumps({"last_logons":get_last_logons()})
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Pip Packages Info Sender Function
def send_pip_packages():
try:
url = str(base_url) + endpoints["pip_pkgs"]
pip_info = get_pip_packages()
payload = json.dumps({
"is_installed": pip_info["is_installed"],
"pip_packages": pip_info["packages"]
})
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Npm Packages Info Sender Function
def send_npm_packages():
try:
url = str(base_url) + endpoints["npm_pkgs"]
npm_pkgs_info = get_npm_packages()
payload = json.dumps({
"is_installed": npm_pkgs_info["is_installed"],
"npm_packages": npm_pkgs_info["packages"]
})
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Docker Info Sender Function
def send_docker_info():
try:
url = str(base_url) + endpoints["docker_info"]
docker_info = get_docker_info()
payload = json.dumps(docker_info)
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Agent Config Sender Function
def send_agent_config():
try:
url = str(base_url) + endpoints["agent_config"]
payload = json.dumps(config)
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Disk Usage Sender Function
def send_disk_usage():
try:
url = str(base_url) + endpoints["disk_usage"]
payload = json.dumps({
"disk_usage": get_disk_usage_info()
})
print(payload)
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# Memory Usage Sender Function
def send_memory_usage():
try:
url = str(base_url) + endpoints["memory_usage"]
payload = json.dumps(get_memory_usage_info())
print(payload)
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
# CPU Usage Sender Function
def send_cpu_usage():
try:
url = str(base_url) + endpoints["cpu_usage"]
payload = json.dumps(get_cpu_usage_info())
headers = {
'Authorization': agent_token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
except Exception as e:
print(e)
##############################################################################
# Schedule jobs
for job_name, job_config in scheduled_jobs.items():
if "interval" in job_config:
interval = job_config["interval"]
unit = job_config.get("unit", "minutes")
getattr(schedule.every(interval), unit).do(eval(job_name))
elif "time" in job_config:
time_str = job_config["time"]
schedule.every().day.at(time_str).do(eval(job_name))
# Run
while True:
schedule.run_pending()
time.sleep(60) # Check every 60 seconds