def check_replication(service, service_replication,
                      warn_range, crit_range):
    """Check for sufficient replication of a service

    :param service: A string representing the name of the service
                         this replication check is relevant to.
    :param service_replication: An int representing the number of available
                                service instances
    :param warn_range: A two tuple of integers representing the minimum and
                       maximum allowed replication before entering the WARNING
                       state.
    :param crit_range: A two tuple of integers representing the minimum and
                       maximum allowed replication before entering the CRITICAL
                       state.

    Note that all ranges are closed interval. If the replication is outside the
    closed interval for the relevant level (e.g. warning, critical), then
    the error code will change appropriately.

    :returns check_result: A tuple of error code and a human readable error
        message. The error codes conform to the nagios plugin api.

        e.g. for an OK service
        (0,
        "OK lucy has 1 instance(s)")

        e.g. for a CRITICAL service
        (2,
        "CRITICAL lucy has 0 instance(s), expected value in [1, 1e18])
    """
    code, status, interval = 0, 'OK', None
    if not (crit_range[0] <= service_replication <= crit_range[1]):
        code, status, interval = 2, 'CRITICAL', crit_range
    elif not (warn_range[0] <= service_replication <= warn_range[1]):
        code, status, interval = 1, 'WARNING', warn_range

    expected_message = ""
    if interval is not None:
        expected_message = ", expected value in {0}".format(interval)

    message = "{0} {1} has {2} instance(s){3}".format(
        status, service, service_replication, expected_message
    )

    return code, message