本文介绍了zabbix api 从当前时间获取 24 小时前的项目值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个可以进行一些分析的 Python 脚本.此脚本使用以下 zabbix api 获取项目的最后一个值:

Im working on a python script that does some analysis. This script uses the following zabbix api to get the last value of an item:

getlastvalue = {
   "jsonrpc":"2.0",
   "method":"item.get",
   "params":{
      "output":"extend",
      "hostids":"10084",
      "search":{
         "key_":"vfs.fs.size[/var/log,used]"
      },
      "sortfield":"name"
   },
   "auth":mytoken,
   "id":1
}

我的脚本分析响应并产生此反馈:

My script analyzes the response and produces this feedback:

LatestValue:499728384 LatestValueEpoch:1553573850 HowLongAgo:33secs ItemID:51150

现在,我想知道该项目 24 小时前的价值是多少……意思是从 LatexValueEpoch 时间算起 24 小时.这是我遇到问题的地方.我想我可能没有使用正确的 json.但这是我一直在使用的:

Now, I wish to know what the value of the item was 24 hours ago...meaning 24 hours from the LatestValueEpoch time. This is where Im having an issue. I think I may not be using the right json. But here's what I've been using:

historyget = {
   "jsonrpc":"2.0",
   "method":"history.get",
   "params":{
      "output":[
         "itemid",
         "extend"
      ],
      "time_from":"",
      "time_to":"",
      "itemids":[
         "51150"
      ]
   },
   "auth":mytoken,
   "id":1
}

我在脚本中替换了 time_fromtime_to 的值以反映昨天的时间(24 小时前正好从当前时间开始).但我得到的回应不是我想要的.我在这里做错了什么?

I replace the value of time_from and time_to in my script to reflect yesterday's time (24 hours ago exactly from the current time). But the response I get isnt what I want. What am i doing wrong here?

推荐答案

你必须使用 history.get API 调用.

You have to use the history.get API call.

使用 time_fromtime_tilllimit 的组合,您应该相应地获得一组值或单个值.

Using a combination of time_from, time_till and limit you should get an array of values or a single value accordingly.

重要:您必须在 history.get 调用中指定 history 参数(要返回的历史对象类型):我通常会一个 item.get 来捕捉我需要的东西,然后是一个 history.get.

Important: You have to specify the history parameter (History object types to return) in the history.get call: I usually make an item.get to catch the stuff I need, then an history.get.

我作为帮手编写的一个小python示例:

A small python sample I wrote as a helper:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Get history values for specific items in a time range:

# ./getItemHistoryByName.py -H some-host  -I "ICMP response time" -f "26/6/2018 16:00" -t "27/6/2018 23:59"
ItemID: 77013 - Item: ICMP response time - Key: icmppingsec
1530021641 26/06/2018 16:00:41 Value: 0.1042
1530021701 26/06/2018 16:01:41 Value: 0.0993
1530021762 26/06/2018 16:02:42 Value: 0.1024
1530021822 26/06/2018 16:03:42 Value: 0.0966
[cut]
"""

from zabbix.api import ZabbixAPI
import sys, argparse
import time
import datetime


zabbixServer    = 'http://yourserver/zabbix/'
zabbixUser      = 'someuser'
zabbixPass      = 'somepass'


def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('-H', required=True, metavar='Hostname')
    parser.add_argument('-I', required=True, metavar='Item Name')
    parser.add_argument('-f', required=True, metavar='From Timestamp')
    parser.add_argument('-t', required=True, metavar='Till Timestamp')

    args = parser.parse_args()


    zapi = ZabbixAPI(url=zabbixServer, user=zabbixUser, password=zabbixPass)

    fromTimestamp = time.mktime(datetime.datetime.strptime(args.f, "%d/%m/%Y %H:%M").timetuple())
    tillTimestamp = time.mktime(datetime.datetime.strptime(args.t, "%d/%m/%Y %H:%M").timetuple())


    f  = {  'name' : args.I  }
    items = zapi.item.get(filter=f, host=args.H, output='extend' )

    for item in items:
        print "ItemID: {} - Item: {} - Key: {}".format(item['itemid'], item['name'], item['key_'])

        values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, history=item['value_type'])

        for historyValue in values:
            currentDate = datetime.datetime.fromtimestamp(int(historyValue['clock'])).strftime('%d/%m/%Y %H:%M:%S')

            print "{} {} Value: {}".format(historyValue['clock'], currentDate, historyValue['value'])

if __name__ == "__main__":
   main(sys.argv[1:])

这篇关于zabbix api 从当前时间获取 24 小时前的项目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:28