<commit_before>'''''''''
# DNS Manager to update dss related dns records n DNSaaS  
'''''''''
import threading
import json
import time
import logging

import httplib2 as http

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

def config_logger(log_level=logging.DEBUG):
    logging.basicConfig(format='%(threadName)s \t %(levelname)s %(asctime)s: \t%(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p',
                        log_level=log_level)
    logger = logging.getLogger(__name__)
    logger.setLevel(log_level)
    return logger

LOG = config_logger()

# To be replaced with python logging
def writeLogFile(swComponent ,msgTo, statusReceived, jsonReceived):
    LOG.debug(swComponent + ' ' + msgTo)
    '''
    with os.fdopen(os.open(os.path.join(HERE, 'LOG_ERROR_FILE.csv'), os.O_WRONLY | os.O_CREAT, 0600), 'a') as csvfile:
        writer = csv.writer(csvfile, delimiter=';',quotechar='|', quoting=csv.QUOTE_MINIMAL)
        writer.writerow([str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')),' ['+swComponent+'] ',msgTo, statusReceived, jsonReceived ])
        csvfile.close()
    '''
        
class SOMonitor(threading.Thread):
    '''
    This class is responsible to add triggers and items which are needed for monitoring DSS SICs
    It also updates the decision array ( according to the added triggers ) which will be used in decision part of service orchestrator
    And contacts zabbix to get the problematic triggers so that SO-D can take scale decisions accordingly 
    '''
    def __init__(self, executionModule, decisionModule, ipMaaS = '192.168.100.21', portMaaS = 0 , apiurlMaaS = 'http://192.168.100.21/zabbix/api_jsonrpc.php', apiUserMaaS = 'admin', apiPassMaaS = 'zabbix'):
        self.swComponent = 'SO-Monitor'
        threading.Thread.__init__(self)
        
        self.ipMaaS = ipMaaS
        self.portMaaS = portMaaS
        self.apiurlMaaS = apiurlMaaS
        self.apiUserMaaS = apiUserMaaS
        self.apiPassMaaS = apiPassMaaS
        self.__authId = ''
        self.addedServers = {}
        self.so_e = executionModule
        self.so_d = decisionModule
        
        self.mode = "addtriggers"
        writeLogFile(self.swComponent,"SOMonitor initiated ................",'','')
        
    def run(self):
        i = 0
        result = -1
        while (result < 0):
            time.sleep(1)
            result, serverList = self.so_e.getServerNamesList()
        
        while 1:
            # Getting the triggers which are triggered if in monitoring mode        
            if self.mode == "checktriggers":
                time.sleep(10)
                if i > 6:
                    result, serverList = self.so_e.getServerNamesList()
                    i = 0
                else:
                    i += 1
                writeLogFile(self.swComponent,time.strftime("%H:%M:%S") ,'','')
                self.so_d.hostsWithIssues = []
                for item in serverList:
                    writeLogFile(self.swComponent,item ,'','')
                    if len(item) > 1:
                        if 'mcr' in item:
                            self.so_d.playerCount = self.getMetric(item.replace("_","-"),"DSS.Players.CNT")
                            writeLogFile(self.swComponent,"Number of active players: " + str(self.so_d.playerCount),'','')

                        res = self.getProblematicTriggers(item.replace("_","-"))
                        for trigger in res:
                            self.so_d.hostsWithIssues.append(trigger)
                        writeLogFile(self.swComponent,str(res) ,'','')
                writeLogFile(self.swComponent,str(self.so_d.hostsWithIssues) ,'','')
            # Idle mode will be enabled when scaling out is happening    
            elif self.mode == "idle":
                time.sleep(5)

    #Add trigger to zabbix and update decision array            
    def configTrigger(self, tName, zName, tCondition):
        triggerName = tName
        zabbixName = zName
        if zabbixName + ":" + triggerName not in self.addedServers.keys():
            if len(zabbixName) > 1:
                result = self.addTriggerToMaas(zabbixName, triggerName,'{' + zabbixName + tCondition)
                if result != -1:
                    self.so_d.decisionArray.update({str(result):[str(zabbixName), str(triggerName)]})
                    self.addedServers[zabbixName + ":" + triggerName] = result 
                    writeLogFile(self.swComponent,"Trigger added and id is : " + result ,'','')
            return 1<commit_msg>- fixing minor issue with Monitoring class<commit_after>'''''''''
# DNS Manager to update dss related dns records n DNSaaS  
'''''''''
import threading
import json
import time
import logging

import httplib2 as http

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

