forked from BrutalBirdie/eze-openHAB-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ckm_agent.sh
92 lines (87 loc) · 4.78 KB
/
ckm_agent.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#### CHECKMK AGENT CONFIGURATION ####
read -p "Do you want to install checkmk agent of this machine? [y/N] " install_checkmk
if [[ "$install_checkmk" == "y" ]]; then
read -p "Please specify the base url to your checkmk server (e.g. https://check.zmb.rocks/bashclub): " cmk_agent_url
read -p "Enable agent encryption (requires setup of Agent Encryption on your checkmk instance). Do you want to activate agent encryption? [y/N] " cmk_encrypt
if [[ "$cmk_encrypt" == "y" ]]; then
read -p "Please enter the encryption passphrase: " cmk_enc_pass
fi
read -p "Register your machine on your checkmk server (requires preconfigured automation secret)? [y/N] " cmk_register
if [[ "$cmk_register" == "y" ]]; then
read -p "Please enter your automation secret: " cmk_secret
read -p "Please enter the folder where to store the host: " cmk_folder
cmk_site=$(echo $cmk_agent_url | cut -d'/' -f4)
read -p "Please enter the checkmk site name: [$cmk_site]" user_input
if [[ $(echo -n "$user_input") != "" ]]; then
cmk_site=$user_input
fi
echo "Please select which agent ip address to register:"
select ip in $(ip a | grep "inet " | cut -d ' ' -f6 | cut -d/ -f1); do
cmk_reg_ip=$ip
break
done
fi
fi
if [[ "$install_checkmk" == "y" ]]; then
echo "Installing checkmk agent..."
if [[ $( echo -n "$(openssl s_client -connect $(echo $cmk_agent_url | cut -d'/' -f3):443 <<< "Q" 2>/dev/null | grep "Verify return code" | cut -d ' ' -f4)" ) -gt 0 ]]; then
wget_opts="--no-check-certificate"
curl_opts="--insecure"
fi
wget -q -O /usr/local/bin/check_mk_agent $wget_opts $cmk_agent_url/check_mk/agents/check_mk_agent.linux
wget -q -O /usr/local/bin/mk-job $wget_opts $cmk_agent_url/check_mk/agents/mk-job
wget -q -O /usr/local/bin/check_mk_caching_agent $wget_opts $cmk_agent_url/check_mk/agents/check_mk_caching_agent.linux
wget -q -O /usr/local/bin/waitmax $wget_opts $cmk_agent_url/check_mk/agents/waitmax
chmod +x /usr/local/bin/check_mk_agent
chmod +x /usr/local/bin/mk-job
chmod +x /usr/local/bin/check_mk_caching_agent
chmod +x /usr/local/bin/waitmax
/usr/local/bin/check_mk_agent > /dev/null
wget -q -O /etc/systemd/system/check_mk.socket $wget_opts $cmk_agent_url/check_mk/agents/cfg_examples/systemd/check_mk.socket
cat << EOF > /etc/systemd/system/check_mk@.service
# systemd service definition file
[Unit]
Description=Check_MK
[Service]
# "-" path prefix makes systemd record the exit code,
# but the unit is not set to failed.
ExecStart=-/usr/local/bin/check_mk_agent
Type=forking
User=root
Group=root
StandardInput=socket
EOF
mkdir -p /etc/check_mk
if [[ "$cmk_encrypt" == "y" ]]; then
mkdir -p /etc/check_mk
cat << EOF > /etc/check_mk/encryption.cfg
ENCRYPTED=yes
PASSPHRASE='$cmk_enc_pass'
EOF
chmod 600 /etc/check_mk/encryption.cfg
fi
mkdir -p /var/lib/check_mk_agent
mkdir -p /var/lib/check_mk_agent/spool
mkdir -p /var/lib/check_mk_agent/job
mkdir -p /usr/lib/check_mk_agent/local
mkdir -p /usr/lib/check_mk_agent/plugins
wget -q -O /usr/lib/check_mk_agent/plugins/smart $wget_opts $cmk_agent_url/check_mk/agents/plugins/smart
chmod +x /usr/lib/check_mk_agent/plugins/smart
wget -q -O /usr/lib/check_mk_agent/plugins/mk_inventory $wget_opts $cmk_agent_url/check_mk/agents/plugins/mk_inventory.linux
chmod +x /usr/lib/check_mk_agent/plugins/mk_inventory
wget -q -O /usr/lib/check_mk_agent/plugins/mk_docker.py $wget_opts $cmk_agent_url/check_mk/agents/plugins/mk_docker.py
chmod +x /usr/lib/check_mk_agent/plugins/mk_docker.py
#Plugins inside /usr/lib/check_mk_agent/plugins/86400 only run once a day (every 86400 seconds)
mkdir -p /usr/lib/check_mk_agent/plugins/86400
wget -q -O /usr/lib/check_mk_agent/plugins/86400/mk_apt $wget_opts $cmk_agent_url/check_mk/agents/plugins/mk_apt
chmod +x /usr/lib/check_mk_agent/plugins/86400/mk_apt
#LocalDirectory: /usr/lib/check_mk_agent/local
systemctl daemon-reload
systemctl enable check_mk.socket
systemctl restart sockets.target
if [[ "$cmk_register" == "y" ]]; then
cmk_request="request={\"hostname\":\"$(echo -n $(hostname -f))\",\"folder\":\"$cmk_folder\",\"attributes\":{\"ipaddress\":\"$cmk_reg_ip\",\"site\":\"$cmk_site\",\"tag_agent\":\"cmk-agent\"},\"create_folders\":\"1\"}"
curl $curl_opts "$cmk_agent_url/check_mk/webapi.py?action=add_host&_secret=$cmk_secret&_username=automation" -d $cmk_request
curl $curl_opts "$cmk_agent_url/check_mk/webapi.py?action=activate_changes&_secret=$cmk_secret&_username=automation" -d "request={\"sites\":[\"$cmk_site\"],\"allow_foreign_changes\":\"0\"}"
fi
fi