Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Removing deprecated fields on apilistener resource and old notify scripts #325

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .kitchen.dokken.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ suites:
verifier:
inspec_tests:
- test/smoke/server
attributes:
install_monitoring_plugins: true
run_list:
- recipe[icinga2::default]
- recipe[icinga2-test::default]
Expand Down
2 changes: 2 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
driver:
name: vagrant
customize:
audio: none

verifier:
name: inspec
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ icinga2 CHANGELOG

This file is used to list changes made in each version of the icinga2 cookbook.

- Alberto Lorenzo - Workaround for VBox 5.2.8+ `Stderr: VBoxManage: error: The specified string / bytes buffer was to small` https://github.com/hashicorp/vagrant/issues/9524
- Alberto Lorenzo - Implement install monitoring plugins with boolean default['icinga2']['install_monitoring_plugins']
- Alberto Lorenzo - Updated old notification scripts
- Alberto Lorenzo - Fix deprecated fields on apilistener
- Alberto Lorenzo - Fix service config file removing the `$` (https://github.com/Icinga/icinga2/issues/6434)


4.0.0
-----

Expand Down
3 changes: 0 additions & 3 deletions LWRP.md
Original file line number Diff line number Diff line change
Expand Up @@ -1370,9 +1370,6 @@ LWRP `apilistener` creates an icinga `ApiListener` object.

```ruby
icinga2_apilistener 'master' do
cert_path 'SysconfDir + "/icinga2/pki/" + NodeName + ".crt"'
key_path 'SysconfDir + "/icinga2/pki/" + NodeName + ".key"'
ca_path 'SysconfDir + "/icinga2/pki/ca.crt"'
bind_host 'host address'
bind_port '5665'
ticket_salt 'TicketSalt'
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,13 @@ See LWRP.md for icinga2 resources.




## Cookbook Core Attributes

* `default['icinga2']['version']` (default: `2.8.0-X, calculated`): icinga2 package version

* `default['icinga2']['install_monitoring_plugins']` (default: `false`): whether to install monitoring-plugins (install_monitoring_plugins) or not

* `default['icinga2']['setup_epel']` (default: `true`): if set includes cookbook recipe `yum-epel::default` for rhel and fedora platform_family

* `default['icinga2']['cookbook']` (default: `icinga2`): icinga2 resources cookbook name
Expand Down
9 changes: 9 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
%w(itl plugins)
end

# Install monitoring-plugins
default['icinga2']['install_monitoring_plugins'] = false

# includes yum-epel cookbook to setup yum epel repository
default['icinga2']['setup_epel'] = true

Expand Down Expand Up @@ -85,6 +88,7 @@

case node['platform_family']
when 'fedora', 'rhel', 'amazon'
default['icinga2']['monitoring_plugins_packages'] = ['nagios-plugins-all']
default['icinga2']['user'] = 'icinga'
default['icinga2']['group'] = 'icinga'
default['icinga2']['cmdgroup'] = 'icingacmd'
Expand All @@ -97,6 +101,11 @@
end

when 'debian'
default['icinga2']['monitoring_plugins_packages'] = if node['platform_version'].to_f <= 14.04
['nagios-plugins']
else
['monitoring-plugins']
end
default['icinga2']['user'] = 'nagios'
default['icinga2']['group'] = 'nagios'
default['icinga2']['cmdgroup'] = 'nagios'
Expand Down
29 changes: 1 addition & 28 deletions libraries/resource_apilistener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,6 @@ def cookbook(arg = nil)
)
end

def cert_path(arg = nil)
set_or_return(
:cert_path, arg,
:kind_of => String,
:required => true,
:default => nil
)
end

def key_path(arg = nil)
set_or_return(
:key_path, arg,
:kind_of => String,
:required => true,
:default => nil
)
end

def ca_path(arg = nil)
set_or_return(
:ca_path, arg,
:kind_of => String,
:required => true,
:default => nil
)
end

def crl_path(arg = nil)
set_or_return(
:crl_path, arg,
Expand Down Expand Up @@ -110,7 +83,7 @@ def resource_properties(arg = nil)
set_or_return(
:resource_properties, arg,
:kind_of => Array,
:default => %w(cert_path key_path ca_path crl_path bind_host bind_port ticket_salt accept_config accept_commands)
:default => %w(crl_path bind_host bind_port ticket_salt accept_config accept_commands)
)
end
end
Expand Down
1 change: 1 addition & 0 deletions recipes/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
variables(
:log_dir => node['icinga2']['log_dir'],
:conf_dir => node['icinga2']['conf_dir'],
:run_dir => node['icinga2']['run_dir'],
:user => node['icinga2']['user'],
:group => node['icinga2']['group'],
:cmdgroup => node['icinga2']['cmdgroup']
Expand Down
7 changes: 7 additions & 0 deletions recipes/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
action :install
end

node['icinga2']['monitoring_plugins_packages'].each do |pkg|
package pkg do
action :install
only_if { node['icinga2']['install_monitoring_plugins'] }
end
end

package 'icinga2' do
version node['icinga2']['version'] + node['icinga2']['version_suffix'] unless node['icinga2']['ignore_version']
notifies :restart, 'service[icinga2]', :delayed
Expand Down
4 changes: 2 additions & 2 deletions templates/default/icinga2.service.config.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#
DAEMON=/usr/sbin/icinga2
ICINGA2_CONFIG_FILE=<%= @conf_dir -%>/icinga2.conf
ICINGA2_RUN_DIR=/var/run
ICINGA2_RUN_DIR=<%= @run_dir %>
ICINGA2_STATE_DIR=/var
ICINGA2_PID_FILE=$ICINGA2_RUN_DIR/icinga2/icinga2.pid
ICINGA2_PID_FILE=<%= @run_dir %>/icinga2.pid
ICINGA2_ERROR_LOG=<%= @log_dir -%>/error.log
ICINGA2_STARTUP_LOG=<%= @log_dir -%>/startup.log
ICINGA2_LOG=<%= @log_dir -%>/icinga2.log
Expand Down
179 changes: 164 additions & 15 deletions templates/default/mail-host-notification.sh.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,175 @@
#!/usr/bin/env bash
#
# Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)
#
# Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)
# Except of function urlencode which is Copyright (C) by Brian White ([email protected]) used under MIT license

# This file is managed by Chef.
# Do NOT modify this file directly.
#
template=$(cat <<TEMPLATE
***** Icinga *****

Notification Type: $NOTIFICATIONTYPE
PROG="`basename $0`"
ICINGA2HOST="`hostname`"
MAILBIN="mail"

if [ -z "`which $MAILBIN`" ] ; then
echo "$MAILBIN not found in \$PATH. Consider installing it."
exit 1
fi

## Function helpers
Usage() {
cat << EOF

Required parameters:
-d LONGDATETIME (\$icinga.long_date_time\$)
-e SERVICENAME (\$service.name\$)
-l HOSTNAME (\$host.name\$)
-n HOSTDISPLAYNAME (\$host.display_name\$)
-o SERVICEOUTPUT (\$service.output\$)
-r USEREMAIL (\$user.email\$)
-s SERVICESTATE (\$service.state\$)
-t NOTIFICATIONTYPE (\$notification.type\$)
-u SERVICEDISPLAYNAME (\$service.display_name\$)

Optional parameters:
-4 HOSTADDRESS (\$address\$)
-6 HOSTADDRESS6 (\$address6\$)
-b NOTIFICATIONAUTHORNAME (\$notification.author\$)
-c NOTIFICATIONCOMMENT (\$notification.comment\$)
-i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset)
-f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE))
-v (\$notification_sendtosyslog\$, Default: false)

EOF
}

Help() {
Usage;
exit 0;
}

Error() {
if [ "$1" ]; then
echo $1
fi
Usage;
exit 1;
}

urlencode() {
local LANG=C i c e=''
for ((i=0;i<${#1};i++)); do
c=${1:$i:1}
[[ "$c" =~ [a-zA-Z0-9\.\~\_\-] ]] || printf -v c '%%%02X' "'$c"
e+="$c"
done
echo "$e"
}

## Main
while getopts 4:6:b:c:d:e:f:hi:l:n:o:r:s:t:u:v: opt
do
case "$opt" in
4) HOSTADDRESS=$OPTARG ;;
6) HOSTADDRESS6=$OPTARG ;;
b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
c) NOTIFICATIONCOMMENT=$OPTARG ;;
d) LONGDATETIME=$OPTARG ;; # required
e) SERVICENAME=$OPTARG ;; # required
f) MAILFROM=$OPTARG ;;
h) Usage ;;
i) ICINGAWEB2URL=$OPTARG ;;
l) HOSTNAME=$OPTARG ;; # required
n) HOSTDISPLAYNAME=$OPTARG ;; # required
o) SERVICEOUTPUT=$OPTARG ;; # required
r) USEREMAIL=$OPTARG ;; # required
s) SERVICESTATE=$OPTARG ;; # required
t) NOTIFICATIONTYPE=$OPTARG ;; # required
u) SERVICEDISPLAYNAME=$OPTARG ;; # required
v) VERBOSE=$OPTARG ;;
\?) echo "ERROR: Invalid option -$OPTARG" >&2
Usage ;;
:) echo "Missing option argument for -$OPTARG" >&2
Usage ;;
*) echo "Unimplemented option: -$OPTARG" >&2
Usage ;;
esac
done

shift $((OPTIND - 1))

## Keep formatting in sync with mail-host-notification.sh
for P in LONGDATETIME HOSTNAME HOSTDISPLAYNAME SERVICENAME SERVICEDISPLAYNAME SERVICEOUTPUT SERVICESTATE USEREMAIL NOTIFICATIONTYPE ; do
eval "PAR=\$${P}"

if [ ! "$PAR" ] ; then
Error "Required parameter '$P' is missing."
fi
done

## Build the message's subject
SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!"

## Build the notification message
NOTIFICATION_MESSAGE=`cat << EOF
***** Service Monitoring on $ICINGA2HOST *****

$SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!

Info: $SERVICEOUTPUT

When: $LONGDATETIME
Service: $SERVICENAME
Host: $HOSTNAME
EOF
`

## Check whether IPv4 was specified.
if [ -n "$HOSTADDRESS" ] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
IPv4: $HOSTADDRESS"
fi

## Check whether IPv6 was specified.
if [ -n "$HOSTADDRESS6" ] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
IPv6: $HOSTADDRESS6"
fi

## Check whether author and comment was specified.
if [ -n "$NOTIFICATIONCOMMENT" ] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE

Comment by $NOTIFICATIONAUTHORNAME:
$NOTIFICATIONCOMMENT"
fi

## Check whether Icinga Web 2 URL was specified.
if [ -n "$ICINGAWEB2URL" ] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE

Host: $HOSTALIAS
Address: $HOSTADDRESS
State: $HOSTSTATE
$ICINGAWEB2URL/monitoring/service/show?host=$(urlencode "$HOSTNAME")&service=$(urlencode "$SERVICENAME")"
fi

Date/Time: $LONGDATETIME
## Check whether verbose mode was enabled and log to syslog.
if [ "$VERBOSE" == "true" ] ; then
logger "$PROG sends $SUBJECT => $USEREMAIL"
fi

Additional Info: $HOSTOUTPUT
## Send the mail using the $MAILBIN command.
## If an explicit sender was specified, try to set it.
if [ -n "$MAILFROM" ] ; then

Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
TEMPLATE
)
## Modify this for your own needs!

/usr/bin/printf "%b" "$template" | mail -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME is $HOSTSTATE" $USEREMAIL
## Debian/Ubuntu use mailutils which requires `-a` to append the header
if [ -f /etc/debian_version ]; then
/usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL
## Other distributions (RHEL/SUSE/etc.) prefer mailx which sets a sender address with `-r`
else
/usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $MAILBIN -r "$MAILFROM" -s "$SUBJECT" $USEREMAIL
fi

else
/usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
| $MAILBIN -s "$SUBJECT" $USEREMAIL
fi
Loading