Zabbix监控基础

 

1. 安装前准备

1
2
3
4
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
yum install net-tools openssl-devel openssl epel-release wget vim gcc gcc-c++ lsof chrony tree unzip rsync -y

2. LAMP环境搭建

安装:

1
yum install -y httpd mariadb mariadb-server mariadb-client php php-mysql

启动mysql,设置开机启动,修改mysql密码,注意下面的yourpassword要替换为你自己的密码

1
2
3
systemctl start mariadb.service
systemctl enable mariadb.service #设置开机启动
mysqladmin -u root -p password yourpassword

3. 安装Zabbix服务

1
2
3
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-get zabbix-agent

4. 创建Zabbix数据库

1
2
3
4
5
mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE zabbix DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit

导入数据:

1
2
cd /usr/share/doc/zabbix-server-mysql-3.2.10/
zcat create.sql.gz | mysql -uzabbix -pzabbix

5. 修改zabbix配置

注意:下面的zabbix请替换为你自己的数据库密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
grep -Ev "^$|#" /etc/zabbix/zabbix_server.conf
 
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBHost=localhost
DBName=zabbix
DBName=zabbix
DBUser=zabbix
DBUser=zabbix
DBPassword=zabbix
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000

修改时区:

1
2
vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai

6. 修改zabbix登录密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 326
Server version: 5.5.56-MariaDB MariaDB Server
 
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> use zabbix
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
MariaDB [zabbix]> select userid, alias, name, passwd from users;
+--------+-------+--------+----------------------------------+
| userid | alias | name   | passwd                           |
+--------+-------+--------+----------------------------------+
|      1 | Admin | Zabbix | 5fce1b3e34b520afeffb37ce08c7cd66 |
|      2 | guest |        | d41d8cd98f00b204e9800998ecf8427e |
+--------+-------+--------+----------------------------------+
2 rows in set (0.00 sec)
 
