Ad Widget

Collapse

Problem with Discovery Rule by custom script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • igogorevi4
    Junior Member
    • Nov 2014
    • 7

    #1

    Problem with Discovery Rule by custom script

    Hello, everyone!

    I'm trying to make discovery rule to add file size monitor.
    But I when add my Template to Host, zabbix says me Value should be a JSON object.


    Zabbix Agent (daemon) v2.2.10 (revision 54806) (10 August 2015)
    Zabbix server v2.2.9 (revision 52686) (12 March 2015)


    I've written the python-script:

    Code:
    import os
    import sys
    import json
    
    logdir = sys.argv[1]
    
    data = []
    
    for (logdir, _, files) in os.walk(logdir):
            for f in files:
                    if f.endswith(".log"):
                            path = os.path.join(logdir, f)
                            data.append({'#LOGFILEPATH':path})
                            jsondata = json.dumps(data)
    
    print jsondata
    It works fine and gets follows:

    HTML Code:
    [{"#LOGFILEPATH": "/opt/logs/projects/cms/cms.log"}, {"#LOGFILEPATH": "/opt/logs/projects/books/nginx.log"}]
    I've checked it by http://jsonlint.com/ - valid JSON.

    UserParameter in conf.d:

    HTML Code:
    UserParameter = discovery.logfile.path, python /opt/scripts/zabbix/find.logfile.path.and.size.py /opt/logs/
    There are attachments show my discovery configuration:

    Click image for larger version

Name:	1.PNG
Views:	1
Size:	16.5 KB
ID:	317355

    Click image for larger version

Name:	2.PNG
Views:	1
Size:	19.3 KB
ID:	317356



    User zabbix has permission to directory with script and logs.

    Please, help me to sort out.
  • gtj
    Junior Member
    • Sep 2015
    • 4

    #2
    I think you have to make the array a value with a key of "data".

    Code:
    print json.dumps({"data": data})
    so it produces...

    Code:
    { "data": [{"#LOGFILEPATH": "/opt/logs/projects/cms/cms.log"}, {"#LOGFILEPATH": "/opt/logs/projects/books/nginx.log"}] }

    Comment

    • igogorevi4
      Junior Member
      • Nov 2014
      • 7

      #3
      Originally posted by gtj
      have to make the array a value with a key of "data"
      Of course! gtj, thank you!

      Another one mistake: macro {#LOGFILEPATH} should be in brackets {}

      Comment

      • Daikazahn
        Member
        • Nov 2016
        • 69

        #4
        HI.

        I used you script, but item not created.
        Problem with Discovery Rule by custom script

        Code:
        UserParameter=discovery.logfile.path, python /usr/local/bin/find.logfile.path.and.size.py /var/log/zabbix
        Code:
        import os
        import sys
        import json
        
        logdir = sys.argv[1]
        
        data = []
        
        for (logdir, _, files) in os.walk(logdir):
                for f in files:
                        if f.endswith(".log"):
                                path = os.path.join(logdir, f)
                                data.append({'#LOGFILEPATH':path})
                                jsondata = json.dumps(data)
        
        print json.dumps({"data": data})
        ScreenShot
        i5.imageban.ru/out/2017/02/27/b1be1751183d3542695930db1dd76198.jpg
        i2.imageban.ru/out/2017/02/27/1807da10ed84dae2cb5bb44bce74e7ba.jpg
        i5.imageban.ru/out/2017/02/27/ef2828eb2aa38047e155688b8c76d2bb.jpg
        i1.imageban.ru/out/2017/02/27/c16ba23227354b37e27621322c5de3a4.jpg

        zabbix server get data.
        Code:
         29892:20170227:124701.595 get value from agent result: '{"data": [{"#LOGFILEPATH": "/var/log/zabbix/zabbix_agentd.log"}]}'
        Tell me, please, why not create new generated item?

        Comment

        • nobodysu
          Member
          • Sep 2016
          • 84

          #5
          Code:
                          if f.endswith(".log"):
                                  path = os.path.join(logdir, f)
                                  data.append({'{#LOGFILEPATH}':path})
          
          print json.dumps({"data": data})
          As been said - curly brackets.

          Comment

          • Daikazahn
            Member
            • Nov 2016
            • 69

            #6
            Thanks!! It Works

            Comment

            Working...