Skip to content

Commit 2ff0723

Browse files
committed
improvded error handling for _command where the
executed command exits with non-zero exit code Also improved command building to by more syntacitcally correct.
1 parent 19163bf commit 2ff0723

File tree

1 file changed

+16
-8
lines changed
  • openwisp_monitoring/check/classes

1 file changed

+16
-8
lines changed

openwisp_monitoring/check/classes/ping.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,18 @@ def check(self, store=True):
8888
command = [
8989
'fping',
9090
'-e', # show elapsed (round-trip) time of packets
91-
'-c %s' % count, # count of pings to send to each target,
92-
'-p %s' % interval, # interval between sending pings(in ms)
93-
'-b %s' % bytes_, # amount of ping data to send
94-
'-t %s' % timeout, # individual target initial timeout (in ms)
91+
'-c', str(count), # count of pings to send to each target
92+
'-p', str(interval), # interval between sending pings (in ms)
93+
'-b', str(bytes_), # amount of ping data to send
94+
'-t', str(timeout), # individual target initial timeout (in ms)
9595
'-q',
96-
ip,
96+
ip
9797
]
98-
stdout, stderr = self._command(command)
98+
stdout, stderr, returncode = self._command(command)
99+
# Check the return code and raise an exception if it's not zero
100+
if returncode not in [0, 1]:
101+
message = f'fping command failed with return code {returncode}:\n\n{stderr.decode("utf8")}'
102+
raise OperationalError(message)
99103
# fpings shows statistics on stderr
100104
output = stderr.decode('utf8')
101105
try:
@@ -148,8 +152,12 @@ def _command(self, command):
148152
"""
149153
Executes command (easier to mock)
150154
"""
151-
p = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
152-
return p.stdout, p.stderr
155+
try:
156+
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
157+
stdout, stderr = p.communicate()
158+
return stdout, stderr, p.returncode
159+
except Exception as e:
160+
return None, str(e), 99
153161

154162
def _get_metric(self):
155163
"""

0 commit comments

Comments
 (0)