#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pycurl
import sgmllib

class MyParser(sgmllib.SGMLParser):
    "A simple parser class."
    
    def __init__(self, verbose=0):
        "Initialise an object, passing 'verbose' to the superclass."
        self.inside_a_element = 0
        sgmllib.SGMLParser.__init__(self, verbose)
        self.current_header = ""
        self.descriptions = []
	
    def parse(self, s):
        "Parse the given string 's'."
        self.feed(s)
        self.close()
        
    def start_td(self, attributes):
        "Process a hyperlink and its 'attributes'."
        for name, value in attributes:
          if name == "headers":
            if value !="comp" and value != "src" and value != "fan":
              self.current_header = value
        self.inside_a_element = 1
    def end_td(self):
	self.current_header = ""
        self.inside_a_element = 0
    def get_hyperlinks(self):
        "Return the list of hyperlinks."

        return self.hyperlinks
        
    def handle_data(self, data):
        "Handle the textual 'data'."

        if self.inside_a_element:
            self.descriptions.append({'name':self.current_header.replace(" ","").replace("volt","").replace("+","").replace(".","_"),'data':data.strip().replace("+ ","").replace("+","").replace("%","")})
    def get_descriptions(self):
        "Return a list of descriptions."
        results = []
        for pair in self.descriptions:
		if pair['name'] !='':
			results.append(pair)
        return results
class Test:
	def __init__(self):
		self.contents = ''

	def body_callback(self, buf):
		self.contents = self.contents + buf
def connect():
	c = pycurl.Curl()
	c.setopt(pycurl.USERPWD, "%s:%s" % ('munin', 'munin1'))
	c.setopt(c.URL, 'http://192.168.0.5/')
	c.setopt(c.WRITEFUNCTION, t.body_callback)
	c.perform()
	c.setopt(c.URL, 'http://192.168.0.5/private/welcome.ssi')
	c.setopt(c.WRITEFUNCTION, t.body_callback)
	c.perform()
	c.setopt(c.URL, 'http://192.168.0.5/private/syshealth.ssi')
	c.setopt(c.WRITEFUNCTION, t.body_callback)
	c.perform()
	c.close()

try:
	if sys.argv[1] == "config":
		print """multigraph temps
graph_title System Tempuratures
graph_vlabel Temp in C \${graph_period}
graph_category System Status
graph_info This graph shows the System Tempurature
_cpu1temp.label CPU1
_cpu2temp.label CPU2
_dasd1temp.label DASD
_ambtemp.label Ambient

multigraph voltages
graph_title System Voltages
graph_vlabel Voltage
graph_category System Status
graph_info This graph shows the systems power supply voltages
_5v.label +5v
_3_3v.label +3.3v
_12v.label +12v
_2_5v.label +2.5v
_1_5v.label +1.5v
_vrm1.label VRM1
_vrm2.label VRM2

multigraph fans
graph_title Fan Speeds
graph_args --upper-limit 100 -l 0
graph_scale no
graph_vlabel %
graph_category System Status
graph_info This graph shows the systems fan speed
_fan1speed.label Fan1
_fan2speed.label Fan2
_fan3speed.label Fan3
_fan4speed.label Fan4
_fan5speed.label Fan5
_fan6speed.label Fan6
_fan7speed.label Fan7
_fan8speed.label Fan8
"""

except:
	t = Test()
	while(1):
		try:
			connect()
			break
		except:
			pass

	myparser = MyParser()
	myparser.parse(t.contents)
	print "multigraph temps"
	for pair in myparser.get_descriptions():
		if pair['name'] == "5v":
			print "multigraph voltages"
		elif pair['name'] == "fan1speed":
			print "multigraph fans"
		print "_%s.value %s"%(pair['name'],pair['data'])
