Skip to content

Commit 2f0c507

Browse files
committed
fix(updates): "The syntax of the command is incorrect."
1 parent 22336a6 commit 2f0c507

File tree

4 files changed

+29
-45
lines changed

4 files changed

+29
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1212
Monitoring Plugins:
1313

1414
* disk-usage: handle disk accessibility ([#792](https://github.com/Linuxfabrik/monitoring-plugins/issues/792))
15+
* updates: "The syntax of the command is incorrect."
1516

1617

1718
### Changed ("refactor", "chore" etc.)

check-plugins/updates/README.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ Fact Sheet
1717
"Check Interval Recommendation", "Once a day"
1818
"Can be called without parameters", "Yes"
1919
"Compiled for Windows", "Yes"
20-
"3rd Party Python modules", "``psutil``"
2120

2221

2322
Help
2423
----
2524

2625
.. code-block:: text
2726
28-
usage: updates [-h] [-V] [--always-ok] [-c CRIT] [--timeout TIMEOUT] [-w WARN]
27+
usage: updates [-h] [-V] [--always-ok] [-c CRIT] [-w WARN]
2928
3029
Checks the number of pending Windows updates.
3130
@@ -35,7 +34,6 @@ Help
3534
--always-ok Always returns OK.
3635
-c, --critical CRIT Set the critical threshold for the number of pending
3736
updates. Default: 50
38-
--timeout TIMEOUT Network timeout in seconds. Default: 300 (seconds)
3937
-w, --warning WARN Set the warning threshold for the number of pending
4038
updates. Default: 2
4139

check-plugins/updates/icingaweb2-module-director/updates.json

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
"--critical": {
99
"value": "$updates_windows_critical$"
1010
},
11-
"--timeout": {
12-
"value": "$updates_windows_timeout$"
13-
},
1411
"--warning": {
1512
"value": "$updates_windows_warning$"
1613
}
@@ -32,11 +29,6 @@
3229
"datafield_id": 3,
3330
"is_required": "n",
3431
"var_filter": null
35-
},
36-
{
37-
"datafield_id": 4,
38-
"is_required": "n",
39-
"var_filter": null
4032
}
4133
],
4234
"imports": [],
@@ -110,7 +102,6 @@
110102
"criticality": "C",
111103
"updates_windows_always_ok": false,
112104
"updates_windows_critical": 50,
113-
"updates_windows_timeout": 300,
114105
"updates_windows_warning": 2
115106
},
116107
"volatile": null,
@@ -140,17 +131,6 @@
140131
"uuid": "9188240c-b08e-450a-bb30-3388c215f68d"
141132
},
142133
"3": {
143-
"varname": "updates_windows_timeout",
144-
"caption": "Updates: Timeout",
145-
"description": "Network timeout in seconds.",
146-
"datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString",
147-
"format": null,
148-
"settings": {
149-
"visibility": "visible"
150-
},
151-
"uuid": "de8dfdee-a209-40d3-95b7-239d22b1bed0"
152-
},
153-
"4": {
154134
"varname": "updates_windows_warning",
155135
"caption": "Updates: Warning",
156136
"description": "Set the warning threshold for the number of pending updates.",

check-plugins/updates/updates

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import sys # pylint: disable=C0413
1616

1717
import lib.args # pylint: disable=C0413
1818
import lib.base # pylint: disable=C0413
19-
import lib.shell # pylint: disable=C0413
20-
from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
21-
STATE_UNKNOWN, STATE_WARN)
19+
import lib.powershell # pylint: disable=C0413
20+
import lib.txt # pylint: disable=C0413
21+
from lib.globals import (STATE_OK, STATE_UNKNOWN)
22+
2223

2324
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
24-
__version__ = '2023112901'
25+
__version__ = '2025061701'
2526

2627
DESCRIPTION = 'Checks the number of pending Windows updates.'
2728

