#
# Copyright (c) 2006 rPath, Inc.
#

import os
import re
from nagpy.plugin import NagiosPlugin
from nagpy.util.exceptionHooks import stdout_hook
from mint_nagpy.config import CheckJobsConfig
from conary.lib import cfgtypes

class CheckJS(NagiosPlugin):
    cfgPath = '/srv/rbuilder/nagios/check.cfg'
    def __init__(self):
        NagiosPlugin.__init__(self)
        self.cfg = CheckJobsConfig()
        try:
            self.cfg.read(self.cfgPath)
        except cfgtypes.CfgEnvironmentError:
            pass

    @stdout_hook
    def check(self):
        status = self.nagios_ok
        jsdirs = [x for x in os.listdir(self.cfg.jsPath) if os.path.isdir(os.path.join(self.cfg.jsPath, x))]
        for x in jsdirs:
            f = open(os.path.join(self.cfg.jsPath, x, self.cfg.jsConfig))
            conf = f.read()
            f.close()
            match = re.search('lockFile\s+(.+)\s*\n', conf)
            lockFile = match.groups()[0].lstrip('/')
            try:
                f = open(os.path.join(self.cfg.jsPath, x, lockFile))
            except IOError:
                if status == self.nagios_ok:
                    status = self.nagios_warning
            else:
                pid = f.read()
                f.close()
                fd = os.popen('ps -p %s -o comm=' % pid)	
                res = fd.read().strip()
                fd.close()
                if (res != 'job-server'):
                    status = self.nagios_critical
        return status

@@ -728,96 +728,418 @@
    f
- = open(os.path.join(self.cfg.jsPath, x, self.cfg.jsConfig))%0A            conf = f.read()
+or y in self.cfg.jsConfig:%0A                try:%0A                    f = open(os.path.join(self.cfg.jsPath, x, y))%0A                except IOError:%0A                    pass%0A                else:%0A                    break%0A            # if f is undefined or closed, no config file could be opened%0A            try:%0A                conf = f.read()%0A            except (ValueError, NameError):%0A                continue
 %0A   

check multiple directories for iso_gen.conf