Zabbix的搭建与应用

一、搭建Zabbix监控服务器
1.1 准备安装环境(LAMP)
[root@server cacti]# rpm -q httpd php php-mysql mysql-server mysql
yum -y install httpd
yum -y install php php-mysql
yum -y install mysql-server mysql

echo 123 > /var/www/html/index.html
[root@server html]# /etc/init.d/httpd start;chkconfig httpd on
[root@server html]# /etc/init.d/mysqld start ; chkconfig mysqld on
[root@server cacti]# mysqladmin -hlocalhost -uroot password "123"
[root@server cacti]# mysql -uroot -p123
mysql> show databases;
mysql> create database zabbixdb;
mysql> grant all on zabbixdb.* to lyd@"localhost" identified by "123456";
mysql> quit

[root@server cacti]# mysql -ulyd -p123456
mysql> show databases;

vim /var/www/html/linkdb.php
<?php
$linkdb=mysql_connect("localhost","lyd","123456")
if($linkdb){
       echo "linkdb ok";
}else{
       echo "linkdb no";
}
?>
[root@server html]# /etc/init.d/httpd restart
[root@server html]# elinks --dump http://localhost/linkdb.php


/etc/init.d/httpd start
chkconfig httpd on

1.2 安装zabbix软件
[root@server ~]# rpm -q gcc gcc-c++
[root@server zabbix]# useradd zabbix
[root@server zabbix]# yum list|grep -i mysql
[root@server zabbix]# rpm -q mysql-devel
[root@server zabbix]#  yum -y install mysql-devel
[root@server zabbix]# tar -xf zabbix-2.2.1.tar.gz
[root@server zabbix]# cd zabbix-2.2.1
[root@server zabbix-2.2.1]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql
[root@server zabbix-2.2.1]# make&&make install
[root@server zabbix-2.2.1]# ls /usr/local/zabbix/
[root@server zabbix-2.2.1]# cd frontends/
[root@server frontends]# cp -r php/ /var/www/html/zabbix
[root@server frontends]# ls -ld /var/www/html/zabbix/
[root@server frontends]# chown -R apache:apache /var/www/html/zabbix/
[root@server zabbix-2.2.1]# cd database/
[root@server database]# cd mysql/
[root@server mysql]# mysql -ulyd -p123456 zabbixdb
mysql> select database();
mysql> show tables;
[root@server mysql]# mysql -ulyd -p123456 zabbixdb < schema.sql
[root@server mysql]# mysql -ulyd -p123456 zabbixdb < images.sql
[root@server mysql]# mysql -ulyd -p123456 zabbixdb < data.sql
mysql> show tables;



[root@server html]# vim /etc/php.ini
post_max_size        16M
max_execution_time        300
max_input_time        300
[root@server zabbix]# /etc/init.d/httpd restart

[root@server zabbix]# rpm -ivh --nodeps php-bcmath-5.3.3-22.el6.x86_64.rpm
[root@server zabbix]# rpm -ivh --nodeps php-mbstring-5.3.3-22.el6.x86_64.rpm
[root@server zabbix]# /etc/init.d/httpd restart

[root@server zabbix]# yum list |grep -i ^php
[root@server zabbix]# yum -y install php-gd php-xml
[root@server zabbix]# /etc/init.d/httpd restart

访问web页面安装zabbix
http://192.168.4.77/zabbix
账号:admin
密码:qwe123

PHP bcmath    off         Fail
PHP mbstring    off         Fail

PHP gd    unknown    2.0    Fail
PHP gd PNG support    off         Fail
PHP gd JPEG support    off         Fail
PHP gd FreeType support    off         Fail
PHP libxml    2.7.6    2.6.15    OK
PHP xmlwriter    off         Fail
PHP xmlreader    off         Fail

1.3 启动zabbix-server监控服务
[root@server ~]# cd  zabbix/zabbix-2.2.1/misc/init.d/fedora/core
[root@server core]# ls
zabbix_agentd  zabbix_server
[root@server core]# cp zabbix_server /etc/init.d/
[root@server core]# chmod +x /etc/init.d/zabbix_server
[root@server core]# chkconfig --add zabbix_server
[root@server core]# chkconfig --list zabbix_server
[root@server core]# chkconfig zabbix_server on
[root@server core]# chkconfig --list zabbix_server

[root@server etc]# sed -n '22p' /etc/init.d/zabbix_server
    BASEDIR=/usr/local/zabbix
[root@server etc]# sed -n '39p;87p;103p;111p' /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log
DBName=zabbixdb
DBUser=lyd
DBPassword=123456

[root@server etc]# /etc/init.d/zabbix_server restart
[root@server etc]# /etc/init.d/zabbix_server status
zabbix_server (pid 3740 3739 3738 3737 3736 3735 3734 3733 3732 3731 3730 3725 3724 3723 3722 3721 3720 3719 3718 3717 3716 3715 3714 3713 3712 3711 3708) 正在运行...

[root@server etc]# netstat -anptul|grep zabbix
tcp        0      0 0.0.0.0:10051               0.0.0.0:*                   LISTEN      3708/zabbix_server

+++++++++++++++++++++++++++++++++++++++++++++
二、配置
2.1监控远端服务器 192.168.4.67
2.1.1 运行zabbix_agentd服务

