static ngx_int_t
ngx_http_sysguard_handler(ngx_http_request_t *r)
{
    ngx_http_limit_conn_conf_t     *lccf;

    lccf = ngx_http_get_module_loc_conf(r, ngx_http_limit_conn_module);

    if (!lccf->sysguard_enable) {
        return NGX_OK;
    }

    if (r->limit_conn_set) {
        return NGX_DECLINED;
    }

    /* load */

    if (lccf->load != NGX_CONF_UNSET) {

        if (ngx_http_sysguard_cached_load_exptime < ngx_time()) {
            ngx_http_sysguard_update_load(r, lccf->interval);
        }

        ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "http sysguard handler load: %1.3f %1.3f %V",
                       ngx_http_sysguard_cached_load * 1.0 / 1000,
                       lccf->load * 1.0 / 1000,
                       &r->uri);

        if (ngx_http_sysguard_cached_load > lccf->load) {

            ngx_log_error(lccf->log_level, r->connection->log, 0,
                          "sysguard load limited, current:%1.3f conf:%1.3f",
                          ngx_http_sysguard_cached_load * 1.0 / 1000,
                          lccf->load * 1.0 / 1000);

            lccf->overload = 1;
            return NGX_OK;
        }
    }

    /* swap */

    if (lccf->swap != NGX_CONF_UNSET) {

        if (ngx_http_sysguard_cached_mem_exptime < ngx_time()) {
            ngx_http_sysguard_update_mem(r, lccf->interval);
        }

        ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "http sysguard handler swap: %i %i %V",
                       ngx_http_sysguard_cached_swapstat,
                       lccf->swap,
                       &r->uri);

        if (ngx_http_sysguard_cached_swapstat > lccf->swap) {

            ngx_log_error(lccf->log_level, r->connection->log, 0,
                          "sysguard swap limited, current:%i conf:%i",
                          ngx_http_sysguard_cached_swapstat,
                          lccf->swap);

            lccf->overload = 1;
            return NGX_OK;
        }
    }

    /* mem free */

    if (lccf->free != NGX_CONF_UNSET_SIZE) {

        if (ngx_http_sysguard_cached_mem_exptime < ngx_time()) {
            ngx_http_sysguard_update_mem(r, lccf->interval);
        }

        if (ngx_http_sysguard_cached_free != NGX_CONF_UNSET_SIZE) {

            ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                           "http sysguard handler free: %uz %uz %V",
                           ngx_http_sysguard_cached_free,
                           lccf->free,
                           &r->uri);

            if (ngx_http_sysguard_cached_free < lccf->free) {

                ngx_log_error(lccf->log_level, r->connection->log, 0,
                              "sysguard free limited, current:%uzM conf:%uzM",
                              ngx_http_sysguard_cached_free / 1024 / 1024,
                              lccf->free / 1024 / 1024);

                lccf->overload = 1;
                return NGX_OK;
            }
        }

    }

    return NGX_DECLINED;
}
示例#2
0
static ngx_int_t
ngx_http_sysguard_handler(ngx_http_request_t *r)
{
    ngx_http_sysguard_conf_t  *glcf;

    if (r->main->sysguard_set) {
        return NGX_DECLINED;
    }

    glcf = ngx_http_get_module_loc_conf(r, ngx_http_sysguard_module);

    if (!glcf->enable) {
        return NGX_DECLINED;
    }

    r->main->sysguard_set = 1;

    /* load */

    if (glcf->load != NGX_CONF_UNSET) {

        if (ngx_http_sysguard_cached_load_exptime < ngx_time()) {
            ngx_http_sysguard_update_load(r, glcf->interval);
        }

        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "http sysguard handler load: %1.3f %1.3f %V %V",
                       ngx_http_sysguard_cached_load * 1.0 / 1000,
                       glcf->load * 1.0 / 1000,
                       &r->uri,
                       &glcf->load_action);

        if (ngx_http_sysguard_cached_load > glcf->load) {

            ngx_log_error(glcf->log_level, r->connection->log, 0,
                          "sysguard load limited, current:%1.3f conf:%1.3f",
                          ngx_http_sysguard_cached_load * 1.0 / 1000,
                          glcf->load * 1.0 / 1000);

            return ngx_http_sysguard_do_redirect(r, &glcf->load_action);
        }
    }

    /* swap */

    if (glcf->swap != NGX_CONF_UNSET) {

        if (ngx_http_sysguard_cached_mem_exptime < ngx_time()) {
            ngx_http_sysguard_update_mem(r, glcf->interval);
        }

        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "http sysguard handler swap: %i %i %V %V",
                       ngx_http_sysguard_cached_swapstat,
                       glcf->swap,
                       &r->uri,
                       &glcf->swap_action);

        if (ngx_http_sysguard_cached_swapstat > glcf->swap) {

            ngx_log_error(glcf->log_level, r->connection->log, 0,
                          "sysguard swap limited, current:%i conf:%i",
                          ngx_http_sysguard_cached_swapstat,
                          glcf->swap);

            return ngx_http_sysguard_do_redirect(r, &glcf->swap_action);
        }
    }

    /* mem free */

    if (glcf->free != NGX_CONF_UNSET_SIZE) {

        if (ngx_http_sysguard_cached_mem_exptime < ngx_time()) {
            ngx_http_sysguard_update_mem(r, glcf->interval);
        }

        if (ngx_http_sysguard_cached_free != NGX_CONF_UNSET_SIZE) {

            ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                           "http sysguard handler free: %uz %uz %V %V",
                           ngx_http_sysguard_cached_free,
                           glcf->free,
                           &r->uri,
                           &glcf->free_action);

            if (ngx_http_sysguard_cached_free < glcf->free) {

                ngx_log_error(glcf->log_level, r->connection->log, 0,
                              "sysguard free limited, current:%uzM conf:%uzM",
                              ngx_http_sysguard_cached_free / 1024 / 1024,
                              glcf->free / 1024 / 1024);

                return ngx_http_sysguard_do_redirect(r, &glcf->free_action);
            }
        }

    }

    return NGX_DECLINED;
}
static ngx_int_t
ngx_http_sysguard_handler(ngx_http_request_t *r)
{
    ngx_http_sysguard_conf_t  *glcf;

    if (r->main->sysguard_set) {
        return NGX_DECLINED;
    }

    glcf = ngx_http_get_module_loc_conf(r, ngx_http_sysguard_module);

    if (!glcf->enable) {
        return NGX_DECLINED;
    }

    r->main->sysguard_set = 1;

    /* load */

    if (glcf->load >= 0) {

        if (ngx_http_sysguard_cached_load_exptime < ngx_time()) {
            ngx_http_sysguard_update_load(r, glcf->interval);
        }

        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "http sysguard handler load: %i %i %V %V",
                       ngx_http_sysguard_cached_load,
                       glcf->load,
                       &r->uri,
                       &glcf->load_action);

        if (ngx_http_sysguard_cached_load > glcf->load) {

            ngx_log_error(glcf->log_level, r->connection->log, 0,
                          "sysguard load limited, current:%i conf:%i",
                          ngx_http_sysguard_cached_load,
                          glcf->load);

            return ngx_http_sysguard_do_redirect(r, &glcf->load_action);
        }
    }

    /* swap */

    if (glcf->swap >= 0) {

        if (ngx_http_sysguard_cached_swap_exptime < ngx_time()) {
            ngx_http_sysguard_update_swap(r, glcf->interval);
        }

        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "http sysguard handler swap: %i %i %V %V",
                       ngx_http_sysguard_cached_swapstat,
                       glcf->swap,
                       &r->uri,
                       &glcf->swap_action);

        if (ngx_http_sysguard_cached_swapstat > glcf->swap) {

            ngx_log_error(glcf->log_level, r->connection->log, 0,
                          "sysguard swap limited, current:%i conf:%i",
                          ngx_http_sysguard_cached_swapstat,
                          glcf->swap);

            return ngx_http_sysguard_do_redirect(r, &glcf->swap_action);
        }
    }

    return NGX_DECLINED;
}