summaryrefslogtreecommitdiff
path: root/monitoring/icinga-agent
diff options
context:
space:
mode:
Diffstat (limited to 'monitoring/icinga-agent')
-rwxr-xr-xmonitoring/icinga-agent108
1 files changed, 108 insertions, 0 deletions
diff --git a/monitoring/icinga-agent b/monitoring/icinga-agent
new file mode 100755
index 0000000..328d65b
--- /dev/null
+++ b/monitoring/icinga-agent
@@ -0,0 +1,108 @@
1#!/bin/sh
2#
3# Configirues an icinga2 agent (with on-demand csr signing)
4
5icingauser="nagios"
6certdir="/etc/icinga2/pki"
7api_certdir="/var/lib/icinga2/certs"
8nodename="$(hostname)"
9global_zone="director-global"
10master_fqdn=""
11
12# Install packages
13apt install -y icinga2 monitoring-plugins monitoring-plugins-contrib
14
15# Register with master via self-service API
16apikey=""
17displayname=""
18# Not pretty but gets the job done
19dev="$(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)"
21ipv6="$(ip addr show $dev | grep "inet6 " | sed "s/^\s*//;s/\// /" | cut -d ' ' -f 2)"
22
23result=$(curl -i "http://$master_fqdn/icingaweb2/director/self-service/register-host?name=$nodename&key=$apikey" \
24 -H "Accept: application/json" \
25 -X "POST" \
26 -d "{\"display_name\":\"$displayname\",\"address\":\"$ipv4\",\"address6\":\"$ipv6\"}")
27echo $result | grep -q error && \
28 echo "error: unable to register with master (is the api key correct?)" && \
29 exit 2
30
31
32# Initialize PKI with master
33icinga2 pki new-cert \
34 --cn "pbs.home.local" \
35 --cert "$certdir/$nodename.crt" \
36 --csr "$certdir/$nodename.csr" \
37 --key "$certdir/$nodename.key"
38
39
40icinga2 pki save-cert \
41 --host "$master_fqdn" \
42 --port 5665 \
43 --key "$certdir/$nodename.key" \
44 --trustedcert "$certdir/trusted-master.crt"
45
46icinga2 pki request \
47 --host "$master_fqdn" \
48 --port 5665 \
49 --key "$certdir/$nodename.key" \
50 --cert "$certdir/$nodename.crt" \
51 --trustedcert "$certdir/trusted-master.crt" \
52 --ca "$certdir/ca.crt"
53
54# Deploy config files
55echo "include \"constants.conf\"
56const NodeName = \"$nodename\"
57include \"zones.conf\"
58include \"features-enabled/*.conf\"
59include <itl>
60include <plugins>
61include <plugins-contrib>
62include <manubulon>
63include <windows-plugins>
64include <nscp>" > /etc/icinga2/icinga2.conf
65
66echo "object Endpoint \"$nodename\" {}
67object Zone \"$nodename\" {
68 parent = \"$master_fqdn\"
69 endpoints = [ \"$nodename\" ]
70}
71object Zone \"$master_fqdn\" {
72 endpoints = [ \"$master_fqdn\" ]
73}
74object Endpoint \"$master_fqdn\" {
75 host = \"$master_fqdn\"
76}
77object Zone \"$global_zone\" {
78 global = true
79}" > /etc/icinga2/zones.conf
80
81echo "object ApiListener \"api\" {
82 accept_commands = true
83 accept_config = true
84}" > /etc/icinga2/features-available/api.conf
85
86# Enable API
87icinga2 feature enable api
88mkdir -p $api_certdir
89cp $certdir/$nodename.crt $certdir/$nodename.key $certdir/ca.crt $api_certdir/
90chown -R $icingauser:$icingauser $api_certdir/
91
92# Next step
93echo "
94
95NOW
96
97Run the following on the Icinga master:
98fpr=\"\$(icinga2 ca list | tail -1 | cut -d '|' -f 1)\"
99icinga2 ca sign \$fpr
100
101
102THEN
103
104Restart icinga2 on the agent:
105\"systemctl restart icinga2\"
106
107"
108