#!/bin/bash
# 定义颜色函数
color(){
    case $1 in
        "red") echo -e "\033[31m$2\033[0m" ;;     # 红色
        "green") echo -e "\033[32m$2\033[0m" ;;   # 绿色
        "yellow") echo -e "\033[33m$2\033[0m" ;;  # 黄色
        "blue") echo -e "\033[34m$2\033[0m" ;;    # 蓝色
        *) echo "$2" ;;
    esac
}


set_service() {
    setenforce 0
    sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/sysconfig/selinux
    sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/selinux/config
    systemctl stop iptables firewalld postfix acpid ip6tables mcelogd mdmonitor rpcbind rpcgssd \
                   rpcidmapd auditd haldaemon lldpad atd kdump netfs nfslock openct
    systemctl disable iptables firewalld postfix acpid ip6tables mcelogd mdmonitor rpcbind rpcgssd \
                   rpcidmapd auditd haldaemon lldpad atd kdump netfs nfslock openct 
    systemctl enable sshd crond 
}
  
install_package() {
    yum -y install epel-release
    yum -y install vim lsof zip strace wget openssl openssl-devel gcc gcc-c++ cmake bind-utils \
                   libxml2-devel net-tools sysstat  ntpdate curl telnet lbzip2 bzip2 bzip2-devel \
                   pcre pcre-devel zlib-devel python-devel man whois gita lrzsz
}
  
init_system() {
timedatectl set-timezone $TimeZone
echo 'LANG="en_US.UTF-8"' > /etc/locale.conf
}
  
set_kernel_parameter() {
cat > /etc/sysctl.conf << EOF
fs.file-max = 262144
vm.max_map_count=262144
kernel.core_uses_pid = 1
kernel.msgmax = 65536
kernel.msgmnb = 65536
kernel.shmall = 4294967296
kernel.shmmax = 68719476736
kernel.sysrq = 0
net.bridge.bridge-nf-call-arptables = 0
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 2048
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.default.rp_filter=1
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.lo.arp_announce=2
net.ipv4.ip_forward = 0
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.ip_nonlocal_bind=1
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.tcp_fin_timeout = 12
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 50000
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_sack = 1
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.netfilter.nf_conntrack_max = 655350
net.netfilter.nf_conntrack_tcp_timeout_established = 1200
vm.swappiness = 1
EOF
}
  
set_system_limit() {
cat > /etc/security/limits.conf <<EOF
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4
# End of file

root soft nofile 65535
root hard nofile 65535
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
EOF
}
  
agent_install(){
cd /root/
wget -c http://54.179.201.103:8001/zabbix-agent-3.4.15-1.el7.x86_64.rpm
rpm -ivh /root/zabbix-agent-3.4.15-1.el7.x86_64.rpm
cd /etc/zabbix/ && rm -f zabbix_agentd.conf
cat > /etc/zabbix/zabbix_agentd.conf<<EOF
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=${Zabbix_Server_Address}
ServerActive=${Zabbix_Server_Address}
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
EOF
systemctl start zabbix-agent.service
systemctl status zabbix-agent.service
}
 
echo $(color "green" "Please input TimeZone(For example:Asia/Kolkata,America/Toronto,Europe/London：)") 
read TimeZone
echo $(color "blue" "The TimeZone you entered is：$TimeZone")
#read -e -p "Please input TimeZone(For example:Asia/Kolkata,America/Toronto,Europe/London: " TimeZone
set_service
install_package
init_system
set_kernel_parameter
set_system_limit
echo $(color "green" "Please input Zabbix_Server IP Address：") 
read Zabbix_Server_Address
echo $(color "blue" "The Zabbix_Server_IP you entered is：$Zabbix_Server_Address")
#read -e -p "Please input Zabbix_Server IP Address: " Zabbix_Server_Address
agent_install
