__prometheus_server: switch to packages; assume prometheus 2.x

This commit is contained in:
kamila 2018-03-11 00:47:33 +01:00
parent fe870ba8ba
commit 8a488591bb
4 changed files with 45 additions and 43 deletions
cdist/conf/type/__prometheus_server

View file

@ -10,18 +10,12 @@ DESCRIPTION
-----------
Install and configure Prometheus (https://prometheus.io/).
This type creates a daemontools-compatible service directory under /service/prometheus.
Daemontools (or something compatible) must be installed (in particular, the command `svc` must be executable).
Note that due to significant differences between Prometheus 1.x and 2.x, only 2.x is supported.
REQUIRED PARAMETERS
-------------------
config
Prometheus configuration file. It will be saved as /etc/prometheus/prometheus.yml on the target.
listen-address
Passed as web.listen-address.
alertmanager-url
Passed as alertmanager.url
OPTIONAL PARAMETERS
@ -32,8 +26,6 @@ rule-files
Path to rule files. They will be installed under /etc/prometheus/<filename>. You need to include `rule_files: [/etc/prometheus/<your-pattern>]` in the config file if you use this.
storage-path
Where to put data. Default: /data/prometheus. (Directory will be created if needed.)
target-heap-size
Passed as storage.local.target-heap-size. Default: 1/2 of RAM.
BOOLEAN PARAMETERS
@ -49,22 +41,16 @@ EXAMPLES
PROMPORT=9090
ALERTPORT=9093
__daemontools
__golang_from_vendor --version 1.8.1 # required for prometheus and many exporters
require="__daemontools __golang_from_vendor" __prometheus_server \
--with-daemontools \
__prometheus_server \
--config "$__manifest/files/prometheus.yml" \
--retention-days 14 \
--storage-path /data/prometheus \
--listen-address "[::]:$PROMPORT" \
--rule-files "$__manifest/files/*.rules" \
--alertmanager-url "http://monitoring1.node.consul:$ALERTPORT,http://monitoring2.node.consul:$ALERTPORT"
--rule-files "$__manifest/files/*.rules"
SEE ALSO
--------
:strong:`cdist-type__prometheus_alertmanager`\ (7), :strong:`cdist-type__daemontools`\ (7),
:strong:`cdist-type__prometheus_alertmanager`\ (7), :strong:`cdist-type__grafana_dashboard`\ (7),
Prometheus documentation: https://prometheus.io/docs/introduction/overview/
AUTHORS
@ -73,7 +59,7 @@ Kamila Součková <kamila--@--ksp.sk>
COPYING
-------
Copyright \(C) 2017 Kamila Součková. You can redistribute it
Copyright \(C) 2018 Kamila Součková. You can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

View file

@ -1,52 +1,69 @@
#!/bin/sh -e
GOBIN=/opt/gocode/bin # where to find go binaries
##### HARD-CODED CONFIG #####################################################
CONF_DIR=/etc/prometheus
CONF=$CONF_DIR/prometheus.yml
LOGLEVEL=info
##### GET SETTINGS ##########################################################
config="$(cat "$__object/parameter/config")"
retention_days="$(cat "$__object/parameter/retention-days")"
storage_path="$(cat "$__object/parameter/storage-path")"
listen_address="$(cat "$__object/parameter/listen-address")"
alertmanager_url="$(cat "$__object/parameter/alertmanager-url")"
target_heap_size="$(cat "$__object/parameter/target-heap-size")"
rule_files="$(cat "$__object/parameter/rule-files")"
# explorer in kB => convert; by default we go with 1/2 RAM
[ "$target_heap_size" = "auto" ] && target_heap_size=$(($(cat $__global/explorer/memory)*1024/2))
##### INSTALL THE PACKAGE ###################################################
FLAGS="config.file '$CONF'
storage.local.path '$storage_path'
storage.local.target-heap-size $(($target_heap_size)) # in bytes; should be 2/3 of available memory because it may be hungry
storage.local.retention $(($retention_days*24))h # golang doesn't have days :D
web.listen-address '$listen_address'
alertmanager.url '$alertmanager_url'
log.level $LOGLEVEL"
if [ -f "$__object/parameter/install-from-backports" ]; then
os=$(cat "$__global/explorer/os")
lsb_codename=$(cat "$__global/explorer/lsb_codename")
REAL_FLAGS="$(echo "$FLAGS" | sed -nE 's/^([^#]+).*/ --\1 \\/p')"
case $os in
devuan)
if [ -z "$lsb_codename" ]; then
echo "Command `lsb_release` not functional -- is package `lsb-release` installed?" >&2
exit 1
fi
[ "$lsb_codename" = "n/a" ] && lsb_codename='ascii' # TODO this is a devuan bug that should be fixed soon => remove when no longer needed
__apt_source backports --uri http://auto.mirror.devuan.org/merged --distribution $lsb_codename-backports --component main
require="$require __apt_source/backports" __package_apt prometheus --target-release $lsb_codename-backports
;;
*)
echo "--install-from-backports is only supported on Devuan -- ignoring" >&2
echo "send a pull request if you require it" >&2
;;
esac
else
__package prometheus
fi
__go_get github.com/prometheus/prometheus/cmd/...
##### PREPARE PATHS AND SUCH ################################################
__user prometheus --system
require="__user/prometheus" __directory "$storage_path" --owner prometheus --parents
require="__user/prometheus" __directory "$CONF_DIR" --owner prometheus --parents
require="__package_apt/prometheus" __directory "$storage_path" --owner prometheus --parents
__daemontools_service prometheus --run "setuidgid prometheus $GOBIN/prometheus $REAL_FLAGS"
##### CONFIGURE #############################################################
require="$require __directory/$storage_path __user/prometheus" \
FLAGS="--storage.tsdb.path $storage_path --storage.tsdb.retention $(($retention_days*24))h"
# TODO it would be neat to restart prometheus on change -- __key_value really should have an --onchange parameter
require="$require __package_apt/prometheus" \
__key_value prometheus_args --file /etc/default/prometheus --key "ARGS" --value "\"$FLAGS\"" --delimiter "="
require="$require __directory/$storage_path __package_apt/prometheus" \
__config_file $CONF \
--source $config \
--group prometheus --mode 640 \
--onchange "$GOBIN/promtool check-config $CONF && svc -h /service/prometheus"
--onchange "promtool check config $CONF && service prometheus reload"
for file in $rule_files; do
dest=$CONF_DIR/$(basename $file)
require="$require __directory/$CONF_DIR __user/prometheus" \
require="$require __package_apt/prometheus" \
__config_file "$dest" \
--source "$file" \
--owner prometheus \
--onchange "$GOBIN/promtool check-rules '$dest' && svc -h /service/prometheus"
--onchange "promtool check rules '$dest' && service prometheus reload"
done

View file

@ -0,0 +1 @@
install-from-backports

View file

@ -1,3 +1 @@
alertmanager-url
config
listen-address