Exporting Prometheus Metrics with Bash Scripts

Prometheus has a lot of exporters available. Sometimes, however, you may want to export data to Prometheus as quickly as possible. I have given it some thought and have come up with a relatively simple approach to building a listening exporter. My thought process/plan of action is described below. While I talk about Bash scripts, you can use any software you'd like, I suppose. Creating a simple Exporter For the sake of simplicity, let's build a script that would report values from /proc/loadavg, an example of such a script: And the output of the script: Tip: When building your Exporter, make sure that the Prometheus syntax is correct. You can use the promtool utility to check metric syntax. We've built our exporter, now we need something to serve this over HTTP for Prometheus to scrape. Using Xinetd as your TCP server I've had success using Xinetd as the TCP server to send my data. Xinetd is a generic service server, providing you with an easy way to send and receive data via TCP. You can use other TCP listeners, such as Netcat if you'd like. I've decided on Xinetd because of how easy it is to manage service files, and Xinetd provides basic access control so that only my Prometheus instance can access it. There is a small catch, though - Xinetd does not have the concept of the HTTP protocol that you'd want Prometheus to scrape. An example Xinetd service file could look like this: More on httpwrapper below. Building an HTTP wrapper script My next step was to build a small script that would wrap my metrics in HTTP compliant output that Xinetd would pass to the connecting client. The above script runs whatever you provide it as an argument (relative to $root - in my case /opt/metrics.d/ is where I store my exporters), then wraps … Continue reading Exporting Prometheus Metrics with Bash Scripts