Zabbix Top10页面的制作

github项目地址: https://github.com/zhangrj/Zabbix-Top10

备注:该项目代码粗糙,实现方式(sql查询+数据展示)粗暴危险,不要用于对外开放的生产环境中。以后有时间我再进行改进。

1、项目结构及文件用途

TOP10
--include
------menu.inc.php.sample(web页面添加二级菜单的示例配置文件)
--js
------echarts.min.js(echarts图表库)
------jquery.min.js(使用ajax请求数据)
--top10
------top10_config.php(数据库配置文件)
------*.php(获取top10数据并转换为json格式,如需自定义top10数据,只需更改脚本中数据库查询语句的监控项key值即可)
--top10.php(top10展示页面)

2、使用方法

按Top10文件夹中的文件结构,将文件复制到zabbix-web服务器的/usr/share/zabbix/中,按照include文件夹中menu.inc.php.sample编辑/usr/share/zabbix/include/menu.inc.php,即在一级菜单“监测/Monitoring”下添加二级菜单“TOP10”:

[
    'url' => 'top10.php',
    'label' => _('TOP10')
],

编辑/top10/top10_config.php,填入数据库相应信息 。刷新web页面,即可看到如下效果:

3、工作原理

因为我对zabbix api不熟悉,所以需要查询top10时第一反应是从数据库查询,期望的top10数据应当包含以下信息:主机名称、监控项名称、最新监控数据。在观察了zabbix的表结构后,确认需要使用如下三个表:hosts、items、history。

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]> desc hosts;
+--------------------+---------------------+------+-----+---------+-------+
| Field              | Type                | Null | Key | Default | Extra |
+--------------------+---------------------+------+-----+---------+-------+
| hostid             | bigint(20) unsigned | NO   | PRI | NULL    |       |
| proxy_hostid       | bigint(20) unsigned | YES  | MUL | NULL    |       |
| host               | varchar(128)        | NO   | MUL |         |       |
| status             | int(11)             | NO   | MUL | 0       |       |
| disable_until      | int(11)             | NO   |     | 0       |       |
| error              | varchar(2048)       | NO   |     |         |       |
| available          | int(11)             | NO   |     | 0       |       |
| errors_from        | int(11)             | NO   |     | 0       |       |
| lastaccess         | int(11)             | NO   |     | 0       |       |
| ipmi_authtype      | int(11)             | NO   |     | -1      |       |
| ipmi_privilege     | int(11)             | NO   |     | 2       |       |
| ipmi_username      | varchar(16)         | NO   |     |         |       |
| ipmi_password      | varchar(20)         | NO   |     |         |       |
| ipmi_disable_until | int(11)             | NO   |     | 0       |       |
| ipmi_available     | int(11)             | NO   |     | 0       |       |
| snmp_disable_until | int(11)             | NO   |     | 0       |       |
| snmp_available     | int(11)             | NO   |     | 0       |       |
| maintenanceid      | bigint(20) unsigned | YES  | MUL | NULL    |       |
| maintenance_status | int(11)             | NO   |     | 0       |       |
| maintenance_type   | int(11)             | NO   |     | 0       |       |
| maintenance_from   | int(11)             | NO   |     | 0       |       |
| ipmi_errors_from   | int(11)             | NO   |     | 0       |       |
| snmp_errors_from   | int(11)             | NO   |     | 0       |       |
| ipmi_error         | varchar(2048)       | NO   |     |         |       |
| snmp_error         | varchar(2048)       | NO   |     |         |       |
| jmx_disable_until  | int(11)             | NO   |     | 0       |       |
| jmx_available      | int(11)             | NO   |     | 0       |       |
| jmx_errors_from    | int(11)             | NO   |     | 0       |       |
| jmx_error          | varchar(2048)       | NO   |     |         |       |
| name               | varchar(128)        | NO   | MUL |         |       |
| flags              | int(11)             | NO   |     | 0       |       |
| templateid         | bigint(20) unsigned | YES  | MUL | NULL    |       |
| description        | text                | NO   |     | NULL    |       |
| tls_connect        | int(11)             | NO   |     | 1       |       |
| tls_accept         | int(11)             | NO   |     | 1       |       |
| tls_issuer         | varchar(1024)       | NO   |     |         |       |
| tls_subject        | varchar(1024)       | NO   |     |         |       |
| tls_psk_identity   | varchar(128)        | NO   |     |         |       |
| tls_psk            | varchar(512)        | NO   |     |         |       |
| proxy_address      | varchar(255)        | NO   |     |         |       |
| auto_compress      | int(11)             | NO   |     | 1       |       |
+--------------------+---------------------+------+-----+---------+-------+
41 rows in set (0.00 sec)

