Skip to content
Permalink
Browse files

subprocess.Popen -> cmk.utils.cmk_subprocess.Popen (1)

Change-Id: Ib7440066692dc750bff6ed12fc55f0e0f86e629b
  • Loading branch information
si-23 committed Nov 29, 2019
1 parent 831c101 commit a3cd5b4228677bcd68fa9b78e6436ac5e86e117c
Showing with 72 additions and 45 deletions.
  1. +11 −4 agents/special/agent_ibmsvc
  2. +13 −10 agents/special/agent_ipmi_sensors
  3. +13 −8 bin/mkbackup
  4. +35 −23 bin/mkbench
@@ -31,7 +31,7 @@ from __future__ import print_function
import os
import sys
import getopt
import subprocess
import cmk.utils.cmk_subprocess as subprocess


def usage():
@@ -234,18 +234,25 @@ if opt_debug:
sys.stderr.write("executing external command: %s\n" % cmd)

result = subprocess.Popen( # nosec
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=None)
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=None,
encoding="utf-8",
)
stdout, stderr = result.communicate()
exit_code = result.wait()

if exit_code not in [0, 1]:
sys.stderr.write("Error connecting via ssh: %s\n" % stderr)
msg = "Error connecting via ssh: %s\n" % stderr
sys.stderr.write(msg.encode("utf-8"))
sys.exit(2)

lines = stdout.split('\n')

if lines[0].startswith("CMMVC7016E") or (len(lines) > 1 and lines[1].startswith("CMMVC7016E")):
sys.stderr.write(stdout)
sys.stderr.write(stdout.encode("utf-8"))
sys.exit(2)

# Quite strange.. Why not simply print stdout?
@@ -28,7 +28,7 @@ import os
import errno
import sys
import getopt
import subprocess
import cmk.utils.cmk_subprocess as subprocess


def agent_ipmi_sensors_usage():
@@ -75,9 +75,9 @@ def parse_data(data, excludes):
has_excludes = True
break
if not has_excludes:
sys.stdout.write("%s\n" % line)
sys.stdout.write(("%s\n" % line).encode("utf-8"))
else:
sys.stdout.write("%s\n" % line)
sys.stdout.write(("%s\n" % line).encode("utf-8"))


def agent_ipmi_sensors_main(cmdline_args):
@@ -179,12 +179,14 @@ def agent_ipmi_sensors_main(cmdline_args):
sys.stdout.write("<<<ipmi%s:sep(124)>>>\n" % section)
try:
try:
p = subprocess.Popen(ipmi_cmd + types,
shell=False,
close_fds=True,
stdin=open(os.devnull),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p = subprocess.Popen(
ipmi_cmd + types,
close_fds=True,
stdin=open(os.devnull),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
except OSError as e:
if e.errno == errno.ENOENT: # No such file or directory
raise Exception("Could not find '%s' command (PATH: %s)"\
@@ -200,7 +202,8 @@ def agent_ipmi_sensors_main(cmdline_args):
errors.append(e)

if errors:
sys.stderr.write("ERROR: '%s'.\n" % ", ".join(errors))
msg = "ERROR: '%s'.\n" % ", ".join(errors)
sys.stderr.write(msg.encode("utf-8"))
sys.exit(1)

sys.exit(0)
@@ -37,7 +37,6 @@ import re
import shutil
import socket
import signal
import subprocess
import sys
import syslog
import tempfile
@@ -59,6 +58,8 @@ import cmk.utils.schedule as schedule
import cmk.utils.store as store
from cmk.utils.exceptions import MKTerminate, MKGeneralException

import cmk.utils.cmk_subprocess as subprocess

VERSION = "1.4.0i1"


@@ -111,7 +112,8 @@ def stop_logging():


def log(s):
sys.stdout.write("%s %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), s))
msg = "%s %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), s)
sys.stdout.write(msg.encode("utf-8"))
if is_cma():
syslog.syslog(s)

@@ -697,12 +699,15 @@ def start_site(site):
def omd_command(*args):
cmd = ["omd"] + list(args)
verbose("Command: %s" % " ".join(cmd))
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=open(os.devnull),
close_fds=True)
stdout = p.communicate()[0]
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=open(os.devnull),
close_fds=True,
encoding="utf-8",
)
stdout, _stderr = p.communicate()
verbose(stdout)
if p.returncode != 0:
raise MKGeneralException("Failed to run <tt>%s</tt>: %s" % (" ".join(cmd), stdout))
@@ -69,7 +69,6 @@ import logging
import tempfile
import threading
import traceback
import subprocess
import collections
import multiprocessing
import six
@@ -84,6 +83,7 @@ from cmk.utils.exceptions import (
MKGeneralException,
MKTerminate,
)
import cmk.utils.cmk_subprocess as subprocess

#.
# .--Helpers-------------------------------------------------------------.
@@ -517,11 +517,14 @@ class TestRunner(threading.Thread):
with open("%s/etc/rrdcached.conf" % omd_root(), "w") as f:
f.write("TIMEOUT=300\n" "RANDOM_DELAY=10\n" "FLUSH_TIMEOUT=7200\n" "WRITE_THREADS=4\n")

p = subprocess.Popen(["omd", "restart", "rrdcached"],
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout = p.communicate()[0]
p = subprocess.Popen(
["omd", "restart", "rrdcached"],
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)
stdout, _stderr = p.communicate()
if p.returncode != 0:
raise MKGeneralException("Failed to restart rrdcached: %s" % stdout)

@@ -2693,14 +2696,17 @@ class TestAgentBasedStandardHosts(TestCase):
shutil.copy(src_file, "%s/%s.mk" % (autochecks_dir, host_name))

def _discover_services_of_host(self, host_name):
p = subprocess.Popen(["cmk", "-II", host_name],
stdin=open(os.devnull),
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p = subprocess.Popen(
["cmk", "-II", host_name],
stdin=open(os.devnull),
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)

try:
stdout = p.communicate()[0]
stdout, _stderr = p.communicate()
except KeyboardInterrupt:
try:
p.terminate()
@@ -2785,14 +2791,17 @@ class TestAgentBasedStandardHosts(TestCase):
def _create_rrds_of_host(self, host_name):
# Do 2 iterations to initialize the rrds that base on counter values
for _i in xrange(2):
p = subprocess.Popen(["cmk", "-v", host_name],
stdin=open(os.devnull),
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p = subprocess.Popen(
["cmk", "-v", host_name],
stdin=open(os.devnull),
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)

try:
stdout = p.communicate()[0]
stdout, _stderr = p.communicate()
except KeyboardInterrupt:
try:
p.terminate()
@@ -2824,13 +2833,16 @@ class TestAgentBasedStandardHosts(TestCase):
def _activate_changes(self):
if self._test:
self._test.info(" Activating changes")
p = subprocess.Popen(["cmk", "-R"],
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p = subprocess.Popen(
["cmk", "-R"],
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)

try:
stdout = p.communicate()[0]
stdout, _stderr = p.communicate()
except KeyboardInterrupt:
try:
p.terminate()

0 comments on commit a3cd5b4

Please sign in to comment.
You can’t perform that action at this time.