[root@web1 ~]#  useradd zabbix
[root@web1 ~]#  rpm -q gcc gcc-c++
[root@web1 ~]#  tar -xf zabbix-2.2.1.tar.gz
[root@web1 zabbix-2.2.1]#  cd zabbix-2.2.1
[root@web1 zabbix-2.2.1]#  ./configure --prefix=/usr/local/zabbix --enable-agent
[root@web1 zabbix-2.2.1]#  make&&make install

[root@web1 ~]# cp /root/zabbix-2.2.1/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@web1 ~]# chmod +x /etc/init.d/zabbix_agentd
[root@web1 ~]# chkconfig --add zabbix_agentd
[root@web1 ~]# chkconfig zabbix_agentd on
[root@web1 ~]# chkconfig --list zabbix_agentd

[root@web1 ~]# sed -n '22p' /etc/init.d/zabbix_agentd
    BASEDIR=/usr/local/zabbix

[root@web1 ~]# sed -n '21p;81p;122p' /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=127.0.0.1, 192.168.4.77
ServerActive=192.168.4.77:10051

[root@web1 ~]# /etc/init.d/zabbix_agentd start
[root@web1 ~]# /etc/init.d/zabbix_agentd status
[root@web1 ~]# netstat -anptu|grep :10050
tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      15014/zabbix_agentd

2.1.2 在监控服务器配置监控67服务器
http://192.168.4.77/zabbix

1 添加要监控的主机,并选择监控模板






++++++++++++++++++++++++++++++++++++++++++++++
2.2监控本机  (默认就对本机做监控,不用做任何配置,只要把zabbix_agentd服务启动就可以)
2.2.1 运行zabbix_agentd 服务

[root@server ~]# cp /root/zabbix/zabbix-2.2.1/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@server ~]# chmod +x /etc/init.d/zabbix_agentd
[root@server ~]# chkconfig --add zabbix_agentd
[root@server ~]# chkconfig zabbix_agentd on

[root@server ~]# sed -n "22p" /etc/init.d/zabbix_agentd
    BASEDIR=/usr/local/zabbix
[root@server ~]# sed -n '21p;81p;122p' /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=127.0.0.1
ServerActive=127.0.0.1:10051

[root@server ~]# /etc/init.d/zabbix_agentd start
Starting zabbix_agentd:                                    [确定]
[root@server ~]# /etc/init.d/zabbix_agentd status
zabbix_agentd (pid 5588 5587 5586 5585 5584 5583) 正在运行...
[root@server ~]# netstat -anptul|grep :10050
tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      5583/zabbix_agentd

++++++++++++++++++++++++++++++++++++++++++++++++
2.3自定义监控项目
监控67服务器系统的总用户数量

1、在被监控端启用自定义监控项目功能
[root@web1 zabbix_agentd.conf.d]# sed -n '243p;255p' /usr/local/zabbix/etc/zabbix_agentd.conf
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1




2、在被监控端定义监控命令
[root@web1 ~]# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d
[root@web1 zabbix_agentd.conf.d]# vim mon_67_users.conf
UserParameter=mon_67_users,wc -l /etc/passwd | awk '{print $1}'

# wc -l /etc/passwd | awk '{print $1}'

/etc/init.d/zabbix_agentd restart

3、在被监控端测试定义监控命令
[root@web1 zabbix_agentd.conf.d]# /usr/local/zabbix/bin/zabbix_get -h
usage: zabbix_get [-hV] -s <host name or IP> [-p <port>] [-I <IP address>] -k <key>

[root@web1 zabbix_agentd.conf.d]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -p 10050 -k mon_67_users
27


出错时查看错误日志
[root@web1 zabbix_agentd.conf.d]# /etc/init.d/zabbix_agentd status
zabbix_agentd 已死,但是 subsys 被锁
[root@web1 zabbix_agentd.conf.d]# netstat -anptul|grep :10050
[root@web1 zabbix_agentd.conf.d]# vim /tmp/zabbix_agentd.log
2401:20210731:000232.529 using configuration file: /usr/local/zabbix/etc/zabbix_agentd.conf
2401:20210731:000232.530 cannot add user parameter "1": not comma-separated


监控服务器的配置 192.168.4.77
[root@server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.4.67 -p 10050 -k mon_67_users

在监控67服务器时调用,67服务器上的自定义命令
1、创建监控模板    tep67
2、创建应用集    yyjone
3、创建项目    itemone
4、监控67服务器时调用,新创建的模板
5、查看监控信息

++++++++++++++++++++++++++++++++++++++++++++++++++++
2.4 监控报警(邮件 短信 即时消息)
通过触发器实现
当监控67服务器上的总用户数量大于35时,给zabbix@localhost邮件发报警邮件。

收件人    zabbix@localhost
发件人    root@localhost
邮件服务器  localhost

[root@server ~]# grep zabbix /etc/passwd
zabbix:x:502:503::/home/zabbix:/bin/bash
You have new mail in /var/spool/mail/root
[root@server ~]# mail -s "aaa" zabbix < /etc/hosts
[root@server ~]# su - zabbix
[zabbix@server ~]$ mail


创建触发器    cfqone
创建动作         dzone
指定邮箱地址和邮件服务器
触发器被触发后,查看是否收到报警邮件

posted @ 2021-07-31 09:57  Linux刀客  阅读(530)  评论(0编辑  收藏  举报