Skip to content

Commit

Permalink
Merge pull request #1039 from wazuh/1034-agentd-stats-enhancement
Browse files Browse the repository at this point in the history
wazuh-agentd stats IT
  • Loading branch information
vikman90 committed Feb 24, 2021
2 parents 31c0072 + e8acef9 commit 541da0f
Show file tree
Hide file tree
Showing 8 changed files with 632 additions and 3 deletions.
70 changes: 70 additions & 0 deletions deps/wazuh_testing/wazuh_testing/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Created by Wazuh, Inc. <info@wazuh.com>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

import re

from wazuh_testing.fim import change_internal_options


# Callbacks
def callback_state_interval_not_valid(line):
match = re.match(r'.*Invalid definition for agent.state_interval:', line)
return True if match is not None else None


def callback_state_interval_not_found(line):
match = re.match(r".*Definition not found for: 'agent.state_interval'", line)
return True if match is not None else None


def callback_state_file_not_enabled(line):
match = re.match(r'.*State file is disabled', line)
return True if match is not None else None


def callback_state_file_enabled(line):
match = re.match(r'.*State file updating thread started', line)
return True if match is not None else None


def callback_state_file_updated(line):
match = re.match(r'.*Updating state file', line)
return True if match is not None else None


def callback_ack(line):
match = re.match(r".*Received message: '#!-agent ack ", line)
return True if match is not None else None


def callback_keepalive(line):
match = re.match(r'.*Sending keep alive', line)
return True if match is not None else None


def callback_connected_to_server(line):
match = re.match(r'.*Connected to the server', line)
return True if match is not None else None


def set_state_interval(interval, internal_file_path):
"""Set agent.state_interval value on internal_options.conf
Args:
interval:
- Different than `None`: set agent.state_interval
value on internal_options.conf
- `None`: agent.state_interval will be removed
from internal_options.conf
"""
if interval is not None:
change_internal_options('agent.state_interval', interval, internal_file_path, '.*')
else:
new_content = ''
with open(internal_file_path) as opts:
for line in opts:
new_line = line if 'agent.state_interval' not in line else ''
new_content += new_line

with open(internal_file_path, 'w') as opts:
opts.write(new_content)
5 changes: 3 additions & 2 deletions deps/wazuh_testing/wazuh_testing/fim.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,13 +920,14 @@ def modify_file(path, name, new_content=None, is_binary=False):
modify_file_win_attributes(path, name)


def change_internal_options(param, value, opt_path=None):
def change_internal_options(param, value, opt_path=None, value_regex='[0-9]*'):
"""Change the value of a given parameter in local_internal_options.
Args:
param (str): parameter to change.
value (obj): new value.
opt_path (str, optional): local_internal_options.conf path. Defaults `None`
value_regex (str, optional): regex to match value in local_internal_options.conf. Default '[0-9]*'
"""
if opt_path is None:
local_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') if sys.platform == 'win32' else \
Expand All @@ -941,7 +942,7 @@ def change_internal_options(param, value, opt_path=None):
with open(local_conf_path, "w") as sources:
for line in lines:
sources.write(
re.sub(f'{param}=[0-9]*', f'{param}={value}', line))
re.sub(f'{param}={value_regex}', f'{param}={value}', line))
if param in line:
add_pattern = False

Expand Down
14 changes: 13 additions & 1 deletion deps/wazuh_testing/wazuh_testing/tools/remoted_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(self, server_address='127.0.0.1', remoted_port=1514, protocol='udp'
self.wcom_message_version = None
self.active_response_message = None
self.listener_thread = None
self.last_client = None
if start_on_init:
self.start()

Expand Down Expand Up @@ -379,21 +380,23 @@ def listener(self):
# Wait for a connection
try:
connection, client_address = self.sock.accept()
self.last_client = connection
while self.running:
data = self.recv_all(connection, 4)
data_size = struct.unpack('<I', data[0:4])[0]
data = self.recv_all(connection, data_size)

try:
ret = self.process_message(client_address, data)
except Exception:
time.sleep(1)
connection.close()
self.last_client = None

# Response -1 means connection have to be closed
if ret == -1:
time.sleep(0.1)
connection.close()
self.last_client = None
break
# If there is a response, answer it
elif ret:
Expand Down Expand Up @@ -661,3 +664,12 @@ def wait_upgrade_notification(self, timeout=None):
if timeout is not None:
timeout -= 1
return self.upgrade_notification

def request(self, message):
"""
Send request to agent using current request counter
message: Request content
"""
if self.last_client:
request = self.create_sec_message(f'#!-req {self.request_counter} {message}', 'aes')
self.send(self.last_client, request)
17 changes: 17 additions & 0 deletions tests/integration/test_agentd/data/wazuh_conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,20 @@
value: MAX_RETRIES
- retry_interval:
value: RETRY_INTERVAL
- tags:
- all
apply_to_modules:
- test_agentd_state_config
- test_agentd_state
sections:
- section: client
elements:
- server:
elements:
- address:
value: '127.0.0.1'
- port:
value: '1514'
- protocol:
value: 'tcp'

43 changes: 43 additions & 0 deletions tests/integration/test_agentd/data/wazuh_state_config_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
-
name: "Negative state interval"
test_case:
log_expect: 'interval_not_valid'
interval: -1
state_file_exist: false
agentd_ends: true
-
name: "Undefined state interval value"
test_case:
log_expect: 'file_not_enabled'
interval: " "
state_file_exist: false
agentd_ends: false
-
name: "State interval option dont exist"
test_case:
log_expect: 'interval_not_found'
interval: null
state_file_exist: false
agentd_ends: true
-
name: "Zero state interval"
test_case:
log_expect: 'file_not_enabled'
interval: 0
state_file_exist: false
agentd_ends: false
-
name: "Too big state interval"
test_case:
log_expect: 'interval_not_valid'
interval: 86401
state_file_exist: false
agentd_ends: true
-
name: "Default state interval"
test_case:
log_expect: 'file_enabled'
interval: 5
state_file_exist: true
agentd_ends: false
48 changes: 48 additions & 0 deletions tests/integration/test_agentd/data/wazuh_state_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
-
name: "No connection with remoted"
test_case:
input:
remoted: false
output:
-
type: "file"
fields:
status: "pending"
last_keepalive: ""
last_ack: ""
msg_count: ""
-
name: "Successful connection with remoted"
test_case:
input:
remoted: true
output:
-
type: "file"
fields:
status: "connected"
last_keepalive: true
last_ack: true
msg_count: true
-
type: "remote"
fields:
status: "connected"
last_keepalive: true
last_ack: true
msg_count: true
-
name: "Only remote request available"
test_case:
input:
remoted: true
interval: '0'
output:
-
type: "remote"
fields:
status: "connected"
last_keepalive: true
last_ack: true
msg_count: true
Loading

0 comments on commit 541da0f

Please sign in to comment.