Adding multiple nginx to collectd's monitoring plugin

1.4k views Asked by At

Collectd queries nginx's HttpStubStatusModule in order to find the active connections.

The config end looks like-

<Plugin "nginx">
  URL "https://localhost:8433/nginx_status"
</Plugin>

The plugin is here.

i have a setup wherein i have 4 Nginx instances running on the same physical host, each listening at a different port. How do i make collectd monitor multiple Nginxes? The following does not work-

<Plugin "nginx">
  URL "https://localhost:8433/nginx_status"
</Plugin>

<Plugin "nginx">
  URL "https://localhost:8434/nginx_status"
</Plugin>
2

There are 2 answers

2
gaurav rajput On BEST ANSWER

struct curl_slist *curl_list = NULL;

   curl_list = curl_slist_append(curl_list, header);

   curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_list);
0
magazovski On

I have written a small script for the collectd Python plugin:

https://github.com/magazov/collectd-multinginx-python

It is very simple to use.

Here is the source code:

#! /usr/bin/env python

import re
import urllib2
import collectd    

class Nginx(object):
    def __init__(self):
        self.pattern = re.compile("([A-Z][\w]*).+?(\d+)")
        self.urls = {}

    def do_nginx_status(self):
        for instance, url in self.urls.items():
            try:
                response = urllib2.urlopen(url)
            except urllib2.HTTPError, e:
                collectd.error(str(e))
            except urllib2.URLError, e:
                collectd.error(str(e))
            else:
                data = response.read()
                m = self.pattern.findall(data)
                for key, value in m:
                    metric = collectd.Values()
                    metric.plugin = 'nginx-%s' % instance
                    metric.type_instance = key.lower()
                    metric.type = 'nginx_connections'
                    metric.values = [value]
                    metric.dispatch()

                requests = data.split('\n')[2].split()[-1]
                collectd.debug('Requests %s' % requests)
                metric = collectd.Values()
                metric.plugin = 'nginx-%s' % instance
                metric.type = 'nginx_requests'
                metric.values = [requests]
                metric.dispatch()

    def config(self, obj):
        self.urls = dict((node.key, node.values[0]) for node in obj.children)


nginx = Nginx()
collectd.register_config(nginx.config)
collectd.register_read(nginx.do_nginx_status)