Zabbix自动化安装脚本

server

#!/bin/bash
# zabbix_lnmp_install.sh
# 2015/12/13


srcdir=/usr/local/src/zabbix_src_install
mysqlsrctgz=$srcdir/mysql-5.6.28.tar.gz
nginxsrctgz=$srcdir/nginx-1.9.9.tar.gz
phpsrctgz=$srcdir/php-5.6.16.tar.gz
zabbixsrctgz=$srcdir/zabbix-2.4.7.tar.gz
mysqlsrcdir=$(echo ${mysqlsrctgz##*/}|sed 's/.tar.gz//')
nginxsrcdir=$(echo ${nginxsrctgz##*/}|sed 's/.tar.gz//')
phpsrcdir=$(echo ${phpsrctgz##*/}|sed 's/.tar.gz//')
zabbixsrcdir=$(echo ${zabbixsrctgz##*/}|sed 's/.tar.gz//')

zabbix_basedir=/home/zabbix
zabbixdir=$zabbix_basedir/zabbix_server
nginxdir=$zabbix_basedir/nginx
phpdir=$zabbix_basedir/php
mysqldir=$zabbix_basedir/mysql

mysqlport=3306
mysqlrootpass=mysql
zabbixmysqluser=zabbix
zabbixmysqlhost=localhost
zabbixmysqlpass=zabbix


yum -y groupinstall "Development Tools"
yum install -y gcc-c++ gcc make cmake ncurses-devel
yum install -y libxml2 libxml2-devel openssl openssl-devel libcurl-devel \
    libpng-devel libjpeg-devel freetype-devel
yum install -y pcre pcre-devel
yum install -y net-snmp net-snmp-devel

groupadd zabbix
useradd -g zabbix -m zabbix
[ ! -d $zabbix_basedir ] && mkdir -p $zabbix_basedir
chmod 755 $zabbix_basedir
chown zabbix. $zabbix_basedir

/etc/init.d/iptables stop
grep -q  '^SELINUX=enforcing' /etc/selinux/config && {
        sed -i '/^SELINUX=enforcing/s/^.*$/SELINUX=disabled/' /etc/selinux/config
        /usr/sbin/setenforce 0
}

#### install mysql
useradd mysql -s /sbin/nologin
(
cd "$srcdir" || exit 1

tar zxf "$mysqlsrctgz"
cd "$mysqlsrcdir" || exit 1

cmake . \
-DCMAKE_INSTALL_PREFIX=$mysqldir  \
-DINSTALL_MYSQLDATADIR="$mysqldir/var"  \
-DMYSQL_DATADIR=$mysqldir/var \
-DSYSCONFDIR=$mysqldir/etc    \
-DWITH_INNOBASE_STORAGE_ENGINE=1  \
-DDEFAULT_CHARSET=utf8mb4   \
-DDEFAULT_COLLATION=utf8mb4_general_ci  \
-DMYSQL_TCP_PORT=$mysqlport  \
-DMYSQL_UNIX_ADDR=$mysqldir/tmp/mysql.sock  \
-DWITH_EXTRA_CHARSETS=all

make -j "$(grep -c processor /proc/cpuinfo)"
make install

cd "$mysqldir" || exit 1
mkdir log tmp etc var
chown -R mysql log tmp var
rm -f /etc/my.cnf   /etc/mysql/my.cnf   ~/.my.cnf
cp $srcdir/my.cnf etc/my.cnf
sed -i "s#/usr/local/mysql#$mysqldir#" etc/my.cnf
scripts/mysql_install_db --user=mysql --defaults-file=$mysqldir/etc/my.cnf \
    --basedir=$mysqldir --datadir=$mysqldir/var
cp support-files/mysql.server ./bin/
./bin/mysql.server start
)

echo "$mysqldir/lib" > /etc/ld.so.conf.d/mysql-x86_64.conf
ldconfig

$mysqldir/bin/mysqladmin password "$mysqlrootpass"
DropNullUserSql=$($mysqldir/bin/mysql -uroot -p"$mysqlrootpass" -NBe "
    select user,host from mysql.user where (user is null or user='') or (password is null or password='')" 2>/dev/null|
        grep -v 'Logging to file'|awk '{if(NF==2)print "drop user \x27"$1"\047@\047"$2"\047;";else print "drop user \x27\047@\047"$1"\047;"}')
echo "$DropNullUserSql" | $mysqldir/bin/mysql -uroot -p"$mysqlrootpass"

$mysqldir/bin/mysql -uroot -p"$mysqlrootpass" -e "
    grant all privileges on *.* to '$zabbixmysqluser'@'$zabbixmysqlhost'
    identified by '$zabbixmysqlpass'"


### install php
(
cd "$srcdir" || exit 1
tar zxf "$phpsrctgz"
cd "$phpsrcdir" || exit 1

./configure  \
--prefix=$phpdir \
--with-config-file-path=$phpdir/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=$mysqldir \
--with-pdo-mysql=$mysqldir \
--with-mysqli=$mysqldir/bin/mysql_config \
--with-gd \
--with-png-dir \
--with-jpeg-dir \
--with-libxml-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--with-iconv-dir \
--with-pear \
--with-curl \
--with-openssl \
--disable-ipv6 \
--enable-bcmath \
--enable-mbstring \
--enable-sockets

make -j "$(grep -c processor /proc/cpuinfo)" && make install

cp php.ini-production $phpdir/etc/php.ini
sed -i '/max_execution_time/s/^.*/max_execution_time = 600 /
        /date.timezone/s/^.*$/date.timezone = Asia\/Shanghai/
        /post_max_size/s/^.*$/post_max_size = 32M/
        /memory_limit/s/^.*/memory_limit = 128M/
        /mbstring.func_overload/s/^.*$/mbstring.func_overload = 2/
        /max_input_time/s/^.*/max_input_time = 600/
        /mbstring.func_overload/s/^.*/mbstring.func_overload = 0/
        /;always_populate_raw_post_data = -1/s/^.*/always_populate_raw_post_data = -1/
        /upload_max_filesize/s/^.*$/upload_max_filesize = 16M /' $phpdir/etc/php.ini

\cp sapi/fpm/init.d.php-fpm $phpdir/bin/php-fpm.server
chmod +x $phpdir/bin/php-fpm.server
cd "$phpdir"/etc || exit 1
\cp php-fpm.conf.default  php-fpm.conf
useradd php-fpm -s /sbin/nologin
$phpdir/bin/php-fpm.server start
)

### nginx install
(
cd "$srcdir" || exit 1
tar zxf "$nginxsrctgz"
cd "$nginxsrcdir" || exit 1
./configure \
--prefix=$nginxdir \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module  \
--with-pcre

make -j "$(grep -c processor /proc/cpuinfo)"
make install
)

cat >$nginxdir/conf/nginx.conf<<EOF
user nobody nobody;
worker_processes 2;
error_log logs/nginx_error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '\$remote_addr \$http_x_forwarded_for [\$time_local]'
    '\$host "\$request_uri" \$status'
    '"\$http_referer" "\$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path $nginxdir/client_body_temp;
    proxy_temp_path $nginxdir/proxy_temp;
    fastcgi_temp_path $nginxdir/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

server
{
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root $nginxdir/html;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $nginxdir/html\$fastcgi_script_name;
    }
}
}
EOF

$nginxdir/sbin/nginx

cat >$nginxdir/html/test.php<<EOF
<?php
    #echo "test";
    phpinfo();
?>
EOF

### zabbix install
! grep -q zabbix /etc/services &&
        cat >>/etc/services <<EOF
zabbix-agent    10050/tcp                       # Zabbix Agent
zabbix-agent    10050/udp                      # Zabbix Agent
zabbix-trapper  10051/tcp                       # Zabbix Trapper
zabbix-trapper  10051/udp                      # Zabbix Trapper
EOF

(
cd "$srcdir" || exit 1
tar zxf "$zabbixsrctgz"
cd "$zabbixsrcdir" || exit 1
./configure --prefix=$zabbixdir \
        --enable-server --enable-agent \
        --with-mysql=$mysqldir/bin/mysql_config \
        --with-net-snmp \
        --with-libcurl
make -j "$(grep -c processor /proc/cpuinfo)"
make install

\cp -r frontends/php $nginxdir/html/zabbix
chmod 777 $nginxdir/html/zabbix/conf

$mysqldir/bin/mysql -uroot -p$mysqlrootpass -e "create database zabbix"
$mysqldir/bin/mysql -uroot -p$mysqlrootpass zabbix <database/mysql/schema.sql
$mysqldir/bin/mysql -uroot -p$mysqlrootpass zabbix <database/mysql/images.sql
$mysqldir/bin/mysql -uroot -p$mysqlrootpass zabbix <database/mysql/data.sql

\cp misc/init.d/tru64/zabbix_server $zabbixdir/bin/zabbix_server_ctrl
\cp misc/init.d/tru64/zabbix_agentd $zabbixdir/bin/zabbix_agentd_ctrl
sed -i "s#DAEMON=/usr/local/sbin/zabbix_server#$zabbixdir/sbin/zabbix_server#" \
    $zabbixdir/bin/zabbix_server_ctrl
sed -i "s#DAEMON=/usr/local/sbin/zabbix_agentd#$zabbixdir/sbin/zabbix_agentd#" \
    $zabbixdir/bin/zabbix_agentd_ctrl
chmod +x $zabbixdir/bin/{zabbix_server_ctrl,zabbix_agentd_ctrl}

sed -i "/# DBHost=localhost/s/.*/DBHost=$zabbixmysqlhost/
    /# DBPort=3306/s/.*/DBPort=$mysqlport/
    /^DBUser=root/s/.*/DBUser=$zabbixmysqluser/
    /# DBPassword=/s/.*/DBPassword=$zabbixmysqlpass/" \
    $zabbixdir/etc/zabbix_server.conf

$zabbixdir/bin/zabbix_server_ctrl start
$zabbixdir/bin/zabbix_agentd_ctrl start
)

cat >>/etc/rc.d/rc.local<<EOF
$mysqldir/bin/mysql.server start
$phpdir/bin/php-fpm.server start
$nginxdir/sbin/nginx
$zabbixdir/bin/zabbix_server_ctrl start
$zabbixdir/bin/zabbix_agentd_ctrl start
EOF


exit 0

Client

#!/bin/bash
# zabbix_agent_install.sh
# 2015/12/13


zabbixsrctgz=/usr/local/src/zabbix-2.4.7.tar.gz
zabbixsrcdir=$(echo ${zabbixsrctgz##*/}|sed 's/.tar.gz//')
zabbixagentdir=/home/zabbix/zabbix_agent
zabbix_server=10.211.55.10
#local_ip=$(ifconfig eth0|grep 'inet addr:'|awk '{print $2}'|awk -F: '{print $2}' )
hostname=$(awk -F'=' '/^HOSTNAME/{print $2}' /etc/sysconfig/network)

grep -q  '^SELINUX=enforcing' /etc/selinux/config && {
    sed -i '/^SELINUX=enforcing/s/^.*$/SELINUX=disabled/' /etc/selinux/config
    /usr/sbin/setenforce 0
}

groupadd zabbix
useradd -g zabbix -m zabbix

! grep -q zabbix /etc/services &&
    cat >>/etc/services <<EOF
zabbix-agent    10050/tcp                      # Zabbix Agent
zabbix-agent    10050/udp                      # Zabbix Agent
EOF

cd ${zabbixsrctgz%/*}
tar zxf $zabbixsrctgz

cd $zabbixsrcdir
./configure --prefix=$zabbixagentdir --enable-agent
make -j `grep -c processor /proc/cpuinfo`
make install

\cp misc/init.d/tru64/zabbix_agentd $zabbixagentdir/bin/zabbix_agentd_ctrl
sed -i "s#DAEMON=/usr/local/sbin/zabbix_agentd#$zabbixagentdir/sbin/zabbix_agentd#" $zabbixagentdir/bin/zabbix_agentd_ctrl
chmod +x $zabbixagentdir/bin/zabbix_agentd_ctrl

grep -q zabbix_agentd_ctrl /etc/rc.d/rc.local ||
    cat >>/etc/rc.d/rc.local<<EOF
$zabbixagentdir/bin/zabbix_agentd_ctrl start
EOF

sed -i '/^Server=/s/^.*$/Server='"$zabbix_server"'/' \
    $zabbixagentdir/etc/{zabbix_agentd.conf,zabbix_agent.conf}
sed -i '/^ServerActive=/s/^.*$/ServerActive='"$zabbix_server"'/' \
    $zabbixagentdir/etc/zabbix_agentd.conf
sed -i '/^Hostname/s/.*/Hostname='$hostname'/' \
    $zabbixagentdir/etc/zabbix_agentd.conf

$zabbixagentdir/bin/zabbix_agentd_ctrl start


exit 0