mirror of
https://github.com/centreon/centreon-plugins.git
synced 2025-09-25 18:58:39 +02:00
tests with centreon_connector_perl
This commit is contained in:
parent
fefa587f63
commit
6839d7c85b
@ -16,7 +16,7 @@
|
|||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
|
||||||
|
|
||||||
package os::linux::snmp::plugin;
|
package os::linux::snmp::plugin;
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ class ConnectorLibrary:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.process = None
|
self.process = None
|
||||||
|
|
||||||
def start_connector(self, command=["/usr/lib64/centreon-connector/centreon_connector_perl", "--log-file=/tmp/connector.log", "--debug"]):
|
def ctn_start_connector(self, command=["/usr/lib64/centreon-connector/centreon_connector_perl", "--log-file=/tmp/connector.log", "--debug"]):
|
||||||
if self.process is None or self.process.poll() is not None:
|
if self.process is None or self.process.poll() is not None:
|
||||||
self.process = subprocess.Popen(
|
self.process = subprocess.Popen(
|
||||||
command,
|
command,
|
||||||
@ -20,7 +20,7 @@ class ConnectorLibrary:
|
|||||||
)
|
)
|
||||||
print("Connector started")
|
print("Connector started")
|
||||||
|
|
||||||
def send_to_connector(self, idf: int, command: str, timeout: int, command_log="/tmp/connector.commands.log"):
|
def ctn_send_to_connector(self, idf: int, command: str, timeout: int, command_log="/tmp/connector.commands.log"):
|
||||||
now = int(time.time())
|
now = int(time.time())
|
||||||
|
|
||||||
# Log to console
|
# Log to console
|
||||||
@ -46,7 +46,7 @@ class ConnectorLibrary:
|
|||||||
else:
|
else:
|
||||||
raise RuntimeError("Connector is not running.")
|
raise RuntimeError("Connector is not running.")
|
||||||
|
|
||||||
def write_next_output_to_file(self, output_file="/tmp/connector.output"):
|
def ctn_write_next_output_to_file(self, output_file="/tmp/connector.output"):
|
||||||
"""
|
"""
|
||||||
Reads the next line from the connector's stdout and appends it to output_file.
|
Reads the next line from the connector's stdout and appends it to output_file.
|
||||||
"""
|
"""
|
||||||
@ -58,13 +58,13 @@ class ConnectorLibrary:
|
|||||||
return line
|
return line
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def stop_connector(self):
|
def ctn_stop_connector(self):
|
||||||
if self.process:
|
if self.process:
|
||||||
self.process.terminate()
|
self.process.terminate()
|
||||||
self.process = None
|
self.process = None
|
||||||
print("Connector stopped")
|
print("Connector stopped")
|
||||||
|
|
||||||
def wait_for_output_file(output_file="/tmp/connector.output", timeout=10, poll_interval=0.2):
|
def ctn_wait_for_output_file(output_file="/tmp/connector.output", timeout=10, poll_interval=0.2):
|
||||||
end_time = time.time() + timeout
|
end_time = time.time() + timeout
|
||||||
while time.time() < end_time:
|
while time.time() < end_time:
|
||||||
if os.path.exists(output_file):
|
if os.path.exists(output_file):
|
||||||
@ -72,8 +72,8 @@ def wait_for_output_file(output_file="/tmp/connector.output", timeout=10, poll_i
|
|||||||
time.sleep(poll_interval)
|
time.sleep(poll_interval)
|
||||||
raise FileNotFoundError(f"Output file {output_file} not found after {timeout} seconds")
|
raise FileNotFoundError(f"Output file {output_file} not found after {timeout} seconds")
|
||||||
|
|
||||||
def read_from_output_file(idf: int, output_file="/tmp/connector.output", wait_timeout=10):
|
def ctn_read_from_output_file(idf: int, output_file="/tmp/connector.output", wait_timeout=10):
|
||||||
wait_for_output_file(output_file, wait_timeout)
|
ctn_wait_for_output_file(output_file, wait_timeout)
|
||||||
with open(output_file, "r") as f:
|
with open(output_file, "r") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@ -81,27 +81,27 @@ def read_from_output_file(idf: int, output_file="/tmp/connector.output", wait_ti
|
|||||||
return line.strip().split(" ", 1)[1]
|
return line.strip().split(" ", 1)[1]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def start_connector():
|
def ctn_start_connector():
|
||||||
global connector
|
global connector
|
||||||
connector = ConnectorLibrary()
|
connector = ConnectorLibrary()
|
||||||
connector.start_connector()
|
connector.ctn_start_connector()
|
||||||
return connector
|
return connector
|
||||||
|
|
||||||
def stop_connector():
|
def ctn_stop_connector():
|
||||||
global connector
|
global connector
|
||||||
if connector:
|
if connector:
|
||||||
connector.stop_connector()
|
connector.ctn_stop_connector()
|
||||||
connector = None
|
connector = None
|
||||||
else:
|
else:
|
||||||
print("No connector to stop.")
|
print("No connector to stop.")
|
||||||
|
|
||||||
def send_to_connector(idf: int, command: str, timeout: int = 5, output_file=None, command_log="/tmp/connector.commands.log"):
|
def ctn_send_to_connector(idf: int, command: str, timeout: int = 5, output_file=None, command_log="/tmp/connector.commands.log"):
|
||||||
global connector
|
global connector
|
||||||
if connector:
|
if connector:
|
||||||
connector.send_to_connector(idf, command, timeout, command_log)
|
connector.ctn_send_to_connector(idf, command, timeout, command_log)
|
||||||
# Capture the output line after sending command
|
# Capture the output line after sending command
|
||||||
output_file = output_file or f"/tmp/connector.output.{idf}"
|
output_file = output_file or f"/tmp/connector.output.{idf}"
|
||||||
line = connector.write_next_output_to_file(output_file)
|
line = connector.ctn_write_next_output_to_file(output_file)
|
||||||
# Copy per-test file to common output
|
# Copy per-test file to common output
|
||||||
if output_file != "/tmp/connector.output":
|
if output_file != "/tmp/connector.output":
|
||||||
import shutil
|
import shutil
|
||||||
@ -111,16 +111,16 @@ def send_to_connector(idf: int, command: str, timeout: int = 5, output_file=None
|
|||||||
raise RuntimeError("Connector is not running.")
|
raise RuntimeError("Connector is not running.")
|
||||||
|
|
||||||
|
|
||||||
def wait_for_result(idf: int, timeout: int = 5, poll_interval=0.2):
|
def ctn_wait_for_result(idf: int, timeout: int = 5, poll_interval=0.2):
|
||||||
end_time = time.time() + timeout
|
end_time = time.time() + timeout
|
||||||
while time.time() < end_time:
|
while time.time() < end_time:
|
||||||
result = read_from_output_file(idf)
|
result = ctn_read_from_output_file(idf)
|
||||||
if result:
|
if result:
|
||||||
return result
|
return result
|
||||||
time.sleep(poll_interval)
|
time.sleep(poll_interval)
|
||||||
raise TimeoutError(f"No result found for id {idf} within {timeout} seconds.")
|
raise TimeoutError(f"No result found for id {idf} within {timeout} seconds.")
|
||||||
|
|
||||||
def clean_connector_output(line):
|
def ctn_clean_connector_output(line):
|
||||||
if not isinstance(line, str):
|
if not isinstance(line, str):
|
||||||
line = str(line)
|
line = str(line)
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
@ -135,7 +135,7 @@ def clean_connector_output(line):
|
|||||||
print(f"CLEANED: {repr(cleaned)}")
|
print(f"CLEANED: {repr(cleaned)}")
|
||||||
return cleaned
|
return cleaned
|
||||||
|
|
||||||
def extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None):
|
def ctn_extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None):
|
||||||
"""
|
"""
|
||||||
Find the line with 'reporting check result' and the right check id.
|
Find the line with 'reporting check result' and the right check id.
|
||||||
Write the 'output:' content to output_path.
|
Write the 'output:' content to output_path.
|
||||||
@ -160,7 +160,7 @@ def extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None)
|
|||||||
raise Exception(f"No result found for id {tc} in log {log_path}")
|
raise Exception(f"No result found for id {tc} in log {log_path}")
|
||||||
|
|
||||||
@keyword
|
@keyword
|
||||||
def extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None):
|
def ctn_extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None):
|
||||||
"""
|
"""
|
||||||
Find the line with 'reporting check result' and the right check id.
|
Find the line with 'reporting check result' and the right check id.
|
||||||
Write the 'output:' content to output_path.
|
Write the 'output:' content to output_path.
|
||||||
|
@ -14,7 +14,7 @@ ${SNMPPORT} 2024
|
|||||||
${SNMPVERSION} 2c
|
${SNMPVERSION} 2c
|
||||||
${PERCENT} %
|
${PERCENT} %
|
||||||
${MOCKOON_LOG_FILE} /tmp/mockoon.log
|
${MOCKOON_LOG_FILE} /tmp/mockoon.log
|
||||||
${TIMEOUT} 30
|
${TIMEOUT} 120
|
||||||
|
|
||||||
*** Keywords ***
|
*** Keywords ***
|
||||||
Start Mockoon
|
Start Mockoon
|
||||||
@ -56,7 +56,7 @@ Ctn Generic Suite Setup
|
|||||||
Set Environment Variable TZ UTC
|
Set Environment Variable TZ UTC
|
||||||
Remove Files /tmp/connector.output.*
|
Remove Files /tmp/connector.output.*
|
||||||
Remove File /tmp/connector.log
|
Remove File /tmp/connector.log
|
||||||
Start Connector
|
Ctn Start Connector
|
||||||
|
|
||||||
Ctn Run Command And Check Result As Regexp
|
Ctn Run Command And Check Result As Regexp
|
||||||
[Arguments] ${command} ${expected_result}
|
[Arguments] ${command} ${expected_result}
|
||||||
@ -94,9 +94,9 @@ Ctn Run Command With Connector And Check Result As Strings
|
|||||||
[Arguments] ${tc} ${command} ${expected_result} ${timeout}=5
|
[Arguments] ${tc} ${command} ${expected_result} ${timeout}=5
|
||||||
Remove File /tmp/connector.output
|
Remove File /tmp/connector.output
|
||||||
Remove File /tmp/connector.command.log
|
Remove File /tmp/connector.command.log
|
||||||
Send To Connector ${tc} ${command} ${timeout}
|
Ctn Send To Connector ${tc} ${command} ${timeout}
|
||||||
# 1. Extract the result from the log and write to /tmp/connector.output
|
# 1. Extract the result from the log and write to /tmp/connector.output
|
||||||
Extract Result From Log ${tc}
|
Ctn Extract Result From Log ${tc}
|
||||||
# 2. Read the file
|
# 2. Read the file
|
||||||
${output} Get File /tmp/connector.output
|
${output} Get File /tmp/connector.output
|
||||||
${output} Strip String ${output}
|
${output} Strip String ${output}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user