Forward nginx stats in from collectd to influxdb

Requirement

Nginx must be compiled with HttpStubStatusModule​ module. You can check that by running
following command:

 nginx -V 2>&1 | grep -o with-http_stub_status_module

If you see following output, you are good to go ahead. Otherwise, you have to install
nginx with this module.

 with-http_stub_status_module

Nginx Config

$ sudo vi /etc/nginx/conf.d/status.conf

server {
	listen 127.0.0.1:80;
	location /nginx_status {
		stub_status on;
		access_log off;
		allow 127.0.0.1;
		deny all;
	}
}
$ sudo nginx -t
$ sudo /etc/init.d/nginx reload

In collectd

Install nginx plugin

$ sudo yum install collectd-nginx
$ sudo vi /etc/collectd.conf
LoadPlugin nginx
<Plugin "nginx>
URL "http://localhost:80/nginx_status"

Restart Collectd.

$ sudo /etc/init.d/collectd restart

Problem

You may get following error:

$ sudo tailf /var/log/message
 nginx plugin: curl_easy_perform failed: Failed to connect to ::1: Permission denied
 Or
 nginx plugin: curl_easy_perform failed: Failed to connect to 127.0.0.1: Permission denied

And plugin may get suspended

 read-function of plugin `nginx' failed. Will suspend it for 160.000 seconds.

Check SElinux

$sudo ls -lZ /etc/nginx/conf.d
 -rw-r--r--. root root system_u:object_r:httpd_config_t:s0 cloudflare.conf
 -rw-r--r--. root root system_u:object_r:httpd_config_t:s0 phusion-passenger.conf
 -rw-r--r--. root root system_u:object_r:httpd_config_t:s0 ssl.conf
 -rw-r--r--. root root unconfined_u:object_r:httpd_config_t:s0 status.conf
 -rw-------. root nginx system_u:object_r:httpd_config_t:s0 zz010_psa_nginx.conf

Change context

$ sudo chcon system_u:object_r:httpd_config_t:s0 status.conf

$ sudo systemctl restart nginx

If this doesn’t work then check if following help

$ sudo setsebool httpd_can_network_connect 1

If this works then set it permanent

$ sudo setsebool -P httpd_can_network_connect 1

If above didn’t work then do following

$ sudo yum -y install policycoreutils-python
$ sudo semanage permissive -a collectd_t

Leave a Reply

Your email address will not be published. Required fields are marked *