Skip to content

Commit 5dfd752

Browse files
committed
refactor(dhcp-scope-usage): improve code
1 parent cc37fbf commit 5dfd752

File tree

3 files changed

+63
-36
lines changed

3 files changed

+63
-36
lines changed

check-plugins/dhcp-scope-usage/README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
Checks the IPv4 scope usage for a Windows DHCP server service using the PowerShell command `Get-DhcpServerv4ScopeStatistics -ComputerName "dhcpServer.contoso.com"`. Have a look at <https://docs.microsoft.com/en-us/powershell/module/dhcpserver/get-dhcpserverv4scopestatistics> for details.
66

7-
If you provide `--winrm-hostname`, the check plugin will execute all Powershell commands via WinRM, otherwise it will run locally. This allows the plugin to run on Linux servers as well. By using the [winrm Python module](https://github.com/diyan/pywinrm), this plugin supports various transport methods in order to authenticate with the WinRM server. The options that are supported in the transport parameter are:
8-
9-
* `basic`: Basic auth only works for local Windows accounts, not domain accounts. Credentials are base64 encoded when sending to the server.
10-
* `kerberos`: Will use Kerberos authentication for domain accounts which only works when the client is in the same domain as the server and the required dependencies are installed. Obtain a Kerberos ticket on Linux (`kinit`) or use a keytab. Ensure `/etc/krb5.conf` and DNS/SPNs are correct so Kerberos to the Windows service succeeds.
11-
* `ntlm`: Will use NTLM authentication for both domain and local accounts (default).
7+
If you provide `--winrm-hostname`, the check plugin will execute all Powershell commands via WinRM, otherwise it will run locally. This allows the plugin to run on Linux servers as well.
128

139
Hints:
1410

@@ -32,9 +28,9 @@ Hints:
3228
```text
3329
usage: dhcp-scope-usage [-h] [-V] [--always-ok] [-c CRIT] [-H HOSTNAME]
3430
[--test TEST] [-w WARN] [--winrm-domain WINRM_DOMAIN]
35-
[--winrm-hostname WINRM_HOSTNAME]
36-
[--winrm-password WINRM_PASSWORD]
37-
[--winrm-transport {basic,ntlm,kerberos}]
31+
--winrm-hostname WINRM_HOSTNAME
32+
--winrm-password WINRM_PASSWORD
33+
[--winrm-transport {basic,ntlm,kerberos,credssp,plaintext}]
3834
[--winrm-username WINRM_USERNAME]
3935
4036
Checks the IPv4 scope usage for a Windows DHCP server service.
@@ -54,14 +50,14 @@ options:
5450
--winrm-domain WINRM_DOMAIN
5551
WinRM Domain Name. Default: None
5652
--winrm-hostname WINRM_HOSTNAME
57-
Target Windows computer on which the Windows commands
58-
are to be executed. Default: None
53+
Target Windows computer on which the command will be
54+
executed.
5955
--winrm-password WINRM_PASSWORD
60-
WinRM Account Password. Default: None
61-
--winrm-transport {basic,ntlm,kerberos}
56+
WinRM Account Password.
57+
--winrm-transport {basic,ntlm,kerberos,credssp,plaintext}
6258
WinRM transport type. Default: ntlm
6359
--winrm-username WINRM_USERNAME
64-
WinRM Account Name. Default: None
60+
WinRM Account Name. Default: Administrator
6561
```
6662

6763

check-plugins/dhcp-scope-usage/dhcp-scope-usage

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2424
STATE_UNKNOWN, STATE_WARN)
2525

2626
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
27-
__version__ = '2025100601'
27+
__version__ = '2025103002'
2828

2929
DESCRIPTION = """Checks the IPv4 scope usage for a Windows DHCP server service."""
3030

31+
DEFAULT_CRIT = 90
3132
DEFAULT_HOSTNAME = 'localhost'
3233
DEFAULT_WARN = 80
33-
DEFAULT_CRIT = 90
34+
DEFAULT_WINRM_DOMAIN = None
35+
DEFAULT_WINRM_TRANSPORT = 'ntlm'
36+
DEFAULT_WINRM_USERNAME = 'Administrator'
3437

3538

3639
def parse_args():
@@ -84,40 +87,44 @@ def parse_args():
8487

8588
parser.add_argument(
8689
'--winrm-domain',
87-
help='WinRM Domain Name. Default: %(default)s',
90+
help='WinRM Domain Name. '
91+
'Default: %(default)s',
8892
dest='WINRM_DOMAIN',
89-
default=None,
93+
default=DEFAULT_WINRM_DOMAIN,
9094
)
9195

9296
parser.add_argument(
9397
'--winrm-hostname',
94-
help='Target Windows computer on which the Windows commands are to be executed. Default: %(default)s',
98+
help='Target Windows computer on which the command will be executed.',
9599
dest='WINRM_HOSTNAME',
96-
default=None,
100+
required=True,
97101
)
98102

99103
parser.add_argument(
100104
'--winrm-password',
101-
help='WinRM Account Password. Default: %(default)s',
105+
help='WinRM Account Password.',
102106
dest='WINRM_PASSWORD',
103-
default=None,
107+
required=True,
104108
)
105109

