]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/influx: Revise configuration of module
authorWido den Hollander <wido@42on.com>
Mon, 11 Dec 2017 14:12:51 +0000 (15:12 +0100)
committerWido den Hollander <wido@42on.com>
Mon, 11 Dec 2017 14:33:53 +0000 (15:33 +0100)
It's a lot like the Zabbix module and allows for setting configuration
options on run-time and also fetch them from the module.

A few additional commands have been registered to make sure it is easy
to interact with the module.

Signed-off-by: Wido den Hollander <wido@42on.com>
src/pybind/mgr/influx/module.py

index 5f71c61714801e0f552f8341b81bd7a03d51e4f3..beabce72d24adf7c209835cd66663eb75ef00bfc 100644 (file)
@@ -14,6 +14,22 @@ except ImportError:
 
 class Module(MgrModule):
     COMMANDS = [
+        {
+            "cmd": "influx config-set name=key,type=CephString "
+                   "name=value,type=CephString",
+            "desc": "Set a configuration value",
+            "perm": "rw"
+        },
+        {
+            "cmd": "influx config-show",
+            "desc": "Show current configuration",
+            "perm": "r"
+        },
+        {
+            "cmd": "influx send",
+            "desc": "Force sending data to Influx",
+            "perm": "rw"
+        },
         {
             "cmd": "influx self-test",
             "desc": "debug the module",
@@ -21,10 +37,20 @@ class Module(MgrModule):
         },
     ]
 
+    config_keys = {
+        'hostname': None,
+        'port': 8086,
+        'database': 'ceph',
+        'username': None,
+        'password': None,
+        'interval': 5
+    }
+
     def __init__(self, *args, **kwargs):
         super(Module, self).__init__(*args, **kwargs)
         self.event = Event()
         self.run = True
+        self.config = dict()
 
     def get_fsid(self):
         return self.get('mon_map')['fsid']
@@ -97,22 +123,50 @@ class Module(MgrModule):
 
         return data
 
+    def set_config_option(self, option, value):
+        if option not in self.config_keys.keys():
+            raise RuntimeError('{0} is a unknown configuration '
+                               'option'.format(option))
+
+        if option in ['port', 'interval']:
+            try:
+                value = int(value)
+            except (ValueError, TypeError):
+                raise RuntimeError('invalid {0} configured. Please specify '
+                                   'a valid integer'.format(option))
+
+        if option == 'interval' and value < 5:
+            raise RuntimeError('interval should be set to at least 5 seconds')
+
+        self.config[option] = value
+
+    def init_module_config(self):
+        self.config['hostname'] = \
+            self.get_config("hostname", default=self.config_keys['hostname'])
+        self.config['port'] = \
+            int(self.get_config("port", default=self.config_keys['port']))
+        self.config['database'] = \
+            self.get_config("database", default=self.config_keys['database'])
+        self.config['username'] = \
+            self.get_config("username", default=self.config_keys['username'])
+        self.config['password'] = \
+            self.get_config("password", default=self.config_keys['password'])
+        self.config['interval'] = \
+            int(self.get_config("interval",
+                                default=self.config_keys['interval']))
+
     def send_to_influx(self):
-        host = self.get_config("hostname")
-        if not host:
-            self.log.error("No InfluxDB server configured, please set"
-                           "`hostname` configuration key.")
+        if not self.config['hostname']:
+            self.log.error("No Influx server configured, please set one using: "
+                           "ceph influx config-set hostname <hostname>")
             return
 
-        port = int(self.get_config("port", default="8086"))
-        database = self.get_config("database", default="ceph")
-
         # If influx server has authentication turned off then
         # missing username/password is valid.
-        username = self.get_config("username", default="")
-        password = self.get_config("password", default="")
-
-        client = InfluxDBClient(host, port, username, password, database)
+        client = InfluxDBClient(self.config['hostname'], self.config['port'],
+                                self.config['username'],
+                                self.config['password'],
+                                self.config['database'])
 
         # using influx client get_list_database requires admin privs,
         # instead we'll catch the not found exception and inform the user if
@@ -125,8 +179,9 @@ class Module(MgrModule):
                 self.log.info("Database '%s' not found, trying to create "
                               "(requires admin privs).  You can also create "
                               "manually and grant write privs to user "
-                              "'%s'", database, username)
-                client.create_database(database)
+                              "'%s'", self.config['database'],
+                              self.config['username'])
+                client.create_database(self.config['database'])
             else:
                 raise
 
@@ -136,6 +191,21 @@ class Module(MgrModule):
         self.event.set()
 
     def handle_command(self, cmd):
+        if cmd['prefix'] == 'influx config-show':
+            return 0, json.dumps(self.config), ''
+        elif cmd['prefix'] == 'influx config-set':
+            key = cmd['key']
+            value = cmd['value']
+            if not value:
+                return -errno.EINVAL, '', 'Value should not be empty or None'
+
+            self.log.debug('Setting configuration option %s to %s', key, value)
+            self.set_config_option(key, value)
+            self.set_config(key, value)
+            return 0, 'Configuration option {0} updated'.format(key), ''
+        elif cmd['prefix'] == 'influx send':
+            self.send_to_influx()
+            return 0, 'Sending data to Influx', ''
         if cmd['prefix'] == 'influx self-test':
             daemon_stats = self.get_daemon_stats()
             assert len(daemon_stats)
@@ -158,14 +228,11 @@ class Module(MgrModule):
             return
 
         self.log.info('Starting influx module')
+        self.init_module_config()
         self.run = True
+
         while self.run:
             self.send_to_influx()
             self.log.debug("Running interval loop")
-            interval = self.get_config("interval")
-
-            if interval is None:
-                interval = 5
-
-            self.log.debug("sleeping for %d seconds", interval)
-            self.event.wait(interval)
+            self.log.debug("sleeping for %d seconds", self.config['interval'])
+            self.event.wait(self.config['interval'])