Skip to content

Commit

Permalink
Merge branch 'topic/christian/management-telemetry-addition'
Browse files Browse the repository at this point in the history
* topic/christian/management-telemetry-addition:
  Additions for a metrics_port in the node state.
  • Loading branch information
ckreibich committed Jul 9, 2024
2 parents 855b037 + 0791f38 commit d495295
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.3.1-17 | 2024-07-08 22:21:09 -0700

* Additions for a metrics_port in the node state. (Christian Kreibich, Corelight)

1.3.1-15 | 2023-09-18 11:00:08 -0700

* Instance constructor now checks if the IP addr is valid. Tests check for ValueError if not. (Michael Dopheide)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.1-15
1.3.1-17
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def test_cmd_get_config_as_json(self):
"env": {},
"instance": "instance1",
"interface": null,
"metrics_port": null,
"name": "worker1",
"options": null,
"port": null,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_config_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TestRendering(unittest.TestCase):
instance = agent
port = 5000
role = manager
metrics_port = 6000
[logger-01]
instance = agent
Expand All @@ -49,6 +50,7 @@ class TestRendering(unittest.TestCase):
role = worker
interface = enp3s0
cpu_affinity = 8
metrics_port = 6001
"""
INI_EXPECTED = """[instances]
agent
Expand All @@ -63,6 +65,7 @@ class TestRendering(unittest.TestCase):
instance = agent
role = MANAGER
port = 5000
metrics_port = 6000
[worker-01]
instance = agent
Expand All @@ -76,6 +79,7 @@ class TestRendering(unittest.TestCase):
role = WORKER
interface = enp3s0
cpu_affinity = 8
metrics_port = 6001
"""
JSON_EXPECTED = """{
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
Expand All @@ -90,6 +94,7 @@ class TestRendering(unittest.TestCase):
"env": {},
"instance": "agent",
"interface": null,
"metrics_port": null,
"name": "logger-01",
"options": null,
"port": 5001,
Expand All @@ -103,6 +108,7 @@ class TestRendering(unittest.TestCase):
"env": {},
"instance": "agent",
"interface": null,
"metrics_port": 6000,
"name": "manager",
"options": null,
"port": 5000,
Expand All @@ -117,6 +123,7 @@ class TestRendering(unittest.TestCase):
},
"instance": "agent",
"interface": "lo",
"metrics_port": null,
"name": "worker-01",
"options": null,
"port": null,
Expand All @@ -128,6 +135,7 @@ class TestRendering(unittest.TestCase):
"env": {},
"instance": "agent",
"interface": "enp3s0",
"metrics_port": 6001,
"name": "worker-02",
"options": null,
"port": null,
Expand Down
1 change: 1 addition & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def test_node(self):
interface="eth0",
cpu_affinity=13,
env={"FOO": "BAR"},
metrics_port=9000,
)
val1 = self.brokertype_roundtrip(val0)
self.assertEqual(val0, val1)
Expand Down
2 changes: 1 addition & 1 deletion zeekclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from .logs import LOG

__version__ = "1.3.1-15"
__version__ = "1.3.1-17"
__all__ = [
"brokertypes",
"cli",
Expand Down
4 changes: 4 additions & 0 deletions zeekclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ def cmd_get_nodes(_args):
json_data["results"][res.instance][nstat.node]["pid"] = nstat.pid
if nstat.port is not None:
json_data["results"][res.instance][nstat.node]["port"] = nstat.port
if nstat.metrics_port is not None:
json_data["results"][res.instance][nstat.node][
"metrics_port"
] = nstat.metrics_port
except TypeError as err:
LOG.error("NodeStatus data invalid: %s", err)
LOG.debug(traceback.format_exc())
Expand Down
46 changes: 45 additions & 1 deletion zeekclient/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def __init__(
interface=None,
cpu_affinity=None,
env=None,
metrics_port=None,
):
self.name = name
self.instance = instance
Expand All @@ -315,6 +316,7 @@ def __init__(
self.interface = interface
self.cpu_affinity = cpu_affinity
self.env = env or {}
self.metrics_port = metrics_port

def __lt__(self, other):
return self.name < other.name
Expand All @@ -332,6 +334,7 @@ def __eq__(self, other):
and self.interface == other.interface
and self.cpu_affinity == other.cpu_affinity
and self.env == other.env
and self.metrics_port == other.metrics_port
)

def __hash__(self):
Expand All @@ -354,6 +357,7 @@ def __hash__(self):
self.interface,
self.cpu_affinity,
env,
self.metrics_port,
)
)

Expand All @@ -374,6 +378,7 @@ def to_brokertype(self):
bt.from_py(self.interface),
bt.from_py(self.cpu_affinity, typ=bt.Count),
bt.from_py(self.env),
bt.from_py(self.metrics_port, typ=bt.Port),
]
)

