Zabbix安装部署,在Zabbix监控里添加主机,并借助API实现在命令行对主机进行管理,监控httpd、Nginx、MySQL

1.安装zabbix必要安装包

[root@server1 ~]# ls
4.0
[root@server1 ~]# cd 4.0/
[root@server1 4.0]# ls
fping-3.10-1.el7.x86_64.rpm
iksemel-1.4-2.el7.centos.x86_64.rpm
php-bcmath-5.4.16-42.el7.x86_64.rpm
php-mbstring-5.4.16-42.el7.x86_64.rpm
repodata
zabbix-agent-4.0.5-1.el7.x86_64.rpm
zabbix-get-4.0.5-1.el7.x86_64.rpm
zabbix-java-gateway-4.0.5-1.el7.x86_64.rpm
zabbix-proxy-mysql-4.0.5-1.el7.x86_64.rpm
zabbix-sender-4.0.5-1.el7.x86_64.rpm
zabbix-server-mysql-4.0.5-1.el7.x86_64.rpm
zabbix-web-4.0.5-1.el7.noarch.rpm
zabbix-web-mysql-4.0.5-1.el7.noarch.rpm

[root@server1 4.0]# yum install -y fping-3.10-1.el7.x86_64.rpm \
iksemel-1.4-2.el7.centos.x86_64.rpm \
php-bcmath-5.4.16-42.el7.x86_64.rpm \
php-mbstring-5.4.16-42.el7.x86_64.rpm \
zabbix-agent-4.0.5-1.el7.x86_64.rpm \
zabbix-server-mysql-4.0.5-1.el7.x86_64.rpm \
zabbix-web-4.0.5-1.el7.noarch.rpm \
zabbix-web-mysql-4.0.5-1.el7.noarch.rpm \

2.安装数据库

[root@server1 4.0]# yum install -y mariadb-server

3.开启mariadb服务,并安全初始化

[root@server1 4.0]# systemctl start mariadb.service 
[root@server1 4.0]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y   
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

4.创建zabbix用户,并授权

[root@server1 4.0]# mysql -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> Bye

5.导入数据

[root@server1 4.0]# zcat /usr/share/doc/zabbix-server-mysql-4.0.5/create.sql.gz  | mysql -uzabbix -p zabbix
Enter password: 

6.进入数据库查看

[root@server1 4.0]# mysql -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zabbix             |
+--------------------+
4 rows in set (0.00 sec)

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]> show tables;
+----------------------------+
| Tables_in_zabbix           |
+----------------------------+
| acknowledges               |
| actions                    |
| alerts                     |
| application_discovery      |
| application_prototype      |
| application_template       |
| applications               |
| auditlog                   |
| auditlog_details           |
| autoreg_host               |
| conditions                 |
| config                     |
| corr_condition             |
| corr_condition_group       |
| corr_condition_tag         |
| corr_condition_tagpair     |
| corr_condition_tagvalue    |
| corr_operation             |
| correlation                |
| dashboard                  |
| dashboard_user             |
| dashboard_usrgrp           |
| dbversion                  |
| dchecks                    |
| dhosts                     |
| drules                     |
| dservices                  |
| escalations                |
| event_recovery             |
| event_suppress             |
| event_tag                  |
| events                     |
| expressions                |
| functions                  |
| globalmacro                |
| globalvars                 |
| graph_discovery            |
| graph_theme                |
| graphs                     |
| graphs_items               |
| group_discovery            |
| group_prototype            |
| history                    |
| history_log                |
| history_str                |
| history_text               |
| history_uint               |
| host_discovery             |
| host_inventory             |
| hostmacro                  |
| hosts                      |
| hosts_groups               |
| hosts_templates            |
| housekeeper                |
| hstgrp                     |
| httpstep                   |
| httpstep_field             |
| httpstepitem               |
| httptest                   |
| httptest_field             |
| httptestitem               |
| icon_map                   |
| icon_mapping               |
| ids                        |
| images                     |
| interface                  |
| interface_discovery        |
| item_application_prototype |
| item_condition             |
| item_discovery             |
| item_preproc               |
| items                      |
| items_applications         |
| maintenance_tag            |
| maintenances               |
| maintenances_groups        |
| maintenances_hosts         |
| maintenances_windows       |
| mappings                   |
| media                      |
| media_type                 |
| opcommand                  |
| opcommand_grp              |
| opcommand_hst              |
| opconditions               |
| operations                 |
| opgroup                    |
| opinventory                |
| opmessage                  |
| opmessage_grp              |
| opmessage_usr              |
| optemplate                 |
| problem                    |
| problem_tag                |
| profiles                   |
| proxy_autoreg_host         |
| proxy_dhistory             |
| proxy_history              |
| regexps                    |
| rights                     |
| screen_user                |
| screen_usrgrp              |
| screens                    |
| screens_items              |
| scripts                    |
| service_alarms             |
| services                   |
| services_links             |
| services_times             |
| sessions                   |
| slides                     |
| slideshow_user             |
| slideshow_usrgrp           |
| slideshows                 |
| sysmap_element_trigger     |
| sysmap_element_url         |
| sysmap_shape               |
| sysmap_url                 |
| sysmap_user                |
| sysmap_usrgrp              |
| sysmaps                    |
| sysmaps_elements           |
| sysmaps_link_triggers      |
| sysmaps_links              |
| tag_filter                 |
| task                       |
| task_acknowledge           |
| task_check_now             |
| task_close_problem         |
| task_remote_command        |
| task_remote_command_result |
| timeperiods                |
| trends                     |
| trends_uint                |
| trigger_depends            |
| trigger_discovery          |
| trigger_tag                |
| triggers                   |
| users                      |
| users_groups               |
| usrgrp                     |
| valuemaps                  |
| widget                     |
| widget_field               |
+----------------------------+
144 rows in set (0.00 sec)

MariaDB [zabbix]> Bye

7.配置zabbix的配置文件

[root@server1 4.0]# cd /etc/zabbix/
[root@server1 zabbix]# vim zabbix_server.conf 
=================================================
DBName=zabbix
DBUser=zabbix
DBPassword=redhat

8.开启zabbix,服务端与客户端服务

[root@server1 zabbix]# systemctl start zabbix-server.service 
[root@server1 zabbix]# systemctl start zabbix-agent.service

9.修改httpd配置目录下的zabbix配置文件

[root@server1 zabbix]# vim /etc/httpd/conf.d/zabbix.conf
==============================================================
        php_value date.timezone Asia/Shanghai

10.开启httpd服务

[root@server1 zabbix]# systemctl start httpd

11.浏览器输入http://172.25.13.1/zabbix/

  • 默认帐密

用户名:Admin
密码:zabbix

一、利用zabbix_API管理zabbix
1.开启服务

  • server1:
[root@server1 ~]# systemctl start httpd
[root@server1 ~]# systemctl start mariadb
[root@server1 ~]# systemctl start zabbix-server.service 
[root@server1 ~]# systemctl start zabbix-agent.service 
  • server2-3
[root@server2 ~]# systemctl start zabbix-agent.service

[root@server3 ~]# systemctl start zabbix-agent.service 

2.关闭网页界面上的动作及自动发现

3.编辑API文件

[root@server1 ~]# vim zabbix-api
===================================
curl -s XPOST -H "Content-Type: application/json-rpc" -d '
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}' http://172.25.13.1/zabbix/api_jsonrpc.php

#改变输出格式

curl -s XPOST -H "Content-Type: application/json-rpc" -d '
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}' http://172.25.13.1/zabbix/api_jsonrpc.php | python -m json.tool

4.赋予API文件执行权限

[root@server1 ~]# chmod +x zabbix-api

5.执行该文件

[root@server1 ~]# ./zabbix-api 
{"jsonrpc":"2.0","result":"43980b818d61f5742623f7a76081f6aa","id":1}
=======================================================================
[root@server1 ~]# ./zabbix-api 
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "781aff1790c39e2d8e6c1593d1c68836"
}

6.检索主机

[root@server1 ~]# vim zabbix-api
=================================
curl -s XPOST -H "Content-Type: application/json-rpc" -d '
{    
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
    },
    "id": 1,
    "auth": "781aff1790c39e2d8e6c1593d1c68836"
}' http://172.25.13.1/zabbix/api_jsonrpc.php | python -m json.tool

#执行

[root@server1 ~]# ./zabbix-api 
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "host": "Zabbix server",
            "hostid": "10084",
            "interfaces": [
                {
                    "interfaceid": "1",
                    "ip": "127.0.0.1"
                }
            ]
        },
        {
            "host": "server2",
            "hostid": "10265",
            "interfaces": [
                {
                    "interfaceid": "4",
                    "ip": "172.25.13.2"
                }
            ]
        },
        {
            "host": "server3",
            "hostid": "10266",
            "interfaces": [
                {
                    "interfaceid": "5",
                    "ip": "172.25.13.3"
                }
            ]
        }
    ]
}

7.删除主机

[root@server1 ~]# vim zabbix-api 
==================================
curl -s XPOST -H "Content-Type: application/json-rpc" -d '
{   
    "jsonrpc": "2.0",
    "method": "host.delete",
    "params": [
        "10266"
    ], 
    "id": 1,
    "auth": "781aff1790c39e2d8e6c1593d1c68836"
}' http://172.25.13.1/zabbix/api_jsonrpc.php | python -m json.tool

#执行

