Exemple #1
0
def main():
    client = Client("cream-jobSubmit", "1.0")
    client.createParser()
    client.optionParser.add_option("-m",
                         "--metrics",
                         dest="metrics_list",
                         help="The metrics file path")
    client.readOptions()

    if not client.options.metrics_list:
        optionParser.error("metrics file path not specified")

    try:
        with open(client.options.metrics_list) as infile:
            data = infile.read()

        metrics = data.splitlines()
    except Exception as ex:
        client.nagiosExit(client.CRITICAL, ex)
 
    try:
        client.checkProxy()

        jobId = client.jobSubmit()

        client.debug("job id: " + jobId)
    except Exception as ex:
        client.nagiosExit(client.CRITICAL, ex)

    terminalStates = ['DONE-OK', 'DONE-FAILED', 'ABORTED', 'CANCELLED']
    lastStatus = ""
    exitCode = None

    while not lastStatus in terminalStates:
        time.sleep(10)
        try:
            lastStatus, exitCode = client.jobStatus(jobId)

            client.debug("job status: " + lastStatus)
        except Exception as ex:
            client.nagiosExit(client.CRITICAL, ex)

    try:
        client.dir = "/var/spool/cream/" + client.hostname
        if not os.path.isdir(client.dir):
            os.makedirs(client.dir)

        osbdir = client.getOutputSandbox(jobId)

        client.debug("output sandbox dir: " + osbdir)

        outfiles = dircache.listdir(osbdir)

    except Exception as ex:
        client.nagiosExit(client.CRITICAL, ex)

    for metric in metrics:
        client.debug("metric is " + metric)
        outfilename = osbdir + "/" + metric + ".out"
        resfilename = osbdir + "/" + metric + ".res"
        client.debug("outfile is " + outfilename)
        client.debug("resfile is " + resfilename)
        if os.path.isfile(outfilename) and os.path.isfile(resfilename) :
            with open(outfilename) as outfile:
                data = outfile.read()
            output = data.splitlines()

            outmsg = ""
            sep = "\\\\n"
            for line in output:
                outmsg += line + "\\\\n"

            with open(resfilename) as resfile:
                data = resfile.read()
            exitvalue = data.splitlines()

            currtime = str(int(time.mktime(time.localtime())))
            
            nagmsg = "[" + currtime + "] PROCESS_SERVICE_CHECK_RESULT;" + client.hostname + \
                  ";eu.egi.sec.WN-" + metric + "-ops;" + exitvalue[0] + ";" + outmsg + '\n'
            client.debug("Results submitted to nagios: " + nagmsg)
            with open(nagcmd, 'a') as nagcmdfile:
                  nagcmdfile.write(nagmsg)

        else:
            currtime = str(int(time.mktime(time.localtime())))
            nagmsg = "[" + currtime + "] PROCESS_SERVICE_CHECK_RESULT;" + client.hostname + \
                  ";eu.egi.sec.WN-" + metric + "-ops;" + "3;(No output returned from plugin)" + '\n'
            client.debug("Results submitted to nagios: " + nagmsg)
            with open(nagcmd, 'a') as nagcmdfile:
                  nagcmdfile.write(nagmsg)

    try:       
        client.jobPurge(jobId)
    except Exception as ex:
        client.debug("cannot purge the job" + ex)

    if lastStatus == terminalStates[0] :
        client.nagiosExit(client.OK, "Job terminated with status=" + lastStatus)
    else:
        client.nagiosExit(client.CRITICAL, "Job terminated with status=" + lastStatus)