Muehlenhoff has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/395751 )
Change subject: Add a prometheus exporter for ircd ...................................................................... Add a prometheus exporter for ircd Based on the previous custom Diamond collector. Change-Id: Iba73c77b0b895db2174467e300bcb29d66aed015 --- A debian/changelog A debian/compat A debian/control A debian/copyright A debian/dirs A debian/install A debian/postinst A debian/rules A debian/service A prometheus-ircd-exporter 10 files changed, 210 insertions(+), 0 deletions(-) Approvals: Muehlenhoff: Verified; Looks good to me, approved Filippo Giunchedi: Looks good to me, but someone else must approve diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..2c57135 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +prometheus-ircd-exporter (0.1) jessie-wikimedia; urgency=medium + + * Initial release + + -- Moritz Muehlenhoff <[email protected]> Wed, 06 Dec 2017 16:24:50 +0000 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..1678e97 --- /dev/null +++ b/debian/control @@ -0,0 +1,12 @@ +Source: prometheus-ircd-exporter +Section: net +Priority: extra +Maintainer: Moritz Muehlenhoff <[email protected]> +Build-Depends: debhelper (>= 9), dh-systemd (>= 1.5) +Standards-Version: 3.9.8 + +Package: prometheus-ircd-exporter +Architecture: all +Depends: python-prometheus-client, ${misc:Depends} +Description: Prometheus exporter for irc.wikimedia.org + Prometheus exporter for irc.wikimedia.org server metrics. diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..8733b8f --- /dev/null +++ b/debian/copyright @@ -0,0 +1,23 @@ +Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ + +Files: * +Copyright; 2014 Chase Pettet + 2017 Moritz Muehlenhoff + 2017 Filippo Giunchedi +License: Apache-2.0 + +License: Apache-2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + . + On Debian systems, the full text of the Apache License version 2 can be found + in the file `/usr/share/common-licenses/Apache-2.0'. diff --git a/debian/dirs b/debian/dirs new file mode 100644 index 0000000..e772481 --- /dev/null +++ b/debian/dirs @@ -0,0 +1 @@ +usr/bin diff --git a/debian/install b/debian/install new file mode 100644 index 0000000..3941572 --- /dev/null +++ b/debian/install @@ -0,0 +1 @@ +prometheus-ircd-exporter usr/bin \ No newline at end of file diff --git a/debian/postinst b/debian/postinst new file mode 100644 index 0000000..a43ff27 --- /dev/null +++ b/debian/postinst @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +case "$1" in + configure) + # Add prometheus user + if ! getent passwd prometheus > /dev/null; then + adduser --quiet --system --no-create-home \ + --group --gecos "Prometheus daemon" prometheus || true + fi + + ;; + + abort-upgrade|abort-remove|abort-deconfigure) + : + ;; + + *) + echo "postinst called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +#DEBHELPER# + +exit 0 diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..305c822 --- /dev/null +++ b/debian/rules @@ -0,0 +1,4 @@ +#!/usr/bin/make -f + +%: + dh $@ --with systemd diff --git a/debian/service b/debian/service new file mode 100644 index 0000000..14aedf0 --- /dev/null +++ b/debian/service @@ -0,0 +1,11 @@ +[Unit] +Description=Prometheus IRCD Exporter + +[Service] +Restart=always +User=prometheus +ExecStart=/usr/bin/prometheus-ircd-exporter $ARGS +EnvironmentFile=-/etc/default/prometheus-ircd-exporter + +[Install] +WantedBy=multi-user.target diff --git a/prometheus-ircd-exporter b/prometheus-ircd-exporter new file mode 100755 index 0000000..675889a --- /dev/null +++ b/prometheus-ircd-exporter @@ -0,0 +1,125 @@ +#!/usr/bin/python +# Copyright 2014 Chase Pettet +# 2017 Moritz Muehlenhoff +# 2017 Filippo Giunchedi +# Wikimedia Foundation +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import logging +import sys +import time +import urllib2 +import json +import socket +import re + +from prometheus_client import start_http_server, Summary +from prometheus_client.core import (GaugeMetricFamily, REGISTRY) + +log = logging.getLogger(__name__) + + +class PrometheusIrcdCollector(object): + scrape_duration = Summary( + 'ircd_scrape_duration_seconds', 'ircd exporter scrape duration') + + user = 'ircd_stats_bot-prometheus' + + def __init__(self, server, port): + self.server = server + self.port = port + + def recv_until(self, the_socket, end): + total_data = '' + while True: + data = the_socket.recv(8192) + total_data += (data) + if end in total_data: + break + return total_data.rstrip('\0').strip() + + @scrape_duration.time() + def collect(self): + + irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + irc.connect((self.server, self.port)) + irc.send("USER %s %s %s :%s\n" % (self.user, self.user, self.user, self.user)) + irc.send("NICK %s\n" % self.user) + termout = self.recv_until(irc, 'End of /MOTD command') + users = re.search("There\sare\s(\d+)\susers", termout) + chans = re.search("(\d+)\s:channels\sformed", termout) + + if users: + users_metric = GaugeMetricFamily('ircd_users', 'Numbers of connected users') + users_metric.add_metric([], float(users.groups()[0].strip())) + yield users_metric + + if chans: + chans_metric = GaugeMetricFamily('ircd_channels', 'Numbers of IRC channels') + chans_metric.add_metric([], float(chans.groups()[0].strip())) + yield chans_metric + + up = GaugeMetricFamily('ircd_up', 'ircd is running') + up.add_metric([], 1) + yield up + + except socket.error: + log.error('Failed to connect to IRC server') + up = GaugeMetricFamily('ircd_up', 'ircd is running') + up.add_metric([], 0) + yield up + + finally: + try: + irc.send("QUIT \n") + irc.close() + except socket.error: + log.error('Failed to close connection to IRC server') + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('-l', '--listen', metavar='ADDRESS', + help='Listen on this address', default=':9197') + parser.add_argument('-d', '--debug', action='store_true', + help='Enable debug logging') + parser.add_argument('-p', '--port', type=int, + help='The port of the IRC server to collect metrics from', + default=6667) + parser.add_argument('-s', '--server', help='The IRC server to collect metrics from', + default='localhost') + args = parser.parse_args() + + if args.debug: + logging.basicConfig(level=logging.DEBUG) + else: + logging.basicConfig(level=logging.WARNING) + + address, port = args.listen.split(':', 1) + + log.info('Starting ircd_exporter on %s:%s', address, port) + + REGISTRY.register(PrometheusIrcdCollector(args.server, args.port)) + start_http_server(int(port), addr=address) + + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + return 1 + + +if __name__ == "__main__": + sys.exit(main()) -- To view, visit https://gerrit.wikimedia.org/r/395751 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Iba73c77b0b895db2174467e300bcb29d66aed015 Gerrit-PatchSet: 3 Gerrit-Project: operations/debs/prometheus-ircd-exporter Gerrit-Branch: master Gerrit-Owner: Muehlenhoff <[email protected]> Gerrit-Reviewer: Filippo Giunchedi <[email protected]> Gerrit-Reviewer: Muehlenhoff <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
