Beispiel #1
0
def main():
    vMs = getHealthChecksData("virtualMachines")

    if vMs is None or len(vMs) == 0:
        print "No VMs running data available, skipping"
        exit(0)

    with open('/etc/hosts', 'r') as hostsFile:
        allHosts = hostsFile.readlines()
        hostsFile.close()

    failedCheck = False
    failureMessage = "Missing entries for VMs in /etc/hosts -\n"
    for vM in vMs:
        foundEntry = False
        for host in allHosts:
            components = host.split('\t')
            if len(components) == 2 and components[0].strip() == vM["ip"] \
                    and components[1].strip() == vM["vmName"]:
                foundEntry = True
                break

        if not foundEntry:
            failedCheck = True
            failureMessage = failureMessage + vM["ip"] + " " + vM[
                "vmName"] + ", "

    if failedCheck:
        print failureMessage[:-2]
        exit(1)
    else:
        print "All " + str(len(vMs)) + " VMs are present in /etc/hosts"
        exit(0)
Beispiel #2
0
def main():
    gws = getHealthChecksData("gateways")
    if gws is None and len(gws) == 0:
        print "No gateways data available, skipping"
        exit(0)

    unreachableGateWays = []
    gwsList = gws[0]["gatewaysIps"].strip().split(' ')
    for gw in gwsList:
        if len(gw) == 0:
            continue
        reachableGw = False
        for i in range(5):
            pingCmd = "ping " + gw + " -c 5 -w 10"
            pout = Popen(pingCmd, shell=True, stdout=PIPE)
            if pout.wait() == 0:
                reachableGw = True
                break

        if not reachableGw:
            unreachableGateWays.append(gw)

    if len(unreachableGateWays) == 0:
        print "All " + str(len(gwsList)) + " gateways are reachable via ping"
        exit(0)
    else:
        print "Unreachable gateways found-"
        print unreachableGateWays
        exit(1)
def main():
    entries = getHealthChecksData("systemThresholds")
    data = {}
    if entries is not None and len(entries) == 1:
        data = entries[0]

    if "maxMemoryUsage" not in data:
        print "Missing maxMemoryUsage in health_checks_data " + \
              "systemThresholds, skipping"
        exit(0)

    maxMemoryUsage = float(data["maxMemoryUsage"])
    cmd = "free | awk 'FNR == 2 { print $3 * 100 / $2 }'"
    pout = Popen(cmd, shell=True, stdout=PIPE)

    if pout.wait() == 0:
        currentUsage = float(pout.communicate()[0].strip())
        if currentUsage > maxMemoryUsage:
            print "Memory Usage " + str(currentUsage) + \
                  "% has crossed threshold of " + str(maxMemoryUsage) + "%"
            exit(1)
        print "Memory Usage within limits with current at " + \
              str(currentUsage) + "%"
        exit(0)
    else:
        print "Failed to retrieve memory usage using " + cmd
        exit(1)
Beispiel #4
0
def main():
    portForwards = getHealthChecksData("portForwarding")
    if portForwards is None or len(portForwards) == 0:
        print "No portforwarding rules provided to check, skipping"
        exit(0)

    failedCheck = False
    failureMessage = "Missing port forwarding rules in Iptables-\n "
    for portForward in portForwards:
        entriesExpected = []
        destIp = portForward["destIp"]
        srcIpText = "-d " + portForward["sourceIp"]
        srcPortText = "--dport " + formatPort(portForward["sourcePortStart"], portForward["sourcePortEnd"], ":")
        dstText = destIp + ":" + formatPort(portForward["destPortStart"], portForward["destPortEnd"], "-")
        for algo in [["PREROUTING", "--to-destination"],
                     ["OUTPUT", "--to-destination"],
                     ["POSTROUTING", "--to-source"]]:
            entriesExpected.append([algo[0], srcIpText, srcPortText, algo[1] + " " + dstText])

        fetchIpTableEntriesCmd = "iptables-save | grep " + destIp
        pout = Popen(fetchIpTableEntriesCmd, shell=True, stdout=PIPE)
        if pout.wait() != 0:
            failedCheck = True
            failureMessage = failureMessage + "Unable to execute iptables-save command " \
                                              "for fetching rules by " + fetchIpTableEntriesCmd + "\n"
            continue

        ipTablesMatchingEntries = pout.communicate()[0].strip().split('\n')
        for pfEntryListExpected in entriesExpected:
            foundPfEntryList = False
            for ipTableEntry in ipTablesMatchingEntries:
                # Check if all expected parts of pfEntryList
                # is present in this ipTableEntry
                foundAll = True
                for expectedEntry in pfEntryListExpected:
                    if ipTableEntry.find(expectedEntry) == -1:
                        foundAll = False
                        break

                if foundAll:
                    foundPfEntryList = True
                    break

            if not foundPfEntryList:
                failedCheck = True
                failureMessage = failureMessage + str(pfEntryListExpected) + "\n"

    if failedCheck:
        print failureMessage
        exit(1)
    else:
        print "Found all entries (count " + str(len(portForwards)) + ") in iptables"
        exit(0)
