Skip to content
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

Additions for a metrics_port in the node state. #39

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading