编译lamp安装方式:lamp
初始配置
systemctl disable firewalld
systemctl stop firewalld
sed -i 's/SELINUX=enforcing$/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
设置yum源
yum install epel-release -y 设置epel源为清华源
sed -e 's!^metalink=!#metalink=!g' \
-e 's!^#baseurl=!baseurl=!g' \
-e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
-e 's!//download\.example/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
-e 's!http://mirrors!https://mirrors!g' \
-i /etc/yum.repos.d/epel*.repo
设置mysql源
vi /etc/yum.repos.d/mysql.repo
[mysql]
name=mysql5.6
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/
gpgcheck=0
yum -y install httpd php php-mysqlnd php-gd mysql-community-server
官网源教程:zabbix源
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
yum install zabbix-server-mysql zabbix-agent zabbix-web-mysql zabbix-get -y
systemctl start mysqld
systemctl enable mysqld
mysql 进入SQL命令行
create database zabbix character set utf8 collate utf8_bin;
create user zabbix@localhost identified by 'zabbix1234';
create user zabbix@'%' identified by 'zabbix1234';
grant all privileges on zabbix.* to zabbix@localhost;
grant all privileges on zabbix.* to zabbix@'%';
quit;
mysql -uzabbix -p'zabbix1234' 测试账户是否创建成功
用zabbix自带的SQL文件导入到数据库
zcat /usr/share/doc/zabbix-server-mysql-4.0.37/create.sql.gz |mysql -uzabbix -p'zabbix1234' zabbix
vi /etc/zabbix/zabbix_server.conf
DBHost=192.168.116.130 #设置数据库服务器IP
DBName=zabbix #设置数据库名字
DBUser=zabbix #设置数据库账户zabbix
DBPassword=zabbix1234 #设置zabbix密码
DBPort=3306 #设置数据库端口
配置网页
vi /etc/httpd/conf.d/zabbix.conf
<IfModule mod_php5.c>
php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value max_input_vars 10000
php_value always_populate_raw_post_data -1
php_value date.timezone Asia/Shanghai #添加这句,设置时区
# php_value date.timezone Europe/Riga
</IfModule>
systemctl start zabbix-server zabbix-agent httpd
systemctl enable zabbix-server zabbix-agent httpd
访问网页 http://192.168.116.130/zabbix/setup.php
输入服务器的信息
192.168.116.130 数据库服务器IP
3306 数据库服务器端口
zabbix 数据库名
zabbix 数据库账户
zabbix1234 数据库zabbix账户密码
http://192.168.116.130/zabbix/index.php
默认账户Admin,密码zabbix
虽然zabbix显示修改为中文格式,但是图形会是乱码
先在网站下载字体文件,后缀名是ttf格式的就行:字体网站
然后在zabbix服务器上再配置
cd /usr/share/zabbix/assets/fonts 将下载好的字体放在这个目录下
mv SourceHanSansCN-Normal.ttf syfont.ttf 重命名,用小写,不要大写
chown zabbix.zabbix syfont.ttf 修改权限
vi /usr/share/zabbix/include/defines.inc.php 修改字体配置文件
把这2行的graphfont改成syfont
define('ZBX_GRAPH_FONT_NAME', 'syfont'); // font file name
define('ZBX_FONT_NAME', 'syfont');
systemctl restart zabbix-server 重启服务,确认图形没有乱码
在被监控服务器上安装zabbix客户端和nginx
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
yum install zabbix-agent -y
设置yum源
cd /etc/yum.repos.d/
yum install -y wget
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
mv CentOS-Base.repo CentOS-Base.repo.bak
yum install -y nginx
yum install -y httpd-tools 安装账户认证工具
cd /etc/nginx/conf.d/ 进入账户目录
创建登录账户nginx,密码zabbix-nginx
htpasswd -bc /etc/nginx/conf.d/.nginxpasswd nginx zabbix-nginx
chown nginx.nginx .nginxpasswd
chown 600 .nginxpasswd
配置nginx状态页
vi /etc/nginx/conf.d/nginx_status.conf
server {
listen 80;
server_name 192.168.116.132;
location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /etc/nginx/conf.d/.nginxpasswd;
allow 127.0.0.1;
allow 192.168.116.0/24;
deny all;
}
}
设置主页
echo nginx-192.168.116.132 >/usr/share/nginx/html/index.html
systemctl start nginx
systemctl enable nginx
测试访问
curl 192.168.116.132
curl -u nginx:zabbix-nginx http://192.168.116.132/nginx_status/
vi /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1
UnsafeUserParameters=1
Server=192.168.116.130 #zabbix监控服务器的IP
ListenPort=10050
ListenIP=0.0.0.0 #允许访问被监控服务器的地址段
StartAgents=5 #线程数,如果有大量监控项可以设大
Hostname=192.168.116.132 #设置本机IP
Timeout=10
systemctl start zabbix-agent
ss -tnl #默认被动模式,确认10050端口打开
systemctl enable zabbix-agent
cd /etc/zabbix/zabbix_agentd.d/
vi nginx_status.sh 创建脚本
#!/bin/bash
nginx_status_fun(){
NGINX_PORT=$1
NGINX_COMMAND=$2
nginx_active(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
nginx_reading(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
nginx_writing(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
nginx_waiting(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
nginx_accepts(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
nginx_handled(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
nginx_requests(){
/usr/bin/curl -u nginx:zabbix-nginx "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
esac
}
main(){
case $1 in
nginx_status)
nginx_status_fun $2 $3;
;;
*)
echo $"Usage: $0 {nginx_status key}"
esac
}
main $1 $2 $3
chmod u+x nginx_status.sh
创建zabbix监控项配置文件
vi /etc/zabbix/zabbix_agentd.d/zabbix_nginx_status.conf
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3"
chown zabbix.zabbix nginx_status.sh
chown zabbix.zabbix zabbix_nginx_status.conf
systemctl restart zabbix-agent
5.1创建自定义监控nginx模板
5.2 为监控模板添加监控项目
先在zabbix服务器的命令行测试能否获取到被监控服务器nginx状态页的值
zabbix_get -s 192.168.116.132 -p 10050 -k "nginx_status["nginx_status","80","active"]"
在模板界面选择监控项
创建监控项
nginx_active 设置监控名字
nginx_status[“nginx_status”,“80”,“active”] 这里就是-k后面的语句
以此类推,分别添加nginx_active,nginx_reading,nginx_writing,nginx_waiting,nginx_accepts,nginx_handled,nginx_requests 这些监控项
5.3 创建图形,选择nginx-80自定义模板,将监控项的值生成图形
nginx-status(active,reading,writing,waiting) 输入图形名字,添加监控项
nginx-status(accepts,handled,requests) 输入图形名字,添加监控项
5.4 创建聚合图形
nginx-status-juhe
选择构造函数,将上面2个图形添加进去
点击更改
5.5 添加被监控主机
zabbix-client-192.168.116.132
选择zabbix-servers默认群组
192.168.116.132 输入被监控端IP和端口
选择第2列,添加做好的自定义模板,搜索后点击添加
返回第一列主机界面,添加主机
选择监测,选择主机群组,选择主机,点击应用
点击这里查看聚合图形
被监控服务器配置:
安装zabbix客户端和mysql
设置yum源
yum install epel-release -y 设置epel源为清华源
sed -e 's!^metalink=!#metalink=!g' \
-e 's!^#baseurl=!baseurl=!g' \
-e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
-e 's!//download\.example/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
-e 's!http://mirrors!https://mirrors!g' \
-i /etc/yum.repos.d/epel*.repo
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
yum install zabbix-agent -y
编译mysql链接:编译mysql
设置mysql源
vi /etc/yum.repos.d/mysql.repo
[mysql]
name=mysql5.6
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/
gpgcheck=0
yum -y install mysql-community-server 安装mysql
systemctl start mysqld
systemctl enable mysqld
mysql 连接SQL命令行
创建test账户用来连接mysql
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('Test12#$'); #设置root密码
create database test character set utf8 collate utf8_bin;
create user test@localhost identified by 'Test12#$';
create user test@'%' identified by 'Test12#$';
grant all privileges on test.* to test@localhost;
grant all privileges on test.* to test@'%';
quit;
mysql -utest -p'Test12#$' 测试连接
quit
vi /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1
UnsafeUserParameters=1
Server=192.168.116.130 #zabbix监控服务器的IP
ListenPort=10050
ListenIP=0.0.0.0 #允许访问被监控服务器的地址段
StartAgents=5 #线程数,如果有大量监控项可以设大
Hostname=192.168.116.132 #设置本机IP
Timeout=10
systemctl start zabbix-agent
systemctl enable zabbix-agent
ss -tnl #默认被动模式,确认10050端口打开
cd /etc/zabbix/zabbix_agentd.d/
vi zabbix_mysql_connections.sh
#!/bin/bash
mysql_conn(){
PORT=$1
mysql_conn_num=$(/usr/bin/mysql -utest -p'Test12#$' -P"$PORT" -e "show status like 'Threads_connected';" 2>/dev/null | awk NR==2'{print $2}')
echo $mysql_conn_num |awk '{print int($0)}'
}
main(){
case $1 in
mysql_conn)
mysql_conn $2;
;;
esac
}
main $1 $2
vi zabbix_mysql_connections.conf
UserParameter=mysql_conn[*],/etc/zabbix/zabbix_agentd.d/zabbix_mysql_connections.sh "$1" "$2"
chown zabbix.zabbix zabbix_mysql_connections.conf
chown zabbix.zabbix zabbix_mysql_connections.sh
chmod u+x zabbix_mysql_connections.sh
systemctl restart zabbix-agent
visudo 修改权限,让zabbix账户有权限执行命令
root ALL=(ALL) ALL
zabbix ALL = NOPASSWD: ALL #添加这行
在zabbix监控服务器上测试监控项脚本是否成功能获取到值
zabbix_get -s 192.168.116.132 -p 10050 -k "mysql_conn["mysql_conn","3306"]"
http://192.168.116.130/zabbix/hosts.php?ddreset=1 访问网站
6.2 选择添加监控项
mysql_conn_status 设置监控项名称
mysql_conn[“mysql_conn”,“3306”] 设置键值对
6.3 添加触发器
mysql_conn_status_full 设置名称,选择添加表达式
配置表达式,当mysql的连接次数等于3个时,触发器开始工作
6.4 创建动作,当触发器触发时,重启被监控端的mysql
mysql_conn_full_restart 设置动作名,选择触发器后添加
输入自定义脚本
sudo systemctl restart mysqld && echo $(/usr/bin/date +%F-%T) "mysql is restart">>/tmp/zabbix_mysql_restart.log
在被监控主机修改mysql的最大连接数为3
mysql -uroot -p'Test12#$'
show variables like 'max_connections'; #查看默认连接数
set global max_connections=3; #临时修改连接数,重启失效
show variables like 'max_connections'; #查看修改成功
show status like 'Threads_connected'; #查看当前mysql连接数为1
在zabbix网页上查看最新获取的数据是1
开多几个终端连接被监控端的mysql
mysql -utest -p'Test12#$' -h192.168.116.132 可以看到当连接数量到第4个时,弹出报错
ERROR 1040 (08004): Too many connections
再次在zabbix查看最新数据为3
在zabbix的仪表盘可以看到动作被触发
在zabbix网页上可以查看动作日志
在被监控服务器查看生成的重启日志信息
cat /tmp/zabbix_mysql_restart.log
输入应用名字
微信告警
在应用这里查看微信告警,保存这2个值
AgentId 1000002
Secret 这个需要手机下载企业微信才能查看
yum install -y python-pip 安装python
pip install requests
cd /usr/lib/zabbix/alertscripts/ 创建发送消息脚本
vi zabbix_weixin.py
#!/usr/bin/python2.7
#_*_coding:utf-8 _*_
import requests,sys,json
import urllib3
urllib3.disable_warnings()
reload(sys)
sys.setdefaultencoding('utf-8')
def GetTokenFromServer(Corpid,Secret):
Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
Data = {
"corpid":Corpid,
"corpsecret":Secret
}
r = requests.get(url=Url,params=Data,verify=False)
print(r.json())
if r.json()['errcode'] != 0:
return False
else:
Token = r.json()['access_token']
file = open('/tmp/zabbix_wechat_config.json', 'w')
file.write(r.text)
file.close()
return Token
def SendMessage(User,Agentid,Subject,Content):
try:
file = open('/tmp/zabbix_wechat_config.json', 'r')
Token = json.load(file)['access_token']
file.close()
except:
Token = GetTokenFromServer(Corpid, Secret)
n = 0
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
Data = {
"touser": "xxx", # 发送消息的账户名,也就是自己的账户名字
"toparty": 1, # 企业号中的部门id
"msgtype": "text", # 消息类型
"agentid": "1000002", # 企业号中的应用id,也就是AgentId
"text": {
"content": Subject + '\n' + Content
},
"safe": "0"
}
r = requests.post(url=Url,data=json.dumps(Data, ensure_ascii=False),verify=False)
while r.json()['errcode'] != 0 and n < 4:
n+=1
Token = GetTokenFromServer(Corpid, Secret)
if Token:
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
r = requests.post(url=Url,data=json.dumps(Data, ensure_ascii=False),verify=False)
print(r.json())
return r.json()
if __name__ == '__main__':
User = sys.argv[1] # zabbix传过来的第一个参数
Subject = str(sys.argv[2]) # zabbix传过来的第二个参数
Content = str(sys.argv[3]) # zabbix传过来的第三个参数
Corpid = "xxx" # 输入企业ID
Secret = "xxx" # 输入Secret
Agentid = "1000002" # 应用ID
Partyid = "1" # 部门ID
Status = SendMessage(User,Agentid,Subject,Content)
print Status
chown zabbix.zabbix zabbix_weixin.py
chmod u+x zabbix_weixin.py
测试脚本是否能运行
python zabbix_weixin.py "weixin_test" "test" "zheshitest"
"weixin_test" 账户名字
"test" 主题
"zheshitest" 内容
weixin_test 工具名字
{ALERT.SENDTO} 传递的参数
{ALERT.SUBJECT}
{ALERT.MESSAGE}
填写账户信息
选择第2列,选择媒介工具weixin_test
选择第3列,设置管理员权限
上面如果不想设置为超级管理员需要先创建管理组
将用户添加到组
重点这里选择需要监控的主机组,添加被监控主机也要创建在这个主机组中,要不然到时候动作触发不了
被监控服务器配置:
7.1 安装zabbix源
设置yum源
yum install epel-release -y 设置epel源为清华源
sed -e 's!^metalink=!#metalink=!g' \
-e 's!^#baseurl=!baseurl=!g' \
-e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
-e 's!//download\.example/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
-e 's!http://mirrors!https://mirrors!g' \
-i /etc/yum.repos.d/epel*.repo
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
yum install zabbix-agent -y
7.2 配置被监控端
vi /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1
UnsafeUserParameters=1
Server=192.168.116.130 #zabbix监控服务器的IP
ListenPort=10050
ListenIP=0.0.0.0 #允许访问被监控服务器的地址段
StartAgents=5 #线程数,如果有大量监控项可以设大
Hostname=192.168.116.132 #设置本机IP
Timeout=10
systemctl start zabbix-agent
systemctl enable zabbix-agent
ss -tnl #默认被动模式,确认10050端口打开
7.3 安装httpd,实现80端口监控
yum install -y httpd
echo "web-192.168.116.132" >/var/www/html/index.html
systemctl start httpd
systemctl enable httpd
curl 192.168.116.132
7.4 在zabbix监控服务器的网页上添加被监控主机
htttpd_80 输入监控项名字
net.tcp.listen[80] 设置监控的端口信息
httpd_status_down
{zabbix-client-192.168.116.132:net.tcp.port[<192.168.116.132>,80].last(#1,3)}=0
7.7 添加动作
htttpd_80_weixingaojing 设置名字,选择触发器
选择第2列操作,添加微信告警
在zabbix网页上先确认被监控服务器最新值为1,证明80端口打开
在被监控服务器上查看80端口打开
ss -tnl
systemctl stop httpd
ss -tnl
在zabbix网页上查看最新数据为0,证明端口关闭了
在仪表盘查看到消息已经发送
创建分发公钥脚本
vi zabbix-agent.sh
#!/bin/bash
#设置需要分发的IP列表
IPLIST="
192.168.116.132
192.168.116.133"
#安装工具
rpm -q sshpass &> /dev/null || yum -y install sshpass
#生成秘钥
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
#设置密码
export SSHPASS=123456
#遍历IP,分发秘钥
for IP in $IPLIST;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done
bash zabbix-agent.sh 分发公钥
ssh [email protected] 测试是否分发成功
ssh [email protected]
vi /etc/ansible/hosts
[zabbix]
192.168.116.132
192.168.116.133
ansible all -m ping 测试是否能通信
mkdir /ansible-playbook-zabbix 创建ansible-playbook目录
cd /ansible-playbook-zabbix/
vi zabbix-agent.yml 创建ansible-playbook文件
---
- hosts: zabbix
remote_user: root
vars:
pkgs: zabbix-agent
zabbix_server_ip: 192.168.116.130
mysql_ip: 192.168.116.130
tasks:
- name: install_zabbix.repo
shell: rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
- name: install zabbix-agent
yum: name={{ pkgs }} state=latest
- name: change_zabbix.agent.conf
shell: sed -ri -e "/# EnableRemoteCommands=0/a EnableRemoteCommands=1" -e "/# UnsafeUserParameters=0/a UnsafeUserParameters=1" \
-e "/Server=127.0.0.1/c Server={{ zabbix_server_ip }}" -e "/# ListenPort=10050/a ListenPort=10050" -e "/# ListenIP=0.0.0.0/a ListenIP=0.0.0.0" \
-e "/# StartAgents=3/a StartAgents=5" -e "/Hostname=Zabbix server/c Hostname={{ ansible_facts["eth0"]["ipv4"]["address"]}}" -e "/# Timeout=3/a Timeout=5" \
/etc/zabbix/zabbix_agentd.conf
- name: start_zabbix-agent
service: name={{ pkgs }} state=started enabled=yes
ansible-playbook zabbix-agent.yml 开始安装
在192.168.116.132查看状态,确认安装成功
systemctl status zabbix-agent
在zabbix监控服务器上添加监控主机192.168.116.132
http://192.168.116.130/zabbix/hosts.php?ddreset=1
zabbix-agent-192.168.116.132 设置名字
选择第2列模板添加默认模板,测试能否获取到数据
在最新数据选择查看被监控的主机,确认数据生成