簡體   English   中英

使用zabbix監視Elasticsearch時出錯

[英]Error monitoring elasticsearch using zabbix

我有一個腳本是從githubs提取的,以使用zabbix監視我的elasticsearch服務器。 手動運行腳本時,出現以下錯誤:

File "/etc/zabbix/elasticsearch.py", line 15
"status",
^
SyntaxError: invalid syntax

我是python腳本的新手,所以不確定語法有什么問題。 這是python中的腳本:

#!/usr/bin/env python

import requests
import json
import sys
import os
import time

sender = '/usr/bin/zabbix_sender'      # path zabbix_sender
cfg = '/etc/zabbix/zabbix_agentd.conf' # path to zabbix-agent config
tmp = '/tmp/es_stats.tmp'              # temp file to use

# keys for health page
traps1 = {
  "status",
  "active_primary_shards",
  "active_shards",
  "unassigned_shards"
}

# keys for cluster stats page
traps2 = {
  "indices.docs.count",
  "indices.docs.deleted",
  "indices.flush.total",
  "os.mem.actual_used_in_bytes"
}

# read specified keys from json data
def getKeys(stats,traps):
  out=''
  for t in traps:
    c=t.split('.')
    s=stats
    while len(c): s=s.get(c.pop(0),{})
    if s=={}: continue
    out += "- es.{0} {1}\n".format(t,s)
  return out

def main():
  # load json data
  try:
    f = requests.get("http://localhost:9200/_cluster/health")
    health = f.json()
    f = requests.get("http://localhost:9200/_nodes/_local/stats?all=true")
    all = f.json()

    # only for current node
    for node_id in all['nodes']:
      if all['nodes'][node_id]['host'].startswith(os.uname()[1]):
        node = all['nodes'][node_id]
        if len(sys.argv) == 1:
          print "node found"
  except:
    print "Unable to load JSON data!"
    sys.exit(1)

  out = getKeys(health,traps1)  #getting health values
  out += getKeys(node,traps2)    #getting stats  values

  # write data for zabbix sender
  if len(sys.argv) == 1:
    print out

  try:
    with open(tmp,'w') as f: f.write(out)
  except:
    print "Unable to save data to send!"
    sys.exit(1)

  # return active check
  if len(sys.argv) > 1 and sys.argv[1] == 'jvm.uptime_in_millis':
    os.system("{0} -c {1} -i {2} >/dev/null 2>&1".format(sender,cfg,tmp))
    print node['jvm']['uptime_in_millis']
  # send data with debug
  else:
    os.system("{0} -c {1} -i {2} -vv".format(sender,cfg,tmp))

  os.remove(tmp)

if __name__ == "__main__":
    main()

謝謝你的幫助。

Python文件應具有正確的格式(縮進等)。 請下載此文件: https : //raw.githubusercontent.com/michalzubkowicz/Elasticsearch-Zabbix/master/zes.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM