• zabbix监控之自定义item


    zabbix安装完成后,当需要使用自定义脚本构建自定义item必须注意以下几点:

    1.首先使用zabbix_get手动在zabbix-server服务端获取监控的默认的item值,如下:

    [root@aliyun ~]# zabbix_get -s 39.106.219.238 -p 10050 -k "system.swap.size[,free]"
    0
    

     当遇到zabbix_get命令没有找到时,解决办法:yum install -y zabbix_get

    2.在zabbix-server端打开配置文件找到:

    AlertScriptsPath=/usr/lib/zabbix/alertscripts

    默认将自定义的脚本放在该位置

    3.在zabbix-agent端打开配置文件找到:

    Include=/usr/local/etc/zabbix_agentd.conf.d/.conf

    确认上面几步后,现在开始创建自定义脚本创建自定义item:

    1.编写item脚本:

    #!/usr/bin/env python
    import MySQLdb
    
    class Db:
        def __init__(self, host1, user1, password1, db1):
            self.host = host1
            self.user = user1
            self.password = password1
            self.db = db1
            try:
                self._conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password, db=self.db)  
            except Exception as e:
                print e
            self._cursor = self._conn.cursor()  
    
        def fetchone(self, sql1):
            self._cursor.execute(sql1)   
            result = self._cursor.fetchone()   
            return result
    
    
    host = '172.16.23.131'
    user = 'root'
    db = 'mysql'
    password = 'redhat'
    sql = "show global status like 'questions';"
    sql1 = "show global status like 'uptime';"
    if __name__ == '__main__':
        db_conn = Db(host, user, password, db)
        result = db_conn.fetchone(sql)
        result1 = db_conn.fetchone(sql1)
        print int(result[1])/int(result1[1])
    

     手动执行:

    [root@zabbix-server zabbix_agentd.d]# python /usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py 
    7
    

     将该脚本赋予执行权限:chmod +x /usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py

    2.在zabbix-agent端编写配置文件:重启zabbix-agent服务

    /etc/zabbix/zabbix_agentd.d/userparameter_script.conf

    [root@zabbix-server zabbix_agentd.d]# cat userparameter_script.conf 
    UserParameter=script.getQueryCountFromMysql.py,/usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py
    

     格式:UserParameter=key,command

    手动在zabbix-server端获取值:

    [root@zabbix-server zabbix_agentd.d]# zabbix_get -s 172.16.23.131 -k script.getQueryCountFromMysql.py 
    7
    

     手动获取到值后,在zabbix页面进行配置:

    1.创建自定义template:

    2.创建自定义item:

    3.创建自定义graph:

    上面各步骤创建完成后,将此template加在host上:

     过一段时间后,查看graphs:

     可以看出自定义item的值已经可以显示出来了

     如果自定义脚本带有参数的话,做如下修改:

    [root@zabbix-server alertscripts]# cat /usr/lib/zabbix/alertscripts/getmysqlstatu.py 
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    import MySQLdb
    import sys
    
    class Db:
        def __init__(self, host1, user1, password1, db1):
            self.host = host1
            self.user = user1
            self.password = password1
            self.db = db1
            try:
                self._conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password, db=self.db)  # 连接mysql
            except Exception as e:
                print e
            self._cursor = self._conn.cursor()  # 创建游标
    
        def fetchone(self, sql1):
            self._cursor.execute(sql1)   # 执行sql语句
            result = self._cursor.fetchone()    # 获取执行的结果
            return result
    
        def getops(self):
            questions = db_conn.fetchone(sql_questions)
            uptime = db_conn.fetchone(sql_uptime)
            return int(questions[1])/int(uptime[1])
    
    
    host = '127.0.0.1'
    user = 'root'
    db = 'mysql'
    password = 'redhat'
    sql_questions = "show global status like 'questions';"
    sql_uptime = "show global status like 'uptime';"
    if __name__ == '__main__':
        db_conn = Db(host, user, password, db)
        result = db_conn.getops()
        if sys.argv[1] == 'qps':
            print result
    
    # chmod +x getmysqlstatu.py
    

     然后在zabbix-agent做下面配置:

    [root@zabbix-server alertscripts]# cat /etc/zabbix/zabbix_agentd.d/userparameter_script.conf 
    UserParameter=script.getQueryCountFromMysql.py,/usr/lib/zabbix/alertscripts/getQueryCountFromMysql.py
    UserParameter=script.getmysqlstatu.py[*],/usr/lib/zabbix/alertscripts/getmysqlstatu.py $1
    

     在zabbix-server端手动获取item的值:

    [root@zabbix-server alertscripts]# zabbix_get -s 172.16.23.131 -k script.getmysqlstatu.py[qps]
    7
    

     获取qps和tps双指标:

    [root@zabbix-server alertscripts]# cat getmysqlstatu.py 
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    import MySQLdb
    import sys
    
    class Db:
        def __init__(self, host1, user1, password1, db1):
            self.host = host1
            self.user = user1
            self.password = password1
            self.db = db1
            try:
                self._conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.password, db=self.db)  # 连接mysql
            except Exception as e:
                print e
            self._cursor = self._conn.cursor()  # 创建游标
    
        def fetchone(self, sql1):
            self._cursor.execute(sql1)   # 执行sql语句
            result = self._cursor.fetchone()    # 获取执行的结果
            return result
    
        def getqps(self):
            questions = db_conn.fetchone(sql_questions)
            uptime = db_conn.fetchone(sql_uptime)
            return int(questions[1])/int(uptime[1])
    
        def gettps(self):
            Com_rollback = db_conn.fetchone(sql_Com_rollback)
            Com_commit = db_conn.fetchone(sql_Com_commit)
            uptime = db_conn.fetchone(sql_uptime)
            return (int(Com_rollback[1])+int(Com_commit[1]))/int(uptime[1])
    
    
    host = '127.0.0.1'
    user = 'root'
    db = 'mysql'
    password = 'redhat'
    sql_questions = "show global status like 'questions';"
    sql_uptime = "show global status like 'uptime';"
    sql_Com_rollback = "show global status like 'Com_rollback';"
    sql_Com_commit = "show global status like 'Com_commit';"
    if __name__ == '__main__':
        db_conn = Db(host, user, password, db)
        if sys.argv[1] == 'qps':
            result_qps = db_conn.getqps()
            print result_qps
        if sys.argv[1] == 'tps':
            result_tps = db_conn.gettps()
            print float(result_tps)
    

      

  • 相关阅读:
    配置类【RibbonAutoConfiguration】的实例属性【configurations】是如何注入的?---实例化
    配置类【RibbonAutoConfiguration】的实例属性【configurations】是如何注入的?---配置类来源
    为什么启动类被【@SpringBootApplication】注解后,就会自动扫描其包内所有被【@Component】注解的类?
    自定义Ribbon配置时出现自定义的扫描排除注解【excludeFilters】不生效
    Spring
    eureka关闭crsf过滤
    docker-compose
    eureka
    centos7根目录扩容的一些理解
    Error injecting: org.apache.maven.artifact.installer.DefaultArtifactInstaller com.google.inject.ProvisionException: Unable to provision
  • 原文地址:https://www.cnblogs.com/jsonhc/p/10089873.html
Copyright © 2020-2023  润新知