<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from zabbix_api import Zabbix as ZabbixAPI
from physical.models import DatabaseInfra
import logging

LOG = logging.getLogger(__name__)

class ZabbixProvider(object):

    @classmethod
    def get_credentials(self, environment):
        LOG.info("Getting credentials...")
        from dbaas_credentials.credential import Credential
        from dbaas_credentials.models import CredentialType
        integration = CredentialType.objects.get(type= CredentialType.ZABBIX)
        return Credential.get_credentials(environment= environment, integration= integration)

    @classmethod
    def auth(self, dbinfra):
        credentials = self.get_credentials(environment = dbinfra.environment)
        self.clientgroup = credentials.get_parameter_by_name("clientgroup")

        zapi = ZabbixAPI(url=credentials.endpoint,
                user=credentials.user,
                password=<PASSWORD>)
        return zapi

    @classmethod
    def create_monitoring(self, dbinfra, dbtype="mysql"):
        if isinstance(dbinfra, DatabaseInfra):
            zapi = self.auth(dbinfra=dbinfra)
            LOG.info("Creating zabbix monitoring for hosts...")
            self.create_basic_monitors(zapi= zapi, dbinfra=dbinfra)

            LOG.info("Creating zabbix monitoring for database...")
            if dbtype == "redis":
                self.create_db_monitors_redis(zapi= zapi, dbinfra=dbinfra, dbtype=dbtype)
            else:
                self.create_db_monitors(zapi= zapi, dbinfra=dbinfra, dbtype=dbtype)

    @classmethod
    def destroy_monitoring(self, dbinfra, dbtype="mysql"):
        if isinstance(dbinfra, DatabaseInfra):
            LOG.info("Destroying zabbix monitoring...")
            zapi = self.auth(dbinfra=dbinfra)

            self.destroy_basic_monitors(zapi= zapi, dbinfra= dbinfra)
            if dbtype == "redis":
                self.destroy_db_monitors_redis(zapi= zapi, dbinfra= dbinfra)
            else:
                self.destroy_db_monitors(zapi= zapi, dbinfra= dbinfra)

            for attrs in dbinfra.cs_dbinfra_attributes.all():
                self.destroy_flipper_db_monitors(zapi= zapi, host=attrs.dns)

    @classmethod
    def destroy_db_monitors(self, zapi, dbinfra):
        instances = dbinfra.instances.all()
        for instance in instances:
            LOG.info("Destroying instance %s" % instance)
            zapi.globo_deleteMonitors(params={"host":instance.dns,})

    @classmethod
    def destroy_db_monitors_redis(self, zapi, dbinfra):
        instances = dbinfra.instances.all()
        for instance in instances:
            LOG.info("Destroying instance %s" % instance)
            zapi.globo_deleteMonitors(params={"host": "webmonitor_%s-80-redis-mem" % instance.address })
            zapi.globo_deleteMonitors(params={"host": "webmonitor_%s-80-redis-con" % instance.address})

    @classmethod
    def destroy_flipper_db_monitors(self, zapi, host):
        LOG.info("Destroying host %s" % host)
        zapi.globo_deleteMonitors(params={"host": host,})

    @classmethod
    def destroy_basic_monitors(self, zapi, dbinfra):
        #instances = dbinfra.instances.all()
        instances = dbinfra.instances.exclude(database_type=5)
        for instance in instances:
            host = instance.hostname
            LOG.info("Destroying host %s" % host)
            zapi.globo_deleteMonitors(params={"host":host.hostname,})

    @classmethod
    def create_db_monitors(self, zapi, dbinfra, dbtype):
        #instances = dbinfra.instances.all()
        instances = dbinfra.instances.exclude(database_type=5)
        flipper = 0
        for instance in instances:
            LOG.info("Monitoring %s db instance %s" % (dbtype, instance))

            if instance.is_arbiter:
                arbiter = 1
            else:
                arbiter = 0

            params = {"host" : instance.dns, "dbtype" : dbtype, "alarm" : "yes", "arbiter":arbiter}

            if instances.count() > 1 and dbtype=="mysql":
                params['healthcheck'] = {
                    'host' : instance.dns,
                    'port' : '80',
                    'string' : 'WORKING',
                    'uri' : 'health-check/'
                }
                params['healthcheck_monitor'] = {
                    'host' : instance.dns,
                    'port' : '80',
                    'string' : 'WORKING',
                    'uri' : 'health-check/monitor/'
                }
                zapi.globo_createDBMonitors(params=params)

                if flipper == 0 and dbtype=="mysql":
                    LOG.info("Creating zabbix monitoring for flipper ips...")
                    self.create_flipper_db_monitors(zapi= zapi, dbinfra=dbinfra)
                    flipper=1
            else:
                zapi.globo_createDBMonitors(params=params)

    @classmethod
    def create_db_monitors_redis(self, zapi, dbinfra, dbtype):
        #instances = dbinfra.instances.all()
        instances = dbinfra.instances.exclude(database_type=5)
        for instance in instances:
            params = {
                "address" : instance.address,
                "port" : "80",
                "regexp" : "WORKING",
                "uri": "/health-check/redis-con/",
                "var": "redis-con",
                "alarm" : "yes",
                "notes": dbinfra.name,
                "clientgroup": self.clientgroup,
            }
            
            zapi.globo_createWebMonitors(params=params)

            params["uri"] = "/health-check/redis-mem/"
            params["var"] = "redis-mem"

            zapi.globo_createWebMonitors(params=params)

    @classmethod
    def create_basic_monitors(self, zapi, dbinfra):
        #for instance in dbinfra.instances.all():
        for instance in dbinfra.instances.exclude(database_type=5):
            LOG.info("Monitoring instances %s" % instance)
            host = instance.hostname
            zapi.globo_createBasicMonitors(params={"host": host.hostname, "ip": host.address, "clientgroup": self.clientgroup})

    @classmethod
    def create_flipper_db_monitors(self, zapi, dbinfra):
        for instance in dbinfra.cs_dbinfra_attributes.all():
            LOG.info("Monitoring flipper ip instance %s" % instance)
            params = {"host" : instance.dns, "dbtype" : "mysql", "alarm" : "yes"}
            zapi.globo_createDBMonitors(params=params)
