aboutsummaryrefslogtreecommitdiff
path: root/files/usr/local/etc/rc.d/myicinga2.icinga_server
blob: be10192500ae98735632f23a3d96366b7a58b3f0 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh

# PROVIDE: icinga2
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Unfortunately, we must duplicate the icinga rc script here in order
# to provide a mechanism to pass flags to the icinga2 executable.

. /etc/rc.subr

# Add /usr/local/bin to path, so that the notification scripts
# can work (#!/usr/bin/env bash)
export PATH=$PATH:/usr/local/bin:/usr/local/sbin

name="icinga2"
desc="Icinga 2 monitoring (core)"
rcvar=myicinga2_enable

load_rc_config "${name}"

: ${myicinga2_enable:="NO"}
: ${icinga2_configfile="/usr/local/etc/${name}/${name}.conf"}
: ${icinga2_user:="icinga"}
: ${icinga2_group:="icinga"}
: ${icinga2_webgroup:="www"}
: ${icinga2_flags:=""}


command="/usr/local/sbin/${name}"
procname="/usr/local/lib/icinga2/sbin/icinga2"
extra_commands="reload checkconfig configtest"

icinga2_cachedir="/var/cache/${name}"
icinga2_libdir="/var/lib/${name}"
icinga2_logdir="/var/log/icinga2"
icinga2_rundir="/var/run/${name}"
icinga2_spooldir="/var/spool/${name}"

pidfile="${icinga2_rundir}/${name}.pid"
icinga2_logfile="${icinga2_logdir}/${name}.log"
icinga2_errorlogfile="${icinga2_logdir}/error.log"

start_cmd="start_cmd"
start_precmd="start_precmd"
restart_precmd="icinga2_checkconfig"
reload_precmd="reload_precmd"
checkconfig_cmd="icinga2_checkconfig verbose"
configtest_cmd="${checkconfig_cmd}"
sig_reload=HUP

required_files="${icinga2_configfile}"
command_args="daemon -d -e ${icinga2_errorlogfile} -c ${icinga2_configfile} ${icinga2_flags}"

icinga2_checkconfig() {
  echo -n "Performing sanity check of icinga2 configuration: "

  if [ "$1" != "verbose" ]; then
    quietredir="2>&1 >/dev/null"
  fi

  ${command} daemon -c ${icinga2_configfile} -C

  if [ $? -ne 0 ]; then
    echo "FAILED"
    return 1
  else
    echo "OK"
  fi
}

reload_precmd() {
  if ! icinga2_checkconfig; then
    return 1
  fi
}

start_precmd() {
  # Create necessary directories / change ownership
  # While this is also done through pkg-plist, /var might be on a ramdisk,
  # so make sure all needed files and directories are created before starting
  # Icinga.
  for d in "${icinga2_logdir}" "${icinga2_logdir}/compat" \
           "${icinga2_logdir}/compat/archives" "${icinga2_libdir}" \
           "${icinga2_spooldir}" "${icinga2_spooldir}/tmp" \
           "${icinga2_rundir}" "${icinga2_cachedir}"; do
      if [ ! -d "${d}" ]; then
        install -d -o ${icinga2_user} -g ${icinga2_group} "${d}"
      fi
  done

  install -d -o ${icinga2_user} -g ${icinga2_webgroup} -m 2750 "${icinga2_rundir}/cmd"

  chown -R ${icinga2_user}:${icinga2_group} "${icinga2_libdir}"
  chown -R ${icinga2_user}:${icinga2_group} "${icinga2_spooldir}"
  chown -R ${icinga2_user}:${icinga2_group} "${icinga2_cachedir}"
  chown -R ${icinga2_user}:${icinga2_webgroup} "${icinga2_rundir}/cmd"


  if ! icinga2_checkconfig; then
    return 1
  fi

  if [ ! -f "${icinga2_logfile}" ]; then
    install -o "${icinga2_user}" -g "${icinga2_group}" -m 644 /dev/null "${icinga2_logfile}"
  fi
}

start_cmd() {
  ${command} ${command_args}
}

run_rc_command "$1"