def _delete_panel(self, cluster_id, resource_type, resource_name=None):
     alert_dashboard = alert_utils.get_alert_dashboard(resource_type)
     alert_utils.remove_row(alert_dashboard, cluster_id, resource_type,
                            resource_name)
     resp = dashboard._post_dashboard(alert_dashboard)
     response = []
     response.append(copy.deepcopy(resp))
     if resource_name is None:
         dashboard_names = ["volumes", "hosts", "bricks"]
         for dash_name in dashboard_names:
             alert_dashboard = alert_utils.remove_cluster_rows(
                 cluster_id, dash_name)
             resp = dashboard._post_dashboard(alert_dashboard)
             response.append(copy.deepcopy(resp))
     return response
    def __init__(self):

        cluster_detail_list = create_dashboards.get_cluster_details()

        if cluster_detail_list:

            # Uploading Alert Dashboard for Volume
            volume_dashboard = create_dashboards.create_volume_dashboard(
                cluster_detail_list)
            response = dashboard._post_dashboard(volume_dashboard)
            if response.status_code == 200:

                msg = '\n' + "Volume Dashboard uploaded successfully" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})
            else:
                msg = '\n' + "Volume Dashboard uploaded failed" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})

            # Uploading Alert Dashboard for Brick
            brick_dashboard = create_dashboards.create_brick_dashboard(
                cluster_detail_list)
            response = dashboard._post_dashboard(brick_dashboard)
            if response.status_code == 200:

                msg = '\n' + "Brick Dashboard uploaded successfully" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})
            else:
                msg = '\n' + "Brick Dashboard uploaded failed" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})

            # Uploading Alert Dashboard for Host
            host_dashboard = create_dashboards.create_host_dashboard(
                cluster_detail_list)
            response = dashboard._post_dashboard(host_dashboard)
            if response.status_code == 200:

                msg = '\n' + "Host Dashboard uploaded successfully" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})
            else:
                msg = '\n' + "Host Dashboard uploaded failed" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})
    def _add_panel(self, cluster_id, resource_type, resource_name=None):

        if resource_type == "volume":

            alert_dashboard = alert_utils.get_alert_dashboard("volumes")
            alert_row = alert_utils.fetch_row(alert_dashboard)
            alert_utils.add_volume_panel(alert_row, cluster_id, resource_name)
            dash_json = alert_utils.create_updated_dashboard(alert_dashboard,
                                                             alert_row)
            resp = dashboard._post_dashboard(json.dumps(dash_json))
            return resp

        elif resource_type == "host":

            alert_dashboard = alert_utils.get_alert_dashboard("hosts")
            alert_row = alert_utils.fetch_row(alert_dashboard)
            alert_utils.add_node_panel(alert_row, cluster_id, resource_name)
            dash_json = alert_utils.create_updated_dashboard(alert_dashboard,
                                                             alert_row)
            resp = dashboard._post_dashboard(json.dumps(dash_json))
            return resp

        elif resource_type == "brick":

            alert_dashboard = alert_utils.get_alert_dashboard("bricks")
            alert_row = alert_utils.fetch_row(alert_dashboard)
            alert_utils.add_brick_panel(alert_row, cluster_id, resource_name)
            dash_json = alert_utils.create_updated_dashboard(alert_dashboard,
                                                             alert_row)
            resp = dashboard._post_dashboard(json.dumps(dash_json))
            return resp

        elif resource_type == "cluster":

            alert_dashboard = alert_utils.get_alert_dashboard("at-a-glance")
            alert_row = alert_utils.fetch_row(alert_dashboard)
            alert_utils.add_cluster_panel(alert_row, cluster_id)
            dash_json = alert_utils.create_updated_dashboard(alert_dashboard,
                                                             alert_row)
            resp = dashboard._post_dashboard(json.dumps(dash_json))
            return resp