MariaDB [zabbix]> desc items;
+-----------------------+---------------------+------+-----+---------+-------+
| Field                 | Type                | Null | Key | Default | Extra |
+-----------------------+---------------------+------+-----+---------+-------+
| itemid                | bigint(20) unsigned | NO   | PRI | NULL    |       |
| type                  | int(11)             | NO   |     | 0       |       |
| snmp_community        | varchar(64)         | NO   |     |         |       |
| snmp_oid              | varchar(512)        | NO   |     |         |       |
| hostid                | bigint(20) unsigned | NO   | MUL | NULL    |       |
| name                  | varchar(255)        | NO   |     |         |       |
| key_                  | varchar(255)        | NO   |     |         |       |
| delay                 | varchar(1024)       | NO   |     | 0       |       |
| history               | varchar(255)        | NO   |     | 90d     |       |
| trends                | varchar(255)        | NO   |     | 365d    |       |
| status                | int(11)             | NO   | MUL | 0       |       |
| value_type            | int(11)             | NO   |     | 0       |       |
| trapper_hosts         | varchar(255)        | NO   |     |         |       |
| units                 | varchar(255)        | NO   |     |         |       |
| snmpv3_securityname   | varchar(64)         | NO   |     |         |       |
| snmpv3_securitylevel  | int(11)             | NO   |     | 0       |       |
| snmpv3_authpassphrase | varchar(64)         | NO   |     |         |       |
| snmpv3_privpassphrase | varchar(64)         | NO   |     |         |       |
| formula               | varchar(255)        | NO   |     |         |       |
| error                 | varchar(2048)       | NO   |     |         |       |
| lastlogsize           | bigint(20) unsigned | NO   |     | 0       |       |
| logtimefmt            | varchar(64)         | NO   |     |         |       |
| templateid            | bigint(20) unsigned | YES  | MUL | NULL    |       |
| valuemapid            | bigint(20) unsigned | YES  | MUL | NULL    |       |
| params                | text                | NO   |     | NULL    |       |
| ipmi_sensor           | varchar(128)        | NO   |     |         |       |
| authtype              | int(11)             | NO   |     | 0       |       |
| username              | varchar(64)         | NO   |     |         |       |
| password              | varchar(64)         | NO   |     |         |       |
| publickey             | varchar(64)         | NO   |     |         |       |
| privatekey            | varchar(64)         | NO   |     |         |       |
| mtime                 | int(11)             | NO   |     | 0       |       |
| flags                 | int(11)             | NO   |     | 0       |       |
| interfaceid           | bigint(20) unsigned | YES  | MUL | NULL    |       |
| port                  | varchar(64)         | NO   |     |         |       |
| description           | text                | NO   |     | NULL    |       |
| inventory_link        | int(11)             | NO   |     | 0       |       |
| lifetime              | varchar(255)        | NO   |     | 30d     |       |
| snmpv3_authprotocol   | int(11)             | NO   |     | 0       |       |
| snmpv3_privprotocol   | int(11)             | NO   |     | 0       |       |
| state                 | int(11)             | NO   |     | 0       |       |
| snmpv3_contextname    | varchar(255)        | NO   |     |         |       |
| evaltype              | int(11)             | NO   |     | 0       |       |
| jmx_endpoint          | varchar(255)        | NO   |     |         |       |
| master_itemid         | bigint(20) unsigned | YES  | MUL | NULL    |       |
| timeout               | varchar(255)        | NO   |     | 3s      |       |
| url                   | varchar(2048)       | NO   |     |         |       |
| query_fields          | varchar(2048)       | NO   |     |         |       |
| posts                 | text                | NO   |     | NULL    |       |
| status_codes          | varchar(255)        | NO   |     | 200     |       |
| follow_redirects      | int(11)             | NO   |     | 1       |       |
| post_type             | int(11)             | NO   |     | 0       |       |
| http_proxy            | varchar(255)        | NO   |     |         |       |
| headers               | text                | NO   |     | NULL    |       |
| retrieve_mode         | int(11)             | NO   |     | 0       |       |
| request_method        | int(11)             | NO   |     | 0       |       |
| output_format         | int(11)             | NO   |     | 0       |       |
| ssl_cert_file         | varchar(255)        | NO   |     |         |       |
| ssl_key_file          | varchar(255)        | NO   |     |         |       |
| ssl_key_password      | varchar(64)         | NO   |     |         |       |
| verify_peer           | int(11)             | NO   |     | 0       |       |
| verify_host           | int(11)             | NO   |     | 0       |       |
| allow_traps           | int(11)             | NO   |     | 0       |       |
+-----------------------+---------------------+------+-----+---------+-------+
63 rows in set (0.01 sec)

MariaDB [zabbix]> desc history;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| itemid | bigint(20) unsigned | NO   | MUL | NULL    |       |
| clock  | int(11)             | NO   |     | 0       |       |
| value  | double(16,4)        | NO   |     | 0.0000  |       |
| ns     | int(11)             | NO   |     | 0       |       |
+--------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

以下是查询system.cpu.util[,idle] top10的sql语句:

SELECT hosts.name, items.name, b.value
FROM (
	SELECT *
	FROM (
		SELECT *
		FROM history
		WHERE itemid IN (
				SELECT itemid
				FROM items
				WHERE key_ = 'system.cpu.util[,idle]'
					AND hostid IN (
						SELECT hostid
						FROM hosts
						WHERE available = 1
					)
			)
			AND clock IN (
				SELECT MAX(clock)
				FROM history
				GROUP BY itemid
			)
		ORDER BY clock
	) a
	GROUP BY a.itemid
	ORDER BY a.value
	LIMIT 10
) b
	INNER JOIN items ON items.itemid = b.itemid
	INNER JOIN hosts ON hosts.hostid = items.hostid;

如果需要查询其他监控项,更改sql中的”key_”值即可。

取得数据后剩下的就是数据可视化了,不再赘述。

4、存在问题

1、top10页面中的一级子菜单失效。因为我本身没有仔细研究zabbix的源代码和api,只是本着能用即可的想法来做的,所以很粗糙,等有时间了再进行改进吧,我想这个功能使用原生的zabbix api开发应该不难,且视觉效果更好。

2、数据库信息其实可以从/etc/zabbix/server.conf中读取,而无需再次配置。

3、Top10数据直接从数据库中读取(且查询语句效率不高,需要优化),很粗糙实现方式危险性也很大,好在我们的监控系统处在内网中,不建议将代码直接用于开放的生产环境中。

5、后记

我觉得zabbix没有自带top10功能是有些遗憾的,在系统出现故障时top10数据对排障是有重要帮助的。

发表评论

电子邮件地址不会被公开。