码迷,mamicode.com
首页 > 移动开发 > 详细

Nagios监控ganglia的指标

时间:2015-12-29 12:54:04      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

这是nagios与ganglia整合的一部分内容 。

通常我们会把ganglia的监控发送给一个主机,我们可以在这个主机上执行nc localhost 8649 可以获取到所有发往这个主机的信息,以xml的形式查看。

nagios监控ganglia的指标的方法就是使用这个思路,check_ganglia.py

先连通ganglia_host,然后获取所有的xml,然后再解析xml,解析的时候找到匹配的主机名,然后再找到下面的metric和值,与你传进来的critical与waring的进行匹配,

然后判断后返回合适的状态。

感觉有点浪费,数据传来传去的。

#!/usr/bin/env python
 
import sys
import getopt
import socket
import xml.parsers.expat
 
class GParser:
  def __init__(self, host, metric):
    self.inhost =0
    self.inmetric = 0
    self.value = None
    self.host = host
    self.metric = metric
 
  def parse(self, file):
    p = xml.parsers.expat.ParserCreate()
    p.StartElementHandler = parser.start_element
    p.EndElementHandler = parser.end_element
    p.ParseFile(file)
    if self.value == None:
      raise Exception(Host/value not found)
    return float(self.value)
 
  def start_element(self, name, attrs):
    if name == "HOST":
      if attrs["NAME"]==self.host:
        self.inhost=1
    elif self.inhost==1 and name == "METRIC" and attrs["NAME"]==self.metric:
      self.value=attrs["VAL"]
 
  def end_element(self, name):
    if name == "HOST" and self.inhost==1:
      self.inhost=0
 
def usage():
  print """Usage: check_ganglia -h|--host= -m|--metric= -w|--warning= -c|--critical= [-s|--server=] [-p|--port=] """
  sys.exit(3)
 
if __name__ == "__main__":
##############################################################
  ganglia_host = x.x.x.x
  ganglia_port = 8649
  host = None
  metric = None
  warning = None
  critical = None
 
  try:
    options, args = getopt.getopt(sys.argv[1:],
      "h:m:w:c:s:p:",
      ["host=", "metric=", "warning=", "critical=", "server=", "port="],
      )
  except getopt.GetoptError, err:
    print "check_gmond:", str(err)
    usage()
    sys.exit(3)
 
  for o, a in options:
    if o in ("-h", "--host"):
       host = a
    elif o in ("-m", "--metric"):
       metric = a
    elif o in ("-w", "--warning"):
       warning = float(a)
    elif o in ("-c", "--critical"):
       critical = float(a)
    elif o in ("-p", "--port"):
       ganglia_port = int(a)
    elif o in ("-s", "--server"):
       ganglia_host = a
 
  if critical == None or warning == None or metric == None or host == None:
    usage()
    sys.exit(3)
        
  try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ganglia_host,ganglia_port))
    parser = GParser(host, metric)
    value = parser.parse(s.makefile("r"))
    s.close()
  except Exception, err:
    print "CHECKGANGLIA UNKNOWN: Error while getting value \"%s\"" % (err)
    sys.exit(3)
 
  if critical > warning:
    if value >= critical:
      print "CHECKGANGLIA CRITICAL: %s is %.2f" % (metric, value)
      sys.exit(2)
    elif value >= warning:
      print "CHECKGANGLIA WARNING: %s is %.2f" % (metric, value)
      sys.exit(1)
    else:
      print "CHECKGANGLIA OK: %s is %.2f" % (metric, value)
      sys.exit(0)
  else:
    if critical >=value:
      print "CHECKGANGLIA CRITICAL: %s is %.2f" % (metric, value)
      sys.exit(2)
    elif warning >=value:
      print "CHECKGANGLIA WARNING: %s is %.2f" % (metric, value)
      sys.exit(1)
    else:
      print "CHECKGANGLIA OK: %s is %.2f" % (metric, value)
      sys.exit(0)

测试:

python check_ganglia.py -h hostxx -m load_one -w 10 -c 100

Nagios监控ganglia的指标

标签:

原文地址:http://www.cnblogs.com/huaxiaoyao/p/5085108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!