如何使用 check_graphite 插件进行 nagios 和 graphite 集成?

如何使用 check_graphite 插件进行 nagios 和 graphite 集成?

我尝试使用该check_graphite.py插件,但没有成功。
有人能指出我哪里做错了吗?

xxx@xxx-Aspire-5738:/usr/lib/nagios/plugins$ python check_graphite.py -u (http://127.0.0.1:8080/render/?width=586&height=308&_salt=1349434980.323&from=-10minutes&target=system.loadavg_1min) -w 2.25 -c 2.5  
[1] 11103  
[2] 11104  
[3] 11105  
[4] 11106  
Status Unknown: Graphite returned an empty list of values  
-w: command not found  
[1]   Exit 3 ....                 python check_graphite.py -u (http://127.0.0.1:8080/render/?width=586)   
[2]   Done       .....             height=308  
[3]-  Done            .....        _salt=1349434980.323  
[4]+  Done                 .....  from=-10minutes 

就这样结束了。有什么想法吗?

检查石墨.py

from NagAconda import Plugin
import urllib2

def f_avg(values):
  return sum(values)/len(values);

def f_last(values):
  return values[-1]

def f_min(values):
  return min(values)

def f_max(values):
  return max(values)

functionmap = {
  "avg":{  "label": "average", "function": f_avg },
  "last":{ "label": "last",    "function": f_last },
  "min":{  "label": "minimum", "function": f_min },
  "max":{  "label": "maximum", "function": f_max },
}

graphite = Plugin("Plugin to retrieve data from graphite", "1.0")
graphite.add_option("u", "url", "URL to query for data", required=True)
graphite.add_option("U", "username", "User for authentication")
graphite.add_option("P", "password", "Password for authentication")
graphite.add_option("H", "hostname", "Host name to use in the URL")
graphite.add_option("n", "none", "Ignore None values: 'yes' or 'no' (default no)")
graphite.add_option("f", "function", "Function to run on retrieved values: avg/min/max/last (default 'avg')",
  default="avg")

graphite.enable_status("warning")
graphite.enable_status("critical")
graphite.start()

if graphite.options.username and graphite.options.password:
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None,uri=graphite.options.url,
                            user=graphite.options.username,
                            passwd=graphite.options.password)
    auth_handler =urllib2.HTTPBasicAuthHandler(password_mgr)
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)

if graphite.options.hostname:
  graphite.options.url = graphite.options.url.replace('@HOSTNAME@', 
    graphite.options.hostname.replace('.','_'))

usock = urllib2.urlopen(graphite.options.url)
data = usock.read()
usock.close()

if graphite.options.function not in functionmap:
  graphite.unknown_error("Bad function name given to -f/--function option: '%s'" % graphite.options.function)

pieces = data.split("|")
counter = pieces[0].split(",")[0]
values = pieces[1].split(",")[:-1]

if graphite.options.none == 'yes':
  values = map(lambda x: float(x), filter(lambda x: x != 'None', values))
else:
  values = map(lambda x: 0.0 if x == 'None' else float(x), values)
if len(values) == 0:
  graphite.unknown_error("Graphite returned an empty list of values")
else:
  value = functionmap[graphite.options.function]["function"](values)

graphite.set_value(counter, value)
graphite.set_status_message("%s value of %s: %f" % (functionmap[graphite.options.function]["label"], counter, value))

graphite.finish()

答案1

不要用括号括起渲染 URL,请尝试以下步骤。

python check_graphite.py -u "http://127.0.0.1:8080/render/?width=586&height=308&_salt=1349434980.323&from=-10minutes&target=system.loadavg_1min" -w 2.25 -c 2.5

相关内容