Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def version(self):
"""
Finds and sets version
"""
if self._version is None:
# get data
conn = self.object.connect()
try:
with conn as cursor:
cursor.execute("SELECT version();")
self._version = ''.join(cursor.fetchone()).split('-')[0]
except Exception as e:
exception_name = e.__class__.__name__
context.log.debug('failed to collect MySQLd meta due to %s' % exception_name)
context.log.debug('additional info:', exc_info=True)
finally:
conn.close()
self.meta['version'] = self._version
# Send payload to backend.
try:
self.last_http_attempt = time.time()
self._pre_process_payload() # Convert deques to lists for encoding
context.http_client.post('update/', data=self.payload)
context.default_log.debug(self.payload)
self._reset_payload() # Clear payload after successful
if self.first_run:
self.first_run = False # Set first_run to False after first successful send
if self.http_delay:
self.http_fail_count = 0
self.http_delay = 0 # Reset HTTP delay on success
context.log.debug('successful update, reset http delay')
except Exception as e:
self._post_process_payload() # Convert lists to deques since send failed
if isinstance(e, HTTPError) and e.response.status_code == 503:
backpressure_error = HTTP503Error(e)
context.backpressure_time = int(time.time() + backpressure_error.delay)
context.log.debug(
'back pressure delay %s added (next talk: %s)' % (
backpressure_error.delay,
context.backpressure_time
)
)
else:
self.http_fail_count += 1
self.http_delay = exponential_delay(self.http_fail_count)
context.log.debug('http delay set to %s (fails: %s)' % (self.http_delay, self.http_fail_count))
if not root_object:
root_object = get_root_definition()
# talk to cloud
try:
# reset the cloud talk counter to avoid sending new requests every 5.0 seconds
self.last_cloud_talk_time = int(time.time())
cloud_response = CloudResponse(
context.http_client.post('agent/', data=root_object)
)
if self.cloud_talk_delay:
self.cloud_talk_fails = 0
self.cloud_talk_delay = 0
context.log.debug('successful cloud connect, reset cloud talk delay')
except Exception as e:
if isinstance(e, HTTPError) and e.response.status_code == 503:
backpressure_error = HTTP503Error(e)
context.backpressure_time = int(time.time() + backpressure_error.delay)
context.log.debug(
'back pressure delay %s added (next talk: %s)' % (
backpressure_error.delay,
context.backpressure_time
)
)
else:
self.cloud_talk_fails += 1
self.cloud_talk_delay = exponential_delay(self.cloud_talk_fails)
context.log.debug(
'cloud talk delay set to %s (fails: %s)' % (self.cloud_talk_delay, self.cloud_talk_fails)
)
def collect_structure(self, include_ssl_certs=False):
"""
Goes through all files (light-parsed includes) and collects their mtime
:param include_ssl_certs: bool - include ssl certs or not
:return: {} - dict of files
"""
# if self.parser is None, set it up
if self.parser is None:
self._setup_parser()
files, directories = self.parser.get_structure(include_ssl_certs=include_ssl_certs)
context.log.debug('found %s files for %s' % (len(files.keys()), self.filename))
context.log.debug('found %s directories for %s' % (len(directories.keys()), self.filename))
# always teardown the parser
self._teardown_parser()
return files, directories
def parse(self, force=False):
if self.parsed and not force:
return self.parsed_conf
context.log.debug('parsing phpfpm conf "%s"', self.conf_path)
start_time = time.time()
self.parsed_conf = PHPFPMConfig(path=self.conf_path).parsed
self.parsed = True
end_time = time.time()
context.log.debug(
'finished parse of "%s" in %.2f' %
(self.conf_path, end_time - start_time)
)
return self.parsed_conf
new_inode = self._inode
while tries < 2: # Try twice before moving on.
try:
new_inode = self._st_ino()
except:
time.sleep(0.5)
tries += 1
pass
else:
break
# If tries == 2 then we know we broke out of the while above manually.
if tries == 2:
context.log.error('could not check if file "%s" was rotated (maybe file was deleted?)' % self.filename)
context.log.debug('additional info:', exc_info=True)
raise StopIteration
return new_inode != self._inode
new_inode = self._inode
while tries < 2: # Try twice before moving on.
try:
new_inode = self._st_ino()
except:
time.sleep(0.5)
tries += 1
pass
else:
break
# If tries == 2 then we know we broke out of the while above manually.
if tries == 2:
context.log.error('could not check if file "%s" was rotated (maybe file was deleted?)' % self.filename)
context.log.debug('additional info:', exc_info=True)
raise StopIteration
# check for copytruncate
# it will use the same file so inode will stay the same
file_truncated = False
if new_inode == self._inode and self.filename in OFFSET_CACHE:
with open(self.filename, 'r') as temp_fh:
temp_fh.seek(0, 2)
if temp_fh.tell() < OFFSET_CACHE[self.filename]:
# this means the file is smaller than previously cached
# so file must have been truncated
file_truncated = True
if file_truncated:
return True
return new_inode != self._inode
def memory_logger(rss, vms, prefix='', out=None):
"""Just a util for logging into debug memory data"""
prefix += ' ' if not prefix.endswith(' ') else ''
message = 'memory stats (rss: %s, vms: %s)' % (rss, vms)
if out is None:
context.log.debug('%s%s' % (prefix, message))
else:
out.write('%s%s\n' % (prefix, message))
exc_cls = e.__class__.__name__
exc_msg = e.strerror if hasattr(e, 'strerror') else e.message
message = 'failed to %s %s due to: %s' % (what, path, exc_cls)
self.errors.append(message)
if is_dir:
self._broken_directories[path] = '%s: %s' % (exc_cls, exc_msg)
context.log.debug(message, exc_info=exc_info)
else:
self._broken_files[path] = '%s: %s' % (exc_cls, exc_msg)
context.log.error(message)
if isinstance(e, crossplane.errors.NgxParserDirectiveError):
line = _getline(e.filename, e.lineno)
context.log.debug('line where error was raised: %r' % line)
context.log.debug('additional info:', exc_info=exc_info)
def collect(self, *args, **kwargs):
"""
Basic collect method with initial logic for storing status pages for\
parsing.
"""
# hit the status_page and try to store parsed results in _current
self._current = self._parse_status_page(self.status_page.get_status())
self._current_stamp = int(time.time())
# load the parent object reference to minimize calls to ObjectTank
self._parent = context.objects.find_parent(obj=self.object)
# if self._parent is None then something is wrong...
if self._parent is None:
context.log.debug(
'%s failed to collect because parent was "None"' %
self.short_name
)
# TODO: Create a Naas error to serve as this condition and pass it
# to self.handle_exception.
self._current = None # clear current to save memory
self._current_stamp = None
return
super(PHPFPMPoolMetricsCollector, self).collect(*args, **kwargs)
try:
self.increment_counters()
except Exception as e:
self.handle_exception(self.increment_counters, e)