Beispiel #4
0
    def __init__(self):
        org_key = "_NS/monitoring/grafana_org_id"
        auth_key = "_NS/monitoring/grafana_auth_key"
        cluster_detail_list = create_dashboards.get_cluster_details()
        org_id = NS.config.data.get("org_id", None)
        if not org_id:
            try:
                org_id = etcd_utils.read(org_key).value
            except etcd.EtcdKeyNotFound:
                org_id = grafana_org_utils.create_org("Alert_dashboard")
                try:
                    etcd_utils.write(org_key, org_id)
                except etcd.EtcdKeyNotFound:
                    pass
                NS.config.data["org_id"] = org_id
        key = ""
        if grafana_org_utils.switch_context(org_id):
            key = NS.config.data.get("grafana_auth_key", None)
            if not key:
                try:
                    key = etcd_utils.read(auth_key).value
                except etcd.EtcdKeyNotFound:
                    key = grafana_org_utils.create_api_token(
                        "grafana_auth_key", "Admin")
                    try:
                        etcd_utils.write(auth_key, key)
                    except etcd.EtcdKeyNotFound:
                        pass
                    NS.config.data["grafana_auth_key"] = key
            response = datasource.create_datasource()
            if response.status_code == 200:
                msg = '\n' + "Datasource " + \
                      " uploaded successfully" + '\n'
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})

            else:
                msg = "Datasource upload failed. Error code: {0} ," + \
                      "Error message: " + \
                      "{1} ".format(
                          response.status_code,
                          str(self.get_message_from_response(response)))
                logger.log("info", NS.get("publisher_id", None),
                           {'message': msg})
            if cluster_detail_list:
                resource_name = ["volumes", "hosts", "bricks", "clusters"]
                for resource in resource_name:
                    # Uploading Alert Dashboards
                    resource_dashboard = \
                        create_dashboards.create_resource_dashboard(
                            cluster_detail_list, resource)
                    response = dashboard._post_dashboard(
                        resource_dashboard, key)
                    if response.status_code == 200:
                        msg = '\n' + "{} dashboard uploaded successfully". \
                            format(str(resource)) + '\n'
                        logger.log("info", NS.get("publisher_id", None),
                                   {'message': msg})
                    else:
                        msg = '\n' + "{} dashboard upload failed".format(
                            str(resource)) + '\n'
                        logger.log("info", NS.get("publisher_id", None),
                                   {'message': msg})
        else:
            msg = "Could not switch context, Alert dashboard upload failed"
            logger.log("error", NS.get("publisher_id", None), {'message': msg})
    def _add_panel(self, cluster_id, resource_type, resource_name=None):
        if resource_type == "nodes":
            resource_name = resource_name.replace(".", "_")
        alert_dashboard = alert_utils.get_alert_dashboard(resource_type)
        if alert_dashboard:
            alert_row = alert_utils.fetch_row(alert_dashboard)
            alert_utils.add_resource_panel(alert_row, cluster_id,
                                           resource_type, resource_name)
            dash_json = alert_utils.create_updated_dashboard(
                alert_dashboard, alert_row)
            dashboard._post_dashboard(dash_json)
        else:
            logger.log("error", NS.get("publisher_id", None),
                       {'message': "Dashboard not found"})

        if resource_type == "volumes":
            cluster_key = "/clusters/" + str(cluster_id)
            vol_id = None
            try_count = 0
            while try_count < 18 and not vol_id:
                time.sleep(10)
                if vol_id is not None:
                    break
                volumes = etcd_utils.read(cluster_key + "/Volumes")
                for volume in volumes.leaves:
                    volume_id = volume.key.rsplit("/")[-1]
                    try:
                        vol_name_key = cluster_key + "/Volumes/" + \
                            volume_id + "/name"
                        vol_name = etcd_utils.read(vol_name_key).value
                        if vol_name == resource_name:
                            vol_id = volume_id
                            break
                    except (etcd.EtcdKeyNotFound, KeyError):
                        try_count = try_count + 1
                        pass
            if try_count == 18:
                return

            time.sleep(10)
            volume_key = "/clusters/" + str(cluster_id) + "/Volumes/" + \
                str(vol_id)
            subvols = etcd_utils.read(volume_key + "/Bricks")
            brick_list = []
            for subvol in subvols.leaves:
                subvol_name = subvol.key.rsplit("/")[-1]
                try:
                    subvolume_key = volume_key + "/Bricks/" + subvol_name
                    brick_details = etcd_utils.read(subvolume_key)
                    for entry in brick_details.leaves:
                        try:
                            brick_name = entry.key.rsplit("/", 1)[1]
                            brick_key = cluster_key + "/Bricks/all/" + \
                                brick_name.split(":", 1)[0] + \
                                "/" + brick_name.split(":_", 1)[1] + \
                                "/brick_path"
                            brick_name = etcd_utils.read(brick_key).value
                            brick_list.append(copy.deepcopy(brick_name))
                        except (KeyError, etcd.EtcdKeyNotFound):
                            pass
                except (KeyError, etcd.EtcdKeyNotFound):
                    # This should not happen as the bricks would be
                    # added by this time
                    pass

            for brick in brick_list:
                self._add_panel(cluster_id, "bricks", brick)