From patchwork Mon Apr 20 21:17:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 21372 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 131CF27BBE4; Mon, 20 Apr 2020 22:18:15 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id AE78227BBE1 for ; Mon, 20 Apr 2020 22:18:14 +0100 (BST) Received: from localhost ([::1]:42490 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jQdny-00048k-7v for patchwork@mira.cbaines.net; Mon, 20 Apr 2020 17:18:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41360) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jQdnn-00044d-2H for guix-patches@gnu.org; Mon, 20 Apr 2020 17:18:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jQdnm-0000Q6-GK for guix-patches@gnu.org; Mon, 20 Apr 2020 17:18:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:37002) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jQdnm-0000Q1-2v for guix-patches@gnu.org; Mon, 20 Apr 2020 17:18:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jQdnl-0002g0-TQ for guix-patches@gnu.org; Mon, 20 Apr 2020 17:18:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#40738] [PATCH 2/4] services: Add a Prometheus service. Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 20 Apr 2020 21:18:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 40738 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: moreinfo To: 40738@debbugs.gnu.org Received: via spool by 40738-submit@debbugs.gnu.org id=B40738.158741746810226 (code B ref 40738); Mon, 20 Apr 2020 21:18:01 +0000 Received: (at 40738) by debbugs.gnu.org; 20 Apr 2020 21:17:48 +0000 Received: from localhost ([127.0.0.1]:48534 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jQdnX-0002en-Ri for submit@debbugs.gnu.org; Mon, 20 Apr 2020 17:17:48 -0400 Received: from mira.cbaines.net ([212.71.252.8]:33672) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jQdnW-0002eK-81 for 40738@debbugs.gnu.org; Mon, 20 Apr 2020 17:17:46 -0400 Received: from localhost (unknown [46.237.173.52]) by mira.cbaines.net (Postfix) with ESMTPSA id B9D0727BBE4 for <40738@debbugs.gnu.org>; Mon, 20 Apr 2020 22:17:45 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 95aadcd5 for <40738@debbugs.gnu.org>; Mon, 20 Apr 2020 21:17:43 +0000 (UTC) From: Christopher Baines Date: Mon, 20 Apr 2020 22:17:41 +0100 Message-Id: <20200420211743.23476-2-mail@cbaines.net> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200420211743.23476-1-mail@cbaines.net> References: <87v9lt50d4.fsf@cbaines.net> <20200420211743.23476-1-mail@cbaines.net> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Received-From: 209.51.188.43 X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches --- gnu/services/monitoring.scm | 82 +++++++++++++++++++++++++++++++++++++ gnu/tests/monitoring.scm | 73 ++++++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/gnu/services/monitoring.scm b/gnu/services/monitoring.scm index 511f4fb2fe..a37dfd80d8 100644 --- a/gnu/services/monitoring.scm +++ b/gnu/services/monitoring.scm @@ -40,6 +40,17 @@ darkstat-service-type prometheus-node-exporter-service-type + prometheus-service-type + + prometheus-configuration + prometheus-configuration-package + prometheus-configuration-user + prometheus-configuration-group + prometheus-configuration-config-file + prometheus-configuration-web-listen-address + prometheus-configuration-storage-tsdb-path + prometheus-configuration-extra-options + zabbix-server-configuration zabbix-server-service-type zabbix-agent-configuration @@ -110,6 +121,77 @@ HTTP.") (service-extension shepherd-root-service-type (compose list darkstat-shepherd-service)))))) + +;;; +;;; Prometheus +;;; + +(define-record-type* + prometheus-configuration + make-prometheus-configuration + prometheus-configuration? + (package prometheus-configuration-package + (default prometheus)) + (user prometheus-configuration-user + (default "prometheus")) + (group prometheusconfiguration-group + (default "prometheus")) + (config-file prometheus-config-file + (default (plain-file "prometheus.yml" ""))) + (web-listen-address prometheus-web-listen-address + (default "0.0.0.0:9090")) + (storage-tsdb-path prometheus-storage-tsdb-path + (default "/var/lib/prometheus/data/")) + (extra-options prometheus-configuration-extra-options + (default '()))) + +(define prometheus-shepherd-service + (match-lambda + (($ package user group + config-file web-listen-address + storage-tsdb-path extra-options) + (shepherd-service + (documentation "Prometheus monitoring system and time series database.") + (provision '(prometheus)) + (requirement '(networking)) + (start #~(make-forkexec-constructor + (list #$(file-append package "/bin/prometheus") + "--config.file" #$config-file + "--web.listen-address" #$web-listen-address + "--storage.tsdb.path" #$storage-tsdb-path + #$@extra-options) + #:user #$user + #:group #$group + #:log-file "/var/log/prometheus.log")) + (stop #~(make-kill-destructor)))))) + +(define (prometheus-account config) + (match-record config + (user group) + (list (user-group + (name group) + (system? #t)) + (user-account + (name user) + (group group) + (system? #t) + (comment "Prometheus user") + (home-directory "/var/lib/prometheus") + (shell (file-append shadow "/sbin/nologin")))))) + +(define prometheus-service-type + (service-type + (name 'prometheus) + (description + "Run @command{prometheus} to scrape targets for mertrics and provide the +web interface.") + (extensions + (list (service-extension shepherd-root-service-type + (compose list prometheus-shepherd-service)) + (service-extension account-service-type + prometheus-account))) + (default-value (prometheus-configuration)))) + (define-record-type* prometheus-node-exporter-configuration make-prometheus-node-exporter-configuration diff --git a/gnu/tests/monitoring.scm b/gnu/tests/monitoring.scm index 732fbc54d7..e8c0847229 100644 --- a/gnu/tests/monitoring.scm +++ b/gnu/tests/monitoring.scm @@ -31,9 +31,80 @@ #:use-module (gnu system) #:use-module (gnu tests) #:use-module (guix gexp) - #:export (%test-prometheus-node-exporter + #:export (%test-prometheus + %test-prometheus-node-exporter %test-zabbix)) + +;;; +;;; Prometheus +;;; + +(define* (run-prometheus-test name test-os) + "Run tests in %TEST-OS, which has Prometheus running." + (define os + (marionette-operating-system + test-os + #:imported-modules '((gnu services herd)))) + + (define vm + (virtual-machine + (operating-system os) + (port-forwardings '((8080 . 9090))))) + + (define test + (with-imported-modules '((gnu build marionette)) + #~(begin + (use-modules (srfi srfi-11) + (srfi srfi-64) + (gnu build marionette) + (web client) + (web response)) + + (define marionette + (make-marionette (list #$vm))) + + (mkdir #$output) + (chdir #$output) + + (test-begin #$name) + + (test-assert "prometheus running" + (marionette-eval + '(begin + (use-modules (gnu services herd)) + (match (start-service 'prometheus) + (#f #f) + (('service response-parts ...) + (match (assq-ref response-parts 'running) + ((pid) (number? pid)))))) + marionette)) + + (test-equal "prometheus healthy" + 200 + (begin + (wait-for-tcp-port 9090 marionette) + (let-values (((response text) + (http-get "http://localhost:8080/-/healthy"))) + (response-code response)))) + + (test-end) + (exit (= (test-runner-fail-count (test-runner-current)) 0))))) + + (gexp->derivation (string-append name "-test") test)) + +(define %prometheus-test-os + (simple-operating-system + (service dhcp-client-service-type) + (service prometheus-service-type))) + +(define %test-prometheus + (system-test + (name "prometheus") + (description "Connect to a running Prometheus service.") + (value (run-prometheus-test name + %prometheus-test-os)))) + ;;; ;;; Prometheus Node Exporter