Example #1
0
def post(url, data, json=False):
    if isinstance(data, str):
        data = data.encode('utf-8')
    if python_3():
        req = urllib.request.Request(url)
        if json:
            req.add_header('Content-Type', 'application/json; charset=utf-8')
        return urllib.request.urlopen(req, data)
    else:
        req = urllib2.Request(url, data)
        if json:
            req.add_header('Content-Type', 'application/json; charset=utf-8')
        return urllib2.urlopen(req)
Example #2
0
    def _make_request(self, query):
        if not query.endswith("\n"):
            query += "\n"
        query += "Localtime: %d\nOutputFormat: python\nResponseHeader: fixed16\n\n" % int(time.time())
        self._open_socket()
        if python_3():
            self.__socket.send(bytes(query, encoding='UTF-8'))
        else:
            self.__socket.send(query)
        self.__socket.shutdown(socket.SHUT_WR)
        data = ""
        buffer = self.__socket.recv(BUFFER_SIZE)
        while buffer:
            data += buffer.decode()
            buffer = self.__socket.recv(BUFFER_SIZE)
        return_code = data[0:3]

        if return_code == "200":
            return eval(data[16:])
        else:
            raise Exception("Livestatus returned with " + return_code)
Example #3
0
    def __init__(self, file):
        if python_3():
            self.__settings = configparser.ConfigParser()
        else:
            self.__settings = ConfigParser.SafeConfigParser()
        try:
            with open(file) as f:
                self.__settings.readfp(f)
        except IOError as e:
            raise ConfigfileException(e)

        # Main
        self.data['main'] = {'customMethods': self.__settings.get('Main', 'Custom_Methods'),
                             'log_level': self.__settings.get('Main', 'Log_Level'),
                             'daemon': strtobool(self.__settings.get('Main', 'Daemon')),
                             'update_rate': int(self.__settings.get('Main', 'Config_Updaterate_in_Minutes')) * 60,
                             'log_performance': strtobool(self.__settings.get('Main', 'Log_Performance'))}

        # Livestatus
        livestatus_split = self.__settings.get('Livestatus', 'Address').split(":")
        self.data['livestatus'] = {'protocol': livestatus_split[0], 'address': livestatus_split[1]}
        if len(livestatus_split) == 3:
            self.data['livestatus']['port'] = int(livestatus_split[2])

        # Histou
        # self.data['histou'] = {'prot': "http", 'address': self.__settings.get('Histou', 'Address')}
        histou_split = self.__settings.get('Histou', 'Address').split(":", 1)
        self.data['histou'] = {'prot': histou_split[0], 'address': histou_split[1]}
        self.data['histou']['user'] = self.__settings.get('Histou', 'User')
        self.data['histou']['password'] = self.__settings.get('Histou', 'Password')

        # Influxdb
        self.data['influxdb'] = {'read': {'address': self.__settings.get('InfluxDB', 'Address_Read'),
                                          'db': self.__settings.get('InfluxDB', 'DB_Read'),
                                          'args': self.__settings.get('InfluxDB', 'DB_Read_Args')},
                                 'write': {'address': self.__settings.get('InfluxDB', 'Address_Write'),
                                           'db_forecast': self.__settings.get('InfluxDB', 'DB_Write_Forecast'),
                                           'db_anomaly': self.__settings.get('InfluxDB', 'DB_Write_Anomaly'),
                                           'args': self.__settings.get('InfluxDB', 'DB_Write_Args')}}
Example #4
0
    def _make_request(self, query):
        if not query.endswith("\n"):
            query += "\n"
        query += "Localtime: %d\nOutputFormat: python\nResponseHeader: fixed16\n\n" % int(
            time.time())
        self._open_socket()
        if python_3():
            self.__socket.send(bytes(query, encoding='UTF-8'))
        else:
            self.__socket.send(query)
        self.__socket.shutdown(socket.SHUT_WR)
        data = ""
        buffer = self.__socket.recv(BUFFER_SIZE)
        while buffer:
            data += buffer.decode()
            buffer = self.__socket.recv(BUFFER_SIZE)
        return_code = data[0:3]

        if return_code == "200":
            return eval(data[16:])
        else:
            raise Exception("Livestatus returned with " + return_code)
Example #5
0
import json
import logging

