Commit 3ded6d7a authored by Alain Takoudjou's avatar Alain Takoudjou

migrates old promises to new format

parent 47b0ebc0
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
import re
import time
import os
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# set periodicity to run the promise twice per day
self.custom_frequency = 720
self.setPeriodicity(self.custom_frequency)
def sense(self):
"""
Check if frontend URL is available
"""
log_file = self.getConfig('log-file')
maximum_delay = int(self.getConfig('maximum-delay'))
if not log_file:
raise ValueError("log file was not set in promise parameters.")
regex = re.compile("^(\[[^\]]+\]) (\[[^\]]+\]) (.*)$")
error_amount = 0
no_route_error = 0
network_is_unreacheable = 0
timeout = 0
parsing_failure = 0
if not os.path.exists(log_file):
# file don't exist, nothing to check
self.logger.info("OK")
return
with open(log_file) as f:
f.seek(0, 2)
block_end_byte = f.tell()
f.seek(-min(block_end_byte, 4096), 1)
data = f.read()
for line in reversed(data.splitlines()):
m = regex.match(line)
if m is None:
continue
dt, level, msg = m.groups()
try:
try:
t = time.strptime(dt[1:-1], "%a %b %d %H:%M:%S %Y")
except ValueError:
# Fail to parser for the first time, try a different output.
t = time.strptime(dt[1:-1], "%a %b %d %H:%M:%S.%f %Y")
except ValueError:
# Probably it fail to parse
if parsing_failure < 3:
# Accept failure 2 times, as the line can be actually
# cut on the middle.
parsing_failure += 1
continue
raise
if maximum_delay and (time.time()-time.mktime(t)) > maximum_delay:
# no result in the latest hour
break
if level != "[error]":
continue
# Classify the types of errors
if "(113)No route to host" in msg:
no_route_error += 1
elif "(101)Network is unreachable" in msg:
network_is_unreacheable += 1
elif "(110)Connection timed out" in msg:
timeout += 1
error_amount += 1
if error_amount:
self.logger.error("ERROR=%s (NOTROUTE=%s, UNREACHEABLENET=%s, TIMEOUT=%s)" % (
error_amount, no_route_error, network_is_unreacheable, timeout))
self.logger.info("OK")
def anomaly(self):
# only check the result of the two latest sense call
return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3)
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
import re
import time
from slapos.networkbench.ping import ping, ping6
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# set periodicity to run the promise twice per day
self.custom_frequency = 720
self.setPeriodicity(self.custom_frequency)
def sense(self):
"""
Check if frontend URL is available
"""
# Address to ping to
address = self.getConfig('address')
if not address:
raise ValueError("'address' was not set in promise parameters.")
# Force use ipv4 protocol ?
ipv4 = self.getConfig('ipv4') in ('True', 'true', '1')
count = self.getConfig('count', 10)
if ipv4:
result = ping(address, count=count)
else:
result = ping6(address, count=count)
message = "%s host=%s code=%s, result=%s, packet_lost_ratio=%s msg=%s" % result
if result[4] != "0":
# Packet lost occurred
self.logger.error(message)
else:
self.logger.info(message)
def anomaly(self):
# only check the result of the two latest sense call
return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3)
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
import re
import time
from slapos.networkbench.ping import ping, ping6
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# set periodicity to run the promise twice per day
self.custom_frequency = 720
self.setPeriodicity(self.custom_frequency)
def sense(self):
"""
Check if frontend URL is available
"""
# promise ipv6 and ipv4 address to compare.
ipv4 = self.getConfig('ipv4')
ipv6 = self.getConfig('ipv6')
count = self.getConfig('count', 10)
if not ipv4:
raise ValueError("'ipv4' was not set in promise parameters.")
if not ipv6:
raise ValueError("'ipv6' was not set in promise parameters.")
result_ipv4 = ping(ipv4, count=count)
result_ipv6 = ping6(ipv6, count=count)
# push into to the log file
self.logger.info("%s host=%s code=%s, result=%s, packet_lost_ratio=%s msg=%s" % result_ipv4)
self.logger.info("%s host=%s code=%s, result=%s, packet_lost_ratio=%s msg=%s" % result_ipv6)
if result_ipv4[3] == "failed" and result_ipv6[3] != "failed":
# IPv4 is unreacheable
self.logger.info("OK")
return
if result_ipv6[3] == "failed":
# IPv6 is unreacheable
self.logger.error("FAILED")
return
latency4 = float(result_ipv4[3])
latency6 = float(result_ipv6[3])
# We can consider that at worst 1ms is added to
# ipv4 response, due the usage of openvpn.
acceptable_delay = 1
# We can consider that we accept a certain increase
# on latency, if we are on a bit congested link.
# So 10% is reseonable enough.
acceptable_lost = 0.10
# Increase latency with the value.
latency4 += acceptable_delay + latency4 * acceptable_lost
if latency4 < latency6:
self.logger.error("FAIL %s (latency4) > %s (latence6)" % (latency4, latency6))
else:
# Compare if both has Same working rate
self.logger.info("OK")
def anomaly(self):
# only check the result of the two latest sense call
return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3)
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
import os
import pycurl
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# SR can set custom periodicity
self.setPeriodicity(float(self.getConfig('frequency', 2)))
def sense(self):
"""
Check if frontend URL is available
"""
url = self.getConfig('url')
timeout = int(self.getConfig('timeout', 20))
expected_http_code = int(self.getConfig('http_code', '200'))
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.TIMEOUT, timeout)
curl.setopt(pycurl.FOLLOWLOCATION, True)
curl.setopt(pycurl.SSL_VERIFYPEER, False)
ca_cert_file = self.getConfig('ca-cert-file')
cert_file = self.getConfig('cert-file')
key_file = self.getConfig('key-file')
if key_file and cert_file and ca_cert_file:
# set certificate configuration
curl.setopt(curl.CAINFO, ca_cert_file)
curl.setopt(curl.SSLCERT, cert_file)
curl.setopt(curl.SSLKEY, key_file)
try:
curl.perform()
except pycurl.error, e:
code, message = e
self.logger.error("%s: %s" % (code, message))
return
http_code = curl.getinfo(pycurl.HTTP_CODE)
check_secure = self.getConfig('check_secure')
if http_code == 0:
self.logger.error("%s is not available (server not reachable)." % url)
elif http_code == 401 and check_secure == "1":
self.logger.error("%s is protected (returned %s)." % (url, http_code))
elif http_code != expected_http_code:
self.logger.error("%s is not available (returned %s, expected %s)." % (
url, http_code, expected_http_code))
else:
self.logger.info("URL is available")
def anomaly(self):
return self._test(result_count=3, failure_amount=3)
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
import os
import subprocess
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
# check configuration every 5 minutes (only for anomaly)
self.setPeriodicity(minute=5)
def sense(self):
"""
RUn frontend validatation script
"""
validate_script = self.getConfig('verification-script')
result = float(subprocess.check_output([validate_script]))
process = subprocess.Popen(
[validate_script],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout, stderr = process.communicate()
if process.returncode != 0:
self.logger.info("OK")
else:
if stderr:
self.logger.error(stderr)
else:
self.logger.error(stdout)
def anomaly(self):
return self._test(result_count=2, failure_amount=2)
from zope import interface as zope_interface
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
import subprocess
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
def __init__(self, config):
GenericPromise.__init__(self, config)
self.setPeriodicity(minute=5)
def sense(self):
"""
Check trafficserver cache availability
"""
traffic_line = self.getConfig('wrapper-path')
process = subprocess.Popen(
[traffic_line, '-r', 'proxy.node.cache.percent_free'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
result = process.communicate()[0].strip()
if process.returncode != 0:
self.logger.info("OK")
else:
self.logger.error("Cache not available, availability: %s" % result)
def anomaly(self):
"""
There is an anomaly if last 3 senses were bad.
"""
return self._anomaly(result_count=3, failure_amount=3)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment