Hi Guys,

I know that it is not a big help, but I have unsucessfully tried to
profile code of munin-cgi-html so I have written small quick and dirty
cgi script in python, which does everything what we want (we can
navigate through graphs, view the same as in munin, have dynamic zoom
enabled and ... have fast navigation even that we have about 400
servers). If you want, you can try.

BR,

Ondřej Kunc
#!/usr/bin/python
MUNIN_DB='/var/lib/munin/'
import cgi
import cgitb
import os,os.path
import time
cgitb.enable()

CGI_DYNAZOOM_URL='/static/dynazoom.html?cgiurl_graph=/munin-cgi/munin-cgi-graph&plugin_name=%s&start_epoch=%d&stop_epoch=%d&size_x=800&size_y=400'
CGI_GRAPH_URL='/munin-cgi/munin-cgi-graph/%s/%s/%s-%s.png'

def getgraphlist():
	for i in open(os.path.join(MUNIN_DB,'graphs'),'r').readlines():
		x=i.strip().split('/')[1:]
		if x[2].endswith('-day.png'):
			yield [x[0],x[1],x[2][:-8]]
		if len(x) > 3:
			if x[3].endswith('-day.png'):
				yield [x[0],x[1],x[2],x[3][:-8]]


def listgroups():
	last=None
	for i in getgraphlist():
		if last!=i[0]:
			print '<li>',genLink(i[0])
			last=i[0]

def listhosts(group):
	last=None
	print '%s<hr>'%group
	for i in getgraphlist():
		if i[0]==group:
			if last!=i[1]:
				print '<li>',genLink(i[0],i[1])
				last=i[1]

def listservices(group,host,showgraph=True):
	last=None
	print '%s:%s<hr>'%(group,host)
	for i in getgraphlist():
		if i[0]==group and i[1]==host:
			if len(i) == 3:
				print genLink(i[0],i[1],i[2],showgraph=showgraph),'<br>'
			elif len(i) == 4:
				print genLink(i[0],i[1],i[2]+'/'+i[3],showgraph=showgraph),'<br>'

def getgraphtag(group,host,service,period='day'):
	link=CGI_GRAPH_URL%(group,host,service,period)
	return '<img src="%s">'%link

def showservice(group,host,service):
	t=int(time.time())
	print '<a href="%s">%s</a>'%(CGI_DYNAZOOM_URL%('/'.join((group,host,service)),t-86400,t),getgraphtag(group,host,service))
	print '<a href="%s">%s</a>'%(CGI_DYNAZOOM_URL%('/'.join((group,host,service)),t-86400*7,t),getgraphtag(group,host,service,'week'))
	print "<br>"
	print '<a href="%s">%s</a>'%(CGI_DYNAZOOM_URL%('/'.join((group,host,service)),t-86400*28,t),getgraphtag(group,host,service,'month'))
	print '<a href="%s">%s</a>'%(CGI_DYNAZOOM_URL%('/'.join((group,host,service)),t-86400*365,t),getgraphtag(group,host,service,'year'))
def genLink(group='',host='',service='',fullname=False,showgraph=False):
	name=''
	link=''
	if group:
		link="?group=%s"%group
		if fullname or not host:
			name=group
	if host:
		link+="&host=%s"%host
		if fullname or not service:
			name+=':%s'%host
	if service:
		link+="&service=%s"%service
		name+=':%s'%service
	tag=''
	if showgraph:
		tag="<br>%s"%getgraphtag(group,host,service)
		tag+=getgraphtag(group,host,service,'week')
	return "<a href='%s'>[%s]%s</a>"%(link,name,tag)

print "Content-Type: text/html;charset=utf-8\r\n\r\n"
print '<b><a href="?index">Ignum FastMunin</a></b> <small>by Ondrej Kunc</small><br>'
fs=cgi.FieldStorage()
grp=fs.getfirst('group')
host=fs.getfirst('host')
service=fs.getfirst('service')

if not grp:
	listgroups()
elif not host:
	listhosts(grp)
elif not service:
	listservices(grp,host)
else:
	showservice(grp,host,service)

Reply via email to