#!/usr/bin/env python

import os
from munin import MuninPlugin
import mysql.connector
import random



conn = mysql.connector.Connect(host='localhost',user='root',\
                        password='smx',database='meteopy')


class LoadAVGPlugin(MuninPlugin):
    title = "Humitat"
    args = ""
#    args = "--base 1000"
    vlabel = "valor"
    category = "meteopy"

    @property

    def fields(self):
        warning = os.environ.get('load_warn', 100)
        critical = os.environ.get('load_crit', 2000)
        return [("humitat", dict(
                label = "humitat",
                info = 'humitat recollida per el sensor.',
                type = "GAUGE",
                min = "0",
                warning = str(warning),
                critical = str(critical)))]

    def execute(self):
        c = conn.cursor()
        conn.commit()

    	c.execute ("""SELECT random2 FROM random2 WHERE ID= 1""")

        for row in c:
            resultat=(row)

        final=str(resultat)[3:-3]
        #print final

        aleatori=(random.uniform(-1, 1))/20

        ale=float(aleatori)+float(final)
        #print ale

        c.execute("UPDATE random2 SET random2=" + str(ale) + " WHERE ID=1")
        conn.commit()



        return dict(humitat=final)

if __name__ == "__main__":
    LoadAVGPlugin().run()
