self.defs = {}
def _parse_config(self, configuration):
- # parse configuration to collect data from mysql server
+ """
+ Parse configuration to collect data from MySQL server
+ :param configuration: dict
+ :return: dict
+ """
if self.name is None:
self.name = 'local'
if 'user' not in configuration:
return configuration
def _connect(self):
+ """
+ Try to connect to MySQL server
+ """
try:
self.connection = MySQLdb.connect(user=self.configuration['user'],
passwd=self.configuration['password'],
raise RuntimeError
def _get_data(self):
+ """
+ Get raw data from MySQL server
+ :return: dict
+ """
if self.connection is None:
try:
self._connect()
return dict(raw_data)
def check(self):
+ """
+ Check if service is able to connect to server
+ :return: boolean
+ """
try:
self.connection = self._connect()
return True
return False
def create(self):
+ """
+ Create graphs
+ :return: boolean
+ """
for name in ORDER:
self.defs[name] = []
for line in CHARTS[name][1]:
return True
def update(self, interval):
+ """
+ Update data on graphs
+ :param interval: int
+ :return: boolean
+ """
data = self._get_data()
if data is None:
return False
class BaseService(object):
+ """
+ Prototype of Service class.
+ Implemented basic functionality to run jobs by `python.d.plugin`
+ """
def __init__(self, configuration=None, name=None):
+ """
+ This needs to be initialized in child classes
+ :param configuration: dict
+ :param name: str
+ """
if configuration is None:
self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
raise RuntimeError
self.execution_name = ""
def _extract_base_config(self, config):
+ """
+ Get basic parameters to run service
+ Minimum config:
+ config = {'update_every':1,
+ 'priority':100000,
+ 'retries':0}
+ :param config: dict
+ """
self.update_every = int(config['update_every'])
self.priority = int(config['priority'])
self.retries = int(config['retries'])
self.retries_left = self.retries
def create_timetable(self, freq=None):
+ """
+ Create service timetable.
+ `freq` is optional
+ Example:
+ timetable = {'last': 1466370091.3767564,
+ 'next': 1466370092,
+ 'freq': 1}
+ :param freq: int
+ """
if freq is None:
freq = self.update_every
now = time()
sys.stderr.flush()
def check(self):
+ """
+ check() prototype
+ :return: boolean
+ """
self.error("Service " + str(self.__name__) + "doesn't implement check() function")
return False
def create(self):
+ """
+ create() prototype
+ :return: boolean
+ """
self.error("Service " + str(self.__name__) + "doesn't implement create() function?")
return False
def update(self):
+ """
+ update() prototype
+ :return: boolean
+ """
self.error("Service " + str(self.__name__) + "doesn't implement update() function")
return False