Esempio n. 1
0
 def __init__(self,config):
     self.config=config
     self.controller=MainController(config)
     self.notifyclasses=self.controller.supportednotifications
     self.checkclasses=self.controller.supportedchecktypes
     self.fc=FunkyConsole()
Esempio n. 2
0
 def __init__(self,config):
     self.config=config
     self.controller=MainController(config)
     self.fc=FunkyConsole()
Esempio n. 3
0
class RunOnce(object):
    
    def __init__(self,config):
        self.config=config
        self.controller=MainController(config)
        self.notifyclasses=self.controller.supportednotifications
        self.checkclasses=self.controller.supportedchecktypes
        self.fc=FunkyConsole()
        
    def run(self,section):
        if section not in self.config.sections():
            print "section not found in config: %s"%section
            print "available sections are:"
            l=sorted(self.config.sections())
            l=[x for x in l if x.startswith("Check_") or x.startswith("Test_") or x.startswith("Notification_")]
            print "\n".join(l)
            sys.exit(1)
            
        if section.startswith("Notification_"):
            self.run_notify(section)
            sys.exit(0)
            
        if section.startswith("Check_"):
            self.run_check(section)
            sys.exit(0)
        
        if section.startswith("Test_"):
            self.run_test(section)
            sys.exit(0)
        
    
    def run_notify(self,section):
        
        ntype=self.config.get(section,"type")
        if ntype not in self.notifyclasses:
            print "unsupported type: %s"%ntype
            sys.exit(1)
            
        cls=self.notifyclasses[ntype]
        instance=cls()
        
        
        instance.section=section
        instance.recipient=self.config.get(section,'recipient').strip()
    
        defaultconfig=instance.configvars
        actualconfig={}
        
        for key,defaultvalue in defaultconfig.iteritems():
            defaultvalue=self.controller._get_value(self.config,"%s_default"%ntype,key,defaultvalue)
                                          
            if self.config.has_option(section,key):
                actualconfig[key]=self.config.get(section,key)
            else:
                if defaultvalue==None:
                    print "not all required variables configured. please run --lint"
                    sys.exit(1)
                else:
                    actualconfig[key]=defaultvalue
        instance.configvars=actualconfig
        
        print "Running notification test: %s (type %s)"%(section,ntype)
        instance.send_message("Monsta Notification Test",subject="[monsta] Notification test")
    
    
    def run_check(self,section):
        ctype=self.config.get(section,"type")
        if ctype not in self.checkclasses:
            print "unsupported type: %s"%ctype
            sys.exit(1)
            
        cls=self.checkclasses[ctype]
        instance=cls()


        defaultconfig=instance.configvars
        actualconfig={}
        for key,defaultvalue in defaultconfig.iteritems():
            defaultvalue=self.controller._get_value(self.config,"%s_default"%ctype,key,defaultvalue)

            if self.config.has_option(section,key):
                actualconfig[key]=self.config.get(section,key)
            else:
                if defaultvalue==None:
                    print "not all required variables configured. please run --lint"
                    sys.exit(1)
                else:
                    actualconfig[key]=defaultvalue
        instance.configvars=actualconfig
        
        print "Running check %s (type %s)"%(section,ctype)
        result=instance.performCheck()
        success,errors,stats=result
        if success:
            print self.fc.strcolor("CHECK OK", "green")
        else:
            print self.fc.strcolor("CHECK FAILED","red")
        
        if len(errors)>0:
            print "Errors:"
            print "\n".join(errors)
        
        if len(stats)>0:
            print "Statistics:"
            print "\n".join(["%s : %s"%(k,v) for k,v in stats.items()])
            

    def run_test(self,section):
        checks=self.config.get(section,"checks").split()
        print "Running all checks in %s "%section
        for check in checks:
            self.run_check("Check_%s"%check)
            print ""
Esempio n. 4
0
class MonstaHelp(object):
    def __init__(self,config):
        self.config=config
        self.controller=MainController(config)
        self.fc=FunkyConsole()
        
    def command(self,*args):
        """console command"""
        
            
        if len(args)==0:
            options=[
                ("--lint","lint config"),
                ("--version","show version"),  
                ("--help check","list available checks"), 
                ("--help check [type]","show help for checktype [type]"), 
                ("--help notification","list available notification types"), 
                ("--help notification [type]","show help for notificationtype"), 
                ("--run [sectionname]","run a Test/Check/notification once"), 
               
            ]
            for k,v in options:
                print self.fc.strcolor(k, [self.fc.MODE["bold"],]),
                print "\t",
                print v
            sys.exit(0)
        
        classtype=args[0]
        
        typedict=self.controller.supportedchecktypes
        notificationdict=self.controller.supportednotifications
        
        if classtype=="check":
            if len(args)==1: # list checks
                self._help_index("check", typedict)
                
            if len(args)==2:
                ctype=args[1]
                self._help_details("check", typedict, ctype)
                
        elif classtype=="notification":
            if len(args)==1: # list checks
                self._help_index("notification",notificationdict)
                
                
            if len(args)==2:
                ctype=args[1]
                self._help_details("notification", notificationdict, ctype)
        else:
            print "unknown help topic."
            sys.exit(1)
            
    def _help_index(self,typename,thedict):
        print self.fc.strcolor("Available %s types:"%typename,"yellow")
        for key in sorted(thedict.keys()):
            cls=thedict[key]
            docstr=cls.__doc__
            if docstr==None:
                docstr="(no description available)"
            else:
                docstr=self.fc.strcolor(docstr, [self.fc.MODE["bold"],])
            colorkey=self.fc.strcolor(key,"magenta")
            print "%s : %s"%(colorkey,docstr)
        print
        sys.exit(0)
    
    def _help_details(self,typename,thedict,value):
        if value not in thedict:
            print "No such %s type. run 'monsta --help %s' for a list"%(typename,typename)
            sys.exit(1)
        cls=thedict[value]
        info=self.help_string(cls)
        print info
        print
        sys.exit(0)

    
    def help_string(self,monstaclass):
        docstr=monstaclass.__doc__
        if docstr==None:
            docstr="(no description)"
        else:
            docstr=self.fc.strcolor(docstr, [self.fc.MODE["bold"],])
        instance=monstaclass()
        vardict=instance.configvars
        varnames=sorted(vardict.keys())
        
        helpstring="%s\nConfiguration:"%docstr
        for var in varnames:
            default=vardict[var]
            if default==None:
                default=self.fc.strcolor("REQUIRED","red")
            elif default=='':
                default="(default empty)"
            else:
                default="(default "+self.fc.strcolor(default, [self.fc.MODE["bold"],])+" )"
                
            description="(undocumented)"
            if var in instance.helpstrings:
                description=instance.helpstrings[var]
                
            helpline="%s %s :: %s"%(self.fc.strcolor(var,'magenta'),default,description)
            helpstring=helpstring+"\n%s"%helpline
        return helpstring