29a05cdff582cad64fcd441465591f8180090714
[monit-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for monit-cn
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 # summary of how this script can be called:
9 #        * <postinst> `configure' <most-recently-configured-version>
10 #        * <old-postinst> `abort-upgrade' <new version>
11 #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
12 #          <new-version>
13 #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
14 #          <failed-install-package> <version> `removing'
15 #          <conflicting-package> <version>
16 # for details, see http://www.debian.org/doc/debian-policy/ or
17 # the debian-policy package
18 #
19
20 case "$1" in
21     configure|reconfigure)
22       # continue below
23     ;;
24
25     *)
26         exit 0
27     ;;
28 esac
29
30 # import CN-functions
31 . /usr/share/carnet-tools/functions.sh
32
33 # check if monitrc is ours
34 installfile=1
35 if [ -e /etc/monit/monitrc ]; then
36     if ! grep -q "CARNet package monit-cn" /etc/monit/monitrc; then
37         installfile=1
38         mv -f /etc/monit/monitrc /etc/monit/monitrc.dpkg-old
39     else
40         installfile=0
41     fi
42 fi
43 if [ $installfile -eq 1 ]; then
44     echo "CN: Installing default monitrc in /etc/monit/monitrc"
45     cp -a /usr/share/monit-cn/monitrc /etc/monit/monitrc
46     chmod u=rw,go= /etc/monit/monitrc
47 fi
48
49 # sed monitrc with proper hostname
50 if grep -q '@localhost' /etc/monit/monitrc; then
51     CARNET_HOSTNAME=`hostname`
52     CARNET_DOMAINNAME=`hostname --domain`
53     cp_check_and_sed '@localhost' \
54         "s/@localhost/@$CARNET_HOSTNAME.$CARNET_DOMAINNAME/g" \
55         /etc/monit/monitrc || true
56 fi
57
58 # migrate to "but not on { instance }" alerting style
59 cp_check_and_sed '^set alert [^[:space:]]*@[^[:space:]]* ' \
60     's/^set alert \([^[:space:]]*@[^[:space:]]*\) .*$/set alert \1 but not on { instance }/g' \
61     /etc/monit/monitrc || true
62
63 # migrate from old hierarchy
64 if [ -d /etc/monit.d ]; then
65     find /etc/monit.d -type f -print0 | xargs -0r -I% mv % /etc/monit/conf.d || true
66     rmdir /etc/monit.d >/dev/null 2>&1 || true
67 fi
68
69 # (re)generate monit.d files
70 update-monit.d || true
71
72 # disable monit startup
73 cp_check_and_sed '^startup[[:space:]]*=[[:space:]]*0' \
74     's/^startup[[:space:]]*=.*/startup=1/g' /etc/default/monit || true
75 cp_check_and_sed '^START[[:space:]]*=[[:space:]]*no' \
76     's/^START[[:space:]]*=.*/START=yes/g' /etc/default/monit || true
77
78 # remove us from systemv monit script
79 if grep -q 'CARNet package monit-cn' /etc/init.d/monit; then
80   echo "CN: Removing monit workaround from /etc/init.d/monit -- systemd is in use"
81   cp-update -r monit-cn /etc/init.d/monit
82   chmod +x /etc/init.d/monit
83 fi
84
85 service monit restart
86
87 #DEBHELPER#
88
89 exit 0