def config_logger(log_level=logging.DEBUG):
    logging.basicConfig(format='%(threadName)s \t %(levelname)s %(asctime)s: \t%(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p',
                        log_level=log_level)
    logger = logging.getLogger(__name__)
    logger.setLevel(log_level)
    return logger

LOG = config_logger()

# To be replaced with python logging
def writeLogFile(swComponent ,msgTo, statusReceived, jsonReceived):
    LOG.debug(swComponent + ' ' + msgTo)
    '''
    with os.fdopen(os.open(os.path.join(HERE, 'LOG_ERROR_FILE.csv'), os.O_WRONLY | os.O_CREAT, 0600), 'a') as csvfile:
        writer = csv.writer(csvfile, delimiter=';',quotechar='|', quoting=csv.QUOTE_MINIMAL)
        writer.writerow([str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')),' ['+swComponent+'] ',msgTo, statusReceived, jsonReceived ])
        csvfile.close()
    '''
        
class SOMonitor(threading.Thread):
    '''
    This class is responsible to add triggers and items which are needed for monitoring DSS SICs
    It also updates the decision array ( according to the added triggers ) which will be used in decision part of service orchestrator
    And contacts zabbix to get the problematic triggers so that SO-D can take scale decisions accordingly 
    '''
    def __init__(self, executionModule, decisionModule, ipMaaS = '192.168.100.21', portMaaS = 0 , apiurlMaaS = 'http://192.168.100.21/zabbix/api_jsonrpc.php', apiUserMaaS = 'admin', apiPassMaaS = '<PASSWORD>'):
        self.swComponent = 'SO-Monitor'
        threading.Thread.__init__(self)
        
        self.ipMaaS = ipMaaS
        self.portMaaS = portMaaS
        self.apiurlMaaS = apiurlMaaS
        self.apiUserMaaS = apiUserMaaS
        self.apiPassMaaS = apiPassMaaS
        self.__authId = ''
        self.addedServers = {}
        self.so_e = executionModule
        self.so_d = decisionModule
        
        self.mode = "addtriggers"
        writeLogFile(self.swComponent,"SOMonitor initiated ................",'','')
        
    def run(self):
        i = 0
        result = -1
        while (result < 0):
            time.sleep(1)
            result, serverList = self.so_e.getServerNamesList()
        
        while 1:
            # Getting the triggers which are triggered if in monitoring mode        
            if self.mode == "checktriggers":
                time.sleep(10)
                if i > 6:
                    result, serverList = self.so_e.getServerNamesList()
                    i = 0
                else:
                    i += 1
                writeLogFile(self.swComponent,time.strftime("%H:%M:%S") ,'','')
                self.so_d.hostsWithIssues = []
                for item in serverList:
                    writeLogFile(self.swComponent,item ,'','')
                    if len(item) > 1:
                        if 'mcr' in item:
                            self.so_d.playerCount = self.getMetric(item.replace("_","-"),"DSS.Players.CNT")
                            writeLogFile(self.swComponent,"Number of active players: " + str(self.so_d.playerCount),'','')

                        res = self.getProblematicTriggers(item.replace("_","-"))
                        try:
                            for trigger in res:
                                self.so_d.hostsWithIssues.append(trigger)
                        except:
                            writeLogFile(self.swComponent,'Be careful! Your trigger stats are messed up!' ,'','')
                            continue;
                        writeLogFile(self.swComponent,str(res) ,'','')
                writeLogFile(self.swComponent,str(self.so_d.hostsWithIssues) ,'','')
            # Idle mode will be enabled when scaling out is happening    
            elif self.mode == "idle":
                time.sleep(5)

    #Add trigger to zabbix and update decision array            
    def configTrigger(self, tName, zName, tCondition):
        triggerName = tName
        zabbixName = zName
        if zabbixName + ":" + triggerName not in self.addedServers.keys():
            if len(zabbixName) > 1:
                result = self.addTriggerToMaas(zabbixName, triggerName,'{' + zabbixName + tCondition)
                if result != -1:
                    self.so_d.decisionArray.update({str(result):[str(zabbixName), str(triggerName)]})
                    self.addedServers[zabbixName + ":" + triggerName] = result 
                    writeLogFile(self.swComponent,"Trigger added and id is : " + result ,'','')
            return 1
{'fraction_non_alphanumeric': 0.11752272246882266, 'fraction_numerical': 0.009088987529063623, 'mean_word_length': 2.0515962592712027, 'pattern_counts': {'":': 8, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '1864270', 'n_tokens_mistral': 2883, 'n_tokens_neox': 2723, 'n_words': 632}