首页 > 代码库 > Zabbix 监控服务
Zabbix 监控服务
一、搭建Zabbix监控服务器
1、部署服务运行环境(LAMP)
rpm -q httpd mysql-server php
rpm -q mysql php-mysql
service httpd start ;chkconfig httpd on
service mysqld start ;chkconfig mysqld on
[root@p34 ~]# vim /var/www/html/db.php
<?php
$db=mysql_connect("localhost","root","");
if($db){
echo "ok";
}else{
echo "no";
}
?>
[root@p34 ~]# service httpd restart
[root@p34 ~]#service mysqld start
[root@P33 ~]# firefox http://172.40.55.34/db.php (测试ok)
ok
[root@p34 ~]# service mysqld stop
[root@P33 ~]# firefox http://172.40.55.34/db.php (测试成功)
no
[root@p34 ~]#service mysqld start
2.安装zabbix
安装准备
rpm -q gcc gcc-c++
useradd zabbix
# mysql
mysql> create database zabbixdb;
mysql> grant all on zabbixdb.* to jing@"localhost" identified by "123456";
mysql> quit
[root@p34 ~]# mysql -hlocalhost -ujing -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
| zabbixdb |
+--------------------+
mysql> quit
3.装包:
解包 进源码目录 配置 编译 安装 查看安装目录
【zabbix_server 服务端运行的服务
zabbix_agentd 客户端运行的服务(本监控的主机运行的服务)】
[root@p34 ~]# unzip zabbix.zip
[root@p34 ~]# cd zabbix
[root@p34 zabbix]# ls
php-bcmath-5.3.3-22.el6.x86_64.rpm zabbix-2.2.1.tar.gz
php-mbstring-5.3.3-22.el6.x86_64.rpm
# tar -zxvf zabbix-2.2.1.tar.gz
# cd zabbix-2.2.1
[root@p34 zabbix-2.2.1]# ./configure --help
[root@p34 zabbix-2.2.1]# ./configure --prefix=/usr/lcoal/zabbix --enable-server --enable-agent --with-mysql=/usr/bin/mysql_config (只要说哪个库文件没有,就是哪个包后面带devel包没装)
[root@p34 zabbix-2.2.1]# rpm -q mysql-devel
package mysql-devel is not installed
[root@p34 zabbix-2.2.1]# yum -y install mysql-devel
[root@p34 zabbix-2.2.1]# ./configure --prefix=/usr/lcoal/zabbix --enable-server --enable-agent --with-mysql=/usr/bin/mysql_config
[root@p34 zabbix-2.2.1]# make install
[root@p34 zabbix-2.2.1]# ls /usr/lcoal/zabbix/
bin etc lib sbin share
[root@p34 zabbix-2.2.1]# cd frontends/
[root@p34 frontends]# ls
php
[root@p34 frontends]# cd php
[root@p34 php]# ls
acknow.php index.php
actionconf.php items.php
adm.gui.php
.............................
访问web页面做初始化配值
拷贝网页文件到网站目录下
创建存储配置信息的表
[root@P33 ~]# firefox http://172.40.55.34/zabbix
[root@p34 frontends]# cp -r php /var/www/html/zabbix/
[root@p34 frontends]# ls /var/www/html/
db.php zabbix
[root@p34 frontends]# ls /var/www/html/zabbix/ -ld
drwxr-xr-x. 11 root root 4096 3月 13 02:02 /var/www/html/zabbix/
[root@p34 frontends]# chown -R apache:apache /var/www/html/zabbix
[root@p34 frontends]# ls /var/www/html/zabbix/ -ld
drwxr-xr-x. 11 apache apache 4096 3月 13 02:02 /var/www/html/zabbix/
[root@p34 zabbix-2.2.1]# cd database/
[root@p34 database]# ls
ibm_db2 Makefile Makefile.am Makefile.in mysql oracle postgresql sqlite3
[root@p34 database]# cd mysql/
[root@p34 mysql]# ls (从右往前恢复)
data.sql images.sql schema.sql
[root@p34 mysql]# mysql -ujing -p123456 zabbixdb < schema.sql
[root@p34 mysql]# mysql -ujing -p123456 zabbixdb < images.sql
[root@p34 mysql]# mysql -ujing -p123456 zabbixdb <data.sql
[root@p34 mysql]# mysql -ujing -p123456 zabbixdb
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
| zabbixdb |
+--------------------+
3 rows in set (0.00 sec)
mysql> use zabbixdb;
Database changed
。。。。
mysql> desc users;
+----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+-------+
| userid | bigint(20) unsigned | NO | PRI | NULL | |
| alias | varchar(100) | NO | MUL | | |
| name | varchar(100) | NO | | | |
| surname | varchar(100) | NO | | |
mysql> select userid,alias,name,passwd from users\G;
*************************** 1. row ***************************
userid: 1
alias: Admin
name: Zabbix
passwd: 5fce1b3e34b520afeffb37ce08c7cd66
*************************** 2. row ***************************
userid: 2
alias: guest
name:
passwd: d41d8cd98f00b204e9800998ecf8427e
2 rows in set (0.00 sec)
ERROR:
No query specified
客户端访问监控页面 做初始化设置
[root@P33 ~]# firefox http://172.40.55.34/zabbix
打开zabbix页面点击下一步,会有好多彪红,需要做下面对应的配置,就ok
[root@p34 mysql]# vim /etc/php.ini
729 post_max_size = 16M
440 max_execution_time = 300
449 max_input_time = 300
946 date.timezone = Asia/Chongqing
[root@p34 mysql]# sed -n ‘440p;449p;729p;946p‘ /etc/php.ini
max_execution_time = 300
max_input_time = 300
post_max_size = 16M
date.timezone = Asia/Chongqing
[root@p34 mysql]# cd
[root@p34 ~]# ls
anaconda-ks.cfg install.log YUM.SH zabbix.zip
Desktop install.log.syslog zabbix
[root@p34 ~]# cd zabbix
[root@p34 zabbix]# ls
php-bcmath-5.3.3-22.el6.x86_64.rpm zabbix-2.2.1
php-mbstring-5.3.3-22.el6.x86_64.rpm zabbix-2.2.1.tar.gz
[root@p34 zabbix]# rpm -ivh --nodeps php-bcmath-5.3.3-22.el6.x86_64.rpm
warning: php-bcmath-5.3.3-22.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 41a40948: NOKEY
Preparing... ########################################### [100%]
1:php-bcmath ########################################### [100%]
[root@p34 zabbix]# rpm -ivh --nodeps php-mbstring-5.3.3-22.el6.x86_64.rpm
warning: php-mbstring-5.3.3-22.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 192a7d7d: NOKEY
Preparing... ########################################### [100%]
1:php-mbstring ########################################### [100%]
[root@p34 zabbix]# service httpd restart
停止 httpd: [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for p34
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
[确定]
[root@p34 zabbix]# yum list |grep php-gd
php-gd.x86_64 5.3.3-40.el6_6 abc
[root@p34 zabbix]# rpm -q php-gd
package php-gd is not installed
[root@p34 zabbix]# yum -y install php-gd
[root@p34 zabbix]# yum -y install php-xml
[root@p34 zabbix]# service httpd restart
[root@P33 ~]# firefox http://172.40.55.34/zabbix (刷新一下)
——————————————————————————————————————————
——————————————————————————————————————————
如果在监控页面上写错了,可以在这个文件中改一下。去一个公司想看监控的一些信息在该文件可以知道
[root@p34 zabbix]# cat /var/www/html/zabbix/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file
global $DB;
$DB[‘TYPE‘] = ‘MYSQL‘;
$DB[‘SERVER‘] = ‘localhost‘;
$DB[‘PORT‘] = ‘0‘;
$DB[‘DATABASE‘] = ‘zabbixdb‘;
$DB[‘USER‘] = ‘jing‘;
$DB[‘PASSWORD‘] = ‘123456‘;
// SCHEMA is relevant only for IBM_DB2 database
$DB[‘SCHEMA‘] = ‘‘;
$ZBX_SERVER = ‘172.40.55.34‘;
$ZBX_SERVER_PORT = ‘10051‘;
$ZBX_SERVER_NAME = ‘plj-zabbix‘;
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
?>
进入页面初始密码用户。
admin
zabbix
————————————————————————————————————————————
————————————————————————————————————————————
4软件的使用 (在浏览器中查看)
查看监控的哪些主机
查看监控模板
查看监控信息
5.在监控服务器启动监控服务:
创建启动脚本 并添加系统服务 开机自启
[root@p34 zabbix]# cd zabbix-2.2.1
[root@p34 zabbix-2.2.1]# ls
aclocal.m4 conf configure.in INSTALL man upgrades
AUTHORS config.guess COPYING install-sh misc
bin config.log database m4 missing
build config.status depcomp Makefile NEWS
ChangeLog config.sub frontends Makefile.am README
compile configure include Makefile.in src
[root@p34 zabbix-2.2.1]# cd misc
[root@p34 misc]# ls
images init.d Makefile Makefile.am Makefile.in snmptrap
[root@p34 misc]# cd init.d/
[root@p34 init.d]# ls
aix debian fedora freebsd gentoo README suse tru64 ubuntu
[root@p34 init.d]# cd fedora/
[root@p34 fedora]# ls(看服务器是单核还是双核,选不同的脚本)
core core5
[root@p34 fedora]# cd core (单核)
[root@p34 core]# ls
zabbix_agentd zabbix_server
[root@p34 core]# cp zabbix_server /etc/init.d/
[root@p34 core]# chmod +x /etc/init.d/zabbix_server
[root@p34 core]# chkconfig --add zabbix_server
[root@p34 core]# chkconfig zabbix_server on
[root@p34 core]# service zabbix_server status
zabbix_server 已停
[root@p34 core]#
修改启动脚本加载程序的目录 /usr/lcoal/zabbix (正常是/usr/local/zabbix 但下载的时候保存的位置放在了/usr/lcoal下所以要一致)
[root@p34 etc]# vim /etc/init.d/zabbix_server
22 BASEDIR=/usr/lcoal/zabbix
修改服务的主配置文件,指定服务使用的库 登录用户密码
[root@p34 etc]# pwd
/usr/lcoal/zabbix/etc
[root@p34 etc]# vim zabbix_server.conf
39 LogFile=/tmp/zabbix_server.log
87 DBName=zabbixdb
103 DBUser=jing
111 DBPassword=123456
启动监控服务:
[root@p34 etc]# service zabbix_server start
Starting zabbix_server:
[root@p34 etc]# netstat -anptu |grep :10051
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 19443/zabbix_server
6.配置被监控服务
监控本机
运行zabbix_agentd服务:创建启动脚本 并添加系统服务同时设置为开机运行
[root@p34 etc]# cd /root/zabbix/zabbix-2.2.1
[root@p34 zabbix-2.2.1]# cd mis
misc/ missing
[root@p34 zabbix-2.2.1]# cd misc
[root@p34 misc]# ls
images init.d Makefile Makefile.am Makefile.in snmptrap
[root@p34 misc]# cd init.d/
[root@p34 init.d]# ls
aix debian fedora freebsd gentoo README suse tru64 ubuntu
[root@p34 init.d]# cd fedora/
[root@p34 fedora]# ls
core core5
[root@p34 fedora]# cd core
[root@p34 core]# ls
zabbix_agentd zabbix_server
[root@p34 core]# cp zabbix_agentd /etc/init.d/
[root@p34 core]# chmod +x /etc/init.d/zabbix_agentd
[root@p34 core]# chkconfig --add zabbix_agentd
[root@p34 core]# chkconfig zabbix_agentd on
[root@p34 core]# service zabbix_agentd status
zabbix_agentd 已停
修改 启动脚本加载程序的目录
[root@p34 core]# vim /etc/init.d/zabbix_agentd
22 BASEDIR=/usr/lcoal/zabbix
修改服务的主配文件
[root@p34 core]# cd /usr/lcoal/zabbix/etc
[root@p34 etc]# ls
zabbix_agent.conf zabbix_agentd.conf zabbix_server.conf
zabbix_agent.conf.d zabbix_agentd.conf.d zabbix_server.conf.d
[root@p34 etc]# vim zabbix_agentd.conf
21 LogFile=/tmp/zabbix_agentd.log
81 Server=127.0.0.1
122 ServerActive=127.0.0.1:10051
启动监控服务:
[root@p34 etc]# service zabbix_agentd start
Starting zabbix_agentd: [确定]
[root@p34 etc]# netstat -anptu |grep zabbix_agentd
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 19696/zabbix_agentd
添加监控本机:加主机名组 监测中 模板 保存。
———+++++++++++
监控远端本机:172.44.55.103
被监控的主机做配置:
1运行zabbix_agentd服务
安装准备 装包 创建启动脚本 修改配置 启动服务 查看端口号
监控服务器的配置:
1.添加新监控的主机
2查看监控信息
+++++++++++++++++++++++
#rpm -q gcc gcc-c++
#useradd zabbix
#unzip zabbix.zip
#tar -zxvf zabbix-2.2.1.tar.gz
#cd zabbix-2.2.1/
# ./configure --prefix=/usr/lcoal/zabbix --enable-agent
#make install
#cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
#chmod +x /etc/init.d/zabbix_agentd
#chkconfig --add zabbix_agentd
#chkconfig zabbix_agentd on
#service zabbix_agentd status
# vim /etc/init.d/zabbix_agentd
22 BASEDIR=/usr/local/zabbix
[root@room1pc01 zabbix-2.2.1]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
81 Server=127.0.0.1, 172.40.55.34,172.40.55.33
122 ServerActive=172.40.55.34:10051, 172.40.55.33:10051
[root@room1pc01 zabbix-2.2.1]# sed -n ‘81p;122p‘ /usr/local/zabbix/etc/zabbix_agentd.conf
Server=127.0.0.1, 172.40.55.34,172.40.55.33
ServerActive=172.40.55.34:10051, 172.40.55.33:10051
[root@room1pc01 zabbix-2.2.1]# /etc/init.d/zabbix_agentd start
[root@room1pc01 zabbix-2.2.1]# cat /tmp/zabbix_agentd.log (查看日志)
22418:20170313:151938.081 Starting Zabbix Agent [Zabbix server]. Zabbix 2.2.1 (revision 40808).
22418:20170313:151938.081 using configuration file: /usr/local/zabbix/etc/zabbix_agentd.conf
22421:20170313:151938.099 agent #1 started [listener #1]
22420:20170313:151938.099 agent #0 started [collector]
监控服务器的配置:(172.40.55.34)
1查看新监控的主机
2查看监控信息
7.1自定义监控项目
监控远端主机172.40.55.107 系统的总用户数量
客户端配置
启动自定义监控项功能
定义监控命令
在本机测试监控命令
[root@room1pc01 zabbix-2.2.1]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
243 Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
255 UnsafeUserParameters=1
[root@room1pc01 zabbix-2.2.1]# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/getsunusers.conf
UserParameter=mon_num_users,awk -F ":" ‘BEGIN{i=0}{i++}END{print i}‘ /etc/passwd
——————————————————————————————————————————————————————————————————
259 # Format: UserParameter=<key>,<shell command> 上面这个文件中,用的zabbix_agentd.conf配置文件中这个语法格式
——————————————————————————————————————————————————————————————————
[root@room1pc01 zabbix-2.2.1]# /etc/init.d/zabbix_agentd restart
Shutting down zabbix_agentd: [确定]
Starting zabbix_agentd: [确定]
[root@room1pc01 zabbix-2.2.1]# /usr/local/zabbix/bin/zabbix_get -h (查看使用的方法)
[root@room1pc01 zabbix-2.2.1]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -p10050 -k mon_num_users
50
[root@room1pc01 zabbix-2.2.1]# useradd zh
[root@room1pc01 zabbix-2.2.1]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -p10050 -k mon_num_users
51
7.2监控服务器配置
调用被监控主机定义的监控命令
监控远端主机时,调用其自定义的监控命令
定义监控模板 tmpmon107
定义应用集 firstyyj
定义监控项 firstjkx 绑定mon_num_users
监控远端主机时,调用定义监控模板tmpmon107
查看监控信息
在监控端测试一下远端定义的结果:
[root@p34 ~]# /usr/lcoal/zabbix/bin/zabbix_get -s 172.40.55.107 -p 10050 -k mon_num_users
51
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
——————————————————————————————————————————————————————
(监控本机系统外建用户数的总量 --》uid大于500)
[root@p34 ~]# vim /usr/lcoal/zabbix/etc/zabbix_agentd.conf
243 Include=/usr/lcoal/zabbix/etc/zabbix_agentd.conf.d/
255 UnsafeUserParameters=1
[root@p34 ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/wjsumusers.conf
UserParameter=wj_num_users,awk -F ":" ‘$3>"500"{print}‘ /etc/passwd |wc -l
[root@p34 ~]# /etc/init.d/zabbix_agentd restart
命令测试:
[root@p34 ~]# /usr/lcoal/zabbix/bin/zabbix_get -s 127.0.0.1 -p 10050 -k wj_num_users
12
在页面中添加:
调用被监控主机定义的监控命令
监控远端主机时,调用其自定义的监控命令
定义监控模板 hosttmpmon
定义应用集 hostyyj
定义监控项 hostjkx 绑定wj_num_users
监控远端主机时,调用定义监控模板hosttmpmon
查看监控信息
————————————————————————————————————————————————————
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8.配置监控报警
监控远端主机172.40.55.107当系统的总用户量大于25个时发
报警邮件给本的zabbiuser@localhost邮箱
邮件服务器localhost
收件入zabbiuser@localhost
发件人:root@localhost
[root@p34 ~]# rpm -q postfix
postfix-2.6.6-6.el6_5.x86_64
[root@p34 ~]# /etc/init.d/postfix start
[root@p34 ~]# /etc/init.d/postfix status
master (pid 1764) 正在运行...
[root@p34 ~]# useradd zabbixuser
[root@p34 ~]# mail -s "aaa" zabbixuser </etc/sysconfig/network
[root@p34 ~]# su - zabbixuser
[zabbixuser@p34 ~]$ mail
Heirloom Mail version 12.4 7/29/08. Type ? for help.
"/var/spool/mail/zabbixuser": 1 message 1 new
>N 1 root Mon Mar 13 09:20 20/628 "aaa"
& 1
[zabbixuser@p34 ~]$ su -
8.1.创建触发器 firstcfq
8.2.创建动作 firstact
8.3 创建邮箱帐号 zabbiuser@localhost
8.4指定接收邮件用户zabbiuser@localhost
本文出自 “12336621” 博客,请务必保留此出处http://12346621.blog.51cto.com/12336621/1906060
Zabbix 监控服务