def CheckService(process_list):
    output = os.popen("ps aux")
    outStr = output.read()
    output.close()

    for process in process_list:
        process_name = process[0]
        x = outStr.find(process_name)

        if x > 0:
            print("[INFO]process " + process_name + " is alive.")
            sys.stdout.flush()
        else:
            subject = "IDC process " + process_name + " was dead."
            print("[ERROR]process " + process_name + " was dead.")
            sys.stdout.flush()

            logs = process[1].replace(' ', '').split(',')
            mmail.send_mail_with_files(subject + ' ' + host, logs)

            os.system('service ' + process_name + ' restart')
            print("[ERROR]process " + process_name + " was restarted.")
            sys.stdout.flush()
            pass
        pass
    pass
def CheckMem(threshold):
    cmd = "free -m | grep Mem | awk '{print $4}'"
    output = os.popen(cmd)
    outStr = output.read()
    free_size = int(outStr)
    if free_size < threshold:
        print "[ERROR]Mem Check OK."
        mmail.send_mail_with_files('Mem out of range. Please check it.' + host)
        pass
    else:
        print "[INFO]Mem Check OK."
        pass
def CheckDiskCapacity(filesystem_list):
    for filesystem in filesystem_list:
        cmd = "df -h | grep " + filesystem + " | awk '{print $5}'"
        output = os.popen(cmd)
        outStr = output.read()
        if outStr == '':
            print('[ERROR]THE FILESYSTEM [' + filesystem + '] IS NOT EXIST!')
            mmail.send_mail_with_files('THE FILESYSTEM [' + filesystem + '] IS NOT EXIST! CHECK IT AS SOON AS YOU CAN! From: ' + host)
            return
        percentage = int(outStr.replace('%','').replace('\r', '').replace('\n', ''))
        output.close()
        if percentage >= 90:
            print('[ERROR]THE FILESYSTEM [' + filesystem + '] IS FULL!')
            mmail.send_mail_with_files('THE FILESYSTEM [' + filesystem + '] IS FULL! CHECK IT AS SOON AS YOU CAN! From: ' + host)
            pass
        elif percentage >= 80 and percentage < 90:
            print('[WARNING]The filesystem [' + filesystem + '] will be full!')
            mmail.send_mail_with_files('The filesystem [' + filesystem + '] will be full. Please delete some file. ' + host)
            pass
        else:
            # it's ok
            print('[INFO]Check disk capacity, all right. Percentage is: ' + str(percentage) + '%.')
            sys.stdout.flush()
            pass
        pass
    pass