def to_stdout(storage_endpoints, args): """Print to stdout the storage stats information including memcache indices. Does this for each StorageShare belonging to each StorageEndpoint in the storage_endpoints list, including the last warning/error. Connects to a memcached instance and tries to obtain the storage stats from the index belonging to the storage_share's. Arguments: storage_endpoints -- list of dynafed_storagestats StorageEndpoint objects. args -- argparse object. Argument flags args.debug -- full warning/error information from the exceptions. args.memcached_ip -- memcached instance IP. args.memcached_port -- memcached instance Port. """ for _storage_endpoint in storage_endpoints: for _storage_share in _storage_endpoint.storage_shares: _memcached_index = "Ugrstoragestats_" + _storage_share.id try: _memcached_contents = memcache.get(_memcached_index, args.memcached_ip, args.memcached_port) except dynafed_storagestats.exceptions.MemcachedIndexError as ERR: _memcached_contents = 'No content found or error connecting to memcached service.' _storage_share.debug.append("[ERROR]" + ERR.debug) print('\n#####', _storage_share.id, '#####' \ '\n{0:12}{1}'.format('URL:', _storage_share.uri['url']), \ '\n{0:12}{1}'.format('Protocol:', _storage_share.storageprotocol), \ '\n{0:12}{1}'.format('Time:', _storage_share.stats['starttime']), \ '\n{0:12}{1}'.format('Quota:', _storage_share.stats['quota']), \ '\n{0:12}{1}'.format('Bytes Used:', _storage_share.stats['bytesused']), \ '\n{0:12}{1}'.format('Bytes Free:', _storage_share.stats['bytesfree']), \ '\n{0:12}{1}'.format('FileCount:', _storage_share.stats['filecount']), \ '\n{0:12}{1}'.format('Status:', _storage_share.status), \ ) print('\nMemcached:', \ '\n{0:12}{1}'.format('Index:', _memcached_index), \ '\n{0:12}{1}'.format('Contents:', _memcached_contents), \ ) if args.debug: print('\nDebug:') for _error in _storage_share.debug: print('{0:12}{1}'.format(' ', _error))
def get_cached_storage_stats(storage_share_objects, return_as='string', memcached_ip='127.0.0.1', memcached_port='11211'): """Obtain storage status string in memcached and return as requested object. Check if memcached has cached storage status for the StorageShares, which would have been uploaded by this script previously. Turn the string of information into a dictionary of StorageShare ID's and their stats, which is then returned. The returned object can be a string containing all StorageShares, or an array of strings separated by StorageShares, or a dictionary whose keys are the StorageShare ID's and the value is a string with all the stats, or a dictionary of dictionaries where each stat has a key/value as well. This is specified with the 'return_as' argument as: 'string' -- single sting containing all StorageShares, each delimieted '&&' 'array' -- each StorageShare's string is separated and the array returned. 'dictionary' -- each key is the StorageShare ID's and the value is a string with all the stats. 'expanded_dictionary' -- each key is the StorageShare ID's and each value is a key under it with it's own value. Arguments: storage_share_objects -- list of dynafed_storagestats StorageShare objects. memcached_ip -- memcached instance IP. memcahced_port -- memcached instance Port. Returns: array OR dictionary OR string """ _logger.info( "Checking memcached server %s:%s for StorageShares storage stats.", memcached_ip, memcached_port) _array_of_stats = [] for _storage_share in storage_share_objects: # Index in memcache to look for: _idx = 'Ugrstoragestats_' + _storage_share.id # Obtain the latest status uploaded by UGR and typecast to str if needed. # Different versions of memcache module return bytes. _logger.debug("Using memcached storage stats index: %s", _idx) _storage_stats = memcache.get(_idx, memcached_ip, memcached_port) # Typecast to str if needed. Different versions of memcache module return # bytes. if isinstance(_storage_stats, bytes): _storage_stats = str(_storage_stats, 'utf-8') _array_of_stats.append(_storage_stats) _logger.debug("Storage stats obtained: %s", _array_of_stats) # Format and return according to request 'return_as' if return_as == 'string': return '&&'.join(_array_of_stats) elif return_as == 'array': return _array_of_stats elif return_as == 'dictionary': _dictonary_of_stats = {} for _element in _array_of_stats: _storage_share, _stats = _element.split("%%", 1) _dictonary_of_stats[_storage_share] = _stats return _dictonary_of_stats elif return_as == 'expanded_dictionary': _dictonary_of_stats = {} for _element in _array_of_stats: _storage_share, _protocol, _timestamp, _quota, _bytesused, _bytesfree, _status = _element.split( "%%") _dictonary_of_stats[_storage_share] = { 'timestamp': _timestamp, 'bytesused': _bytesused, 'bytesfree': _bytesfree, } return _dictonary_of_stats
def get_cached_connection_stats(return_as='string', memcached_ip='127.0.0.1', memcached_port='11211'): """Obtain connection status string in memcached and return as requested object. Check if memcached has cached connection status for the StorageShares, which would have been uploaded by UGR/Dynafed's periodic test. Turn the string of information into a dictionary of StorageShare ID's and their stats, which is then returned. The returned object can be a sting containing all StorageShares, or an array of strings separated by StorageShares, or a dictionary whose keys are the StorageShare ID's and the value is a string with all the stats, or a dictionary of dictionaries where each stat has a key/value as well. This is specified with the 'return_as' argument as: 'string' -- single sting containing all StorageShares, each delimieted '&&' 'array' -- each StorageShare's string is separated and the array returned. 'dictionary' -- each key is the StorageShare ID's and the value is a string with all the stats. 'expanded_dictionary' -- each key is the StorageShare ID's and each value is a key under it with it's own value. Arguments: memcached_ip -- memcached instance IP. memcahced_port -- memcached instance Port. Returns: array OR dictionary OR string """ _logger.info( "Checking memcached server %s:%s for StorageShares connection stats.", memcached_ip, memcached_port) # Obtain the latest index number used by UGR and typecast to str if # needed. Different versions of memcache module return bytes. _idx = memcache.get('Ugrpluginstats_idx', memcached_ip, memcached_port) if isinstance(_idx, bytes): _idx = str(_idx, 'utf-8') # Obtain the latest status uploaded by UGR and typecast to str if needed. # Different versions of memcache module return bytes. _logger.debug("Using memcached connection stats index: Ugrpluginstats_%s", _idx) _connection_stats = memcache.get('Ugrpluginstats_' + _idx, memcached_ip, memcached_port) # Typecast to str if needed. Different versions of memcache module return # bytes. if isinstance(_connection_stats, bytes): _connection_stats = str(_connection_stats, 'utf-8') # Remove the \x00 character. if '\x00' in _connection_stats: _connection_stats = _connection_stats.rsplit('\x00', 1)[0] _logger.debug("Connection stats obtained: %s", _connection_stats) # Split the stats per storage_share, delimited by '&&' _array_of_stats = [] _connection_stats = _connection_stats.rsplit('&&') for _element in _connection_stats: # When the connection status is OK the last element is empty. So we add an 'OK' if _element.split("%%")[-1] == '': _element = _element + 'OK' _discard_duplicate, _element = _element.split("%%", 1) _array_of_stats.append(_element) # Format and return according to request 'return_as' if return_as == 'string': return '&&'.join(_array_of_stats) elif return_as == 'array': return _array_of_stats elif return_as == 'dictionary': _dictonary_of_stats = {} for _element in _array_of_stats: _storage_share, _stats = _element.split("%%", 1) _dictonary_of_stats[_storage_share] = _stats return _dictonary_of_stats elif return_as == 'expanded_dictionary': _dictonary_of_stats = {} for _element in _array_of_stats: _storage_share, _timestamp, _status, _latency, _status_code, _error = _element.split( "%%") _dictonary_of_stats[_storage_share] = { 'timestamp': _timestamp, 'status': _status, 'latency': _latency, 'status_code': _status_code, 'error': _error } return _dictonary_of_stats