<commit_msg>Redis HA<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from zabbix_api import Zabbix as ZabbixAPI
from physical.models import DatabaseInfra
from physical.models import Host
from physical.models import Instance
import logging

LOG = logging.getLogger(__name__)

class ZabbixProvider(object):

    @classmethod
    def get_credentials(self, environment):
        LOG.info("Getting credentials...")
        from dbaas_credentials.credential import Credential
        from dbaas_credentials.models import CredentialType
        integration = CredentialType.objects.get(type= CredentialType.ZABBIX)
        return Credential.get_credentials(environment= environment, integration= integration)

    @classmethod
    def auth(self, dbinfra):
        credentials = self.get_credentials(environment = dbinfra.environment)
        self.clientgroup = credentials.get_parameter_by_name("clientgroup")

        zapi = ZabbixAPI(url=credentials.endpoint,
                user=credentials.user,
                password=credentials.password)
        return zapi

    @classmethod
    def create_monitoring(self, dbinfra, dbtype="mysql"):
        if isinstance(dbinfra, DatabaseInfra):
            zapi = self.auth(dbinfra=dbinfra)
            LOG.info("Creating zabbix monitoring for hosts...")
            self.create_basic_monitors(zapi= zapi, dbinfra=dbinfra)

            LOG.info("Creating zabbix monitoring for database...")
            if dbtype == "redis":
                self.create_db_monitors_redis(zapi= zapi, dbinfra=dbinfra, dbtype=dbtype)
            else:
                self.create_db_monitors(zapi= zapi, dbinfra=dbinfra, dbtype=dbtype)

    @classmethod
    def destroy_monitoring(self, dbinfra, dbtype="mysql"):
        if isinstance(dbinfra, DatabaseInfra):
            LOG.info("Destroying zabbix monitoring...")
            zapi = self.auth(dbinfra=dbinfra)

            self.destroy_basic_monitors(zapi= zapi, dbinfra= dbinfra)
            if dbtype == "redis":
                self.destroy_db_monitors_redis(zapi= zapi, dbinfra= dbinfra)
            else:
                self.destroy_db_monitors(zapi= zapi, dbinfra= dbinfra)

            for attrs in dbinfra.cs_dbinfra_attributes.all():
                self.destroy_flipper_db_monitors(zapi= zapi, host=attrs.dns)

    @classmethod
    def destroy_db_monitors(self, zapi, dbinfra):
        instances = dbinfra.instances.all()
        for instance in instances:
            LOG.info("Destroying instance %s" % instance)
            zapi.globo_deleteMonitors(params={"host":instance.dns,})

    @classmethod
    def destroy_db_monitors_redis(self, zapi, dbinfra):
        instances = dbinfra.instances.all()
        for instance in instances:
            LOG.info("Destroying instance %s" % instance)
            if instance.database_type == Instance.REDIS:
                zapi.globo_deleteMonitors(params={"host": "webmonitor_%s-80-redis-mem" % instance.dns })
                zapi.globo_deleteMonitors(params={"host": "webmonitor_%s-80-redis-con" % instance.dns})
            elif instance.database_type == Instance.REDIS_SENTINEL:
                zapi.globo_deleteMonitors(params={"host": "webmonitor_%s-80-sentinel-con" % instance.dns})

    @classmethod
    def destroy_flipper_db_monitors(self, zapi, host):
        LOG.info("Destroying host %s" % host)
        zapi.globo_deleteMonitors(params={"host": host,})

    @classmethod
    def destroy_basic_monitors(self, zapi, dbinfra):
        for host in Host.objects.filter(instance__databaseinfra=dbinfra).distinct():
            LOG.info("Destroying host %s" % host)
            zapi.globo_deleteMonitors(params={"host":host.hostname,})

    @classmethod
    def create_db_monitors(self, zapi, dbinfra, dbtype):
        instances = dbinfra.instances.all()
        flipper = 0
        for instance in instances:
            LOG.info("Monitoring %s db instance %s" % (dbtype, instance))

            if instance.is_arbiter:
                arbiter = 1
            else:
                arbiter = 0

            params = {"host" : instance.dns, "dbtype" : dbtype, "alarm" : "yes", "arbiter":arbiter}

            if instances.count() > 1 and dbtype=="mysql":
                params['healthcheck'] = {
                    'host' : instance.dns,
                    'port' : '80',
                    'string' : 'WORKING',
                    'uri' : 'health-check/'
                }
                params['healthcheck_monitor'] = {
                    'host' : instance.dns,
                    'port' : '80',
                    'string' : 'WORKING',
                    'uri' : 'health-check/monitor/'
                }
                zapi.globo_createDBMonitors(params=params)

                if flipper == 0 and dbtype=="mysql":
                    LOG.info("Creating zabbix monitoring for flipper ips...")
                    self.create_flipper_db_monitors(zapi= zapi, dbinfra=dbinfra)
                    flipper=1
            else:
                zapi.globo_createDBMonitors(params=params)

    @classmethod
    def create_db_monitors_redis(self, zapi, dbinfra, dbtype):
        instances = dbinfra.instances.all()
        for instance in instances:
            if instance.database_type == Instance.REDIS:
                params = {
                    "address" : instance.dns,
                    "port" : "80",
                    "regexp" : "WORKING",
                    "uri": "/health-check/redis-con/",
                    "var": "redis-con",
                    "alarm" : "yes",
                    "notes": dbinfra.name,
                    "clientgroup": self.clientgroup,
                }

                zapi.globo_createWebMonitors(params=params)

                params["uri"] = "/health-check/redis-mem/"
                params["var"] = "redis-mem"

                zapi.globo_createWebMonitors(params=params)

            elif instance.database_type == Instance.REDIS_SENTINEL:
                params = {
                    "address" : instance.dns,
                    "port" : "80",
                    "regexp" : "WORKING",
                    "uri": "/health-check/sentinel-con/",
                    "var": "sentinel-con",
                    "alarm" : "yes",
                    "notes": dbinfra.name,
                    "clientgroup": self.clientgroup,
                }

                zapi.globo_createWebMonitors(params=params)


    @classmethod
    def create_basic_monitors(self, zapi, dbinfra):
        for host in Host.objects.filter(instance__databaseinfra=dbinfra).distinct():
            LOG.info("Monitoring hosts %s" % host)
            zapi.globo_createBasicMonitors(params={"host": host.hostname, "ip": host.address, "clientgroup": self.clientgroup})

    @classmethod
    def create_flipper_db_monitors(self, zapi, dbinfra):
        for instance in dbinfra.cs_dbinfra_attributes.all():
            LOG.info("Monitoring flipper ip instance %s" % instance)
            params = {"host" : instance.dns, "dbtype" : "mysql", "alarm" : "yes"}
            zapi.globo_createDBMonitors(params=params)

{'fraction_non_alphanumeric': 0.10919710187786486, 'fraction_numerical': 0.0031051308590862043, 'mean_word_length': 2.1385150812064966, 'pattern_counts': {'":': 39, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84311', 'n_tokens_mistral': 4213, 'n_tokens_neox': 3915, 'n_words': 746}