get_services moves into WindowsServicesMixin and reads, beside state and start mode, Win32_Service's ExitCode (1066 hands over to ServiceSpecificExitCode, 1077 is not started since boot), StartName and whether a TriggerInfo key starts the service, as napalm-device-types' ServiceDict result, exit_code, trigger_start and account.
99 lines
4.0 KiB
Python
99 lines
4.0 KiB
Python
"""The host's services, and how each one's last run ended (NetOrk/netork#2897).
|
|
|
|
:class:`WindowsServicesMixin` reads ``get_services`` for the driver, which carries
|
|
the script across ``PsrpTransport.run``.
|
|
|
|
One script: every ``Win32_Service`` with its state, start mode and process, the
|
|
account it runs as (``StartName``) and the code its last run ended with
|
|
(``ExitCode``). Both exit codes are UInt32 and leave the host as ``[int64]``, since
|
|
``[int]`` would throw on an HRESULT and fail the reading. Which services a trigger
|
|
starts (a device arriving, a network change, a request) is one registry read for all
|
|
of them: the ``TriggerInfo`` keys under ``HKLM:\\SYSTEM\\CurrentControlSet\\Services``.
|
|
|
|
**How the last run ended** is said in systemd's words (napalm-device-types'
|
|
``ServiceDict.result``), so that one rule reads it for every host:
|
|
|
|
* ``0`` is ``success``: running, or stopped on purpose.
|
|
* ``1077`` (``ERROR_SERVICE_NEVER_STARTED``) is no result: not started since the
|
|
host booted -- a demand-start service, or a delayed one in the minutes after boot.
|
|
* ``1066`` (``ERROR_SERVICE_SPECIFIC_ERROR``) is ``exit-code`` with the service's own
|
|
code, ``ServiceSpecificExitCode``.
|
|
* Any other code is ``exit-code`` with that code: ``1067`` is a process that ended
|
|
without being stopped.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from napalm_device_types import SERVICE_RESULT_EXIT_CODE, SERVICE_RESULT_SUCCESS, ServiceDict
|
|
|
|
from napalm_windows._powershell import _as_list
|
|
|
|
_PS_SERVICES = r"""
|
|
$ErrorActionPreference = 'Stop'
|
|
$triggered = @{}
|
|
$keys = 'HKLM:\SYSTEM\CurrentControlSet\Services\*\TriggerInfo'
|
|
Get-Item -Path $keys -ErrorAction SilentlyContinue |
|
|
ForEach-Object { $triggered[(Split-Path -Path $_.PSParentPath -Leaf)] = $true }
|
|
ConvertTo-Json -Compress -Depth 3 -InputObject @(
|
|
Get-CimInstance -ClassName Win32_Service | ForEach-Object {
|
|
[pscustomobject]@{
|
|
name = $_.Name
|
|
state = [string]$_.State
|
|
start_mode = [string]$_.StartMode
|
|
pid = [int]$_.ProcessId
|
|
exit_code = [int64]$_.ExitCode
|
|
service_code = [int64]$_.ServiceSpecificExitCode
|
|
account = $_.StartName
|
|
trigger_start = [bool]$triggered[$_.Name]
|
|
}
|
|
}
|
|
)
|
|
"""
|
|
|
|
_ENABLED_START_MODES = {"auto", "boot", "system"}
|
|
#: ``ERROR_SERVICE_NEVER_STARTED``: no attempt to start it since the host booted.
|
|
_NEVER_STARTED = 1077
|
|
#: ``ERROR_SERVICE_SPECIFIC_ERROR``: the code is the service's own.
|
|
_SERVICE_SPECIFIC = 1066
|
|
|
|
|
|
def _result(raw: dict[str, Any]) -> tuple[str | None, int | None]:
|
|
"""How the last run ended, and its code where that was not success."""
|
|
code = raw.get("exit_code")
|
|
if code is None or int(code) == _NEVER_STARTED:
|
|
return None, None
|
|
if int(code) == 0:
|
|
return SERVICE_RESULT_SUCCESS, None
|
|
if int(code) == _SERVICE_SPECIFIC and raw.get("service_code"):
|
|
return SERVICE_RESULT_EXIT_CODE, int(raw["service_code"])
|
|
return SERVICE_RESULT_EXIT_CODE, int(code)
|
|
|
|
|
|
def _service(raw: dict[str, Any]) -> ServiceDict:
|
|
running = str(raw.get("state") or "").lower() == "running"
|
|
result, exit_code = _result(raw)
|
|
return {
|
|
"name": str(raw["name"]),
|
|
"running": running,
|
|
"enabled": str(raw.get("start_mode") or "").lower() in _ENABLED_START_MODES,
|
|
"pid": int(raw.get("pid") or 0) if running else 0,
|
|
"result": result,
|
|
"exit_code": exit_code,
|
|
"trigger_start": bool(raw.get("trigger_start")),
|
|
"account": str(raw["account"]) if raw.get("account") else None,
|
|
}
|
|
|
|
|
|
class WindowsServicesMixin:
|
|
"""``get_services`` from ``Win32_Service``; the driver carries the script."""
|
|
|
|
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
|
|
|
|
def _run_ps(self, script: str) -> Any: ...
|
|
|
|
def get_services(self) -> list[ServiceDict]:
|
|
"""Every service with whether it runs, starts at boot, and how its last run ended."""
|
|
return [_service(s) for s in _as_list(self._run_ps(_PS_SERVICES))]
|