[root@server1 ~]# ./zabbix-api 
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10266"
        ]
    }
}

8.添加主机

[root@server1 ~]# vim zabbix-api 
=================================
curl -s XPOST -H "Content-Type: application/json-rpc" -d '
{  
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "server3",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "172.25.13.3",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    }, 
    "id": 1,
    "auth": "781aff1790c39e2d8e6c1593d1c68836"
}' http://172.25.13.1/zabbix/api_jsonrpc.php | python -m json.tool

#执行

[root@server1 ~]# ./zabbix-api 
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10267"
        ]
    }
}

二、监控httpd服务

1.开启server2httpd服务

[root@server2 ~]# yum install -y httpd
[root@server2 ~]# systemctl start httpd.service

2.网页界面server2添加监控

配置->主机->server2->模板->(链接指示器)选择->Template App HTTP Service->选择->添加->更新

3.查看
server2->监控项目

三、配置图形监控

1.server3部署Nginx服务

[root@server3 ~]# tar zxf nginx-1.16.0.tar.gz 
[root@server3 ~]# cd nginx-1.16.0/
[root@server3 nginx-1.16.0]# vim auto/cc/gcc 
==================================================
171 # debug
172 #CFLAGS="$CFLAGS -g"


[root@server3 nginx-1.16.0]# yum install -y gcc pcre-devel zlib-devel

[root@server3 nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module

[root@server3 nginx-1.16.0]# make && make install

2.修改server3的Nginx配置文件

[root@server3 nginx-1.16.0]# vim /usr/local/nginx/conf/nginx.conf
====================================================================
 47         location /status {
 48                 stub_status on;
 49                 access_log off;
 50                 allow 127.0.0.1;
 51                 deny all;
 52         }

3.制作软链接

[root@server3 nginx-1.16.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

4.检查配置文件语法,并开启服务

[root@server3 nginx-1.16.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server3 nginx-1.16.0]# nginx 

5.安装httpd,包含http-tools

[root@server3 nginx-1.16.0]# yum install -y httpd

6.查看

  • server3:
[root@server3 zabbix_agentd.d]# curl http://127.0.0.1/status

模拟

[root@server3 nginx-1.16.0]# ab -c 1 -n 1000 http://127.0.0.1/status

再次查看

[root@server3 zabbix_agentd.d]# curl http://127.0.0.1/status

7.配置键值

[root@server3 nginx-1.16.0]# cd  /etc/zabbix/zabbix_agentd.d/
[root@server3 zabbix_agentd.d]# cp userparameter_mysql.conf userparameter_nginx.conf 
[root@server3 zabbix_agentd.d]# vim userparameter_nginx.conf
==================================================================
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'

8.重启zabbix-agent服务

[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent.service

9.server1安装zabbix-get

[root@server1 4.0]# yum install -y zabbix-get-4.0.5-1.el7.x86_64.rpm

10.测试配置的键值正确性

[root@server1 4.0]# zabbix_get -s 172.25.13.3 -p 10050 -k "nginx.active"

11.配置网页端
配置->主机->server3->监控项->创建监控项

名称:nginx_active
键值:nginx.active

添加->

图形->创建图形->

名称:nginx_status

监控项添加->添加->nginx_active->选择

预览——可以看到绘制的图形

12.解决网页端显示乱码

  • server1:
    #获取适配字体
[root@server1 4.0]# cd /usr/share/zabbix/fonts/
[root@server1 fonts]# ls
graphfont.ttf
[root@server1 fonts]# lftp 172.25.13.250
lftp 172.25.13.250:~> cd pub/zabbix/
lftp 172.25.13.250:/pub/zabbix> get simkai.ttf 
4135804 bytes transferred                       
lftp 172.25.13.250:/pub/zabbix> exit
[root@server1 fonts]# ls
graphfont.ttf  simkai.ttf
#修改字体配置文件
[root@server1 include]# cd /usr/share/zabbix/include/
[root@server1 include]# vim defines.inc.php 
===================================================
/graphfont
:%s/graphfont/simkai/g
:wq

13.浏览器刷新重新查看

14.添加新的监控内容

  • server3:
[root@server3 zabbix_agentd.d]# vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf 
==============================================================================
UserParameter=nginx.accept,curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $1}'

15.重启服务

[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent.service

16.server1测试

[root@server1 include]# zabbix_get -s 172.25.13.3 -p 10050 -k "nginx.accept"
2331

17.配置网页端

配置->主机->server3->监控项->创建监控项

名称:nginx_accept
键值:nginx.accept

添加->

图形->nginx_status->

监控项添加->添加->nginx_accept->选择

预览——可以看到绘制的图形

四、监控mysql

配置->主机->Zabbix server->模板->选择->(群组)Template/Databases->Template DB MySQL->添加->更新

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值