-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix agents purge #82
Fix agents purge #82
Conversation
Hello @Lifka, Please, update the changelog and the mocha tests with these changes. Also, filtering by # curl -u foo:bar "localhost:55000/agents?pretty&status=Active"
{
"error": 0,
"data": {
"totalItems": 3,
"items": [
{
"status": "Active",
"name": "localhost.localdomain",
"ip": "127.0.0.1",
"node_name": "node01",
"dateAdd": "2018-05-23 09:17:40",
"version": "Wazuh v3.2.3",
"manager_host": "localhost.localdomain",
"lastKeepAlive": "9999-12-31 23:59:59",
"os": {
"major": "7",
"name": "CentOS Linux",
"uname": "Linux |localhost.localdomain |3.10.0-693.21.1.el7.x86_64 |#1 SMP Wed Mar 7 19:03:37 UTC 2018 |x86_64",
"platform": "centos",
"version": "7",
"codename": "Core",
"arch": "x86_64"
},
"id": "000"
},
{
"status": "Active",
"configSum": "ab73af41699f13fdd81903b5f23d8d00",
"group": "default",
"name": "perico",
"mergedSum": "d9835ca466a5f6ede52e0684537f76bd",
"ip": "any",
"node_name": "node01",
"dateAdd": "2018-05-23 09:17:40",
"version": "Wazuh v3.2.3",
"manager_host": "localhost.localdomain",
"lastKeepAlive": "2018-05-23 09:29:47",
"os": {
"major": "7",
"name": "CentOS Linux",
"uname": "Linux |localhost.localdomain |3.10.0-693.el7.x86_64 |#1 SMP Tue Aug 22 21:09:27 UTC 2017 |x86_64",
"platform": "centos",
"version": "7",
"codename": "Core",
"arch": "x86_64"
},
"id": "001"
},
{
"status": "Pending",
"dateAdd": "2018-05-23 09:27:32",
"name": "josefa",
"ip": "any",
"lastKeepAlive": "2018-05-23 09:25:47",
"id": "004",
"node_name": "unknown"
}
]
}
}
# curl -u foo:bar "localhost:55000/agents?pretty&status=pending"
{
"error": 0,
"data": {
"totalItems": 1,
"items": [
{
"status": "Pending",
"dateAdd": "2018-05-23 09:27:32",
"name": "josefa",
"ip": "any",
"lastKeepAlive": "2018-05-23 09:25:47",
"id": "004",
"node_name": "unknown"
}
]
}
} I think the problem is the database query used to filter active agents: query += '(last_keepalive >= :time_active or id = 0) OR ' Note that @staticmethod
def calculate_status(last_keep_alive, pending, today=datetime.today()):
"""
Calculates state based on last keep alive
"""
if not last_keep_alive:
return "Never connected"
else:
limit_seconds = 1830 # 600*3 + 30
# divide date in format YY:mm:dd HH:MM:SS to create a datetime object.
last_date = datetime(year=int(last_keep_alive[:4]), month=int(last_keep_alive[5:7]), day=int(last_keep_alive[8:10]),
hour=int(last_keep_alive[11:13]), minute=int(last_keep_alive[14:16]), second=int(last_keep_alive[17:19]))
difference = (today - last_date).total_seconds()
return "Disconnected" if difference > limit_seconds else ("Pending" if pending else "Active") One more thing, we're moving input validation to the framework level (more info: #80 (comment)). In the API is ok to use the "alphanumeric" filter. It's in the framework where the semantic validation takes place. In fact, it's already implemented. The default timeframe when removing agents is 7 days. It would be nice to add the used timeframe to the response to avoid situations like the following: # curl -u foo:bar "localhost:55000/agents?pretty&status=pending"
{
"error": 0,
"data": {
"totalItems": 1,
"items": [
{
"status": "Pending",
"dateAdd": "2018-05-23 09:27:32",
"name": "josefa",
"ip": "any",
"lastKeepAlive": "2018-05-23 10:45:47",
"id": "004",
"node_name": "unknown"
}
]
}
}
# curl -u foo:bar -XDELETE "localhost:55000/agents?pretty&status=pending"
{
"error": 0,
"data": {
"msg": "All selected agents were removed",
"affected_agents": []
}
}
# curl -u foo:bar -XDELETE "localhost:55000/agents?pretty&status=pending&timeframe=10m"
{
"error": 0,
"data": {
"msg": "All selected agents were removed",
"affected_agents": [
"004"
]
}
} Best regards, |
Done. |
Great job! Some changes:
Thanks. |
Done!
Thank you @jesuslinares! The last commit also fixes a bug handling python exceptions in agents functions: Before:
Then:
|
Summary:Requests for remove agents:
Also, the filters Samples:Remove all agents that have been disconnected for the last week, or added more than a week ago and are never connected:
Remove all agents from a list that have not been active from more than 1 day:
Remove all agents never connected:
Regards! |
Hello team,
this PR solves the issue #79, and it's part of https://github.com/wazuh/wazuh/tree/fix-agents-purge.
Remove
POST/agents/purge
GET/agents/purgeable
Add
status
inGET/agents
can filter by several status separated by commas. Sample:GET/agents
resquest:timeframe
. Timeframe filters out agents that have not connected within the specified time. In case of havingnever connected
status, filter using the date they were registered. Sample:timeframe
andstatus
filter toDELETE /agents
. It's possible to delete agents not connected at a specific time with a specific status. By default, timeframe is7d
(7 days). Samples:Remove all agents that have been disconnected for the last week, or added more than a week ago and are never connected:
Remove all agents from a list that have not been active from more than 1 day:
Delete all agents never connected:
Regards.