28-
DEFAULT_CRIT = 50 # #updates
29-
DEFAULT_TIMEOUT = 300 # seconds
30-
DEFAULT_WARN = 2 # #updates
29+
DEFAULT_CRIT = 50 # #updates
30+
DEFAULT_WARN = 2 # #updates
3131

3232

3333
def parse_args():
@@ -38,7 +38,7 @@ def parse_args():
3838
parser.add_argument(
3939
'-V', '--version',
4040
action='version',
41-
version='{0}: v{1} by {2}'.format('%(prog)s', __version__, __author__),
41+
version=f'%(prog)s: v{__version__} by {__author__}'
4242
)
4343

4444
parser.add_argument(
@@ -53,23 +53,25 @@ def parse_args():
5353
'-c', '--critical',
5454
default=DEFAULT_CRIT,
5555
dest='CRIT',
56-
help='Set the critical threshold for the number of pending updates. Default: %(default)s',
56+
help='Set the critical threshold for the number of pending updates. '
57+
'Default: %(default)s',
5758
type=lib.args.int_or_none,
5859
)
5960

6061
parser.add_argument(
6162
'--timeout',
62-
help='Network timeout in seconds. Default: %(default)s (seconds)',
63+
help=argparse.SUPPRESS,
6364
dest='TIMEOUT',
6465
type=int,
65-
default=DEFAULT_TIMEOUT,
66+
default=300,
6667
)
6768

6869
parser.add_argument(
6970
'-w', '--warning',
7071
default=DEFAULT_WARN,
7172
dest='WARN',
72-
help='Set the warning threshold for the number of pending updates. Default: %(default)s',
73+
help='Set the warning threshold for the number of pending updates. '
74+
'Default: %(default)s',
7375
type=lib.args.int_or_none,
7476
)
7577

@@ -88,31 +90,34 @@ def main():
8890

8991
# fetch data
9092
cmd = '''
91-
powershell -Command "
9293
$WindowsUpdates = New-Object -ComObject 'Microsoft.Update.Session';
9394
$SearchIndex = $WindowsUpdates.CreateUpdateSearcher();
9495
$Pending = $SearchIndex.Search('IsInstalled=0');
9596
foreach ($update in $Pending.Updates) {
96-
[string]$name = [string]::Format('{0} [{1}]', $update.Title, $update.LastDeploymentChangeTime);
97+
$name = "{0} [{1}]" -f $update.Title, $update.LastDeploymentChangeTime;
9798
Write-Output $name;
9899
}
99-
"'''
100+
'''
100101

101102
# execute the shell command and return its result and exit code
102-
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(cmd, timeout=args.TIMEOUT))
103-
if stderr:
104-
# Could not find any documentation in the return codes of 'query user'.
105-
# Therefore we assume any is ok.
106-
lib.base.cu(stderr)
107-
result = stdout.strip()
103+
result = lib.powershell.run_ps(cmd)
104+
if not result:
105+
lib.base.cu('Error calling PowerShell.')
106+
if result['retc']:
107+
lib.base.cu(result['stderr'])
108+
result = result['stdout'].strip()
108109

109110
count = sum(1 for line in result.splitlines())
110111
perfdata = lib.base.get_perfdata('pending_updates', count, None, args.WARN, args.CRIT, 0, None)
111112

112113
if count:
113114
state = lib.base.get_state(count, args.WARN, args.CRIT)
114115
lib.base.oao(
115-
'There are {} pending updates:\n\n* '.format(count) + result.replace('\n', '\n* '),
116+
(
117+
f"There {lib.txt.pluralize('', count, 'is,are')} {count} pending "
118+
f"{lib.txt.pluralize('update', count)}:\n\n"
119+
f"* {result.replace('\n', '\n* ')}"
120+
),
116121
state,
117122
perfdata,
118123
always_ok=args.ALWAYS_OK,

0 commit comments

Comments
 (0)