106110
parser.add_argument(
107111
'--winrm-transport',
108-
help='WinRM transport type. Default: %(default)s',
112+
help='WinRM transport type. '
113+
'Default: %(default)s',
109114
dest='WINRM_TRANSPORT',
110-
choices=['basic', 'ntlm', 'kerberos'],
111-
default='ntlm',
115+
choices=['basic', 'ntlm', 'kerberos', 'credssp', 'plaintext'],
116+
default=DEFAULT_WINRM_TRANSPORT,
112117
)
113118

114119
parser.add_argument(
115120
'--winrm-username',
116-
help='WinRM Account Name. Default: %(default)s',
121+
help='WinRM Account Name. '
122+
'Default: %(default)s',
117123
dest='WINRM_USERNAME',
118-
default=None,
124+
default=DEFAULT_WINRM_USERNAME,
119125
)
120126

127+
121128
args, _ = parser.parse_known_args()
122129
return args
123130

@@ -142,8 +149,6 @@ def main():
142149
if lib.base.LINUX:
143150
lib.base.cu('Running this check plugin directly on Linux is not supported. Use the --winrm parameters.')
144151
result = lib.powershell.run_ps(cmd)
145-
if not result:
146-
lib.base.cu('Got nothing back.')
147152
stdout, stderr, retc = result['stdout'], result['stderr'], result['retc']
148153
else:
149154
# do not call the command, put in test data

check-plugins/dhcp-scope-usage/icingaweb2-module-director/dhcp-scope-usage.json

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@
6060
},
6161
{
6262
"datafield_id": 6,
63-
"is_required": "n",
63+
"is_required": "y",
6464
"var_filter": null
6565
},
6666
{
6767
"datafield_id": 7,
68-
"is_required": "n",
68+
"is_required": "y",
6969
"var_filter": null
7070
},
7171
{
@@ -149,12 +149,12 @@
149149
},
150150
{
151151
"datafield_id": 15,
152-
"is_required": "n",
152+
"is_required": "y",
153153
"var_filter": null
154154
},
155155
{
156156
"datafield_id": 16,
157-
"is_required": "n",
157+
"is_required": "y",
158158
"var_filter": null
159159
},
160160
{
@@ -241,7 +241,8 @@
241241
"dhcp_scope_usage_critical": 90,
242242
"dhcp_scope_usage_hostname": "localhost",
243243
"dhcp_scope_usage_warning": 80,
244-
"dhcp_scope_usage_winrm_transport": "ntlm"
244+
"dhcp_scope_usage_winrm_transport": "ntlm",
245+
"dhcp_scope_usage_winrm_username": "Administrator"
245246
},
246247
"volatile": null,
247248
"zone": null,
@@ -291,7 +292,8 @@
291292
"dhcp_scope_usage_windows_critical": 90,
292293
"dhcp_scope_usage_windows_hostname": "localhost",
293294
"dhcp_scope_usage_windows_warning": 80,
294-
"dhcp_scope_usage_windows_winrm_transport": "ntlm"
295+
"dhcp_scope_usage_windows_winrm_transport": "ntlm",
296+
"dhcp_scope_usage_windows_winrm_username": "Administrator"
295297
},
296298
"volatile": null,
297299
"zone": null,
@@ -320,6 +322,18 @@
320322
"entry_value": "Kerberos",
321323
"format": "string",
322324
"allowed_roles": null
325+
},
326+
{
327+
"entry_name": "credssp",
328+
"entry_value": "Credssp",
329+
"format": "string",
330+
"allowed_roles": null
331+
},
332+
{
333+
"entry_name": "plaintext",
334+
"entry_value": "Plaintext",
335+
"format": "string",
336+
"allowed_roles": null
323337
}
324338
],
325339
"uuid": "aa2cdf76-9797-4c27-b90c-27de3d88824f"
@@ -345,6 +359,18 @@
345359
"entry_value": "Kerberos",
346360
"format": "string",
347361
"allowed_roles": null
362+
},
363+
{
364+
"entry_name": "credssp",
365+
"entry_value": "Credssp",
366+
"format": "string",
367+
"allowed_roles": null
368+
},
369+
{
370+
"entry_name": "plaintext",
371+
"entry_value": "Plaintext",
372+
"format": "string",
373+
"allowed_roles": null
348374
}
349375
],
350376
"uuid": "6f7bcd30-f6e4-490c-b30f-5955422d7bbe"
@@ -407,7 +433,7 @@
407433
"6": {
408434
"varname": "dhcp_scope_usage_winrm_hostname",
409435
"caption": "Dhcp Scope Usage: Winrm Hostname",
410-
"description": "Target Windows computer on which the Windows commands are to be executed.",
436+
"description": "Target Windows computer on which the command will be executed.",
411437
"datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString",
412438
"format": null,
413439
"settings": {
@@ -506,7 +532,7 @@
506532
"15": {
507533
"varname": "dhcp_scope_usage_windows_winrm_hostname",
508534
"caption": "Dhcp Scope Usage: Winrm Hostname",
509-
"description": "Target Windows computer on which the Windows commands are to be executed.",
535+
"description": "Target Windows computer on which the command will be executed.",
510536
"datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString",
511537
"format": null,
512538
"settings": {

0 commit comments

Comments
 (0)