zabbix手动环境搭建及API调用

设置上海时区

timedatectl set-timezone Asia/Shanghai

yum install ntp

systemctl enable ntpd.service

systemctl start ntpd.service

ntpdate 0.centos.pool.ntp.org


[root@kolla ~]# cat /etc/selinux/config 

SELINUX=disabled

[root@kolla ~]#  setenforce 0 &&  getenforce

systemctl status firewalld

systemctl stop firewalld

systemctl disable firewalld

systemctl status firewalld

firewall-cmd --state

//配置zabbix的yum源

[root@kolla ~]#  wget http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm

[root@kolla ~]#  rpm -ivh ./zabbix-release-3.4-2.el7.noarch.rpm 


//安装zabbix程序包,安装mysql,zabbix-agent

[root@kolla ~]#  yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb-serve


//启动mariadb(数据库)并设置开机启动,创建数据库实例,授权


[root@kolla ~]# systemctl start mariadb && systemctl enable mariadb

[root@kolla ~]# mysql


#创建zabbix数据库

MariaDB [(none)]>  create database zabbix character set utf8 collate utf8_bin;


#授权所有主机访问数据库实例zabbix,用户名/密码 zabbix/zabbix

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@'%' identified by 'zabbix';


#授权localhost.localdomain主机访问数据库实例zabbix,用户名zabbix/zabbix

MariaDB [(none)]>  grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; 

MariaDB [(none)]>  flush privileges;

MariaDB [(none)]>  exit;

#导入初始模式和数据

[root@kolla ~]# cd /usr/share/doc/zabbix-server-mysql-3.4.10/

#这里数据库没有设置密码的

[root@kolla zabbix-server-mysql-3.4.10]# zcat create.sql.gz |mysql -uroot zabbix  

[root@kolla zabbix-server-mysql-3.4.10]# 



启动zabbix-server

配置zabbix-server 配置文件zabbix-server.conf

[root@kolla zabbix-server-mysql-3.4.10]# hostname

kolla

[root@kolla zabbix-server-mysql-3.4.10]# vi /etc/zabbix/zabbix_server.conf


    91 DBHost=localhost     #数据主机名

    101 DBName=zabbix     #数据库实例

    117 DBUser=zabbix     #用户名

    126 DBPassword=zabbix #密码

启动zabbix-server服务,并开机启动zabbix-server服务


[root@kolla ~]# systemctl start zabbix-server && systemctl enable zabbix-server


编辑Apache的配置文件,注释设置正确的时区

[root@kolla ~]# vi /etc/httpd/conf.d/zabbix.conf

     12     <IfModule mod_php5.c>

     13         php_value max_execution_time 300

     14         php_value memory_limit 128M

     15         php_value post_max_size 16M

     16         php_value upload_max_filesize 2M

     17         php_value max_input_time 300

     18         php_value always_populate_raw_post_data -1

     19         php_value  date.timezone Asia/Shanghai


启动httpd服务,设置开机启动httpd服务 

[root@kolla ~]# systemctl start httpd

[root@kolla ~]# systemctl enable httpd


启动zabbix-agent并设置开机自启动

[root@kolla ~]# systemctl start zabbix-agent

[root@kolla ~]# systemctl enable zabbix-agent


zabbix web网页安装

http://192.168.229.130/zabbix/setup.php       #点击下一步

一路往下,登陆

http://192.168.229.130/zabbix/index.php    Admin zabbix


所用到的API

1  认证并取得加密字段

[root@kolla ~]# 

curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":

"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"auth":

null,"id":0}' http://192.168.229.130/zabbix/api_jsonrpc.php;

返回结果

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 08:10:28 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 68

Content-Type: application/json


{"jsonrpc":"2.0","result":"7c4ba89691470b9c7018fa24206e71fd","id":0}

参数说明: result:返回的不需要账号密码得到的token,key值


2获取指定活所有监控主机的hostids

[root@kolla ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid"]},"auth":"7c4ba89691470b9c7018fa24206e71fd","id":1}' http://192.168.229.130/zabbix/api_jsonrpc.php

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 08:15:07 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 54

Content-Type: application/json


{"jsonrpc":"2.0","result":[{"hostid":"10084"}],"id":1}

参数说明: hostid:为请求返回的所有hostid结果


3 zabbix获取cpu信息

3.1获取空闲cpu请求

[root@kolla ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"item.get","params":{"output":"itemids","hostids":"10084","search":{"key_":"system.cpu.util[,idle]"}},"auth":"7c4ba89691470b9c7018fa24206e71fd","id":1}' http://192.168.229.130/zabbix/api_jsonrpc.php

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 08:24:04 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 54

Content-Type: application/json


{"jsonrpc":"2.0","result":[{"itemid":"23299"}],"id":1}


根据itemid获取cpu使用数据

[root@kolla ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"history.get","params":{"output":"extend","history":0,"itemids":"23299","sortfield":"clock","sortorder":"DESC","limit":10},"auth":"7c4ba89691470b9c7018fa24206e71fd","id":1}' http://192.168.229.130/zabbix/api_jsonrpc.php

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 08:29:36 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 784

Content-Type: application/json


{"jsonrpc":"2.0","result":[{"itemid":"23299","clock":"1528964959","value":"99.6607","ns":"17298798"},{"itemid":"23299","clock":"1528964899","value":"99.6775","ns":"962278395"},{"itemid":"23299","clock":"1528964839","value":"99.6997","ns":"899333542"},{"itemid":"23299","clock":"1528964779","value":"99.7108","ns":"837009427"},{"itemid":"23299","clock":"1528964719","value":"99.7497","ns":"782085894"},{"itemid":"23299","clock":"1528964659","value":"99.6997","ns":"727213245"},{"itemid":"23299","clock":"1528964599","value":"99.7330","ns":"670066709"},{"itemid":"23299","clock":"1528964539","value":"99.6941","ns":"591833776"},{"itemid":"23299","clock":"1528964479","value":"99.7275","ns":"535724086"},{"itemid":"23299","clock":"1528964419","value":"99.7331","ns":"466218270"}],"id":1}

[root@kolla ~]#


说明: value:为请求时所定义的itemid历史数据, 对应的值为物理机cpu的空闲百分比

---------------------------------

用户cpu使用情况

curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"item.get","params":{"output":"itemids","hostids":"10084","search":{"key_":"system.cpu.util[,user]"}},

> "auth":"7c4ba89691470b9c7018fa24206e71fd","id":0}' http://192.168.229.130/zabbix/api_jsonrpc.php

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 09:03:24 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 54

Content-Type: application/json


{"jsonrpc":"2.0","result":[{"itemid":"23306"}],"id":0}

[root@kolla ~]#


根据itemid获取cpu的使用数据

 curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0", "method": "history.get","params":{"output":"extend", "history":0, "itemids":"23306","sortfield":"clock","sortorder":"DESC","limit":10}, "auth":"7c4ba89691470b9c7018fa24206e71fd","id":1}' http://192.168.229.130/zabbix/api_jsonrpc.php

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 09:11:56 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 774

Content-Type: application/json


{"jsonrpc":"2.0","result":[{"itemid":"23306","clock":"1528967486","value":"0.0945","ns":"644778520"},{"itemid":"23306","clock":"1528967426","value":"0.1001","ns":"579728930"},{"itemid":"23306","clock":"1528967366","value":"0.0834","ns":"518730693"},{"itemid":"23306","clock":"1528967306","value":"0.0778","ns":"450757183"},{"itemid":"23306","clock":"1528967246","value":"0.0834","ns":"388084039"},{"itemid":"23306","clock":"1528967186","value":"0.1001","ns":"321712401"},{"itemid":"23306","clock":"1528967126","value":"0.0945","ns":"256476597"},{"itemid":"23306","clock":"1528967066","value":"0.1001","ns":"186605449"},{"itemid":"23306","clock":"1528967006","value":"0.1001","ns":"118345066"},{"itemid":"23306","clock":"1528966946","value":"0.1001","ns":"45290570"}],"id":1}[root@kolla ~]# 

[root@kolla ~]#



zabbix下获取内存信息:根据(总内存-可用内存)/总内存,得到内存使用率

获取总的内存

[root@kolla ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"item.get","params":{"output":"itemids","hostids":"10084","search":{"key_":"vm.memory.size[total]"}},"auth":"7c4ba89691470b9c7018fa24206e71fd","id":0}' http://192.168.229.130/zabbix/api_jsonrpc.php

HTTP/1.1 200 OK

Date: Thu, 14 Jun 2018 09:57:49 GMT

Server: Apache/2.4.6 (CentOS) PHP/5.4.16

X-Powered-By: PHP/5.4.16

Access-Control-Allow-Origin: *

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: POST

Access-Control-Max-Age: 1000

Content-Length: 54

Content-Type: application/json


{"jsonrpc":"2.0","result":[{"itemid":"23317"}],"id":0}

根据返回的itemid获取内存总数































阅读更多
个人分类: OpenStack
上一篇openstack queens特性
下一篇kubespray安装kubernetes
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