Beispiel #5
0
def main():
    '''
    Checks for max con and each load balancing rule - source ip, ports and destination
    ips and ports. Also checks for http mode. Does not check for stickiness policies.
    '''
    haproxyData = getHealthChecksData("haproxyData")
    if haproxyData is None or len(haproxyData) == 0:
        print "No data provided to check, skipping"
        exit(0)

    with open("/etc/haproxy/haproxy.cfg", 'r') as haCfgFile:
        haCfgLines = haCfgFile.readlines()
        haCfgFile.close()

    if len(haCfgLines) == 0:
        print "Unable to read config file /etc/haproxy/haproxy.cfg"
        exit(1)

    haCfgSections = {}
    currSection = None
    currSectionDict = {}
    for line in haCfgLines:
        line = line.strip()
        if len(line) == 0:
            if currSection is not None and len(currSectionDict) > 0:
                haCfgSections[currSection] = currSectionDict

            currSection = None
            currSectionDict = {}
            continue

        if currSection is None:
            currSection = line
        else:
            lineSec = line.split(' ', 1)
            if lineSec[0] not in currSectionDict:
                currSectionDict[lineSec[0]] = []

            currSectionDict[lineSec[0]].append(
                lineSec[1] if len(lineSec) > 1 else '')

    checkMaxConn = checkMaxconn(haproxyData[0], haCfgSections)
    checkLbRules = checkLoadBalance(haproxyData, haCfgSections)

    if checkMaxConn and checkLbRules:
        print "All checks pass"
        exit(0)
    else:
        exit(1)
Beispiel #6
0
def main():
    vMs = getHealthChecksData("virtualMachines")

    if vMs is None or len(vMs) == 0:
        print "No VMs running data available, skipping"
        exit(0)

    try:
        with open('/etc/dhcphosts.txt', 'r') as hostsFile:
            allHosts = hostsFile.readlines()
            hostsFile.close()
    except IOError:
        allHosts = []

    failedCheck = False
    failureMessage = "Missing elements in dhcphosts.txt - \n"
    COUNT = 0
    for vM in vMs:
        if vM["dhcp"] == "false":
            continue
        COUNT = COUNT + 1
        entry = vM["macAddress"] + " " + vM["ip"] + " " + vM["vmName"]
        foundEntry = False
        for host in allHosts:
            host = host.strip().split(',')
            if len(host) < 4:
                continue

            if host[0].strip() == vM["macAddress"] and host[1].strip() == vM["ip"]\
                    and host[2].strip() == vM["vmName"]:
                foundEntry = True
                break

            nonDefaultSet = "set:" + vM["ip"].replace(".", "_")
            if host[0].strip() == vM["macAddress"] and host[1].strip() == nonDefaultSet \
                    and host[2].strip() == vM["ip"] and host[3].strip() == vM["vmName"]:
                foundEntry = True
                break

        if not foundEntry:
            failedCheck = True
            failureMessage = failureMessage + entry + ", "

    if failedCheck:
        print failureMessage[:-2]
        exit(1)
    else:
        print "All " + str(COUNT) + " VMs are present in dhcphosts.txt"
        exit(0)
Beispiel #7
0
def main():
    entries = getHealthChecksData("routerVersion")
    data = {}
    if entries is not None and len(entries) == 1:
        data = entries[0]

    if len(data) == 0:
        print "Missing routerVersion in health_checks_data, skipping"
        exit(0)

    templateVersionMatches = True
    scriptVersionMatches = True

    if "templateVersion" in data:
        expected = data["templateVersion"].strip()
        releaseFile = "/etc/cloudstack-release"
        found = getFirstLine(releaseFile)
        if found is None:
            print "Release version not yet setup at " + releaseFile +\
                  ", skipping."
        elif expected != found:
            print "Template Version mismatch. Expected: " + \
                  expected + ", found: " + found
            templateVersionMatches = False

    if "scriptsVersion" in data:
        expected = data["scriptsVersion"].strip()
        sigFile = "/var/cache/cloud/cloud-scripts-signature"
        found = getFirstLine(sigFile)
        if found is None:
            print "Scripts signature is not yet setup at " + sigFile +\
                  ", skipping"
        if expected != found:
            print "Scripts Version mismatch. Expected: " + \
                  expected + ", found: " + found
            scriptVersionMatches = False

    if templateVersionMatches and scriptVersionMatches:
        print "Template and scripts version match successful"
        exit(0)
    else:
        exit(1)
def main():
    entries = getHealthChecksData("systemThresholds")
    data = {}
    if entries is not None and len(entries) == 1:
        data = entries[0]

    if "minDiskNeeded" not in data:
        print "Missing minDiskNeeded in health_checks_data systemThresholds, skipping"
        exit(0)

    minDiskNeeded = float(data["minDiskNeeded"]) * 1024
    s = statvfs('/')
    freeSpace = (s.f_bavail * s.f_frsize) / 1024

    if (freeSpace < minDiskNeeded):
        print "Insufficient free space is " + str(freeSpace / 1024) + " MB"
        exit(1)
    else:
        print "Sufficient free space is " + str(freeSpace / 1024) + " MB"
        exit(0)