import datascryer
from datascryer.helper.http import quote, get
from datascryer.helper.python import python_3
from datascryer.influxdb.connection import gen_url

if python_3():
    pass
else:
    pass


class InfluxDBReader:
    url = None

    def __init__(self, address, db, args):
        InfluxDBReader.url = gen_url(address, db, args, "query")
        if not str.endswith(InfluxDBReader.url, "&"):
            InfluxDBReader.url += "&"
        InfluxDBReader.url += "q="

    @staticmethod
    def request_past(host, service, performance_label, lookback, command=None):
        if not command:
            query = """SELECT value FROM metrics WHERE host = '%s' AND service = '%s' AND performanceLabel = '%s' AND time > now() - %dms""" % (
                host, service, performance_label, lookback)
        else:
            query = """SELECT value FROM metrics WHERE host = '%s' AND service = '%s' AND command = '%s' AND performanceLabel = '%s' AND time > now() - %dms""" % (
                host, service, command, performance_label, lookback)
Example #6
0
def read(response):
    if python_3():
        return response.read().decode('utf8')
    else:
        return response.read()
Example #7
0
def quote(string):
    if python_3():
        return urllib.parse.quote(string)
    else:
        return urllib2.quote(string)
Example #8
0
def get(url):
    if python_3():
        return urllib.request.urlopen(urllib.request.Request(url))
    else:
        return urllib2.urlopen(urllib2.Request(url))
Example #9
0
from datascryer.helper.python import python_3

if python_3():
    import urllib.request
    import urllib.error
    import urllib.parse
else:
    import urllib2


def get(url):
    if python_3():
        return urllib.request.urlopen(urllib.request.Request(url))
    else:
        return urllib2.urlopen(urllib2.Request(url))


def quote(string):
    if python_3():
        return urllib.parse.quote(string)
    else:
        return urllib2.quote(string)


def read(response):
    if python_3():
        return response.read().decode('utf8')
    else:
        return response.read()

Example #10
0
class ForecastMethod:
    __metaclass__ = ABCMeta
    if python_3():
        INF = math.inf
    else:
        INF = float("inf")

    @abstractmethod
    def calc_forecast(self, options, forecast_start, forecast_range, forecast_interval, lookback_range, lookback_data):
        """
        Generates an array bases on the given data.
        :param options: dict with whatever the config passes.
        :param forecast_start: timestamp to start forecast.
        :param forecast_range: Time in seconds to forecast after start.
        :param forecast_interval: Timeinterval to forecast.
        :param lookback_range: Time in seconds of existing data.
        :param lookback_data: Two dimensional array of data of existing data.
        :return: Two dimensional array of forecasted data.
        """
        raise NotImplementedError()

    @abstractmethod
    def calc_intersection(self, options, forecast_start, forecast_range, forecast_interval,
                          lookback_range, lookback_data, y):
        """
        Returns the x value of the intersection of the given y and the estimated forecast function.
        :param options: dict with whatever the config passes.
        :param forecast_start: timestamp to start forecast.
        :param forecast_range: Time in seconds to forecast after start.
        :param forecast_interval: Timeinterval to forecast.
        :param lookback_range: Time in seconds of existing data.
        :param lookback_data: Two dimensional array of data of existing data.
        :param y: Value to be reached.
        :return:
        """
        raise NotImplementedError()

    @staticmethod
    def gen_forecast_data(func, forecast_start, forecast_range, forecast_interval):
        return [(x, func(x)) for x in xrange(forecast_start, forecast_start + forecast_range, forecast_interval)]

    def gen_intersection(self, forecast_data, forecast_start, y):
        for i in range(len(forecast_data) - 1):
            if forecast_data[i][1] <= y <= forecast_data[i + 1][1] \
                    or forecast_data[i][1] >= y >= forecast_data[i + 1][1]:
                return forecast_data[i][0] - forecast_start
        return self.INF

    @staticmethod
    def sum_x_y(data, power=1):
        sum_x = 0
        sum_y = 0
        if power == 1:
            for (x, y) in data:
                sum_x += x
                sum_y += y
        else:
            for (x, y) in data:
                sum_x += pow(x, power)
                sum_y += pow(y, power)
        return sum_x, sum_y
Example #11
0
 def get_method(c):
     if python_3():
         method_name = c[METHOD]
     else:
         method_name = c[METHOD].encode('utf8')
     return str.lower(method_name)