Python Forum
Erreur de code python sur un template rpm de zabbix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Erreur de code python sur un template rpm de zabbix
#1
Après avoir importé un template rpm zabbix sur un de mes hôtes j'ai cette erreur sur le code qui me bloque
English: After importing a zabbix rpm template on one of my hosts I have this error on the code that is blocking me

Invalid discovery rule value: cannot parse as a valid JSON object: invalid object format, expected opening character '{' or '[' at: 'delimiter nor 6 ot 10'

voici le code :

#!/usr/bin/python
"""
 Zabbix discovery junper rpm tests external script
 this script is intended to find RPM (Real test perfomance monitoring) owners and tests out of a juniper network devices

 the script accepts 2 arguments: hostname and community
 and returns a structured data back to a zabbix server

 first it does snmpwalk over jnxRpmResSumSent MIB Object
 from a device perspective it looks like this:

 user@ex2200> show snmp mib walk jnxRpmResSumSent ascii
 jnxRpmResSumSent."ISP1"."Test1".1 = 14
 jnxRpmResSumSent."ISP1"."Test1".2 = 15
 jnxRpmResSumSent."ISP1"."Test1".4 = 45614
 jnxRpmResSumSent."ISP2"."Test2".1 = 13
 jnxRpmResSumSent."ISP2"."Test2".2 = 15
 jnxRpmResSumSent."ISP2"."Test2".4 = 6073

 using jnxRpmResSumSent MIB Object we can find all existed tests. even those that are not performed successfully

 returned data are being structured to a form of a Zabbix discobery JSON
 { data: [
 {"{#RPMUUID}":"3.73.83.80.49.6.84.69.83.84.49", "{#RPMOWNER}":"ISP1", "{#RPMTEST}":"Test1" },
 {"{#RPMUUID}":"4.73.83.80.50.84.69.83.84.50", "{#RPMOWNER}":"ISP2", "{#RPMTEST}":"Test2" },
 ]}
"""

import sys
from pysnmp.hlapi import *
import json

def findsubstrings(s):
    la = s.split('.')
    lb = la[1:]

    if '6' in lb: # looking for a delimiter. 6 for EX or 8,10 for SRX
        i = lb.index('6')
    elif '8' in lb:
        i = lb.index('8')
    elif '10' in lb:
        i = lb.index('10')
    else:
        sys.stderr.write("delimiter nor 6 ot 10")
        exit(0)

    l1 = lb[:i]
    l2 = lb[i+1:]
    param2 = ''.join([chr(int(i)) for i in l1])
    param3 = ''.join([chr(int(i)) for i in l2])
    return param2, param3


eRR = '{ data: ["Error parsing arguments"]}\n'

if len(sys.argv)!=3:
    sys.stderr.write(eRR)
    exit()

hostname = sys.argv[1]
community=sys.argv[2]
jnxRpmResSumSent = "1.3.6.1.4.1.2636.3.50.1.2.1.2"
l = []

# init snmpwalk over jnxRpmResSumSent MIB Object
varBind = nextCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((hostname, 161)),
    ContextData(), ObjectType(ObjectIdentity(jnxRpmResSumSent)),
    lexicographicMode=False)

# do snmmpwalk and collect an rpm specific substring
for res in varBind:
    s = str(res[3][0][0])[len(jnxRpmResSumSent)+1:-2]
    l.append(s)

# lets make values inside the list l uniq
u = set(l)


jsonData=[]
for param1 in u:
    d={}
    param2, param3 = findsubstrings(param1)
    d["{#RPMUUID}"] = param1
    d["{#RPMOWNER}"] = param2
    d["{#RPMTEST}"] = param3
    jsonData.append(d)

print (json.dumps({"data": jsonData}, indent=4))
Larz60+ write Apr-02-2021, 01:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What is bug template using python Anldra12 0 970 Nov-02-2021, 11:36 AM
Last Post: Anldra12
  Python C++ Template Equivalent deanhystad 7 3,421 May-04-2021, 07:45 PM
Last Post: deanhystad
  Erreur Code access violation reading 0x00000004 yan_mhb 0 2,337 Jul-10-2020, 02:28 PM
Last Post: yan_mhb
  Erreur Code yan_mhb 2 2,129 Jul-09-2020, 03:50 PM
Last Post: Larz60+
  -> Help: erreur print cherry_cherry 4 2,728 Apr-09-2020, 05:05 AM
Last Post: cherry_cherry
  python Render template Akhou 2 2,692 Apr-26-2018, 10:48 PM
Last Post: Akhou

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020