Nginx sysguard module
This document describes nginx-module-sysguard v0.1.0
released on 23 Feb 2017.
Earlier versions is not tested.
shell> git clone git://github.com/vozlt/nginx-module-sysguard.git
--add-module=/path/to/nginx-module-sysguard
Build the nginx binary.
Install the nginx binary.
http {
...
server {
...
sysguard on;
sysguard_mode or;
sysguard_load load=10.5 action=/loadlimit;
sysguard_mem swapratio=20% action=/swaplimit;
sysguard_mem free=100M action=/freelimit;
sysguard_rt rt=0.01 period=5s method=AMM:10 action=/rtlimit;
location /loadlimit {
return 503;
}
location /swaplimit {
return 503;
}
location /freelimit {
return 503;
}
location /rtlimit {
return 503;
}
}
...
server {
...
location /api {
sysguard on;
sysguard_mode or;
sysguard_load load=20 action=/limit;
sysguard_mem swapratio=10% action=/limit;
sysguard_rt rt=2.01 period=5s method=WMA:10 action=/limit;
...
}
location /images {
sysguard on;
sysguard_mode and;
sysguard_load load=20 action=/limit;
sysguard_mem swapratio=10% action=/limit;
sysguard_rt rt=2.01 period=5s method=WMA:10 action=/limit;
...
}
location /limit {
return 503;
}
}
}
This module can be used to protect your server in case system load, memory use goes too high or requests are responded too slow. This is a porting version of the sysguard in tengine to the pure NGINX so as to support the same features.
Caveats:
Note this module requires the sysinfo(2) system call, or getloadavg(3) function in glibc. It also requires the /proc file system to get memory information.
The following embedded variables are provided:
$sysguard_load
's value is 100, then load is 0.1(100/1000). (/msec)$sysguard_rt
's value is 100, then response time is 0.1sec(100/1000). (/msec)- | - |
---|---|
Syntax |
sysguard |
Default | off |
Context | http, server, location |
Description:
Enables or disables the module working.
- | - |
---|---|
Syntax | sysguard_load load=number [action=/url] |
Default | - |
Context | http, server, location |
Description:
Specify the load threshold. When the system load exceeds this threshold, all subsequent requests will be redirected to the URL specified by the 'action' parameter. It will return 503 if there's no 'action' URL defined. This directive also support using ncpuratio to instead of the fixed threshold, 'ncpu' means the number of cpu's cores, you can use this directive like this: load=ncpu1.5
- | - |
---|---|
Syntax | sysguard_mem swapratio=ratio% free=size [action=/url] |
Default | - |
Context | http, server, location |
Description:
Specify the used swap memory or free memory threshold. When the swap memory use ratio exceeds this threshold or memory free less than the size, all subsequent requests will be redirected to the URL specified by the 'action' parameter. It will return 503 if there's no 'action' URL. Sysguard uses this strategy to calculate memory free: "memfree = free + buffered + cached"
- | - |
---|---|
Syntax |
sysguard_rt rt=second period=time [method= |
Default | - |
Context | http, server, location |
Description:
Specify the response time threshold.
Parameter rt is used to set a threshold of the average response time, in second.
Parameter period is used to specify the period of the statistics cycle.
If the average response time of the system exceeds the threshold specified by the user,
the incoming request will be redirected to a specified url which is defined by parameter 'action'.
If no 'action' is presented, the request will be responsed with 503 error directly.
The method
is a formula that calculate the average of response processing times.
The number
in method is the number of samples to calculate the average.
The default method is set to be method=AMM:period
.
- | - |
---|---|
Syntax |
sysguard_mode |
Default | or |
Context | http, server, location |
Description:
If there are more than one type of monitor, this directive is used to specified the relations among all the monitors which are: 'and' for all matching and 'or' for any matching.
- | - |
---|---|
Syntax | sysguard_interval time |
Default | 1s |
Context | http, server, location |
Description:
Specify the time interval to update your system information.
The default value is one second, which means sysguard updates the server status once a second.
- | - |
---|---|
Syntax |
sysguard_log_level |
Default | error |
Context | http, server, location |
Description:
Specify the log level of sysguard.