@@ -628,27 +628,44 @@ def read_all_ups_list_items(self, command_list, errups=True):
628628 results [ups_name ] = self .read_ups_list_items (command_list )
629629 return results
630630
631- def read_ups_list_items (self , command_list ):
632- results = {'display_name' : self .ups_name (),
633- 'name' : self .ups_name (),
634- 'uuid' : self .ups_uuid (),
635- 'ups_IP' : self .ups_ip (),
636- 'ups_type' : self .ups_type ()}
631+ def read_ups_list_items (self , command_list , tups = None ):
632+ """ Read the specified list of monitor mib commands for all UPSs.
633+ :param command_list: A list of mib commands to be read from the active UPS
634+ :type command_list: list
635+ :param tups: The target ups dictionary from list or None.
636+ :type tups: dict
637+ :return: dict of results from the reading of all commands target UPS.
638+ """
639+ if not tups :
640+ tups = self .active_ups
641+ results = {'display_name' : self .ups_name (tups = tups ),
642+ 'name' : self .ups_name (tups = tups ),
643+ 'uuid' : self .ups_uuid (tups = tups ),
644+ 'ups_IP' : self .ups_ip (tups = tups ),
645+ 'ups_type' : self .ups_type (tups = tups )}
637646 for cmd in command_list :
638- results [cmd ] = self .send_snmp_command (cmd )
647+ results [cmd ] = self .send_snmp_command (cmd , tups = tups )
639648 return results
640649
641- def send_snmp_command (self , command_name , target_ups = None , display = False ):
642- if not target_ups :
643- target_ups = self .active_ups
644- if not self .is_responsive (target_ups ):
650+ def send_snmp_command (self , command_name , tups = None , display = False ):
651+ """ Read the specified mib commands results for specified UPS or active UPS if not specified.
652+ :param command_name: A command to be read from the target UPS
653+ :type command_name: str
654+ :param tups: The target ups dictionary from list or None.
655+ :type tups: dict
656+ :param display: If true the results will be printed
657+ :type display: bool
658+ :return: The results from the read
659+ """
660+ if not tups :
661+ tups = self .active_ups
662+ if not self .is_responsive (tups ):
645663 return 'Invalid UPS'
646- snmp_mib_commands = self .get_mib_commands (target_ups )
664+ snmp_mib_commands = self .get_mib_commands (tups )
647665 if command_name not in snmp_mib_commands :
648666 return 'No data'
649667 cmd_mib = snmp_mib_commands [command_name ]['iso' ]
650- cmd_str = 'snmpget -v2c -c {} {} {}' .format (target_ups ['snmp_community' ],
651- target_ups ['ups_IP' ], cmd_mib )
668+ cmd_str = 'snmpget -v2c -c {} {} {}' .format (tups ['snmp_community' ], tups ['ups_IP' ], cmd_mib )
652669 try :
653670 snmp_output = subprocess .check_output (shlex .split (cmd_str ), shell = False ,
654671 stderr = subprocess .DEVNULL ).decode ().split ('\n ' )
@@ -667,7 +684,7 @@ def send_snmp_command(self, command_name, target_ups=None, display=False):
667684 if snmp_mib_commands [command_name ]['decode' ]:
668685 if value in snmp_mib_commands [command_name ]['decode' ].keys ():
669686 value = snmp_mib_commands [command_name ]['decode' ][value ]
670- if target_ups ['ups_type' ] == 'eaton-pw' :
687+ if tups ['ups_type' ] == 'eaton-pw' :
671688 if command_name == 'mib_output_voltage' or command_name == 'mib_output_frequency' :
672689 value = int (value ) / 10.0
673690 elif command_name == 'mib_output_current' :
@@ -676,11 +693,11 @@ def send_snmp_command(self, command_name, target_ups=None, display=False):
676693 value = int (value ) / 10.0
677694 elif command_name == 'mib_system_temperature' :
678695 value = int (value ) / 10.0
679- if command_name == 'mib_system_status' and target_ups ['ups_type' ] == 'apc-ap9630' :
696+ if command_name == 'mib_system_status' and tups ['ups_type' ] == 'apc-ap9630' :
680697 value = self .bit_str_decoder (value , self .decoders ['apc_system_status' ])
681698 if command_name == 'mib_time_on_battery' or command_name == 'mib_battery_runtime_remain' :
682699 # Create a minute, string tuple
683- if target_ups ['ups_type' ] == 'eaton-pw' :
700+ if tups ['ups_type' ] == 'eaton-pw' :
684701 # Process time for eaton-pw
685702 if command_name == 'mib_time_on_battery' :
686703 # Measured in seconds.
@@ -704,10 +721,11 @@ def send_snmp_command(self, command_name, target_ups=None, display=False):
704721 @staticmethod
705722 def bit_str_decoder (value , decode_key ):
706723 """ Bit string decoder
707-
708- :param value: A string representing a bit encoded set of flags
709- :param decode_key: A list representing the meaning of a 1 for each bit field
710- :returns: A string of concatenated bit decode strings
724+ :param value: A string representing a bit encoded set of flags
725+ :type value: str
726+ :param decode_key: A list representing the meaning of a 1 for each bit field
727+ :type decode_key: list
728+ :return: A string of concatenated bit decode strings
711729 """
712730 value_str = ''
713731 for index , bit_value in enumerate (value ):
@@ -721,6 +739,11 @@ def bit_str_decoder(value, decode_key):
721739 return value_str
722740
723741 def print_snmp_commands (self , tups = None ):
742+ """ Print all supported mib commands for the target UPS, which is the active UPS when not specified.
743+ :param tups: The target ups dictionary from list or None.
744+ :type tups: dict
745+ :return: None
746+ """
724747 if not tups :
725748 tups = self .active_ups
726749 for k , v in self .get_mib_commands (tups ).items ():
@@ -733,6 +756,9 @@ def print_snmp_commands(self, tups=None):
733756
734757 # Set parameters required for daemon mode.
735758 def set_daemon_parameters (self ):
759+ """ Set all daemon parameters based on defaults in env.ut_const and the config.py file.
760+ :return: None
761+ """
736762 if env .ut_const .ERROR_config :
737763 print ('Error in config.py file. Using defaults' )
738764 return
@@ -818,11 +844,17 @@ def set_daemon_parameters(self):
818844 print ('Invalid threshold_battery_capacity_warn in config.py. Using default.' )
819845
820846 def print_daemon_parameters (self ):
847+ """ Print all daemon parameters.
848+ :return: None
849+ """
821850 print ('Daemon parameters:' )
822851 for k , v in self .daemon_params .items ():
823852 print (' {}: {}' .format (k , v ))
824853
825854 def shutdown (self ):
855+ """ Execute the shutdown script as defined in the daemon parameters.
856+ :return: None
857+ """
826858 if not self .daemon_params ['shutdown_script' ]:
827859 print ('No shutdown script defined' )
828860 return
@@ -838,6 +870,9 @@ def shutdown(self):
838870 file = sys .stderr )
839871
840872 def cancel_shutdown (self ):
873+ """ Execute the cancel shutdown script as defined in the daemon parameters.
874+ :return: None
875+ """
841876 if not self .daemon_params ['cancel_shutdown_script' ]:
842877 print ('No cancel shutdown script defined' )
843878 return
@@ -853,6 +888,9 @@ def cancel_shutdown(self):
853888 self .daemon_params ['cancel_shutdown_script' ]), file = sys .stderr )
854889
855890 def resume (self ):
891+ """ Execute the resume script as defined in the daemon parameters.
892+ :return: None
893+ """
856894 if not self .daemon_params ['resume_script' ]:
857895 print ('No resume script defined' )
858896 return
@@ -868,6 +906,9 @@ def resume(self):
868906 file = sys .stderr )
869907
870908 def suspend (self ):
909+ """ Execute the suspend script as defined in the daemon parameters.
910+ :return: None
911+ """
871912 if not self .daemon_params ['suspend_script' ]:
872913 print ('No suspend script defined' )
873914 return
@@ -885,6 +926,9 @@ def suspend(self):
885926
886927
887928def about ():
929+ """ Display details about this module.
930+ :return: None
931+ """
888932 # About me
889933 print (__doc__ )
890934 print ("Author: " , __author__ )
0 commit comments