# https://github.com/PyMySQL/PyMySQL
sys.stderr.write(NAME + ": using pymysql\n")
except ImportError:
- sys.stderr.write(NAME + ": You need to install PyMySQL module to use mysql.chart.py plugin\n")
+ sys.stderr.write(NAME + ": You need to install MySQLdb or PyMySQL module to use mysql.chart.py plugin\n")
+ raise ImportError
+
+from base import BaseService
# default configuration (overriden by python.d.plugin)
-config = [
- {
- 'name' : 'local',
+# FIXME change password
+config = {
+ 'local': {
'user' : 'root',
- 'password' : 'a',
+ 'password' : '',
'socket' : '/var/run/mysqld/mysqld.sock',
'update_every' : 3,
- 'retries' : 4
+ 'retries' : 4,
+ 'priority' : 100
}
-]
+}
+
+# default module values (can be overridden per job in `config`)
+update_every = 3
+priority = 90000
+retries = 7
# query executed on MySQL server
QUERY = "SHOW GLOBAL STATUS"
}
-class BaseService(object):
- def __init__(self,configuration,update_every,priority,retries):
- if configuration is None:
- # use defaults
- configuration = config
- self.error(NAME+": no configuration supplied. using defaults.")
-
- self._parse_base_config(configuration,update_every,priority,retries)
-
- def _parse_base_config(self,config,update_every,priority,retries):
- # parse configuration options to run this Service
- try:
- self.update_every = int(config['update_every'])
- except (KeyError, ValueError):
- self.update_every = update_every
- try:
- self.priority = int(config['priority'])
- except (KeyError, ValueError):
- self.priority = priority
- try:
- self.retries = int(config['retries'])
- except (KeyError, ValueError):
- self.retries = retries
- self.retries_left = self.retries
-
- def error(self, msg, exception=""):
- if exception != "":
- exception = " " + str(exception).replace("\n"," ")
- sys.stderr.write(str(msg)+exception+"\n")
- sys.stderr.flush()
-
- def check(self):
- # TODO notify about not overriden function
- self.error("Where is your check()?")
- return False
-
- def create(self):
- # TODO notify about not overriden function
- self.error("Where is your create()?")
- return False
-
- def update(self):
- # TODO notify about not overriden function
- self.error("Where is your update()?")
- return False
-
-
class Service(BaseService):
- def __init__(self,configuration=None,update_every=3,priority=90000,retries=2):
- super().__init__(configuration,update_every,priority,retries)
+ def __init__(self,configuration=None,name=None):
+ super().__init__(configuration=configuration)
+ self.name = name
self.configuration = self._parse_config(configuration)
self.connection = None
self.defs = {}
def _parse_config(self,configuration):
# parse configuration to collect data from mysql server
- if 'name' not in configuration:
- configuration['name'] = 'local'
+ if self.name is None:
+ self.name = 'local'
if 'user' not in configuration:
configuration['user'] = 'root'
if 'password' not in configuration:
def create(self):
for name in ORDER:
- print(name)
self.defs[name] = []
for line in CHARTS[name][1]:
self.defs[name].append(line[0])
data = self._get_data()
for name in ORDER:
header = "CHART mysql_" + \
- str(self.configuration['name']) + "." + \
+ str(self.name) + "." + \
name + " " + \
CHARTS[name][0] + " " + \
str(self.priority + idx) + " " + \
except Exception:
pass
for chart, dimensions in self.defs.items():
- header = "BEGIN mysql_" + str(self.configuration['name']) + "." + chart + " " + str(interval) + '\n'
+ header = "BEGIN mysql_" + str(self.name) + "." + chart + " " + str(interval) + '\n'
lines = ""
for d in dimensions:
try:
print(header + lines + "END")
return True
-
-if __name__ == "__main__":
- my = Service(config[0])
- my.check()
- my.create()
- my.update(1)