Пример #1
0
def parse_yaml(_file):
    with open(_file) as stream:
        try:
            result = yaml.load(stream)
        except yaml.YAMLError as exc:
            logger.error('Error parsing yaml file'+str(exc), exc_info=True)
    return result
Пример #2
0
def main_config():
    form = TextEditForm()

    if request.method == 'GET':
        try:
            with open(MAIN_CONFIG, 'r') as f:
                file_content = f.read()
        except:
            logger.error('Can\'t read main config file')

        form.text.data = file_content
        return render_template('main_config.html', form=form)

    elif request.method == 'POST' and form.goback_button.data:
        return redirect(url_for('overview'))

    elif request.method == 'POST' and form.saving_button.data and form.validate():
        try:
            _bck_name = backup_file(MAIN_CONFIG)
        except:
            flash('Can\'t make backup of the main config file. Stopping oprations.')
            logger.error('Can\'t make backup of the main config file. Stopping oprations.')
            return redirect(url_for('overview'))

        save_file(MAIN_CONFIG,form.text.data)
        flash('Created backup of the main config file in backup folder. '+MAIN_CONFIG+' updated.')
        return redirect(url_for('overview'))
Пример #3
0
def edit_rule(name):
    rule_obj = RuleObj()
    form = TextEditForm()
    rule_filename = RULES_PATH+name+'.yaml'

    if request.method == 'GET':
        try:
            with open(rule_filename, 'r') as f:
                file_content = f.read()
        except:
            logger.error('Error reading rule file')

        form.text.data = file_content
        return render_template('edit_rule.html', form=form)

    elif request.method == 'POST' and form.goback_button.data:
        return redirect(url_for('rules_list'))

    elif request.method == 'POST' and form.saving_button.data and form.validate():
        try:
            _bck_name = backup_file(rule_filename)
        except:
            flash('Can\'t make backup of the rule file. Stopping oprations.')
            logger.error('Can\'t make backup of the rule file. Stopping oprations.')
            return redirect(url_for('rules_list'))

        save_file(rule_filename,form.text.data)
        flash('Created backup of the rulefile in backup folder. '+rule_filename+' updated.')
        return redirect(url_for('rules_list'))
Пример #4
0
def backup_file(_file):
    _bck_file = os.path.basename(_file)+'.'+datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M')
    try:
        shutil.copy(_file, BACKUP_PATH+_bck_file)
    except:
        logger.error('Error during file backup.', exc_info=True)
        raise ValueError('Error during file backup.')

    return _bck_file
Пример #5
0
def overview():
    try:
        files = list_files(RULES_PATH)
    except:
        logger.error('Cannot access rules directory.')
    files_no_ex = []
    for x in range(len(files)):
        #remove extension
        _temp = os.path.splitext(files[x])[0]
        files_no_ex.append(_temp)

    return render_template('overview.html', files=files_no_ex)
Пример #6
0
def save_rule(_file,_data):
    with open(_file, 'w+') as stream:
        for key,value in _data.items():
            if str(key) == 'filter':
                try:
                    stream.write('filter:\n'+'- query:\n'+'    query_string:\n'+'       query: \''+str(value)+'\'\n')
                except:
                    logger.error('Error writing: '+str(key)+': '+str(value)+'\n', exc_info=True)
            elif str(key) == 'alert':
                try:
                    stream.write('alert:\n'+'- "email"\n')
                except:
                    logger.error('Error writing: '+str(key)+': '+str(value)+'\n', exc_info=True)
            elif str(key) == 'email':
                try:
                    stream.write('email:\n'+'- '+'"'+str(value)+'"\n')
                except:
                    logger.error('Error writing: '+str(key)+': '+str(value)+'\n', exc_info=True)
            elif ': ' in str(value):
                try:
                    stream.write(str(key)+': \n'+'  '+str(value)+'\n')
                except:
                    logger.error('Error writing: '+str(key)+': '+str(value)+'\n', exc_info=True)
            elif str(key) == 'saving_button' or str(key) == 'reading_button' or str(key) == 'goback_button':
                try:
                    stream.write('\n')
                except:
                    logger.error('Error writing: '+str(key)+': '+str(value)+'\n', exc_info=True)
            else:
                try:
                    stream.write(str(key)+': '+str(value)+'\n')
                except:
                    logger.error('Error writing: '+str(key)+': '+str(value)+'\n', exc_info=True)
Пример #7
0
def save_file(_file,_data):
    try:
        with open(_file, 'w') as f:
            f.write(_data)
    except:
        logger.error('Error saving file', exc_info=True)
Пример #8
0
def delete_rule(_file):
    try:
        os.remove(_file)
    except:
        logger.error('Error deleting file', exc_info=True)