MariaDB [zabbix]> update users set passwd=md5('newpassword'where userid='1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

7. 启动并设置开机启动

1
2
3
4
5
6
7
systemctl start httpd
systemctl start zabbix-server
systemctl start zabbix-agent
 
systemctl enable httpd
systemctl enable zabbix-server
systemctl enable zabbix-agent

8. 访问zabbix页面  

通过http://hostname/zabbix访问zabbix的安装页面,填写安装信息,完成安装。完成安装后,初始登录账户密码为Admin/zabbix。

9. 安装zabbix-agent

zabbix-server安装好了,下面需要“接入”其他机器,将它们纳入到zabbix-server的管理

1
2
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
yum install -y zabbix-agent

配置zabbix-agent:

1
2
3
4
5
6
7
8
# grep -Ev "^$|#" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.1.1
ServerActive=192.168.1.1
Hostname=192.168.1.2
Include=/etc/zabbix/zabbix_agentd.d/*.conf

启动zabbix-agent:

1
2
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service

操作命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
yum install -y zabbix-agent
 
cp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak
sed -i s'$ServerActive=127.0.0.1$ServerActive=192.168.1.1$'g /etc/zabbix/zabbix_agentd.conf
sed -i s'$Server=127.0.0.1$Server=192.168.1.1$'g /etc/zabbix/zabbix_agentd.conf
sed -i s'$# UnsafeUserParameters=0$UnsafeUserParameters=1$'g /etc/zabbix/zabbix_agentd.conf
sed -i s'$# EnableRemoteCommands=0$EnableRemoteCommands=1$'g /etc/zabbix/zabbix_agentd.conf
 
HOSTIP=`ifconfig | grep "inet" | egrep -v '(192.168.50|127.0.0.1)'|awk -F ' ' '{print $2}'`
sed -i s"/Hostname=Zabbix server/Hostname=$HOSTIP/"g /etc/zabbix/zabbix_agentd.conf
 
检查:
grep -Ev "^$|#" /etc/zabbix/zabbix_agentd.conf
 
启动zabbix-agent服务:
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service

10. 添加服务器

注意:中间的Agent interfaces是提供给Zabbix Server调用的接口,需确保该ip和port可以访问
绑定模板:

注意:如果zabbix-agent需要被其他机器调用,例如通过zabbix_get获得监控信息,需要在防火墙设置“放开10050端口”

11. Zabbix批量添加web监控

本文参考自 清风拂面 《python实现zabbix批量监控WEB网站和批量监控主机端口》 修改,使得以后添加web时更加方便,zabbix agent端完成1-3步,在zabbix web端直接导入模板即可使用。

web监控模板(zbx_web_monitor_templates.xml)

(1) agent端批量添加脚本/data/zabbix/scripts/web_site_moniter.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# coding:utf-8
 
import os, sys, json
 
# 将要监控的web站点url添加到urllist列表
# urllist = ["http://baidu.com",
#            "http://www.qq.com",
#            "http://www.sina.com.cn/"]
files='/etc/zabbix/test/WEB.txt'
 
fd = open(files,'r')
urllist=[]
for in fd.readlines():
    if i[0] != '#':
        urllist.append(i.strip('\n'))
fd.close()
 
# 这个函数主要是构造出一个特定格式的字典,用于zabbix
def web_site_discovery():
    web_list = []
    web_dict = {"data": None}
 
    for url in urllist:
        url_dict = {}
        url_dict["{#SITENAME}"] = url
        web_list.append(url_dict)
 
    web_dict["data"] = web_list
    jsonStr = json.dumps(web_dict, sort_keys=True, indent=4)
    return jsonStr
 
 
# 这个函数用于测试站点返回的状态码,注意在cmd命令中如果有%{}这种字符要使用占位符代替,否则
# 会报错
def web_site_code():
    cmd = 'curl -o /dev/null -s -w %s %s' % ("%{http_code}", sys.argv[2])
    reply_code = os.popen(cmd).readlines()[0]
    return reply_code
 
 
if __name__ == "__main__":
    try:
        if sys.argv[1] == "web_site_discovery":
            print web_site_discovery()
        elif sys.argv[1] == "web_site_code":
            print web_site_code()
        else:
            print "Pls sys.argv[0] web_site_discovery | web_site_code[URL]"
    except Exception as msg:
        print msg
        # 这里对脚本传递进来的第一个参数做判断去执行不同的函数,为什么要这样,因为通过一个脚本写了两个功能

(2) zabbix agent配置文件添加如下内容,内容含义请参考《python实现zabbix批量监控WEB网站和批量监控主机端口》已经详细解释说明 

1
2
3
UnsafeUserParameters=1
UserParameter=web_site_discovery,/data/zabbix/scripts/web_site_moniter.py web_site_discovery
UserParameter=web_site_code[*],/data/zabbix/scripts/web_site_moniter.py web_site_code $1

(3) zabbix agent添加需要监控web /data/zabbix/test/WEB.txt

1
2
3
4
http://www.ttlsa.com/zabbix/zabbix-zabbix_get-get-items/
#www.qq.com
www.baidu.com
www.sina.com

#号将表示取消web监控

修改zabbix agent配置后需要重启agent生效

(4)导入web监控模板zbx_web_monitor_templates.xml

Configuration --->  Templates --->  Import  --->选择模板zbx_web_monitor_templates.xml

(5)模板导入之后Configuration --->  Templates可以找到Templates Web Monitor 模板

(6)选择Templates Web Monitor模板进行管理主机组或者主机

(7)Configuration --->  Hosts  --->某主机Applications 
可以发现Templates Web Monitor: Web Monitor Service监控项目

(8)查看监控项目

 

参考:http://www.jianshu.com/p/4d3af373e682

 

 

posted @ 2017-11-16 09:13  『心向阳﹡无所惧』  阅读(123)  评论(0)    收藏  举报