Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit 6b5d86e

Browse files
authored
Merge pull request #67 from IBM/error_catching
Exception-Catching
2 parents f7a7303 + 17bc34d commit 6b5d86e

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

python/sppmon.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
from subprocess import CalledProcessError
6767
import sys
6868
import time
69-
from typing import Any, Dict, List, NoReturn, Union
69+
from typing import Any, Dict, List, NoReturn, Union, Optional
7070

7171
from influx.influx_client import InfluxClient
7272
from sppConnection.api_queries import ApiQueries
@@ -145,7 +145,8 @@
145145
LOGGER_NAME = 'sppmon'
146146
LOGGER = logging.getLogger(LOGGER_NAME)
147147

148-
ERROR_CODE_CMD_LINE = 2
148+
ERROR_CODE_START_ERROR = 3
149+
ERROR_CODE_CMD_ARGS = 2
149150
ERROR_CODE = 1
150151

151152

@@ -261,13 +262,13 @@ class SppMon:
261262
"""Configured spp log rentation time, logs get deleted after this time."""
262263

263264
# set later in each method, here to avoid missing attribute
264-
influx_client: InfluxClient = None
265-
rest_client: RestClient = None
266-
api_queries: ApiQueries = None
267-
system_methods: SystemMethods = None
268-
job_methods: JobMethods = None
269-
protection_methods: ProtectionMethods = None
270-
ssh_methods: SshMethods = None
265+
influx_client: Optional[InfluxClient] = None
266+
rest_client: Optional[RestClient] = None
267+
api_queries: Optional[ApiQueries] = None
268+
system_methods: Optional[SystemMethods] = None
269+
job_methods: Optional[JobMethods] = None
270+
protection_methods: Optional[ProtectionMethods] = None
271+
ssh_methods: Optional[SshMethods] = None
271272

272273
def __init__(self):
273274
self.log_path: str = ""
@@ -280,7 +281,7 @@ def __init__(self):
280281
LOGGER.info("Starting SPPMon")
281282
if(not self.check_pid_file()):
282283
ExceptionUtils.error_message("Another instance of sppmon with the same args is running")
283-
self.exit(ERROR_CODE_CMD_LINE)
284+
self.exit(ERROR_CODE_START_ERROR)
284285

285286
# everything is option, otherwise its a typo.
286287
if(len(ARGS) > 0):
@@ -296,12 +297,12 @@ def __init__(self):
296297

297298
if(not OPTIONS.confFileJSON):
298299
ExceptionUtils.error_message("missing config file, aborting")
299-
self.exit(error_code=ERROR_CODE_CMD_LINE)
300+
self.exit(error_code=ERROR_CODE_CMD_ARGS)
300301
try:
301302
self.config_file = SppUtils.read_conf_file(config_file_path=OPTIONS.confFileJSON)
302303
except ValueError as error:
303304
ExceptionUtils.exception_info(error=error, extra_message="Error when trying to read Config file, unable to read")
304-
self.exit(error_code=ERROR_CODE_CMD_LINE)
305+
self.exit(error_code=ERROR_CODE_START_ERROR)
305306

