summaryrefslogtreecommitdiff
path: root/monitoring/icinga-agent
diff options
context:
space:
mode:
authorSam Chudnick <sam@chudnick.com>2022-04-17 12:29:35 -0400
committerSam Chudnick <sam@chudnick.com>2022-04-17 12:29:35 -0400
commitccc0e036fd52e84ce47af4ad11cb6ecd271309c0 (patch)
tree106b33afe8bf3ec61515f194261aa6ae531ab4e2 /monitoring/icinga-agent
parent85c561f9a32f8f2b9ddf34e7d60ef4b7bf0d3680 (diff)
better error handling
Diffstat (limited to 'monitoring/icinga-agent')
-rwxr-xr-xmonitoring/icinga-agent59
1 files changed, 47 insertions, 12 deletions
diff --git a/monitoring/icinga-agent b/monitoring/icinga-agent
index 328d65b..e4690fe 100755
--- a/monitoring/icinga-agent
+++ b/monitoring/icinga-agent
@@ -1,37 +1,72 @@
1#!/bin/sh 1#!/bin/sh
2# 2#
3# Configirues an icinga2 agent (with on-demand csr signing) 3# Configures an icinga2 agent (with on-demand csr signing)
4 4
5icingauser="nagios" 5icingauser="nagios"
6certdir="/etc/icinga2/pki" 6certdir="/etc/icinga2/pki"
7api_certdir="/var/lib/icinga2/certs" 7api_certdir="/var/lib/icinga2/certs"
8nodename="$(hostname)" 8nodename="$(hostname)"
9global_zone="director-global" 9global_zone="director-global"
10master_fqdn="" 10
11apikey=
12displayname=
13master_fqdn=
14
15help() {
16 echo "usage: icinga-agent --apikey apikey --display name --master master_fqdn"
17 echo "-a, --apikey:\t self-service api key to register with"
18 echo "-d, --display:\t display name for host in Icinga"
19 echo "-m, --master:\t full hostname of Icinga master (e.g. monitoring.example.com)"
20 exit 1
21}
22
23error() {
24 echo "error: $1"
25 exit 2
26}
27
28[ $(id -u) -ne 0 ] && echo "error: must be run as root" && exit 1
29
30opts=$(getopt -o "a:,d:,h,m:" -l "apikey:,display:,help,master:" -- "$@")
31eval set -- "$opts"
32while true
33do
34 case "$1" in
35 '-a' | '--apikey') apikey="$2" shift 2; continue ;;
36 '-d' | '--display') displayname="$2" shift 2; continue ;;
37 '-m' | '--master') master_fqdn="$2" shift 2; continue ;;
38 '-h' | '--help') help ;;
39 '--') shift; break ;;
40 esac
41done
42
43[ -z "$apikey" ] && help
44[ -z "$displayname" ] && help
45[ -z "$master_fqdn" ] && help
46
11 47
12# Install packages 48# Install packages
13apt install -y icinga2 monitoring-plugins monitoring-plugins-contrib 49apt install -y icinga2 monitoring-plugins monitoring-plugins-contrib
14 50
15# Register with master via self-service API 51# Register with master via self-service API
16apikey=""
17displayname=""
18# Not pretty but gets the job done 52# Not pretty but gets the job done
19dev="$(ip link | grep ^2: | head -1 | cut -d':' -f 2 | tr -d ' ')" 53dev="$(ip link | grep ^2: | head -1 | cut -d':' -f 2 | tr -d ' ')"
20ipv4="$(ip addr show $dev | grep "inet " | sed "s/^\s*//;s/\// /" | cut -d ' ' -f 2)" 54ipv4="$(ip addr show $dev | grep "inet " | sed "s/^\s*//;s/\// /" | cut -d ' ' -f 2)"
21ipv6="$(ip addr show $dev | grep "inet6 " | sed "s/^\s*//;s/\// /" | cut -d ' ' -f 2)" 55ipv6="$(ip addr show $dev | grep "inet6 " | sed "s/^\s*//;s/\// /" | cut -d ' ' -f 2)"
22 56
23result=$(curl -i "http://$master_fqdn/icingaweb2/director/self-service/register-host?name=$nodename&key=$apikey" \ 57proto="http"
24 -H "Accept: application/json" \ 58base="$proto://$master_fqdn/icingaweb2/director/self-service/register-host"
25 -X "POST" \ 59url="$base?name=$nodename&key=$apikey"
26 -d "{\"display_name\":\"$displayname\",\"address\":\"$ipv4\",\"address6\":\"$ipv6\"}") 60result=$(curl -m 30 -i $url -H "Accept: application/json" -X "POST" \
27echo $result | grep -q error && \ 61-d "{\"display_name\":\"$displayname\",\"address\":\"$ipv4\",\"address6\":\"$ipv6\"}" \
28 echo "error: unable to register with master (is the api key correct?)" && \ 62|| error "unable to register with master")
29 exit 2 63
64echo $result | grep -q "error" && error "unable to register with master"
30 65
31 66
32# Initialize PKI with master 67# Initialize PKI with master
33icinga2 pki new-cert \ 68icinga2 pki new-cert \
34 --cn "pbs.home.local" \ 69 --cn "$nodename" \
35 --cert "$certdir/$nodename.crt" \ 70 --cert "$certdir/$nodename.crt" \
36 --csr "$certdir/$nodename.csr" \ 71 --csr "$certdir/$nodename.csr" \
37 --key "$certdir/$nodename.key" 72 --key "$certdir/$nodename.key"