开源监控系统
Nagios
Cacti
企业级Zabbix监控系统介绍
Zabbix监控的搭建理论
LNMP web环境下用户的请求流程
用户 -> Nginx -> php-fpm -> 运行php程序 -> 操作mysql
Zabbix实战环境介绍
部署顺序说明
web服务器
Nginx官网,下载软件建议到官网下载
http://nginx.org/
LNMP之Nginx服务器编译安装
yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local/src
yum install lrzsz -y
wget 'http://nginx.org/download/nginx-1.14.2.tar.gz'
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx
make && make install
手动启动和关闭Nginx
$echo PATH中没有export PATH=$PATH:/usr/local/nginx/sbin/ 所以不能直接nginx;
添加环境变量:
在vi /etc/profile在最后一行加上export PATH=$PATH:/usr/local/nginx/sbin/
输入. /etc/profile刷新生效。就可以直接nginx-t或者启动nginx
环境变量:export PATH=$PATH:/usr/local/nginx/sbin/
验证Nginx安装是否成功
使用systemctl管理Nginx,/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target
Systemctl stopnginx
Systemctl startnginx
为什么需要安装php
Php官网
http://php.net/
php编译安装脚本
yum -y install epel-release
yum -y install gccgcc-c++ make pcrepcre-develzlibzlib-developensslopenssl-devel libxml2 libxml2-devel libcurllibcurl-devellibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-developenldapopenldap-devellibmcryptlibmcrypt-devel
cd /usr/local/src/
wget 'http://hk1.php.net/distributions/php-5.6.40.tar.gz'
tar -zxf php-5.6.40.tar.gz
cd php-5.6.40
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-ctype --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fpm
make && make install
cp php.ini-production /usr/local/php/etc/php.ini
php编译安装说明
--prefix指定php的安装目录
--with-config-file-path指定php的配置文件位置
--with-mysql、--with-mysqli让php可以操作mysql
--enable-fpm主要是nginx要来调用php语言得使用php-fpm
启动php-fpm
环境变量:把export PATH=$PATH:/usr/local/php/sbin/:/usr/local/php/bin/加入/etc/profile环境变量里刷新生效(. /etc/profile)
检查配置文件:php-fpm -t
使用默认配置文件:cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
查看php-fpm的listen配置
验证php-fpm的启动:
使用systemctl管理php-fpm,/usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target
启动:systemctl start php-fpm
查看监听端口:netstat -tulnp |grep php-fpm
查看进程:ps -auxf |grep php-fpm
nginx的默认配置无法处理php程序/usr/local/nginx/html/test.php
echo "taobaozabbix";
?>
nginx+php-fpm结合的配置
vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
记得重启一下nginx(以上配置是nginx+php通过网络的方式访问)
访问测试:http://ip /test.php
Mysql
Mysql编译安装脚本
yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel
useradd -s /sbin/nologin mysql
cd /usr/local/src
wget 'https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39.tar.gz'
tar -zxvf mysql-5.6.39.tar.gz
cd mysql-5.6.39
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1
make && make install
cp support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
编译选项说明
mysql环境变量配置
环境变量:把export PATH=$PATH:/usr/local/mysql/bin/加入/etc/profile环境变量里刷新生效(. /etc/profile)
mysql的启动步骤
更新mysql配置/etc/my.cnf
[mysqld]
bind-address=0.0.0.0//监听地址
port=3306
datadir=/data/mysql//数据目录
user=mysql启动用户
skip-name-resolve
long_query_time=2
slow_query_log_file=/data/mysql/mysql-slow.log
expire_logs_days=2
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
max_allowed_packet = 512M
connect_timeout = 60
net_read_timeout = 120
[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
mysql数据库初始化
mkdir -pv /data/mysql
chown -R mysql:mysql /usr/local/mysql /data/mysql/
yum install -y perl-Module-Install
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/
使用systemctl管理mysqld,/usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
[Install]
WantedBy=multi-user.target
启动mysql服务器 :systemctl start mysqld
验证Mysql是否启动
查看进程:ps -aux |grep mysqld
查看监听:netstat -anpt |grep mysqld
查看日志cat/data/mysql/mysqld.log
mysql的安全方式
给mysql加密码
1. mysqladmin -h 127.0.0.1 -u root password 'zabbixpwd'
2. mysql -h 127.0.0.1 -uroot -pzabbixpwd -A
mysql授权某ip登录
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.237.%' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTION; //给192.168.237.0网段所有表和数据库的权限*.*(整个mysql库)ALL PRIVILEGES(所有权限)
(注意服务所在的服务器ip)
flush privileges; //刷新生效!!!!
网络登录mysql
mysql -h 192.168.1.117 -uroot -p密码 -A
LNMP架构--用户访问顺序
LNMP的环境验收,vi /usr/local/nginx/html/test_mysql.php
$link=mysql_connect("127.0.0.1","root","123456");
if(!$link){
echo "mysql_connect fail";
}else{
echo "mysql_connect success";
}
?>
验证LNMP环境
访问http://192.168.237.49/test_mysql.php
zabbix的部署原理
zabbix的官网
https://www.zabbix.com/
Zabbix Server编译安装
yum install -y libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel
useradd -s /sbin/nologin zabbix
cd /usr/local/src/
wget 'https://nchc.dl.sourceforge.net/project/zabbix/ZABBI X%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz'
tar -zxvf zabbix-4.0.3.tar.gz
cd zabbix-4.0.3
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql=/usr/local/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2
make && make install
选项说明
zabbix server的安装验证
环境变量设置:
在vi /etc/profile 在最后一行加上
export PATH=$PATH:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/;
查看版本:zabbix_server --version
输入. /etc/profile刷新;
zabbix server的启动
Mysql数据库初始化mysql -h 127.0.0.1 -uroot -pzabbixpwd -A
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'zabbixpwd'; //这里的密码是mysql数据密码!!
flush privileges;
set names utf8;
use zabbix;
source /usr/local/src/zabbix-4.0.3/database/mysql/schema.sql;
source /usr/local/src/zabbix-4.0.3/database/mysql/data.sql;
source /usr/local/src/zabbix-4.0.3/database/mysql/images.sql;
zabbix server的配置/usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/zabbix_server.log
DBHost=127.0.0.1 //链接数据库的ip
DBName=zabbix //数据库名字
DBUser=zabbix
DBPassword=zabbixpwd
DBPort=3306
Timeout=30
AlertScriptsPath=/usr/local/zabbix/alertscripts
ExternalScripts=/usr/local/zabbix/externalscripts
LogSlowQueries=3000
启动zabbix server
chown zabbix:zabbix -R /usr/local/zabbix/
zabbix_server
验证
Zabbix Web的安装
mkdir /usr/local/nginx/html/zabbix
cp -a /usr/local/src/zabbix-4.0.3/frontends/php/* /usr/local/nginx/html/zabbix/
访问zabbix web,需要做一些初始化配置
http://192.168.237.49/zabbix/
zabbix web需要php做配置优化,修改php配置文件:/usr/local/php/etc/php.ini
post_max_size = 32M
max_execution_time = 350
max_input_time = 350
date.timezone = Asia/Shanghai
always_populate_raw_post_data = -1
重启php生效:systemctl restart php-fpm
访问http://192.168.1.117/zabbix/setup.php
然后刷新访问:http://192.168.1.117/zabbix/
登录zabbix
默认用户名和密码:Admin zabbix (Admin的A必须是大写)
禁用Zabbix server主机的监控
禁用guest用户(不安全)
更改Admin的密码,改成自己的密码
zabbix网页默认使用英文
zabbix默认的字段问题
解决zabbix部分乱码
网页的字体配置修改vi /usr/local/nginx/html//include/defines.inc.php 中将DejaVuSans字体,改成msyh微软雅黑字体(可能两处需要修改)
(注:可以把目录下默认的文件备份然后把下载的字体文件名字改为默认(DejaVuSans.ttf默认名字)