zabbix server 一键安装脚本

原文链接: https://yq.aliyun.com/articles/682389

!/usr/bin/python

import os
import socket

def sto():
os.system('sed -i "s/SELINUX=enabled/SELINUX=disabled/g" /etc/selinux/config')
os.system('getenforce 0')
os.system('service iptables stop')
os.system('service firewalld stop')
os.system('yum install -y epel-release')
def yumsub():
os.system("yum -y install wget libtool expat-devel pcre-devel zlib-devel openssl openssl-devel pcre")
os.system("yum -y install gcc gcc-c++ glibc automake autoconf libtool make")
os.system("yum -y install libmcrypt-devel mhash-devel libxslt-devel")
os.system("yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel")
os.system("yum -y install zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel")
os.system("yum -y install ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel")
os.system("yum -y install krb5 krb5-devel libidn libidn-devel openssl openssl-devel")
def inspcre():
os.chdir('/data')
os.system('wget http://ftp.pcre.org/pub/pcre/pcre-8.39.tar.gz -O pcre-8.39.tar.gz --no-check-certificate')
os.system('tar -zxvf pcre-8.39.tar.gz')
os.chdir('/data/pcre-8.39')
os.system('./configure')
os.system('make&&make install')
def inszlib():
os.chdir('/data')
os.system('wget http://zlib.net/zlib-1.2.11.tar.gz')
os.system('tar -zxvf zlib-1.2.11.tar.gz')
os.chdir('/data/zlib-1.2.11')
os.system('./configure&&make&&make install')
def insopenssl():
os.chdir('/data')
os.system('wget https://www.openssl.org/source/openssl-1.1.0b.tar.gz')
os.system('tar -zxvf openssl-1.1.0b.tar.gz')
os.chdir('/data/openssl-1.1.0b')
os.system('./config&&make&&make install')
def insnginx():
os.chdir('/data')
os.system('wget http://nginx.org/download/nginx-1.10.2.tar.gz')
os.system('tar -zxvf nginx-1.10.2.tar.gz')
os.chdir('/data/nginx-1.10.2')
os.system('groupadd -r nginx')
os.system('useradd -r -g nginx nginx')
os.system("""
./configure
--prefix=/data/nginx
--sbin-path=/data/nginx/sbin/nginx
--conf-path=/data/nginx/nginx.conf
--pid-path=/data/nginx/nginx.pid
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_flv_module
--with-http_mp4_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/var/tmp/nginx/client/
--http-proxy-temp-path=/var/tmp/nginx/proxy/
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
--http-scgi-temp-path=/var/tmp/nginx/scgi
--with-pcre=/data/pcre-8.39
--with-zlib=/data/zlib-1.2.11
--with-openssl=/data/openssl-1.1.0b
""")
os.system('make && make install')
os.system('mkdir -p /var/tmp/nginx/client')
os.system('/data/nginx/sbin/nginx')
def insmysql():
os.chdir('/data')
os.system('wget http://ftp.kaist.ac.kr/mysql/Downloads/MySQL-5.6/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz')
os.system('tar -zxvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz')
os.system('mkdir -p /data/mysql/logs /data/mysql/data')
os.system('mv mysql-5.6.40-linux-glibc2.12-x86_64/* /data/mysql/')
os.system('useradd -s /sbin/nologin -M mysql')
os.system('chown -R mysql.mysql /data/mysql/')
os.system('chown -R mysql.mysql /var/lib/mysql/')
os.chdir('/data/mysql')
with open(r'/etc/my.cnf','w') as fileobject:
fileobject.write("""
[mysqld]
\nbasedir = /data/mysql
\ndatadir = /data/mysql/data
\ntmpdir = /data/mysql
\nsocket = /data/mysql/mysql.sock
\nskip-external-locking
\nskip-name-resolve
\nlower_case_table_names=1
\nauto_increment_offset = 1
\nauto_increment_increment = 2
\n#server-id
\n########## binlog ##########
\nlog_bin = /data/mysql/logs/mysql-bin
\nbinlog_format = row
\nbinlog_cache_size = 2M
\nexpire-logs-days = 7
\n########## error log ##########
\nlog_error = /data/mysql/logs/mysql-error.log
\n########## slow log ##########
\nslow_query_log = 1
\nslow_query_log_file = /data/mysql/logs/mysql-slow.log
\nlong_query_time = 5
\n########## per_thread_buffers ##########
\nmax_connections = 1024
\nmax_connect_errors = 1000
\nkey_buffer_size = 64M
\nmax_allowed_packet = 128M
\ntable_open_cache = 6144
\ntable_definition_cache = 4096
\nsort_buffer_size = 512K
\nread_buffer_size = 512K
\njoin_buffer_size = 512K
\ntmp_table_size = 64M
\nmax_heap_table_size = 64M
\nthread_cache_size = 64
\nthread_concurrency = 32
\nbulk_insert_buffer_size = 64M
\n########innodb########
\ninnodb_buffer_pool_size = 1G
\ninnodb_log_file_size = 500M
\ninnodb_log_buffer_size = 64M
\ninnodb_flush_log_at_trx_commit = 2
\ninnodb_file_per_table = 1
\ninnodb_file_io_threads = 4
\ninnodb_flush_method = O_DIRECT
\ninnodb_thread_concurrency = 0
\ninnodb_additional_mem_pool_size = 16M
\n[mysqlhotcopy]
\ninteractive-timeout
\n[mysqld_safe]
\nopen_files_limit = 65535
\n[client]
\nsocket = /data/mysql/mysql.sock
\n[mysql]
\nsocket = /data/mysql/mysql.sock
""")
os.chdir('/data/mysql')
os.system('./scripts/mysql_install_db --basedir=/data/mysql/ --datadir=/data/mysql/data/ --user=mysql')
os.system('sed -i "s#/data/mysql#/data/mysql#g" /data/mysql/bin/mysqld_safe')
os.system('/data/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql &')
os.system('cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld')
os.system('/data/mysql/bin/mysqladmin -h 127.0.0.1 -u root password mysqldb')
def insphp():
os.chdir('/data')
os.system('yum -y install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel gd-devel curl-devel openssl-devel libxslt-devel* php-mcrypt libmcrypt libmcrypt-devel mhash mhash-devel libevent libevent-devel libxml2 libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel')
os.system('wget http://au1.php.net/get/php-5.6.30.tar.gz/from/this/mirror -O php-5.6.30.tar.gz')
os.system('tar zxvf php-5.6.30.tar.gz')
os.chdir('/data/php-5.6.30')
os.system("""
./configure
--prefix=/data/php
--with-config-file-path=/data/php/etc
--enable-inline-optimization
--enable-fpm
--with-mysql=/data/mysql
--with-mysqli=/data/mysql/bin/mysql_config
--with-pdo-mysql=/data/mysql
--with-gettext
--enable-mbstring
--with-iconv=/data/libiconv
--with-mcrypt
--with-mhash
--enable-bcmath
--enable-soap
--with-libxml-dir
--enable-sockets
--with-curl
--with-zlib
--enable-zip
--with-bz2
--with-gd
--with-freetype-dir
--with-jpeg-dir
--with-iconv
--with-png-dir
""")
os.system('make&&make install')
os.system('cp php.ini-production /data/php/etc/php.ini')
def inszabbix():
os.chdir('/data')
os.system('wget http://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.1/zabbix-3.2.1.tar.gz')
os.system('yum install -y net-snmp-devel')
os.system('tar zxvf zabbix-3.2.1.tar.gz')
os.chdir('/data/zabbix-3.2.1')
os.system('./configure --prefix=/data/zabbix --enable-server --enable-agent --with-mysql=/data/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2&&make&&make install')
def mysqlzabbix():
os.system('/data/mysql/bin/mysql -h127.0.0.1 -uroot -pmysqldb -e "create database zabbix"')
os.system('/data/mysql/bin/mysql -h127.0.0.1 -uroot -pmysqldb -e "grant all on zabbix.* to 'zabbixuser'@'%' identified by 'mysqldb'"')
os.system('/data/mysql/bin/mysql -h127.0.0.1 -uroot -pmysqldb -e "flush privileges"')
os.system('sed -i "1i\use zabbix;" /data/zabbix-3.2.1/database/mysql/schema.sql')
os.system('sed -i "1i\use zabbix;" /data/zabbix-3.2.1/database/mysql/images.sql')
os.system('sed -i "1i\use zabbix;" /data/zabbix-3.2.1/database/mysql/data.sql')
os.system('/data/mysql/bin/mysql -h127.0.0.1 -uroot -pmysqldb -e "source /data/zabbix-3.2.1/database/mysql/schema.sql"')
os.system('/data/mysql/bin/mysql -h127.0.0.1 -uroot -pmysqldb -e "source /data/zabbix-3.2.1/database/mysql/images.sql"')
os.system('/data/mysql/bin/mysql -h127.0.0.1 -uroot -pmysqldb -e "source /data/zabbix-3.2.1/database/mysql/data.sql"')
def zabbixconf():
os.system('echo "LogFile=/data/zabbix/logs/zabbix_server.log">>/data/zabbix/etc/zabbix_server.conf')
os.system('echo "DBHost=127.0.0.1">>/data/zabbix/etc/zabbix_server.conf')
os.system('echo "DBName=zabbix">>/data/zabbix/etc/zabbix_server.conf')
os.system('echo "DBUser=zabbixuser">>/data/zabbix/etc/zabbix_server.conf')
os.system('echo "DBPassword=mysqldb">>/data/zabbix/etc/zabbix_server.conf')
os.system('echo "LogSlowQueries=3000">>/data/zabbix/etc/zabbix_server.conf')
os.system('cp /data/zabbix-3.2.1/misc/init.d/fedora/core/* /etc/init.d/')
os.system('chmod +x /etc/init.d/zabbix_server')
os.system('chmod +x /etc/init.d/zabbix_agentd')
os.system('sed -i "s#BASEDIR=/data#BASEDIR=/data/zabbix#g" /etc/init.d/zabbix_agentd')
os.system('sed -i "s#BASEDIR=/data#BASEDIR=/data/zabbix#g" /etc/init.d/zabbix_server')
os.system('useradd -s /sbin/nologin -M zabbix')
os.system('mkdir /data/zabbix/logs')
os.system('chown -R zabbix.zabbix /data/zabbix/')
os.system('/etc/init.d/zabbix_agentd start')
os.system('/etc/init.d/zabbix_server start')
os.system('sed -i "s#;date.timezone =#date.timezone = Asia/Shanghai#g" /data/php/etc/php.ini')
os.system('sed -i "s#post_max_size = 8M#post_max_size = 16M#g" /data/php/etc/php.ini')
os.system('sed -i "s#max_execution_time = 30#max_execution_time = 300#g" /data/php/etc/php.ini')
os.system('sed -i "s#max_input_time = 60#max_input_time = 300#g" /data/php/etc/php.ini')
os.system('sed -i "s#;always_populate_raw_post_data = -1#always_populate_raw_post_data = -1#g" /data/php/etc/php.ini')
def get_host_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def nginx():
os.system('mkdir -p /var/log/nginx')
os.system('touch /var/log/nginx/error.log')
with open(r'/data/nginx/nginx.conf','w') as fileobject:
fileobject.write("""
\nuser www www;
\npid /data/nginx/nginx.pid;
\nworker_processes auto;
\nworker_rlimit_nofile 100000;
\nevents
\n {
\n use epoll;
\n multi_accept on;
\n worker_connections 51200;
\n }
\nhttp {
\nserver_tokens off;
\nsendfile on;
\ntcp_nopush on;
\ntcp_nodelay on;
\naccess_log off;
\nerror_log /var/log/nginx/error.log crit;
\nkeepalive_timeout 10;
\nclient_header_timeout 10;
\nclient_body_timeout 10;
\nreset_timedout_connection on;
\nsend_timeout 10;
\nlimit_conn_zone binary_remote_addr zone=addr:5m; \nlimit_conn addr 100; \ninclude /data/nginx/mime.types; \ndefault_type text/html; \ncharset UTF-8; \ngzip on; \ngzip_disable MSIE [1-6].(?!.*SV1)<94>; \nopen_file_cache max=100000 inactive=20s; \nopen_file_cache_valid 30s; \nopen_file_cache_min_uses 2; \nopen_file_cache_errors on; \ninclude /data/nginx/conf.d/*.conf; \nserver \n{ \n listen 9001; \n location ~ /nginx_status { \n stub_status on; \n access_log off; \n allow all; \n } \n} \nserver \n{ \n listen 80; \n server_name zabbix.com; """+"server_name "+get_host_ip()+";"+""" \n root /data/nginx/html; \n location /zabbix{ \n index index.php index.html index.htm; \n } \n location ~ \.php"> {
\n include fastcgi_params;
\n fastcgi_pass 127.0.0.1:9000;
\n fastcgi_index index.php;
\n fastcgi_param SCRIPT_FILENAME /data/nginx/htmlfastcgi_script_name; \n } \n } \n } DB;
\n
\nDB['SERVER'] = '127.0.0.1';
\nDB['DATABASE'] = 'zabbix';
\nDB['PASSWORD'] = 'mysqldb';
\n
\n// Schema name. Used for IBM DB2 and PostgreSQL.
\nZBX_SERVER = 'localhost';
\nZBX_SERVER_NAME = 'zabbixserver';
\n
\n$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
""")
os.system('chown -R zabbix.zabbix /data/nginx/html/zabbix/conf')
os.system('/data/php/sbin/php-fpm')
os.system('/data/nginx/sbin/nginx')
os.chdir('/data/nginx/sbin')
os.system('./nginx -s reload')
os.chdir('/data/nginx/html/')
os.system('wget https://www.zabbix.com/downloads/3.2.0/zabbix_agents_3.2.0.linux2_6_23.amd64.tar.gz')
os.system('wget https://www.zabbix.com/downloads/3.2.0/zabbix_agents_3.2.0.win.zip')
sto()
yumsub()
inspcre()
inszlib()
insopenssl()
insnginx()
insmysql()
insphp()
inszabbix()
mysqlzabbix()
zabbixconf()
nginx()
run()

你可能感兴趣的:(zabbix server 一键安装脚本)