3.动作(active):主要用于处理错误进行远程操作和报警。
二 zabbix部署及监控实例
1.zabbix服务端部署
#安装httpd
yum install httpd -y
systemctl enable httpd
systemctl start httpd
#开启防火墙80端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
#安装mariadb数据库(mysql开源版)
yum install mariadb mariadb-server -y
systemctl enable mariadb.service
systemctl start mariadb.service
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
#初始化数据库(输入密码后选项全选yes)
mysql_secure_installation
#安装php
- yum install php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash -y
#重启
mariadb,httpdsystemctl restart mariadb.service
systemctl restart httpd
#编辑text.php文件测试网页
#登录数据库
mysql -u root -p123456
#创建zabbix数据库使用中文编码
create database zabbix character set utf8;
#赋予zbuser关于zabbix全部权限,并设置zbuser密码(自动创建用户)
grant ALL on zabbix.* to zbuser@localhost identified by 'zbpass';
flush privileges;
exit
#创建zabbix用户(不可登录)
useradd zabbix -M -s /bin/false
cd /usr/local/src/
#下载zabbix源码包 (包中可设置安装服务(监控端)或客户端(被监控端))
wget https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/3.2.4/zabbix-3.2.4.tar.gz
tar -zxvf zabbix-3.2.4.tar.gz
#zabbix数据库导入 注意:顺序不可倒,会报错
mysql -uroot -p123456 zabbixusr/local/src/zabbix-3.2.4/database/mysql/schema.sql
mysql -uroot -p123456 zabbixusr/local/src/zabbix-3.2.4/database/mysql/images.sql
mysql -uroot -p123456 zabbixusr/local/src/zabbix-3.2.4/database/mysql/data.sql
#下载编译zabbix所需的库和文件
yum install gcc make net-snmp-devel curl curl-devel mysql-devel -y
cd /usr/local/src/zabbix-3.2.4
#安装服务端,客户端,分布式监控服务 (测试主机,用zabbix对自身主机监控)(被检测的其它主机可以只安装agent)
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --enable-proxy --with-net-snmp --with-libcurl --with-mysql=/usr/bin/mysql_config
make && make install
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
ln -s /usr/local/zabbix/bin/* /usr/local/bin/
#
修改服务端数据库相关信息vim /usr/local/zabbix/etc/zabbix_server.conf
(可备份后使用 sed -ie '/^#/d;/^$/d' zabbix_server.conf去除注释和空行)
LogFile=/tmp/zabbix_server.log #日志
DBName=zabbix #数据库database
DBUser=zbuser #数据库用户
DBPassword=zbpass #登录密码
ListenIP=127.0.0.1 #数据库IP
Timeout=4
LogSlowQueries=3000
AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts #zabbix运行脚本存放目录
- #客户端配置
vim /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log #日志
Server=127.0.0.1 #被动模式,允许那台服务器连接agent
ServerActive=127.0.0.1 #主动模式,主动向哪台服务器传送数据
Hostname=Zabbix server
#注意:一个agent可同时向几个服务发送数据,分号隔开
#添加开机自启脚本
cp /usr/local/src/zabbix-3.2.4/misc/init.d/fedora/core/zabbix_server /etc/rc.d/init.d/zabbix_server
- cp /usr/local/src/zabbix-3.2.4/misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/zabbix_agentd
- chmod +x /etc/rc.d/init.d/zabbix_server
chmod +x /etc/rc.d/init.d/zabbix_agentd
chkconfig zabbix_server on
chkconfig zabbix_agentd on
vim /etc/rc.d/init.d/zabbix_server
#修改基础目录
BASEDIR=/usr/local/zabbix
vim /etc/rc.d/init.d/zabbix_agentd
BASEDIR=/usr/local/zabbix
#配置Web站点
cp -r /usr/local/src/zabbix-3.2.4/frontends/php/ /var/www/html/zabbix
chown apache.apache -R /var/www/html/zabbix/
service zabbix_server restart
service zabbix_agentd restart
- #可能会有个警告,按提示输入解决
- #配置php相关
- vim /etc/php.ini
- #修改以下参数值(不修改在网页配置安装时会有报错)
- date.timezone = /Asia/Shanghai
- post_max_size = 16M
- max_execution_time = 300
- max_input_time = 300
- systemctl restart httpd
admin
/zabbix
#开启防火墙
firewall-cmd --permanent --add-port=10051/tcp
firewall-cmd --reload
#设置selinux允许
setsebool -P httpd_can_network_connect=on
#创建zabbix用户(不可登录)
useradd zabbix -M -s /bin/false
cd /usr/local/src/
#下载zabbix源码包 (包中可设置安装服务(监控端)或客户端(被监控端))
wget https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/3.2.4/zabbix-3.2.4.tar.gz
tar -zxvf zabbix-3.2.4.tar.gz
- cd /usr/local/src/zabbix-3.2.4
./configure --prefix=/usr/local/zabbix --enable-agent
make && make install
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
ln -s /usr/local/zabbix/bin/* /usr/local/bin/
- #客户端配置
vim /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log #日志
Server=192.168.1.166 #被动模式,允许那台服务器连接agent
ListenPort=10050 #监听端口
ServerActive=192.168.1.166 #主动模式,主动向哪台服务器传送数据
Hostname=192.168.1.168
#注意:一个agent可同时向几个服务发送数据,分号隔开
#添加开机自启脚本
- cp /usr/local/src/zabbix-3.2.4/misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/zabbix_agentd
chmod +x /etc/rc.d/init.d/zabbix_agentd
chkconfig zabbix_agentd on
vim /etc/rc.d/init.d/zabbix_agentd
BASEDIR=/usr/local/zabbix
service zabbix_agentd restart
firewall-cmd --permanent --add-port=10050/tcp
firewall-cmd --reload
[root@test2 ~]# cat /proc/meminfo | awk '/^MemTotal:/{print $2}'
1001360
[root@test2 ~]# cat /proc/meminfo | awk '/^MemFree:/{print $2}'
630748
[root@test2 ~]# cat /proc/meminfo | awk '/^Buffers:/{print $2}'
948
[root@test2 ~]# cat /proc/meminfo | awk '/^Cached:/{print $2}'
111248
- vim /usr/local/zabbix/etc/zabbix_agentd.conf
- #格式:UserParameter=key,commod其中$1代表写入[*]的参数,$$2代表awk提取参数$2
UserParameter=memory.usage[*],/bin/cat /proc/meminfo | awk '/^$1:/{print $$2}'
[root@test1 ~]# zabbix_get -s 192.168.1.168 -k "memory.usage[MemTotal]"
1001360
[root@test1 ~]# zabbix_get -s 192.168.1.168 -k "memory.usage[MemFree]"
624140
[root@test1 ~]# zabbix_get -s 192.168.1.168 -k "memory.usage[Buffers]"
948
[root@test1 ~]# zabbix_get -s 192.168.1.168 -k "memory.usage[Cached]"
115868
- grant all on *.* to zabbix@'localhost' identified by '12345';
flush priveges;
[mysql]
host=localhost
user=zabbix
password=12345
socket=/var/lib/mysql/mysql.sock
[mysqladmin]
host=localhost
user=zabbix
password=12345
socket=/var/lib/mysql/mysql.sock
#将msql的key从源码包中复制至zabbix_agent配置文件
cp /usr/local/src/zabbix-3.2.4/conf/zabbix_agentd/userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/
#编辑修改HOME路径(全修改3)
sed -i 's#/var/lib/zabbix#/usr/loca/zabbix/etc#g' /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf
#加载zabbix_agentd.conf.d目录配置文件
echo "Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/" >>/usr/local/zabbix/etc/zabbix_agentd.conf
- service zabbix_agentd restart
zabbix_get -s 192.168.1.168 -p 10050 -k"mysql.status[Uptime]"
location /NginxStatus {
#启用StubStatus的工作访问状态
stub_status on;
#指定StubStaus模块的访问日志文件
access_log off;
#Nginx认证机制(需Apache的htpasswd命令生成)
#auth_basic "NginxStatus";
#用来认证的密码文件
#auth_basic_user_file ../htpasswd;
allow 127.0.0.1;
deny all;
}
[root@test2 ~]# curl 192.168.1.168:80/NginxStatus
Active connections: 1
server accepts handled requests
5 5 3
Reading: 0 Writing: 1 Waiting: 0
active connections – 活跃的连接数量
server accepts handled requests — 总共处理了n个连接 , 成功创建n握手, 总共处理了n个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
vim /usr/local/zabbix/nginx_status.sh
#!/bin/bash
host='127.0.0.1'
port='80'
status='/NginxStatus'
active() {
curl -s http://${host}:${port}${status} | awk '/^Active/{print $3}'
}
accepts() {
curl -s http://${host}:${port}${status} | awk 'NR==3{print $1}'
}
handled() {
curl -s http://${host}:${port}${status} | awk 'NR==3{print $2}'
}
requests() {
curl -s http://${host}:${port}${status} | awk 'NR==3{print $3}'
}
reading() {
curl -s http://${host}:${port}${status} | awk 'NR==4{print $2}'
}
writing() {
curl -s http://${host}:${port}${status} | awk 'NR==4{print $4}'
}
waiting() {
curl -s http://${host}:${port}${status} | awk 'NR==4{print $6}'
}
#调用上面函数
- $1
vim /usr/local/zabbix/etc/zabbix_agentd.conf
UserParameter=nginx.status[*],/usr/local/zabbix/nginx_status.sh $1
[root@test1 ~]# zabbix_get -s 192.168.1.168 -k "nginx.status[active]"
1
三 zabbix报警(邮件和微信)
#安装邮件
cd /usr/local/src
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar -xzvf sendEmail-v1.56.tar.gz
mv sendEmail-v1.56/sendEmail /usr/local/bin/
chown zabbix:zabbix /usr/local/bin/sendEmail
#发送邮件测试
/usr/local/bin/sendEmail \
-f msun1996@163.com \
-t msun1996@163.com \
-s smtp.163.com \
-u "hello" \
-o message-content-type=html \
-o message-charset=utf8 \
-xu msun1996@163.com \
-xp ****** \
-m "hello"
cd /usr/local/zabbix/share/zabbix/alertscripts/
vim mail.sh
#!/bin/bash
to=$1
subject=$2
body=$3
/usr/local/bin/sendEmail -o tls=auto -f msun1996@163.com -t "$to" -s smtp.163.com -u "$subject" -o message-content-type=html -o message-charset=utf8 -xu msun1996@163.com -xp ****** -m "$body"
chmod +x mail.sh
#邮件脚本测试
./mail.sh msun1996@163.com hello hellword
- service zabbix_agentd stop
cd /usr/local/zabbix/share/zabbix/alertscripts/
vim weixin.sh
- #!/bin/bash
- #send message from weixin for zabbix monitor
- #传递三参数 $1消息接收帐号 $2报警标题 $3报警内容
CorpID='wx118bf3a3202f9fa3' #你的corpID
Secret='WRK9WJ_sTMQlqnup3Aolp8PPz4p4Pk99K9LwG-6oSdyGbgQI9mJIWrxo0qcDnZSk' #你的secret
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $4}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body() {
local int AppID=1 #企业号应用ID
local UserID=$1 #部门成员IDzabbix中微信的接收者(可不设置,不设置则发送到部门)
local PartyID=2 #部门ID.定义范围后,组成员都可接收消息
local Msg=$(echo "$@" | cut -d" " -f3-) #过滤出zabbix中传递三参数
printf '{\n'
printf '\t"touser": "'"$UserID"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n' #消息类型(必须项)
printf '\t"agentid": "'" $AppID "\"",\n"
#企业号应用ID(必须项)printf '\t"text": {\n'
printf '\t\t"content": "'"$Msg"\""\n"
#传递的消息(必须项)printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
chown zabbix.zabbix weixin.sh
chmod +x weixin.sh
- service zabbix_agentd stop
三 zabbix Agent自动注册
一 Zabbix Server配置
学习参考博客:http://www.osyunwei.com/archives/7984.html