示例#1
0
def test_checks(provider_data, credentials, measurement):
    # get the necessary config
    if measurement == "system_ping_vms":
        # this test is very slow at present, and not used in shinken pack, so skip
        pytest.skip(
            "Measurement {} is unused, so skipping".format(measurement))
    measure_func = CHECKS[measurement]
    hostname = provider_data.get("hostname")
    username, password = credentials.get("username"), credentials.get(
        "password")
    logger = get_logger(True)

    # get the host if necessary
    is_host_check = "host" in measure_func.__code__.co_varnames
    host = None
    if is_host_check:
        # get the first host
        host = provider_data.get("hosts")[0].get("name")

    system = VMWareSystem(hostname, username, password)
    if host:
        host = system.get_obj(vim.HostSystem, host)

    # now try run the check
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        measure_func(host or system, logger=logger)
    assert pytest_wrapped_e.type == SystemExit
def _get_vmware_datastore_summary_string(data_store_name=VMWARE_CONSTANTS['datastore']):
    """Return the datastore string summary for data_store_name

    For "Local-Ironforge" datastore the string looks Like:

        "Local-Ironforge (free: 1.66 TB, prov: 2.29 TB, total: 2.72 TB)"
     """
    system = VMWareSystem(
        hostname=settings.vmware.vcenter,
        username=settings.vmware.username,
        password=settings.vmware.password
    )
    data_store_summary = [h for h in system.get_obj_list(vim.Datastore)
                          if h.host and h.name == data_store_name][0].summary
    uncommitted = data_store_summary.uncommitted or 0
    capacity = _get_normalized_size(data_store_summary.capacity)
    free_space = _get_normalized_size(data_store_summary.freeSpace)
    prov = _get_normalized_size(data_store_summary.capacity + uncommitted
                                - data_store_summary.freeSpace)
    return '{0} (free: {1}, prov: {2}, total: {3})'.format(
        data_store_name, free_space, prov, capacity)
示例#3
0
def _get_vmware_datastore_summary_string(data_store_name=VMWARE_CONSTANTS['datastore']):
    """Return the datastore string summary for data_store_name

    For "Local-Ironforge" datastore the string looks Like:

        "Local-Ironforge (free: 1.66 TB, prov: 2.29 TB, total: 2.72 TB)"
     """
    system = VMWareSystem(
        hostname=settings.vmware.vcenter,
        username=settings.vmware.username,
        password=settings.vmware.password
    )
    data_store_summary = [h for h in system.get_obj_list(vim.Datastore)
                          if h.host and h.name == data_store_name][0].summary
    uncommitted = data_store_summary.uncommitted or 0
    capacity = _get_normalized_size(data_store_summary.capacity)
    free_space = _get_normalized_size(data_store_summary.freeSpace)
    prov = _get_normalized_size(data_store_summary.capacity + uncommitted
                                - data_store_summary.freeSpace)
    return '{0} (free: {1}, prov: {2}, total: {3})'.format(
        data_store_name, free_space, prov, capacity)
示例#4
0
def main():
    parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter)
    parser.add_argument("-V",
                        "--vsphere",
                        dest="vsphere",
                        help="Hostname of vSphere client",
                        type=str)
    parser.add_argument(
        "-H",
        "--hostname",
        dest="hostname",
        help="Hostname of vSphere esxi host",
        type=str,
    )
    parser.add_argument(
        "-u",
        "--user",
        dest="user",
        help="remote user to use",
        type=str,
    )
    parser.add_argument("-p",
                        "--password",
                        dest="password",
                        help="password for vSphere client",
                        type=str)
    parser.add_argument("-m",
                        "--measurement",
                        dest="measurement",
                        help="Type of measurement to carry out",
                        type=str)
    parser.add_argument(
        "-w",
        "--warning",
        dest="warning",
        help="Warning value for the check as a fraction. (e.g. 0.8)",
        default=0.75)
    parser.add_argument(
        "-c",
        "--critical",
        dest="critical",
        help="Critical value for the check as a fraction. (e.g. 0.9)",
        default=0.9)
    parser.add_argument("-l",
                        "--local",
                        dest="local",
                        help="Use this field when testing locally",
                        action="store_true",
                        default=False)
    args = parser.parse_args()
    # set logger
    logger = get_logger(args.local)

    if float(args.warning) > float(args.critical):
        logger.error(
            "Error: warning value can not be greater than critical value")
        sys.exit(3)

    # connect to the system
    logger.info("Connecting to Vsphere %s as user %s", args.vsphere, args.user)
    system = VMWareSystem(args.vsphere, args.user, args.password)
    # get the host object
    host = None
    if args.hostname:
        host = system.get_obj(vim.HostSystem, args.hostname)
        if not host:
            logger.error(
                "Error: esxi hostname {} does not exist on vSphere {}".format(
                    args.hostname, args.vsphere))
            sys.exit(3)
    # get measurement function
    measure_func = get_measurement(args.measurement)
    if not measure_func:
        logger.error("Error: measurement {} not understood".format(
            args.measurement))
        sys.exit(3)
    # run the measurement function
    try:
        logger.info("Calling check %s", measure_func.__name__)
        measure_func(host or system,
                     warn=args.warning,
                     crit=args.critical,
                     logger=logger)
    except Exception as e:
        logger.error("Exception occurred during execution of %s",
                     measure_func.__name__,
                     exc_info=True)
        print(
            "ERROR: exception '{}' occurred during execution of '{}', check logs for trace"
            .format(e, measure_func.__name__))
        sys.exit(3)