天天看点

nginx改造“显示当前服务器的负载”

      博文题目之所以用改造,而没有用模块开发之类等。只是个人觉得本篇博文并未上升到真正的开发高度,而是在看懂前人代码的基础上,增加代码或者修改代码。使其更加完善,跟业务结合的更完美,效率更高。小小的改造放置于此,贵在一个思路,详情如下:

1、硬件环境:

   联想R430X 2U服务器

2、软件环境

   centos6.2 2.6.32-220.el6.x86_64

   nginx-1.0.10.tar.gz

3、所用配置文件关键部分

server

  {

    listen       8100;

    server_name  www.nginx.com

192.168.15.127;

    index index.html index.htm;

    root  /data0/htdocs/www;

    location /ok{

    load_status on;

    }

  }

4、nginx模块目录

   cd

/home/xuekun/nginx

   mkdir ngx_module_load

5、编辑nginx模块的编译相关文件(config)

[root@centos6 nginx]# cat

ngx_module_load/config

ngx_addon_name=ngx_module_load

HTTP_MODULES="$HTTP_MODULES

ngx_module_load"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS

$ngx_addon_dir/ngx_module_load.c"

CORE_LIBS="$CORE_LIBS "

6、模块源码文件

#include

static char *ngx_http_set_status(ngx_conf_t

*cf, ngx_command_t *cmd,

                void *conf);

static ngx_int_t ngx_load_average(ngx_int_t

aload[], ngx_int_t nelem);

static ngx_command_t  ngx_load_commands[] = {

        { ngx_string("load_status"),

                NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,

                ngx_http_set_status,

                0,

                NULL },

        ngx_null_command

};

static ngx_http_module_t  ngx_load_module_ctx = {

        NULL,                                  /* preconfiguration */

        NULL,                                  /* postconfiguration */

        NULL,                                  /* create main configuration */

        NULL,                                  /* init main configuration */

        NULL,                                  /* create server configuration */

        NULL,                                  /* merge server configuration */

        NULL,                                  /* create location configuration */

        NULL                                   /* merge location configuration */

ngx_module_t  ngx_module_load = {

        NGX_MODULE_V1,

        &ngx_load_module_ctx,      /*

module context */

        ngx_load_commands,              /*

module directives */

        NGX_HTTP_MODULE,                       /* module type */

        NULL,                                  /* init master */

        NULL,                                  /* init module */

        NULL,                                  /* init process */

        NULL,                                  /* init thread */

        NULL,                                  /* exit thread */

        NULL,                                  /* exit process */

        NULL,                                  /* exit master */

        NGX_MODULE_V1_PADDING

 /*获取系统负载*/

aload[], ngx_int_t nelem)

{

        ngx_int_t i;

        double      loadavg[3];

        getloadavg(loadavg, nelem);

        for (i = 0; i

                aload[i] = loadavg[i] * 1000;

        }

        return NGX_OK;

}

static ngx_int_t

ngx_load_handler(ngx_http_request_t *r)

        size_t             size;

        ngx_int_t          rc;

        ngx_buf_t         *b;

        ngx_chain_t        out;

        /*load value*/

        ngx_int_t          load;

        if (r->method != NGX_HTTP_GET && r->method !=

NGX_HTTP_HEAD) {

                return NGX_HTTP_NOT_ALLOWED;

        rc = ngx_http_discard_request_body(r);

        if (rc != NGX_OK) {

                return rc;

        ngx_str_set(&r->headers_out.content_type,

"text/plain");

    /*get loadavg*/

        ngx_load_average(&load, 1);

        if (r->method == NGX_HTTP_HEAD) {

                r->headers_out.status =

NGX_HTTP_OK;

                rc = ngx_http_send_header(r);

                if (rc == NGX_ERROR || rc >

NGX_OK || r->header_only) {

                        return rc;

                }

        size = sizeof("Current loadavg:  \n") + NGX_ATOMIC_T_LEN;

        b = ngx_create_temp_buf(r->pool, size);

        if (b == NULL) {

                return

NGX_HTTP_INTERNAL_SERVER_ERROR;

        out.buf = b;

        out.next = NULL;

        b->last = ngx_sprintf(b->last, "Current

loadavg:%1.3f\n", load * 1.0 / 1000);

        r->headers_out.status = NGX_HTTP_OK;

        r->headers_out.content_length_n = b->last - b->pos;

        b->last_buf = 1;

        rc = ngx_http_send_header(r);

        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {

        return ngx_http_output_filter(r, &out);

*cf, ngx_command_t *cmd, void *conf)

        ngx_http_core_loc_conf_t  *clcf;

        clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

        clcf->handler = ngx_load_handler;

        return NGX_CONF_OK;

7、configure,make

nginx-1.0.10

8、运行nginx

./objs/nginx -c /home/xuekun/cstudy/nginx/nginx-1.0.10/conf/nginx.conf

9、通过浏览器查看效果

nginx改造“显示当前服务器的负载”

继续阅读