From e41106ca79e9c80c278021ca1cfd3b42aa1d3d6a Mon Sep 17 00:00:00 2001 From: Andreas Maier Date: Wed, 14 Aug 2024 09:27:28 +0200 Subject: [PATCH] Preparation for support of redundant HMCs Details: * Changed the format of the exporter config file to prepare for future support of a set of redundant HMCs, since the config file format changes in 2.0 anyway. This avoids a future config file format change when we add the support for redundant HMCs. Signed-off-by: Andreas Maier --- docs/usage.rst | 10 ++-- tests/test_all.py | 24 ++++----- tests/test_config_upgrade.py | 50 +++++++++---------- .../schemas/config_schema.yaml | 45 +++++++++-------- .../zhmc_prometheus_exporter.py | 45 +++++++++-------- 5 files changed, 91 insertions(+), 83 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 8c03b513..0d857b56 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -853,11 +853,11 @@ The exporter config file is in YAML format and has the following structure: version: 2 - hmc: - host: {hmc-ip-address} - userid: {hmc-userid} - password: {hmc-password} - verify_cert: {hmc-verify-cert} + hmcs: + - host: {hmc-ip-address} + userid: {hmc-userid} + password: {hmc-password} + verify_cert: {hmc-verify-cert} prometheus: # optional port: {prom-port} diff --git a/tests/test_all.py b/tests/test_all.py index 9f326e17..add82788 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -305,16 +305,16 @@ def test_timeout(self): hmc_api_version = (2, 37) hmc_features = [] config_dict = { - "hmc": { - "host": "192.168.0.0", "userid": "user", "password": "pwd" - }, + "hmcs": [ + {"host": "192.168.0.0", "userid": "user", "password": "pwd"} + ], } session = zhmc_prometheus_exporter.create_session( config_dict, "filename") config_dict = { - "hmc": { - "host": "192.168.0.0", "userid": "user", "password": "pwd" - }, + "hmcs": [ + {"host": "192.168.0.0", "userid": "user", "password": "pwd"} + ], "metric_groups": {} } yaml_metric_groups = {} @@ -528,9 +528,9 @@ def test_init(self): session = setup_faked_session() config_dict = { - "hmc": { - "host": "192.168.0.0", "userid": "user", "password": "pwd" - }, + "hmcs": [ + {"host": "192.168.0.0", "userid": "user", "password": "pwd"} + ], "metric_groups": { "dpm-system-usage-overview": {"export": True}, } @@ -583,9 +583,9 @@ def test_collect(self): session = setup_faked_session() config_dict = { - "hmc": { - "host": "192.168.0.0", "userid": "user", "password": "pwd" - }, + "hmcs": [ + {"host": "192.168.0.0", "userid": "user", "password": "pwd"} + ], "metric_groups": { "dpm-system-usage-overview": {"export": True}, } diff --git a/tests/test_config_upgrade.py b/tests/test_config_upgrade.py index 131dd062..8d73c981 100644 --- a/tests/test_config_upgrade.py +++ b/tests/test_config_upgrade.py @@ -84,8 +84,8 @@ password: mypassword """, f"""version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: true @@ -112,8 +112,8 @@ value: HMC1 """, f"""version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: false @@ -145,8 +145,8 @@ """, f"""# exporter config file version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: false @@ -167,8 +167,8 @@ ( "Version 2 config file without metric_groups", """version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: false @@ -177,8 +177,8 @@ value: HMC1 """, """version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: false @@ -196,14 +196,14 @@ ( "Version 2 config file without verify_cert", """version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword """, """version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword """, @@ -217,15 +217,15 @@ ( "Version 2 config file with metric_groups", f"""version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword {DEFAULT_METRIC_GROUPS} """, f"""version: 2 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword {DEFAULT_METRIC_GROUPS} @@ -246,19 +246,19 @@ None, [], zhmc_prometheus_exporter.ImproperExit, - r"^The exporter config file must specify either the new 'hmc' item " + r"^The exporter config file must specify either the new 'hmcs' item " r"or the old 'metrics' item, but it specifies none\.$" ), ( - "Invalid version 1 config file with both 'metrics' and 'hmc' items", + "Invalid version 1 config file with both 'metrics' and 'hmcs' items", """metrics: hmc: 9.10.11.12 userid: myuser password: mypassword verify_cert: false -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: false @@ -266,15 +266,15 @@ None, [], zhmc_prometheus_exporter.ImproperExit, - r"^The exporter config file must specify either the new 'hmc' item " + r"^The exporter config file must specify either the new 'hmcs' item " r"or the old 'metrics' item, but it specifies both\.$" ), ( "Config file with unknown version", """version: 3 -hmc: - host: 9.10.11.12 +hmcs: +- host: 9.10.11.12 userid: myuser password: mypassword verify_cert: false diff --git a/zhmc_prometheus_exporter/schemas/config_schema.yaml b/zhmc_prometheus_exporter/schemas/config_schema.yaml index 7a286b40..d6a704df 100644 --- a/zhmc_prometheus_exporter/schemas/config_schema.yaml +++ b/zhmc_prometheus_exporter/schemas/config_schema.yaml @@ -10,27 +10,30 @@ properties: version: description: "Version of the config file format" type: integer - hmc: - description: The HMC host and credentials - type: object - required: - - host - - userid - - password - additionalProperties: false - properties: - host: - description: "Hostname or IP address of HMC" - type: string - userid: - description: "Userid used by the exporter to log on to the HMC" - type: string - password: - description: "Password of the HCM userid" - type: string - verify_cert: - description: "Controls whether and how the HMC certificate is verified. For details, see doc section 'HMC certificate'" - type: [boolean, string] + hmcs: + description: The set of redundant HMCs + type: array + default: [] + items: + type: object + required: + - host + - userid + - password + additionalProperties: false + properties: + host: + description: "Hostname or IP address of HMC" + type: string + userid: + description: "Userid used by the exporter to log on to the HMC" + type: string + password: + description: "Password of the HCM userid" + type: string + verify_cert: + description: "Controls whether and how the HMC certificate is verified. For details, see doc section 'HMC certificate'" + type: [boolean, string] metrics: description: "Deprecated: The HMC host and credentials" type: object diff --git a/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py b/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py index 52d81bfb..f8af261b 100755 --- a/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py +++ b/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py @@ -301,19 +301,19 @@ def help_config(): # Version of config file format version: 2 -# HMC and its credentials -hmc: - host: 9.10.11.12 - userid: userid - password: password - # Note: The verify_cert parameter controls whether and how the HMC server - # certificate is validated by the exporter. For more details, - # see doc section 'HMC certificate'. - # verify_cert: true # (default) Validate using default CA certs - # verify_cert: my_certs_file # Validate using this CA certs file - # verify_cert: my_certs_dir # Validate using this CA certs directory - # verify_cert: false # Disable validation - verify_cert: false +# Redundant HMCs and their credentials +hmcs: + - host: 9.10.11.12 + userid: userid + password: password + # Note: The verify_cert parameter controls whether and how the HMC server + # certificate is validated by the exporter. For more details, + # see doc section 'HMC certificate'. + # verify_cert: true # (default) Validate using default CA certs + # verify_cert: my_certs_file # Validate using this CA certs file + # verify_cert: my_certs_dir # Validate using this CA certs directory + # verify_cert: false # Disable validation + verify_cert: false # Communication with Prometheus prometheus: @@ -505,16 +505,16 @@ def upgrade_config_dict(config_dict, config_filename, upgrade_config=False): Upgrade the config dict to the current exporter version. """ - if 'metrics' not in config_dict and 'hmc' not in config_dict: + if 'metrics' not in config_dict and 'hmcs' not in config_dict: new_exc = ImproperExit( - "The exporter config file must specify either the new 'hmc' " + "The exporter config file must specify either the new 'hmcs' " "item or the old 'metrics' item, but it specifies none.") new_exc.__cause__ = None # pylint: disable=invalid-name raise new_exc - if 'metrics' in config_dict and 'hmc' in config_dict: + if 'metrics' in config_dict and 'hmcs' in config_dict: new_exc = ImproperExit( - "The exporter config file must specify either the new 'hmc' " + "The exporter config file must specify either the new 'hmcs' " "item or the old 'metrics' item, but it specifies both.") new_exc.__cause__ = None # pylint: disable=invalid-name raise new_exc @@ -527,7 +527,7 @@ def upgrade_config_dict(config_dict, config_filename, upgrade_config=False): new_exc.__cause__ = None # pylint: disable=invalid-name raise new_exc - if 'metrics' in config_dict and 'hmc' not in config_dict: + if 'metrics' in config_dict and 'hmcs' not in config_dict: # Exporter config file has version 1 if upgrade_config: @@ -551,7 +551,7 @@ def upgrade_config_dict(config_dict, config_filename, upgrade_config=False): 'password': old_creds['password'], 'verify_cert': old_creds.get('verify_cert', True), } - config_dict.insert(1, key='hmc', value=hmc_item) + config_dict.insert(1, key='hmcs', value=[hmc_item]) del config_dict['metrics'] if 'metric_groups' not in config_dict: @@ -814,7 +814,12 @@ def create_session(config_dict, config_filename): # These warnings do not concern us urllib3.disable_warnings() - hmc_dict = config_dict["hmc"] + hmcs = config_dict["hmcs"] + if not hmcs: + raise ImproperExit( + "The 'hmcs' item in the exporter config file does not specify " + "any HMCs.") + hmc_dict = hmcs[0] logprint(logging.INFO, PRINT_V, f"HMC host: {hmc_dict['host']}")