Expand All @@ -391,6 +396,7 @@ def to_json_data(self):
"interface": self.interface,
"cpu_affinity": self.cpu_affinity,
"env": self.env,
"metrics_port": self.metrics_port,
}

@classmethod
Expand All @@ -404,6 +410,10 @@ def from_brokertype(cls, data):
if isinstance(data[4], bt.Port):
port = data[4].number

metrics_port = None
if len(data) >= 11 and isinstance(data[10], bt.Port):
metrics_port = data[10].number

return Node(
data[0].to_py(), # name
data[1].to_py(), # instance
Expand All @@ -415,6 +425,7 @@ def from_brokertype(cls, data):
data[7].to_py(), # interface
data[8].to_py(), # cpu_affinity
data[9].to_py(), # env
metrics_port,
)
except (IndexError, TypeError, ValueError) as err:
raise TypeError(f"unexpected Broker data for Node object ({data})") from err
Expand Down Expand Up @@ -447,6 +458,7 @@ def get(typ, *keys):
interface = get(str, "interface")
cpu_affinity = get(int, "cpu_affinity")
env = None
metrics_port = get(int, "metrics_port")

# Validate the specified values
if not instance:
Expand All @@ -472,6 +484,10 @@ def get(typ, *keys):
if port is not None and (port < 1 or port > 65535):
raise ValueError(f"port {port} outside valid range")

# Same for metrics ports.
if metrics_port is not None and (metrics_port < 1 or metrics_port > 65535):
raise ValueError(f"metrics port {port} outside valid range")

try:
# We support multiple scripts as a simple space-separated sequence
# of filenames, with possible quotation marks for strings with
Expand Down Expand Up @@ -510,6 +526,7 @@ def get(typ, *keys):
"interface",
"cpu_affinity",
"env",
"metrics_port",
]
)

Expand All @@ -526,6 +543,7 @@ def get(typ, *keys):
interface=interface,
cpu_affinity=cpu_affinity,
env=env,
metrics_port=metrics_port,
)

def to_config_parser(self, cfp=None):
Expand Down Expand Up @@ -580,6 +598,9 @@ def to_config_parser(self, cfp=None):

cfp.set(self.name, "env", " ".join(env))

if self.metrics_port is not None:
cfp.set(self.name, "metrics_port", str(self.metrics_port))

return cfp


Expand Down Expand Up @@ -744,13 +765,23 @@ def to_config_parser(self, cfp=None):
class NodeStatus(SerializableZeekType):
"""Equivalent of Management::NodeState."""

def __init__(self, node, state, mgmt_role, cluster_role, pid=None, port=None):
def __init__(
self,
node,
state,
mgmt_role,
cluster_role,
pid=None,
port=None,
metrics_port=None,
):
self.node = node # A string containing the name of the node
self.state = state # A State enum value
self.mgmt_role = mgmt_role # A ManagementRole enum value
self.cluster_role = cluster_role # A ClusterRole enum value
self.pid = pid # A numeric process ID
self.port = port # A numeric (TCP) port
self.metrics_port = metrics_port # A numeric (TCP) port for Prometheus

def __lt__(self, other):
return self.node < other.node
Expand All @@ -764,6 +795,7 @@ def __eq__(self, other):
and self.cluster_role == other.cluster_role
and self.pid == other.pid
and self.port == other.port
and self.metrics_port == other.metrics_port
)

def __hash__(self):
Expand All @@ -775,6 +807,7 @@ def __hash__(self):
self.cluster_role,
self.pid,
self.port,
self.metrics_port,
)
)

Expand All @@ -783,6 +816,9 @@ def to_brokertype(self):
# it helps to be able to serialize.
pid = bt.NoneType() if self.pid is None else bt.Integer(self.pid)
port = bt.NoneType() if self.port is None else bt.Port(self.port)
metrics_port = (
bt.NoneType() if self.metrics_port is None else bt.Port(self.metrics_port)
)

return bt.Vector(
[
Expand All @@ -792,6 +828,7 @@ def to_brokertype(self):
self.cluster_role.to_brokertype(),
pid,
port,
metrics_port,
]
)

Expand All @@ -801,13 +838,20 @@ def from_brokertype(cls, data):
if port is not None:
port = port.number

metrics_port = None
if len(data) >= 7:
metrics_port = data[6].to_py()
if metrics_port is not None:
metrics_port = metrics_port.number

return NodeStatus(
data[0].to_py(),
State.from_brokertype(data[1]),
ManagementRole.from_brokertype(data[2]),
ClusterRole.from_brokertype(data[3]),
data[4].to_py(),
port,
metrics_port,
)


Expand Down

0 comments on commit d495295

Please sign in to comment.