306307
LOGGER.info("Setting up configurations")
307308
self.setup_args()
@@ -422,7 +423,7 @@ def set_critial_configs(self, config_file: Dict[str, Any]) -> None:
422423
"""
423424
if(not config_file):
424425
ExceptionUtils.error_message("missing or empty config file, aborting")
425-
self.exit(error_code=ERROR_CODE_CMD_LINE)
426+
self.exit(error_code=ERROR_CODE_START_ERROR)
426427
try:
427428
# critical components only
428429
self.influx_client = InfluxClient(config_file)
@@ -446,7 +447,10 @@ def set_optional_configs(self, config_file: Dict[str, Any]) -> None:
446447

447448
if(not config_file):
448449
ExceptionUtils.error_message("missing or empty config file, aborting.")
449-
self.exit(error_code=ERROR_CODE_CMD_LINE)
450+
self.exit(error_code=ERROR_CODE_START_ERROR)
451+
if(not self.influx_client):
452+
ExceptionUtils.error_message("Influx client is somehow missing. aborting")
453+
self.exit(error_code=ERROR_CODE)
450454

451455
# ############################ REST-API #####################################
452456
try:
@@ -571,7 +575,7 @@ def setup_args(self) -> None:
571575
if((OPTIONS.create_dashboard or bool(OPTIONS.dashboard_folder_path)) and not
572576
(OPTIONS.create_dashboard and bool(OPTIONS.dashboard_folder_path))):
573577
ExceptionUtils.error_message("> Using --create_dashboard without associated folder path. Aborting.")
574-
self.exit(ERROR_CODE_CMD_LINE)
578+
self.exit(ERROR_CODE_CMD_ARGS)
575579

576580
# incremental setup, higher executes all below
577581
all_args: bool = OPTIONS.all
@@ -677,12 +681,15 @@ def exit(self, error_code: int = False) -> NoReturn:
677681

678682
# error with the command line arguments
679683
# dont store runtime here
680-
if(error_code == ERROR_CODE_CMD_LINE):
684+
if(error_code == ERROR_CODE_CMD_ARGS):
681685
prog_args = []
682686
prog_args.append(sys.argv[0])
683687
prog_args.append("--help")
684688
os.execv(sys.executable, ['python'] + prog_args)
685-
sys.exit(ERROR_CODE_CMD_LINE) # unreachable?
689+
sys.exit(ERROR_CODE_CMD_ARGS) # unreachable?
690+
if(error_code == ERROR_CODE_START_ERROR):
691+
ExceptionUtils.error_message("Error when starting SPPMon. Please review the errors above")
692+
sys.exit(ERROR_CODE_START_ERROR)
686693

687694
script_end_time = SppUtils.get_actual_time_sec()
688695
LOGGER.debug("Script end time: %d", script_end_time)
@@ -725,7 +732,7 @@ def main(self):
725732
try:
726733
self.system_methods.sites()
727734
self.influx_client.flush_insert_buffer()
728-
except ValueError as error:
735+
except Exception as error:
729736
ExceptionUtils.exception_info(
730737
error=error,
731738
extra_message="Top-level-error when requesting sites, skipping them all")
@@ -734,7 +741,7 @@ def main(self):
734741
try:
735742
self.system_methods.cpuram()
736743
self.influx_client.flush_insert_buffer()
737-
except ValueError as error:
744+
except Exception as error:
738745
ExceptionUtils.exception_info(
739746
error=error,
740747
extra_message="Top-level-error when collecting cpu stats, skipping them all")
@@ -743,7 +750,7 @@ def main(self):
743750
try:
744751
self.system_methods.sppcatalog()
745752
self.influx_client.flush_insert_buffer()
746-
except ValueError as error:
753+
except Exception as error:
747754
ExceptionUtils.exception_info(
748755
error=error,
749756
extra_message="Top-level-error when collecting file system stats, skipping them all")
@@ -754,7 +761,7 @@ def main(self):
754761
try:
755762
self.job_methods.get_all_jobs()
756763
self.influx_client.flush_insert_buffer()
757-
except ValueError as error:
764+
except Exception as error:
758765
ExceptionUtils.exception_info(
759766
error=error,
760767
extra_message="Top-level-error when requesting jobs, skipping them all")
@@ -764,7 +771,7 @@ def main(self):
764771
try:
765772
self.job_methods.job_logs()
766773
self.influx_client.flush_insert_buffer()
767-
except ValueError as error:
774+
except Exception as error:
768775
ExceptionUtils.exception_info(
769776
error=error,
770777
extra_message="Top-level-error when requesting job logs, skipping them all")
@@ -776,7 +783,7 @@ def main(self):
776783
try:
777784
self.ssh_methods.ssh()
778785
self.influx_client.flush_insert_buffer()
779-
except ValueError as error:
786+
except Exception as error:
780787
ExceptionUtils.exception_info(
781788
error=error,
782789
extra_message="Top-level-error when excecuting ssh commands, skipping them all")
@@ -786,7 +793,7 @@ def main(self):
786793
try:
787794
self.protection_methods.store_vms()
788795
self.influx_client.flush_insert_buffer()
789-
except ValueError as error:
796+
except Exception as error:
790797
ExceptionUtils.exception_info(
791798
error=error,
792799
extra_message="Top-level-error when requesting all VMs, skipping them all")
@@ -797,7 +804,7 @@ def main(self):
797804
self.protection_methods.vms_per_sla()
798805
self.protection_methods.sla_dumps()
799806
self.influx_client.flush_insert_buffer()
800-
except ValueError as error:
807+
except Exception as error:
801808
ExceptionUtils.exception_info(
802809
error=error,
803810
extra_message="Top-level-error when requesting and computing VMs per sla, skipping them all")
@@ -807,7 +814,7 @@ def main(self):
807814
try:
808815
self.protection_methods.create_inventory_summary()
809816
self.influx_client.flush_insert_buffer()
810-
except ValueError as error:
817+
except Exception as error:
811818
ExceptionUtils.exception_info(
812819
error=error,
813820
extra_message="Top-level-error when creating inventory summary, skipping them all")
@@ -816,7 +823,7 @@ def main(self):
816823
try:
817824
self.protection_methods.vadps()
818825
self.influx_client.flush_insert_buffer()
819-
except ValueError as error:
826+
except Exception as error:
820827
ExceptionUtils.exception_info(
821828
error=error,
822829
extra_message="Top-level-error when requesting vadps, skipping them all")
@@ -825,7 +832,7 @@ def main(self):
825832
try:
826833
self.protection_methods.storages()
827834
self.influx_client.flush_insert_buffer()
828-
except ValueError as error:
835+
except Exception as error:
829836
ExceptionUtils.exception_info(
830837
error=error,
831838
extra_message="Top-level-error when collecting storages, skipping them all")
@@ -835,7 +842,7 @@ def main(self):
835842
if(OPTIONS.copy_database):
836843
try:
837844
self.influx_client.copy_database(OPTIONS.copy_database)
838-
except ValueError as error:
845+
except Exception as error:
839846
ExceptionUtils.exception_info(
840847
error=error,
841848
extra_message="Top-level-error when coping database.")
@@ -845,7 +852,7 @@ def main(self):
845852
if(OPTIONS.test):
846853
try:
847854
OtherMethods.test_connection(self.influx_client, self.rest_client, self.config_file)
848-
except ValueError as error:
855+
except Exception as error:
849856
ExceptionUtils.exception_info(
850857
error=error,
851858
extra_message="Top-level-error when testing connection.")
@@ -859,7 +866,7 @@ def main(self):
859866
OtherMethods.create_dashboard(
860867
dashboard_folder_path=OPTIONS.dashboard_folder_path,
861868
database_name=self.influx_client.database.name)
862-
except ValueError as error:
869+
except Exception as error:
863870
ExceptionUtils.exception_info(
864871
error=error,
865872
extra_message="Top-level-error when creating dashboard")

python/sppmonMethods/other.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sppmonMethods.ssh import SshMethods
1010
from utils.methods_utils import MethodUtils
1111
from influx.influx_client import InfluxClient
12-
from typing import Dict, Any, List
12+
from typing import Dict, Any, List, Optional
1313
import logging
1414
import os
1515
import re
@@ -22,7 +22,7 @@
2222
class OtherMethods:
2323

2424
@staticmethod
25-
def test_connection(influx_client: InfluxClient, rest_client: RestClient, config_file: Dict[str, Any]):
25+
def test_connection(influx_client: InfluxClient, rest_client: Optional[RestClient], config_file: Dict[str, Any]):
2626
if(not config_file):
2727
raise ValueError("SPPmon does not work without a config file")
2828

@@ -49,6 +49,8 @@ def test_connection(influx_client: InfluxClient, rest_client: RestClient, config
4949

5050
LOGGER.info("> Testing REST-API of SPP.")
5151
try:
52+
if(not rest_client):
53+
raise ValueError("Rest-client is setup. Unavailable to test it.")
5254
rest_client.login()
5355
(version_nr, build_nr) = rest_client.get_spp_version_build()
5456
LOGGER.info(f">> Sucessfully connected to SPP V{version_nr}, build {build_nr}.")

python/sppmonMethods/protection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ def storages(self) -> None:
123123
# get calulated extra info
124124
for row in result:
125125
row['siteName'] = self.__system_methods.site_name_by_id(row['site'])
126-
if('free' in row and 'total' in row
127-
and row['free'] > 0 and row['total'] > 0):
126+
if('free' in row and row['free'] != None and
127+
'total' in row and row['total'] != None and
128+
row['total'] > 0):
129+
128130
row['used'] = row['total'] - row['free']
129131
row['pct_free'] = row['free'] / row['total'] * 100
130132
row['pct_used'] = row['used'] / row['total'] * 100

python/utils/methods_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ def query_something(
132132

133133
LOGGER.info("> getting %s", name)
134134

135-
# request all Sites from SPP
135+
# request information from SPP via the api_queries.py file
136136
elem_list = source_func()
137137
if(not elem_list):
138-
ExceptionUtils.error_message(f">> No {name} are found")
138+
LOGGER.info(f"WARNING: No {name} are returned when requesting from server")
139139

140140
if(rename_tuples):
141141
for elem in elem_list:

0 commit comments

Comments
 (0)