Пример #1
0
    def _DELETE(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        watch_id = param[1]
        if watch_id is None: return web.notfound()

        watch = w_findby1(self.orm, watch_id)
        w_delete(self.orm, watch)

        # delete setting file
        plugin = watch.plugin
        plugin_selector = watch.plugin_selector
        modules = ["collectdplugin"]
        host_id = self.chk_hostby1(param)
        host = m_findbyhost1(self.orm, host_id)

        ## read config and delete threashold
        extra_args = {'include':'^threshold_'}
        dop = read_conf(modules, webobj=self, machine=host, extra_args=extra_args)
        if dop is False:
            self.logger.debug("Delete watch failed. Failed read conf.")
            return web.internalerror('Internal Server Error. (Read Conf)')
        delete_threshold(plugin, plugin_selector, dop=dop, webobj=self, host=host)

        ## apply setting and collectd restart
        command = "/etc/init.d/collectd condrestart"
        extra_args = {"post-command": command}
        retval = write_conf(dop, webobj=self, machine=host, extra_args=extra_args)
        if retval is False:
            self.logger.debug("Delete watch failed. Failed write conf.")
            return web.internalerror('Internal Server Error. (Write Conf)')

        return web.accepted()
Пример #2
0
    def _GET(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        watch_id = param[1]
        if watch_id is None: return web.notfound()

        watch = w_findby1(self.orm, watch_id)
        self.view.watch = watch
        self.view.plugins = WATCH_PLUGINS

        plugin_selector = plugin_selector_to_dict(watch.plugin_selector)
        self.view.plugin_selector = plugin_selector

        if watch.plugin == COLLECTD_PLUGIN_LIBVIRT:
            libvirt_type = ""
            if plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['VCPU']:
                libvirt_type = "vcpu"
            elif plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['CPU_TOTAL']:
                libvirt_type = "cpu_total"
            elif plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['DISK_OPS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['DISK_OCTETS']:
                libvirt_type = "disk"
            elif plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_OCTETS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_PACKETS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_ERRORS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_DROPPED']:
                libvirt_type = "interface"

            self.view.libvirt_type = libvirt_type

        if self.is_mode_input() is True:
            warning_value = threshold_value_to_dict(watch.warning_value)
            failure_value = threshold_value_to_dict(watch.failure_value)
            self.view.threshold_value_1 = warning_value.values()[0]
            self.view.threshold_value_2 = failure_value.values()[0]
            self.view.threshold_type = failure_value.keys()[0]
            self.view.use_percentage = watch.is_failure_percentage

            self.view.supported_langs = DEFAULT_LANGS.keys()

        self.view.memory_size = string.atol(
            get_proc_meminfo()["MemTotal"][0]) / 1024
        ## disk info
        self.view.disk_size_info = {}
        for disk_data in get_fs_info():
            disk_target = re.sub(r'^/dev/', '', disk_data['Filesystem'])
            disk_target = re.sub(r'/', '_', disk_target)
            self.view.disk_size_info[disk_target] = disk_data['1048576-blocks']
        self.view.interface_type = COLLECTD_INTERFACE_TYPE
        self.view.processer_num = len(get_proc_cpuinfo().keys())
        self.view.mta = "%s:%s" % (
            karesansui.config['application.mail.server'],
            karesansui.config['application.mail.port'])
        self.view.watch_interval = WATCH_INTERVAL

        return True
Пример #3
0
    def _GET(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        watch_id = param[1]
        if watch_id is None: return web.notfound()

        watch = w_findby1(self.orm, watch_id)
        self.view.watch = watch
        self.view.plugins = WATCH_PLUGINS

        plugin_selector = plugin_selector_to_dict(watch.plugin_selector)
        self.view.plugin_selector = plugin_selector

        if watch.plugin == COLLECTD_PLUGIN_LIBVIRT:
            libvirt_type = ""
            if plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['VCPU']:
                libvirt_type = "vcpu"
            elif plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['CPU_TOTAL']:
                libvirt_type = "cpu_total"
            elif plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['DISK_OPS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['DISK_OCTETS']:
                libvirt_type = "disk"
            elif plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_OCTETS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_PACKETS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_ERRORS'] or \
                    plugin_selector['type'] == COLLECTD_LIBVIRT_TYPE['IF_DROPPED']:
                libvirt_type = "interface"

            self.view.libvirt_type = libvirt_type

        if self.is_mode_input() is True:
            warning_value = threshold_value_to_dict(watch.warning_value)
            failure_value = threshold_value_to_dict(watch.failure_value)
            self.view.threshold_value_1 = warning_value.values()[0]
            self.view.threshold_value_2 = failure_value.values()[0]
            self.view.threshold_type = failure_value.keys()[0]
            self.view.use_percentage = watch.is_failure_percentage

            self.view.supported_langs = DEFAULT_LANGS.keys()

        self.view.memory_size = string.atol(get_proc_meminfo()["MemTotal"][0]) / 1024
        ## disk info 
        self.view.disk_size_info = {}
        for disk_data in get_fs_info():
            disk_target = re.sub(r'^/dev/', '', disk_data['Filesystem'])
            disk_target = re.sub(r'/', '_', disk_target)
            self.view.disk_size_info[disk_target] = disk_data['1048576-blocks']
        self.view.interface_type = COLLECTD_INTERFACE_TYPE
        self.view.processer_num = len(get_proc_cpuinfo().keys())
        self.view.mta = "%s:%s" % (karesansui.config['application.mail.server'],
                                   karesansui.config['application.mail.port'])
        self.view.watch_interval = WATCH_INTERVAL

        return True
Пример #4
0
    def _DELETE(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        watch_id = param[1]
        if watch_id is None: return web.notfound()

        watch = w_findby1(self.orm, watch_id)
        w_delete(self.orm, watch)

        # delete setting file
        plugin = watch.plugin
        plugin_selector = watch.plugin_selector
        modules = ["collectdplugin"]
        host_id = self.chk_hostby1(param)
        host = m_findbyhost1(self.orm, host_id)

        ## read config and delete threashold
        extra_args = {'include': '^threshold_'}
        dop = read_conf(modules,
                        webobj=self,
                        machine=host,
                        extra_args=extra_args)
        if dop is False:
            self.logger.debug("Delete watch failed. Failed read conf.")
            return web.internalerror('Internal Server Error. (Read Conf)')
        delete_threshold(plugin,
                         plugin_selector,
                         dop=dop,
                         webobj=self,
                         host=host)

        ## apply setting and collectd restart
        command = "/etc/init.d/collectd condrestart"
        extra_args = {"post-command": command}
        retval = write_conf(dop,
                            webobj=self,
                            machine=host,
                            extra_args=extra_args)
        if retval is False:
            self.logger.debug("Delete watch failed. Failed write conf.")
            return web.internalerror('Internal Server Error. (Write Conf)')

        return web.accepted()
Пример #5
0
    def _PUT(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        watch_id = param[1]
        if watch_id is None: return web.notfound()

        if not validates_watch(self):
            self.logger.debug("Change watch failed. Did not validate.")
            return web.badrequest(self.view.alert)

        watch = w_findby1(self.orm, watch_id)

        ## text
        watch.name               = self.input.watch_name
        watch.continuation_count = self.input.continuation_count
        watch.prohibition_period = self.input.prohibition_period
        if is_param(self.input, 'warning_script'):
            watch.warning_script = self.input.warning_script
        else:
            watch.warning_script = ""
        if is_param(self.input, 'warning_mail_body'):
            watch.warning_mail_body = self.input.warning_mail_body
        else:
            watch.warning_mail_body = ""
        if is_param(self.input, 'failure_script'):
            watch.failure_script = self.input.failure_script
        else:
            watch.failure_script = ""
        if is_param(self.input, 'failure_mail_body'):
            watch.failure_mail_body = self.input.failure_mail_body
        else:
            watch.failure_mail_body = ""
        if is_param(self.input, 'okay_script'):
            watch.okay_script = self.input.okay_script
        else:
            watch.okay_script = ""
        if is_param(self.input, 'okay_mail_body'):
            watch.okay_mail_body = self.input.okay_mail_body
        else:
            watch.okay_mail_body = ""
        if is_param(self.input, 'notify_mail_to'):
            watch.notify_mail_to = self.input.notify_mail_to
        else:
            watch.notify_mail_to = ""
        if is_param(self.input, 'notify_mail_from'):
            watch.notify_mail_from = self.input.notify_mail_from
        else:
            watch.notify_mail_from = ""

        threshold_val1 = self.input.threshold_val1
        threshold_val2 = self.input.threshold_val2
        threshold_type = self.input.threshold_type
        if threshold_type == "max":
            warning_value = create_threshold_value(min_value=None, max_value=threshold_val1)
            failure_value = create_threshold_value(min_value=None, max_value=threshold_val2)
        elif threshold_type == "min":
            warning_value = create_threshold_value(min_value=threshold_val2, max_value=None)
            failure_value = create_threshold_value(min_value=threshold_val1, max_value=None)
        else:
            self.logger.debug("Update watch failed. Unknown threshold type.")
            return web.badrequest()

        watch.warning_value = warning_value
        watch.failure_value = failure_value

        ## bool
        bool_input_key = ["use_percentage", "enable_warning_mail",
                          "enable_failure_mail", "enable_okay_mail",
                          "enable_warning_script", "enable_failure_script",
                          "enable_okay_script"]
        bool_values = {}
        for key in bool_input_key:
            if self.input.has_key(key):
                bool_values.update({key:True})
            else:
                bool_values.update({key:False})

        watch.is_warning_percentage = bool_values.get("use_percentage")
        watch.is_warning_script     = bool_values.get("enable_warning_script")
        watch.is_warning_mail       = bool_values.get("enable_warning_mail")
        watch.is_failure_percentage = bool_values.get("use_percentage")
        watch.is_failure_script     = bool_values.get("enable_failure_script")
        watch.is_failure_mail       = bool_values.get("enable_failure_mail")
        watch.is_okay_script        = bool_values.get("enable_okay_script")
        watch.is_okay_mail          = bool_values.get("enable_okay_mail")

        w_update(self.orm, watch)

        plugin = watch.plugin
        plugin_selector = watch.plugin_selector
        modules = ["collectdplugin"]

        host = m_findbyhost1(self.orm, host_id)
        extra_args = {'include':'^threshold_'}
        #extra_args = {}
        dop = read_conf(modules, webobj=self, machine=host, extra_args=extra_args)
        if dop is False:
            self.logger.debug("Change watch failed. Failed read conf.")
            return web.internalerror('Internal Server Error. (Read Conf)')

        params = {}
        if threshold_type == "max":
            params['WarningMax'] = str(threshold_val1)
            params['FailureMax'] = str(threshold_val2)
        elif threshold_type == "min":
            params['WarningMin'] = str(threshold_val2)
            params['FailureMin'] = str(threshold_val1)

        params['Percentage'] = str(bool_values.get("use_percentage")).lower()
        params['Persist']    = "true"
        delete_threshold(plugin, plugin_selector, dop=dop, webobj=self, host=host)
        set_threshold(plugin,plugin_selector,params,dop=dop,webobj=self, host=host)

        extra_args = {}
        command = "/etc/init.d/collectd condrestart"
        extra_args = {"post-command": command}
        retval = write_conf(dop,  webobj=self, machine=host, extra_args=extra_args)
        if retval is False:
            self.logger.debug("Change watch failed. Failed write conf.")
            return web.internalerror('Internal Server Error. (Write Conf)')

        return web.accepted()
Пример #6
0
    def _PUT(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        watch_id = param[1]
        if watch_id is None: return web.notfound()

        if not validates_watch(self):
            self.logger.debug("Change watch failed. Did not validate.")
            return web.badrequest(self.view.alert)

        watch = w_findby1(self.orm, watch_id)

        ## text
        watch.name = self.input.watch_name
        watch.continuation_count = self.input.continuation_count
        watch.prohibition_period = self.input.prohibition_period
        if is_param(self.input, 'warning_script'):
            watch.warning_script = self.input.warning_script
        else:
            watch.warning_script = ""
        if is_param(self.input, 'warning_mail_body'):
            watch.warning_mail_body = self.input.warning_mail_body
        else:
            watch.warning_mail_body = ""
        if is_param(self.input, 'failure_script'):
            watch.failure_script = self.input.failure_script
        else:
            watch.failure_script = ""
        if is_param(self.input, 'failure_mail_body'):
            watch.failure_mail_body = self.input.failure_mail_body
        else:
            watch.failure_mail_body = ""
        if is_param(self.input, 'okay_script'):
            watch.okay_script = self.input.okay_script
        else:
            watch.okay_script = ""
        if is_param(self.input, 'okay_mail_body'):
            watch.okay_mail_body = self.input.okay_mail_body
        else:
            watch.okay_mail_body = ""
        if is_param(self.input, 'notify_mail_to'):
            watch.notify_mail_to = self.input.notify_mail_to
        else:
            watch.notify_mail_to = ""
        if is_param(self.input, 'notify_mail_from'):
            watch.notify_mail_from = self.input.notify_mail_from
        else:
            watch.notify_mail_from = ""

        threshold_val1 = self.input.threshold_val1
        threshold_val2 = self.input.threshold_val2
        threshold_type = self.input.threshold_type
        if threshold_type == "max":
            warning_value = create_threshold_value(min_value=None,
                                                   max_value=threshold_val1)
            failure_value = create_threshold_value(min_value=None,
                                                   max_value=threshold_val2)
        elif threshold_type == "min":
            warning_value = create_threshold_value(min_value=threshold_val2,
                                                   max_value=None)
            failure_value = create_threshold_value(min_value=threshold_val1,
                                                   max_value=None)
        else:
            self.logger.debug("Update watch failed. Unknown threshold type.")
            return web.badrequest()

        watch.warning_value = warning_value
        watch.failure_value = failure_value

        ## bool
        bool_input_key = [
            "use_percentage", "enable_warning_mail", "enable_failure_mail",
            "enable_okay_mail", "enable_warning_script",
            "enable_failure_script", "enable_okay_script"
        ]
        bool_values = {}
        for key in bool_input_key:
            if self.input.has_key(key):
                bool_values.update({key: True})
            else:
                bool_values.update({key: False})

        watch.is_warning_percentage = bool_values.get("use_percentage")
        watch.is_warning_script = bool_values.get("enable_warning_script")
        watch.is_warning_mail = bool_values.get("enable_warning_mail")
        watch.is_failure_percentage = bool_values.get("use_percentage")
        watch.is_failure_script = bool_values.get("enable_failure_script")
        watch.is_failure_mail = bool_values.get("enable_failure_mail")
        watch.is_okay_script = bool_values.get("enable_okay_script")
        watch.is_okay_mail = bool_values.get("enable_okay_mail")

        w_update(self.orm, watch)

        plugin = watch.plugin
        plugin_selector = watch.plugin_selector
        modules = ["collectdplugin"]

        host = m_findbyhost1(self.orm, host_id)
        extra_args = {'include': '^threshold_'}
        #extra_args = {}
        dop = read_conf(modules,
                        webobj=self,
                        machine=host,
                        extra_args=extra_args)
        if dop is False:
            self.logger.debug("Change watch failed. Failed read conf.")
            return web.internalerror('Internal Server Error. (Read Conf)')

        params = {}
        if threshold_type == "max":
            params['WarningMax'] = str(threshold_val1)
            params['FailureMax'] = str(threshold_val2)
        elif threshold_type == "min":
            params['WarningMin'] = str(threshold_val2)
            params['FailureMin'] = str(threshold_val1)

        params['Percentage'] = str(bool_values.get("use_percentage")).lower()
        params['Persist'] = "true"
        delete_threshold(plugin,
                         plugin_selector,
                         dop=dop,
                         webobj=self,
                         host=host)
        set_threshold(plugin,
                      plugin_selector,
                      params,
                      dop=dop,
                      webobj=self,
                      host=host)

        extra_args = {}
        command = "/etc/init.d/collectd condrestart"
        extra_args = {"post-command": command}
        retval = write_conf(dop,
                            webobj=self,
                            machine=host,
                            extra_args=extra_args)
        if retval is False:
            self.logger.debug("Change watch failed. Failed write conf.")
            return web.internalerror('Internal Server Error. (Write Conf)')

        return web.accepted()