Zabbix服务器监控系统部署之自定义监控项的添加及配置,因为我用到只是监控m


上一篇文章(Zabbix分布式服务器监控系统安装及部署)记录了Zabbix整套系统的安装及部署,其中包含Server端和client段,主要是说安装及配置的事儿了,本文章主要记录Zabbix的自定义监控项配置。

因为我用到只是监控mysql的一些数据等信息,所以也只记录这一块儿的,按照官方帮助文档中的说法,Zabbix提供了mysql的监控的模板,需要用户自己下载导入,官方下载地址。

下载后在Zabbixweb管理页面中导入,Configuration— Templates — Import Templates.选择相应xml模板导入即可。

Zabbix服务器监控系统部署之自定义监控项的添加及配置(二)

编辑 etc/zabbix 下的配置文件,zabbix_agentd.conf 最下方,将以下内容前面的#去掉,记得将mysql的账号跟密码换掉:

UserParameter=mysql.ping,mysqladmin -uroot -ppasswd  ping|grep alive|wc -l
UserParameter=mysql.uptime,mysqladmin -uroot -ppasswd  status|cut -f2 -d":"|cut -f1 -d"T"
UserParameter=mysql.threads,mysqladmin -uroot -ppasswd  status|cut -f3 -d":"|cut -f1 -d"Q"
UserParameter=mysql.questions,mysqladmin -uroot -ppasswd  status|cut -f4 -d":"|cut -f1 -d"S"
UserParameter=mysql.slowqueries,mysqladmin -uroot -ppasswd  status|cut -f5 -d":"|cut -f1 -d"O"
UserParameter=mysql.qps,mysqladmin -uroot -ppasswd  status|cut -f9 -d":"
UserParameter=mysql.version,mysql -V

以上自带的监控参数比较简单。若不够的话,可以将在上述目录下执行以下命令:

vi mysql_status.py

创建这样一个新的文件,在里面输入以下内容:

#!/usr/bin/env python     
# -*- coding: utf-8 -*-     
#file:mysql_status.py     
import MySQLdb,sys     
user = 'root'
passwd = 'passwd'
a = sys.argv[1]     
try:     
    conn = MySQLdb.connect(host = '127.0.0.1',user = user,passwd = passwd,connect_timeout = 2)     
    cursor = conn.cursor()     
    sql = "SHOW STATUS"
    cursor.execute(sql)     
    alldata = cursor.fetchall()     
    for data in alldata:     
        if data[0] == a :     
            #print data[0],data[1]     
            print data[1]     
            break
    cursor.close()     
    conn.close()     
except Exception, e:       
    print e       
    sys.exit()     
 
'''''Open_tables     
Opened_tables     
Max_used_connections     
Threads_connected     
Qcache_free_blocks     
Qcache_total_blocks     
Handler_read_first     
Handler_read_key     
Handler_read_rnd_next     
Slow_queries'''

保存。然后在zabbix_agentd.conf下方加入以下内容:

UnsafeUserParameters=1
UserParameter=mysql.Open_tables,/etc/zabbix/mysql_status.py Open_tables     
UserParameter=mysql.Opened_tables,/etc/zabbix/mysql_status.py Opened_tables     
UserParameter=mysql.Max_used_connections,/etc/zabbix/mysql_status.py Max_used_connections     
UserParameter=mysql.Threads_connected,/etc/zabbix/mysql_status.py Threads_connected     
UserParameter=mysql.Qcache_free_blocks,/etc/zabbix/mysql_status.py Qcache_free_blocks     
UserParameter=mysql.Qcache_total_blocks,/etc/zabbix/mysql_status.py Qcache_total_blocks     
UserParameter=mysql.Handler_read_first,/etc/zabbix/mysql_status.py Handler_read_first     
UserParameter=mysql.Handler_read_key,/etc/zabbix/mysql_status.py Handler_read_key     
UserParameter=mysql.Handler_read_rnd_next,/etc/zabbix/mysql_status.py Handler_read_rnd_next     
UserParameter=mysql.Slow_queries,/etc/zabbix/mysql_status.py Slow_queries

接着便可以在web中添加图形之类的直观数据了。。。

下面记录添加自定义的监控,如:监控用户数的增长,或者某个端口的情况等…在zabbix_agentd.conf中,加入以下内容:

UserParameter=myhost.user,mysql  -u root -ppasswd -e  "use you_dbname;select count(*) from user" |tail -1

解释:

myhost.user 为Key,回头web设置中需要用到;-u root -ppasswd 为mysql的用户名密码;you_dbname 你要操作的数据库;select count(*) from user sql语句;
UserParameter=myhost.port1005,netstat -ant | sed -n '1,2!p'|awk '{ if($6!="LISTEN") {if(match($4,"'1005'")) c=c+1}} END{print c}'

以上一句为端口监听。将1005换成你需要监听的端口号就可以。

重启Zabbix-agentd服务。

sudo service zabbix-agent restart

然后可以在Zabbix Server服务器上测试下是否监听成功,命令:

zabbix_get -s 192.168.1.106 -k myhost.user

若成功的话,便会显示出查得的数据了。接着在 web中添加items,操作步骤,找到:Configuration —— Hosts —— 点击相应host的itmes,进入后会列出当前该主机的itmes列表,点击右上角Create item。

Zabbix服务器监控系统部署之自定义监控项的添加及配置(二)

完成后,在创建图表的时候,便可以选择这个监控项了。

相关内容