#!/bin/bash

FILEBEAT_CONFIG_DIR=/etc/uops-filebeat/conf.d
FILEBEAT_LOG_DIR=/var/log/filebeat
FILEBEAT_CONFIG_FILE=/etc/uops-filebeat/filebeat.yml
FILEBEAT_SYSCONFIG_FILE=/etc/uops-filebeat/conf.d/uops-log.yml

_install() {
    if [[ $ARCH != 'x86_64' ]];then
        return 0
    fi
    yum install -y filebeat > /dev/null 2>&1
    mkdir -p $FILEBEAT_CONFIG_DIR
    mkdir -p $FILEBEAT_LOG_DIR
    cat > $FILEBEAT_CONFIG_FILE << EOF
######################## Filebeat Configuration ############################

#================================ General =====================================
filebeat.config.inputs:
  enabled: true
  path: conf.d/*.yml

name: filebeat
tags: ["$HOSTIP"]
max_procs: 1

#================================ Outputs =====================================

output.kafka:
  hosts: ["kafka.usls.service.ucloud.cn:9092"]
  topic: '%{[type]}'
  partition.round_robin:
    reachable_only: false
  metadata:
    refresh_frequency: 1m
  worker: 3
  bulk_max_size: 20480
  compression: lz4
  version: "1.0"
  max_message_bytes: 1000000

#================================ Logging=====================================

logging.level: error
logging.to_files: false
logging.files:
  path: /var/log/filebeat
  name: uops-filebeat.log
  rotateeverybytes: 104857600 # = 100MB
  keepfiles: 7
EOF

    cat > $FILEBEAT_SYSCONFIG_FILE << EOF
- type: log
  paths:
    - /var/log/messages
  fields:
    type: uops-sys-messages
  fields_under_root: true
  tail_files: false

- type: log
  paths:
    - /var/log/dmesg
  fields:
    type: uops-sys-dmesg
  fields_under_root: true
  tail_files: false

- type: log
  paths:
    - /var/log/ntp.log
  fields:
    type: uops-sys-ntplog
  fields_under_root: true
  tail_files: false

- type: log
  paths:
    - /var/log/cron
  fields:
    type: uops-sys-cron
  fields_under_root: true
  tail_files: false

- type: log
  paths:
    - /var/log/mcelog
  fields:
    type: uops-sys-mcelog
  fields_under_root: true
  tail_files: false
  multiline.pattern: ^Hardware
  multiline.negate: true
  multiline.match: after

- type: log
  paths:
    - /var/log/openvswitch/ovs-vswitchd.log
  fields:
    type: uops-sys-ovs
  fields_under_root: true
  tail_files: false

- type: log
  paths:
    - /root/.bash_history
  fields:
    type: uops-sys-bash_history
  fields_under_root: true
  tail_files: false
  multiline.pattern: ^\#\d{10}
  multiline.negate: true
  multiline.match: after
EOF
    if [[ $OS == "CentOS6" ]];then

        cp -f $BASEDIR/configs/filebeat/uops-filebeat /etc/init.d/ > /dev/null 2>&1

        chmod +x /etc/init.d/uops-filebeat > /dev/null 2>&1

        /etc/init.d/uops-filebeat start > /dev/null 2>&1
        chkconfig uops-filebeat on > /dev/null 2>&1
    elif [[ $OS == "CentOS7" ]];then
        cp -f $BASEDIR/configs/filebeat/uops-filebeat.service /etc/systemd/system/ > /dev/null 2>&1

        chmod 0644 /etc/systemd/system/uops-filebeat.service > /dev/null 2>&1

        systemctl start uops-filebeat > /dev/null 2>&1
        systemctl enable uops-filebeat > /dev/null 2>&1
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi
    return 0

}


_check() {
    _check="PASS"
    if [[ $ENVFLAG = 'offline' ]];then
        return 0
    fi
    if [[ $ARCH != 'x86_64' ]];then
        return 0
    fi
    local is_installed=`rpm -qa|grep -c filebeat`
    if [[ $is_installed -eq 0 ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] Filebeat Not Installed."
    fi
    if [[ ! -f $FILEBEAT_CONFIG_FILE ]] || [[ ! -f $FILEBEAT_SYSCONFIG_FILE ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] Filebeat Config File Not Correct."
    fi
    if [[ $OS == "CentOS6" ]];then
        if [[ $(chkconfig --list |grep uops-filebeat | grep -c :on) -eq 0 ]]; then
            _check="FAIL"
            PRINT_ERR "[ERROR] uops-filebeat Not Enabled In Chkconfig List"
        fi

    elif [[ $OS == "CentOS7" ]];then
        systemctl is-enabled "uops-filebeat" >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            _check="FAIL"
            PRINT_ERR "[ERROR] uops-filebeat Not Enabled In Chkconfig List"
        fi
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi
    service uops-filebeat status &>/dev/null
    if [[ $? -ne 0 ]]; then
        _check="FAIL"
        PRINT_ERR "[ERROR] uops-filebeat not running"
    fi

}
