Ad Widget

Collapse

Strange results from event.get

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nixboi
    Junior Member
    • Jan 2018
    • 1

    #1

    Strange results from event.get

    I have had this strange problem recently with event.get API call. What I'm trying to do, I'm trying to get the time of router being unavailable by ICMP (for every host in a group). What I have understood, is that clock indicates the starting time of an event in unix timestamp, and lastchange indicates the last time when the event was changed. So, lastchange - clock should result in downtime in seconds, which is exactly what I want. Most of time this works fine, and the downtime received by my code is exactly the same that is in Zabbix's GUI. But then sometimes the values my code fetches, indicate millions of seconds of downtime, which is not true. Also, I can't see that much downtime in GUI.

    I'm pretty sure there's something that I don't understand here. My guess is it goes wrong somehow when I try to select an event based on its templateid (in my code that 13554). I'm using the templateid field because I noticed that triggerid is different for every host. Also what is interesting, the templateid I'm using in my code is actually "Unavailable by ICMP" template's triggerid, according to address bar.

    So, any idea what I'm doing wrong here? How could I get that downtime from my routers using the API? Any better methods than mine? The code is below.

    Code:
    #!/usr/bin/python3.5 -W ignore
    import sys
    import requests
    import datetime
    import re
    
    try:
    	time_from = sys.argv[1]
    	time_till = sys.argv[2]
    	unix_from = int(time.mktime(datetime.datetime.strptime(time_from, "%d/%m/%Y").timetuple()))
    	unix_till = int(time.mktime(datetime.datetime.strptime(time_till, "%d/%m/%Y").timetuple()))
    except IndexError:
    	print("Usage: python get_sla.py <from_day> <to_day>")
    	sys.exit()
    
    start_time = re.sub("/", ".", time_from)
    end_time = re.sub("/", ".", time_till)
    
    sla_file = "sla_export_" + str(start_time) + "-" + str(end_time) + ".csv"
    
    with open(sla_file, "w") as slaf:
    	slaf.write("host,dt_seconds\n")
    
    
    def get_token():
    	SESSION = requests.Session()
    	try:
    		payload = {
    			"jsonrpc": "2.0",
    			"method": "user.login",
    			"params": {
    				"user": "myuser",
    				"password": "mypassword"
    			},
    			"id": 1
    		}
    
    		URL = "https://xxxxxxxx/xxx/api_jsonrpc.php"
    		r = SESSION.post(URL, json=payload, verify=False)
    		if r.status_code == 200:
    			return r.json()['result']
    		else:
    			print("Username or password provided are incorrect")
    	except IndexError:
    		print("Something went wrong")
    
    
    
    def get_values(token, unix_from, unix_till, sla_file):
    
    	session = requests.Session()
    
    	payload = {
    			"jsonrpc": "2.0",
    			"method": "event.get",
    			"params": {
    				"output": "extend",
    				"selectRelatedObject": "extend",
    				"selectHosts": "extend",
    				"groupids": "9",
    				"time_from": unix_from,
    				"time_till": unix_till,
    			},
    			"id": 9,
    			"auth": token
    		}
    
    	url = "https://xxxxxxxx/xxx/api_jsonrpc.php"
    	r = session.get(url, json=payload, verify=False)
    
    	if r.status_code == 200:
    		count = 0
    		temp_group = r.json()['result']
    
    		try:
    			while temp_group:
    				data = temp_group[count]['relatedObject']['templateid']
    				if data == "13554":
    					clock = temp_group[count]['clock']
    					lchange = temp_group[count]['relatedObject']['lastchange']
    					hostname = temp_group[count]['hosts'][0]['name']
    					duration = int(lchange) - int(clock)
    					if int(duration) != 0:
    						with open(sla_file, "a") as slaf:
    							slaf.write(hostname + "," + str(duration) + "\n")
    				count += 1
    		except IndexError:
    			pass
    	else:
    		print(r.status_code)
    
    get_values(get_token(), unix_from, unix_till, sla_file)
Working...