fix(processes): keep ps's five-word start time in one field
get_processes split each ps line into 11 parts, but lstart is five words, so started held only the weekday and command began with the rest of the date. The line is now split into 15 parts, started joins the five date words and command is the remainder. ps runs under LC_ALL=C: only there are the weekday and month one word each on every host. Closes #21
This commit is contained in:
1 parent
df4f4b0e4d
commit
54f6844d84
4 files changed
+69
-10
No files matched your search
@@ -57,6 +57,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
could not be used as a URL path segment, so such a tunnel could not be edited in
|
||||
netOrk. The key of a peer whose first eight key characters hold `+` or `/` changes
|
||||
once; `public_key` is reported unchanged.
|
||||
- `get_processes()` reports a process's whole start time in `started`
|
||||
(`Tue Oct 6 08:47:15 2026`) and only the command line in `command` (#21). `started`
|
||||
held the weekday alone and `command` began with the rest of the date. The start time
|
||||
is read in the C locale, so its words are English on every host.
|
||||
|
||||
## [0.15.1] - 2026-10-23
|
||||
|
||||
|
||||
@@ -1852,14 +1852,15 @@ class LinuxDriver(
|
||||
|
||||
def get_processes(self) -> list[ProcessDict]:
|
||||
"""Return running processes via ``ps axo``."""
|
||||
# lstart is five words ("Tue Oct 6 08:47:15 2026") -- one word each only in C.
|
||||
out = self._send(
|
||||
"ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args "
|
||||
"LC_ALL=C ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args "
|
||||
"--no-headers 2>/dev/null"
|
||||
)
|
||||
processes: list[ProcessDict] = []
|
||||
for line in out.splitlines():
|
||||
parts = line.split(None, 10)
|
||||
if len(parts) < 11:
|
||||
parts = line.split(None, 14)
|
||||
if len(parts) < 15:
|
||||
continue
|
||||
try:
|
||||
pid = int(parts[0])
|
||||
@@ -1871,12 +1872,8 @@ class LinuxDriver(
|
||||
rss = int(parts[6])
|
||||
tty = parts[7] if parts[7] != "?" else ""
|
||||
state = parts[8][0] if parts[8] else "?"
|
||||
# lstart is 5 tokens: "Mon May 27 12:34:56 2024" → parts[9..13]
|
||||
# args starts at parts[14] but we merged from 10 onward
|
||||
# With --no-headers and ps axo, lstart takes 5 parts
|
||||
# Rebuild: parts[9] is start, args is parts[10]
|
||||
started = parts[9]
|
||||
command = parts[10]
|
||||
started = " ".join(parts[9:14])
|
||||
command = parts[14]
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
processes.append(
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
"napalm_linux.linux"
|
||||
],
|
||||
"python_file_lines": {
|
||||
"napalm_linux/linux.py": 2260
|
||||
"napalm_linux/linux.py": 2257
|
||||
},
|
||||
"untyped_public": []
|
||||
}
|
||||
@@ -2065,3 +2065,61 @@ class TestWireGuardTunnelKeys:
|
||||
(tunnel,) = driver.get_vpn_tunnels().values()
|
||||
|
||||
assert tunnel["public_key"] == "ab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB="
|
||||
|
||||
|
||||
class TestGetProcesses:
|
||||
"""``lstart`` is five words; the split has to leave them together (#21)."""
|
||||
|
||||
_PS = (
|
||||
" 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6 08:47:15 2026 /sbin/init splash\n"
|
||||
" 1234 1 chris 1.5 2.3 456789 98765 pts/0 S+ Tue Oct 6 09:01:02 2026 bash -c echo two spaces\n"
|
||||
)
|
||||
|
||||
def test_started_is_the_whole_start_time(self, driver):
|
||||
_mock_send(driver, self._PS)
|
||||
|
||||
init, shell = driver.get_processes()
|
||||
|
||||
assert init["started"] == "Tue Oct 6 08:47:15 2026"
|
||||
assert shell["started"] == "Tue Oct 6 09:01:02 2026"
|
||||
|
||||
def test_command_is_the_command_line_alone(self, driver):
|
||||
_mock_send(driver, self._PS)
|
||||
|
||||
init, shell = driver.get_processes()
|
||||
|
||||
assert init["command"] == "/sbin/init splash"
|
||||
assert shell["command"] == "bash -c echo two spaces"
|
||||
|
||||
def test_the_other_columns(self, driver):
|
||||
_mock_send(driver, self._PS)
|
||||
|
||||
shell = driver.get_processes()[1]
|
||||
|
||||
assert (shell["pid"], shell["ppid"], shell["user"], shell["tty"]) == (
|
||||
1234,
|
||||
1,
|
||||
"chris",
|
||||
"pts/0",
|
||||
)
|
||||
assert (shell["cpu"], shell["memory"], shell["vsz"], shell["rss"]) == (
|
||||
1.5,
|
||||
2.3,
|
||||
456789,
|
||||
98765,
|
||||
)
|
||||
assert shell["state"] == "S"
|
||||
assert driver.get_processes()[0]["tty"] == ""
|
||||
|
||||
def test_the_start_time_is_in_c_locale_words(self, driver):
|
||||
"""A German host says "Di Okt" -- one word each too, but only C promises that."""
|
||||
_mock_send(driver, self._PS)
|
||||
|
||||
driver.get_processes()
|
||||
|
||||
assert driver._device.send_command.call_args[0][0].startswith("LC_ALL=C ps ")
|
||||
|
||||
def test_a_line_cut_short_is_skipped(self, driver):
|
||||
_mock_send(driver, " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6\n")
|
||||
|
||||
assert driver.get_processes() == []
|
||||
Reference in new issue
Block a user