Nornir Worker
WatchDog(worker)
¤
Bases: WorkerWatchDog
Class to monitor Nornir worker performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worker
|
Worker
|
The worker instance that this NornirWorker will manage. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
worker |
Worker
|
The worker instance being monitored. |
connections_idle_timeout |
int
|
Timeout value for idle connections. |
failed_hosts_recovery_timeout |
int
|
Timeout before failed hosts recover. |
connections_data |
dict
|
Dictionary to store connection use timestamps. |
failed_hosts_data |
dict
|
Errdisabled timestamps and available reasons. |
started_at |
float
|
Timestamp when the watchdog was started. |
idle_connections_cleaned |
int
|
Counter for idle connections cleaned. |
dead_connections_cleaned |
int
|
Counter for dead connections cleaned. |
watchdog_tasks |
list
|
List of tasks for the watchdog to run in a given order. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
get_stats() -> Dict
¤
Return Nornir-specific values for the worker monitoring snapshot.
Source code in norfab\workers\nornir_worker\nornir_worker.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
configuration() -> Dict
¤
Returns the configuration settings for the worker.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
A dictionary containing the configuration settings:
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
failed_hosts_update(result: Any = None) -> None
¤
Track when hosts enter Nornir's shared failed-host state.
Source code in norfab\workers\nornir_worker\nornir_worker.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
failed_hosts_list() -> list[dict]
¤
Return tracked failed hosts and their remaining recovery time.
Source code in norfab\workers\nornir_worker\nornir_worker.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
failed_hosts_clear() -> list[str]
¤
Recover all hosts currently marked as failed by Nornir.
Source code in norfab\workers\nornir_worker\nornir_worker.py
197 198 199 200 201 202 203 204 | |
failed_hosts_recover() -> None
¤
Recover hosts after the configured errdisabled timeout expires.
Source code in norfab\workers\nornir_worker\nornir_worker.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
connections_get() -> Dict
¤
Retrieve the current connections data.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
A dictionary containing the current connections data. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
226 227 228 229 230 231 232 233 234 235 | |
connections_update(nr: Any, plugin: str) -> None
¤
Update connection use timestamps while the caller holds connections_lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nr
|
Any
|
Nornir object |
required |
plugin
|
str
|
connection plugin name |
required |
Source code in norfab\workers\nornir_worker\nornir_worker.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
connections_clean() -> None
¤
Cleans up idle connections based on the configured idle timeout.
This method checks for connections that have been idle for longer than the
specified connections_idle_timeout and disconnects them. The behavior
varies depending on the value of connections_idle_timeout:
- If
connections_idle_timeoutis None, no connections are disconnected. - If
connections_idle_timeoutis 0, all connections are disconnected. - If
connections_idle_timeoutis greater than 0, only connections that have been idle for longer than the specified timeout are disconnected.
The method acquires a lock to ensure thread safety while modifying the
connections data. It logs the disconnection actions and updates the
idle_connections_cleaned counter.
Raises:
| Type | Description |
|---|---|
Exception
|
If an error occurs while attempting to disconnect idle connections, an error message is logged. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
connections_keepalive() -> None
¤
Keepalive connections and clean up dead connections if any.
This method performs the following tasks:
- If
connections_idle_timeoutis 0, it returns immediately without performing any actions. - Attempts to acquire a lock on
worker.connections_lockto ensure thread safety. - Logs a debug message indicating that the keepalive process is running.
- Uses
HostsKeepaliveto check and clean up dead connections, updating thedead_connections_cleanedcounter. - Removes connections that are no longer present in the Nornir inventory.
- Removes hosts from
connections_dataif they have no remaining connections. - Updates the keepalive statistics for each connection plugin, including the last keepalive time and keepalive count.
- Logs an error message if an exception occurs during the keepalive process.
- Releases the lock on
worker.connections_lockin thefinallyblock to ensure it is always released.
Raises:
| Type | Description |
|---|---|
Exception
|
If an error occurs during the keepalive process, it is logged as an error. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
NornirWorker(inventory: str, broker: str, worker_name: str, exit_event=None, init_done_event=None, log_level: str = None)
¤
Bases: NFPWorker, TaskTask, CliTask, CfgTask, TestTask, NetworkTask, InventoryTasks, ParseTask, FileCopyTask, NetconfTask, SnmpTask
NornirWorker class for managing Nornir Service tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inventory
|
str
|
Path to the inventory file. |
required |
broker
|
str
|
Broker address. |
required |
worker_name
|
str
|
Name of the worker. |
required |
exit_event
|
Event
|
Event to signal worker exit. Defaults to None. |
None
|
init_done_event
|
Event
|
Event to signal initialization completion. Defaults to None. |
None
|
log_level
|
str
|
Logging level. Defaults to None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
init_done_event |
Event
|
Event to signal initialization completion. |
tf_base_path |
str
|
Base path for files folder saved using |
connections_lock |
Lock
|
Lock for managing connections. |
nornir_inventory |
dict
|
Inventory data for Nornir. |
watchdog |
WatchDog
|
Watchdog instance for monitoring. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |
worker_exit() -> None
¤
Executes all functions registered under the "nornir-exit" hook in the inventory.
This method iterates through the list of hooks associated with the "nornir-exit" key in the inventory's hooks.
For each hook, it calls the function specified in the hook, passing the current
instance (self) as the first argument, followed by any additional positional
and keyword arguments specified in the hook.
Source code in norfab\workers\nornir_worker\nornir_worker.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
init_nornir(inventory: dict) -> None
¤
Initializes the Nornir automation framework with the provided inventory.
This method first closes any existing Nornir connections if present, optionally emitting a progress event. It then creates a new Nornir instance using the supplied inventory dictionary, which should contain configuration for logging, runner, hosts, groups, defaults, and user-defined settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inventory
|
dict
|
A dictionary containing Nornir inventory and configuration options. |
required |
Source code in norfab\workers\nornir_worker\nornir_worker.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
filter_hosts_and_validate(kwargs: Dict[str, Any], ret: Result) -> Tuple[Any, Result]
¤
Helper method to filter hosts and validate results.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[Any, Result]
|
(filtered_nornir, Result) where Result status set to
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
update_nornir_result(result: Any, ret: Result) -> None
¤
Track failed hosts and add executed and failed hosts to the result.
Source code in norfab\workers\nornir_worker\nornir_worker.py
529 530 531 532 533 | |
load_job_data(job_data: Any) -> Dict
¤
Helper function to download job data YAML files and load it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_data
|
str
|
job data NorFab file path to download and load using YAML. |
required |
Returns:dict data: The job data loaded from the YAML string.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the job data is a URL and the file download fails. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | |
jinja2_network_hosts(network: str, pfxlen: bool = False) -> list
¤
Custom Jinja2 filter that return a list of hosts for a given IP network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
str
|
The network address in CIDR notation. |
required |
pfxlen
|
bool
|
If True, include the prefix length in the returned host addresses. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
A list of host addresses as strings. If pfxlen is True, each address will include the prefix length. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | |
jinja2_nb_create_ip(prefix: str, device: str = None, interface: str = None, **kwargs: object) -> str
¤
Jinja2 filter to get or create next available IP address from prefix using Netbox service.
Source code in norfab\workers\nornir_worker\nornir_worker.py
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | |
jinja2_nb_create_prefix(parent: str, description: str, prefixlen: int = 30, **kwargs: object) -> str
¤
Jinja2 filter to get or create next available prefix from parent prefix using Netbox service.
Source code in norfab\workers\nornir_worker\nornir_worker.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | |
jinja2_nb_filter(object_type: str, fields: list = None, **filters: object) -> list[dict]
¤
Jinja2 helper to filter objects from NetBox.
Source code in norfab\workers\nornir_worker\nornir_worker.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 | |
jinja2_call_netbox(netbox_task: str) -> callable
¤
Returns a callable function to execute arbitrary NetBox service task.
Source code in norfab\workers\nornir_worker\nornir_worker.py
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 | |
add_jinja2_netbox() -> Dict
¤
Aggregates Jinja2 NetBox-related methods and functions into a dictionary for the ease of use within Jinja2 templates.
Source code in norfab\workers\nornir_worker\nornir_worker.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 | |
add_jinja2_filters() -> Dict
¤
Adds custom filters for use in Jinja2 templates using | syntaxis.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict
|
A dictionary where the keys are the names of the filters and the values are the corresponding filter functions.
|
Source code in norfab\workers\nornir_worker\nornir_worker.py
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | |
get_version() -> Result
¤
Retrieve the versions of various libraries and system information.
This method collects the version information of a predefined set of libraries and system details such as the Python version and platform. It attempts to import each library and fetch its version. If a library is not found, it is skipped.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Result
|
a dictionary with the library names as keys and their respective version numbers as values. If a library is not found, its value will be an empty string. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 | |
get_watchdog_connections() -> Result
¤
Retrieve the list of connections currently managed by watchdog.
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
An instance of the Result class containing the current watchdog connections. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | |
errdisabled_hosts_list() -> Result
¤
List hosts currently held in Nornir's failed-host state.
Source code in norfab\workers\nornir_worker\nornir_worker.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 | |
errdisabled_hosts_clear() -> Result
¤
Recover all hosts currently held in Nornir's failed-host state.
Source code in norfab\workers\nornir_worker\nornir_worker.py
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 | |
refresh_nornir(job: Job) -> Result
¤
Refreshes the Nornir instance by reloading the inventory from configured sources.
This method performs the following steps:
1. Loads the inventory configuration from the broker.
2. If Netbox is specified in the inventory, pulls inventory data from Netbox.
3. If Containerlab is specified in the inventory, pulls inventory data from Containerlab.
4. Initializes the Nornir instance with the refreshed inventory.
Args: job: NorFab Job object containing relevant metadata
The inventory configuration is expected to be a dictionary with the following keys:
- "logging": A dictionary specifying logging configuration (default: {"enabled": False}).
- "runner": A dictionary specifying runner options (default: {}).
- "hosts": A dictionary specifying host details (default: {}).
- "groups": A dictionary specifying group details (default: {}).
- "defaults": A dictionary specifying default values (default: {}).
- "user_defined": A dictionary specifying user-defined options (default: {}).
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
A Result object indicating the outcome of the refresh operation. |
Source code in norfab\workers\nornir_worker\nornir_worker.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 | |