def push(logs,config):
    """
    Pushes logs to redis server
    """    
    redisConfig=dict(config)
    try:
        red=redis.Redis(redisConfig['hostname'])
    
        for log in logs:
            red.lpush(redisConfig['key'],log)
            
    except Exception as err:
        print('Connection to logging redis failed. {0}'.format(err))
        Graylog2Logger.logToGraylog2('Connection to logging redis failed. {0}'.format(err))
def push(logs, config):
    """
    Writes logs to a file
    """
    fileConfig = dict(config)

    try:
        f = open(fileConfig["path"], "a")
        for log in logs:
            f.write(str(log))

    except Exception, e:
        print("Error writing logs to file. {0}".format(e))
        Graylog2Logger.logToGraylog2("Error writing logs to file. {0}".format(e))
def push(logs,config):
    """
        Pushes the logs via TCP
    """
    tcpConfig=dict(config)
    
    tcp_host=str(tcpConfig["hostname"])
    tcp_port=int(tcpConfig["port"])
    
    sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        
    for log in logs:
        try:
            sock.sendto(str(log),(tcp_host,tcp_port))
        except Exception as err:
            print('TCP connection failed. {0}'.format(err))
            Graylog2Logger.logToGraylog2('TCP connection failed. {0}'.format(err))
def pullSlowLogsFromMysql(host,user,password,rotate_logs):
    """
    Pulls slow logs from mysql and rotates the table if asked to
    """
    
    try:
        cnx=mysql.connector.connect(user=user,host=host,password=password,database='mysql')
    
        logs=[]
        
        #rowDict = {'start_time':'','query_time':'','lock_time':'','rows_sent':'','rows_examined':'','db':'','last_insert_id':'','insert_id':'','server_id':'','sql_text':'' }
        cursor = cnx.cursor()
        
        query = ("SELECT start_time, query_time, lock_time, rows_sent, rows_examined, db, `last_insert_id`, insert_id, server_id, sql_text FROM mysql.slow_log limit 10 ; ")

        cursor.execute(query)

        for (start_time, query_time, lock_time, rows_sent, rows_examined, db, last_insert_id, insert_id, server_id, sql_text) in cursor:
            rowDict={}
            rowDict['start_time']=str(start_time)
            rowDict['query_time']=str(query_time)
            rowDict['lock_time']=str(lock_time)
            rowDict['rows_sent']=str(rows_sent)
            rowDict['rows_examined']=str(rows_examined)
            rowDict['db']=str(db)
            rowDict['last_insert_id']=str(last_insert_id)
            rowDict['insert_id']=str(insert_id)
            rowDict['server_id']=str(server_id)
            rowDict['sql_text']=str(sql_text)
            logs.append(rowDict)
            
        cursor.close() 
        
        if rotate_logs:
            cur=cnx.cursor()
            cur.execute('CALL mysql.rds_rotate_slow_log;')
            cur.close()
        
        return logs
    
    except Exception as err:
        Graylog2Logger.logToGraylog2('Connection to production mysql failed. {0}'.format(err))    
    
    finally:
        cnx.close()
def pullGeneralLogsFromMysql(host,user,password,rotate_logs):
    """
    Pulls general logs from mysql and rotate the table
    """
    
    try:
        cnx=mysql.connector.connect(host=host,user=user,password=password,database='mysql')
    
        logs=[]
        
#        rowDict = {'event_time':'','user_host':'','thread_id':'','server_id':'','command_type':'','argument':'' }
        cursor = cnx.cursor()
        
        query = ("SELECT event_time, user_host, thread_id, server_id, command_type, argument FROM mysql.general_log limit 10 ; ")

        cursor.execute(query)

        for (event_time, user_host, thread_id, server_id, command_type, argument) in cursor:
            rowDict={}
            rowDict['event_time']=str(event_time)
            rowDict['user_host']=str(user_host)
            rowDict['thread_id']=str(thread_id)
            rowDict['server_id']=str(server_id)
            rowDict['command_type']=str(command_type)
            rowDict['argument']=str(argument)
            logs.append(rowDict)
            
        cursor.close() 
        
        if rotate_logs:
            cur=cnx.cursor()
            cur.execute('CALL mysql.rds_rotate_general_log;')
            cur.close()
        
        
        return logs
    
    except Exception as err:
        Graylog2Logger.logToGraylog2('Connection to production mysql failed. {0}'.format(err))   
    
    finally:
        cnx.close()
def Parse(path):
    """
        Parses config yaml file passed to Runner as command line argument
    """
    # Check if file exists at path
    
    if(os.path.isfile(str(path)) == False):
        print('Oh dear ! No file found at the specified path for the yaml config file.')
        Graylog2Logger.logToGraylog2('No file found at {0}'.format(str(path)))
        
    # Try loading the yaml config file
    
    
    try:
        f=open(path)
        dataMap=yaml.load(f)
    except:
        print('Error parsing yaml file')
        Graylog2Logger.logToGraylog2('Error parsing yaml file')
        return None
    
    if not(dataMap is None):
        return dataMap