4 pushd "$src_dir" >/dev
/null
2>&1
8 nagios_cfg
=/usr
/local
/nagios
/etc
/nagios.cfg
9 dest_dir
=/usr
/local
/nagios
/addons
/merlin
12 libexecdir
=/usr
/libexec
/merlin
20 install=db
,files
,config
,init
,apps
23 raw_sed_version
=$
(sed --version |
sed '1q')
24 sed_version
=$
(echo "$raw_sed_version" |
sed -e 's/[^0-9]*//' -e 's/[.]//g')
25 if [ "$sed_version" -lt 409 ]; then
26 echo "You need GNU sed version 4.0.9 or above for this script to work"
27 echo "Your sed claims to be \"$raw_sed_version\" ($sed_version)"
40 if ! grep -q "merlin.so" "$nagios_cfg"; then
41 say
"Adding merlin.so as eventbroker to nagios"
42 sed -i "s#^log_file.*#broker_module=$libexecdir/merlin.so $dest_dir/merlin.conf\\n\\n&#" \
47 if grep -q "$libexecdir/merlin.so" "$nagios_cfg"; then
48 say
"merlin.so is already a registered eventbroker in Nagios"
52 say
"Updating path to merlin.so in $nagios_cfg"
53 sed -i "s#broker_module.*merlin.so.*#broker_module=$libexecdir/merlin.so $dest_dir/merlin.conf#" \
60 expr "z$1" : 'z[^=]*=\(.*\)'
66 if [ -n "$db_root_user" ]; then
67 mysql
="$mysql -u$db_root_user"
68 test -n "$db_root_pass" && mysql
="$mysql -p$db_root_pass"
73 # Try to connect to database
74 $
(eval "$mysql -e ''")
76 echo "Couldn't connect to database, giving up."
80 # Create database if it do not exist
81 db_count
=$
(eval "$mysql -N -s -e \"SHOW DATABASES LIKE '$db_name'\"" |
wc -l)
82 if [ $db_count -eq 0 ]; then
83 echo "Creating database $db_name"
84 eval "$mysql -e \"CREATE DATABASE IF NOT EXISTS $db_name\""
87 # Always set privileges (to be on the extra safe side)
89 \"GRANT ALL ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass'\""
90 eval "$mysql -e 'FLUSH PRIVILEGES'"
92 eval "$mysql -f $db_name" < $src_dir/sql
/mysql
/merlin.sql
> /tmp
/merlin-sql-upgrade.log
2>&1
94 # Fetch db_version and do upgrade stuff if/when needed
95 query
="SELECT version FROM db_version"
96 db_version
=$
(eval "$mysql $db_name -BNe \"$query\"" 2>/dev
/null
)
98 # Check for upgrade scripts
100 if test "$db_version"; then
103 f
="$src_dir/sql/update-db-${ver}to${nextver}.sql"
104 test -f "$f" ||
break
105 eval "$mysql $db_name" < $f 2>&1 >>/tmp
/merlin-sql-upgrade.log || abort
"Incremental database upgrade from version $ver to version $nextver failed!"
106 eval "$mysql $db_name -e \"UPDATE db_version SET version=$nextver\"" 2>&1 >> /tmp
/merlin-sql-upgrade.log || abort
"Failed to set new database version $nextver!"
110 if [ $new_install -eq 1 ]; then
111 for index
in $src_dir/sql
/mysql
/*-indexes.sql
; do
112 eval "$mysql -f $db_name" < $index 2>&1 >>/tmp
/merlin-sql-upgrade.log
115 # only check for indexes in report_data. sloppy, yes, but should be sufficient
116 idx
=$
(eval "$mysql $db_name -N -s -e \"SHOW INDEX IN report_data\"" |
wc -l);
117 if [ $idx -eq '1' ]; then
123 Some of your tables lack indexes, which might cause bad performance.
124 Installing indexes might take quite some time (several hours for big installations), so I'm going
125 to let you install them when it's convenient for you.
127 To install them manually, run:
136 echo "Unknown database type '$db_type'"
137 echo "I understand only lower-case database types."
145 sed -e "s/@@DBNAME@@/$db_name/g" -e "s/@@DBTYPE@@/$db_type/g" \
146 -e "s/@@DBUSER@@/$db_user/g" -e "s/@@DBPASS@@/$db_pass/g" \
147 -e "s#@@NAGIOSCFG@@#$nagios_cfg#g" -e "s#@@DESTDIR@@#$dest_dir#g" \
148 -e "s#@@LIBEXECDIR@@#$libexecdir#g" \
149 -e "s#@@SRCDIR@@#$src_dir#g" -e "s#@@BINDIR@@#$bindir#g" \
155 local question options answer
156 question
="$1" options
="$2" default
="$3"
157 test "$batch" && { echo "$default"; return 0; }
160 echo -n "$question " >&2
162 case "$answer,$default" in
169 echo "$options " |
grep -q "$answer" && break
172 echo "Please answer one of '$options'" >&2
179 test "$batch" ||
echo "$@"
184 mkdir
-p $root_path/$bindir
185 mkdir
-p $root_path/$libexecdir/mon
186 rm -f "$root_path/$bindir/mon"
187 rm -f "$root_path/$bindir/op5"
188 macro_subst
"$src_dir/apps/mon.py" > "$root_path/$bindir/op5"
189 ln -s op5
"$root_path/$bindir/mon"
190 cp -a apps
/libexec
/* $root_path/$libexecdir/mon
191 rm -f $root_path/$libexecdir/mon
/-oconf
192 cp oconf
$root_path/$libexecdir/mon
/-oconf
193 chmod 755 $root_path/$bindir/op5
194 chmod 755 $root_path/$libexecdir/mon
/*
199 bins
="merlind" # user-visible binaries
200 libexecs
="import ocimp merlin.so showlog" # binaries that are outside of /opt/monitor lockdown
201 files
="install-merlin.sh rename" # files going straight to /opt/monitor/op5/merlin/
202 subst
="example.conf init.sh" # files needing substitution
203 execs
="import ocimp showlog install-merlin.sh init.sh rename merlind" # everything that should +x
205 for i
in $bins $libexecs $files $execs $subst; do
206 if ! test -f "$src_dir/$i"; then
207 echo "$src_dir/$i is missing"
208 missing
="$missing $src_dir/$i"
211 test "$missing" && abort
"Essential files are missing. Perhaps you need to run 'make'?"
213 test -d "$root_path/$dest_dir" || mkdir
-p -m 755 "$root_path/$dest_dir"
214 test -d "$root_path/$dest_dir/logs" || mkdir
-p -m 777 "$root_path/$dest_dir/logs"
215 test -d "$root_path/$dest_dir" ||
{ echo "$root_path/$dest_dir is not a directory"; return 1; }
216 test -d "$root_path/$libexecdir" || mkdir
-p -m 755 "$root_path/$libexecdir"
219 cp "$src_dir/$f" "$root_path/$dest_dir"
221 mkdir
-p "$root_path/$dest_dir/sql/mysql/"
222 cp -r "$src_dir/sql" "$root_path/$dest_dir"
223 macro_subst
"$src_dir/example.conf" > "$root_path/$dest_dir/sample.merlin.conf"
224 test -f "$root_path/$dest_dir/merlin.conf" || \
225 cp "$root_path/$dest_dir/sample.merlin.conf" \
226 "$root_path/$dest_dir/merlin.conf"
227 mkdir
-p $root_path/$bindir
229 cp "$src_dir/$f" "$root_path/$bindir"
231 for f
in $libexecs; do
232 cp "$src_dir/$f" "$root_path/$libexecdir"
238 if [ $
(id
-u) -eq 0 -o "$root_path" ]; then
239 init_path
="$root_path/etc/init.d"
240 test -d "$init_path" || mkdir
-p "$init_path"
241 macro_subst
"$src_dir/init.sh" > "$init_path/merlind"
242 chmod 755 "$init_path/merlind"
244 say
"Lacking root permissions, so not installing init-script."
252 usage: $progname [options]
254 Where options can be any combination of:
255 --help|-h Print this cruft and exit
256 --nagios-cfg=</path/to/nagios.cfg> Path to nagios.cfg
257 --dest-dir=</install/directory> Where to install Merlin
258 --batch Assume 'yes' to all questions
259 --root=</path/to/fakeroot> Useful for package builders
260 --install=<db,config,files,init> Components to install. Any combo works
261 --db-name=<database name> Name of database to modify
262 --db-type=<mysql> Database type. Only mysql for now
263 --db-user=<username> User merlin should use with db
264 --db-pass=<password> Password for the db user
265 --db-root-user=<db admin> Database admin username
266 --db-root-pass=<pass> Database admin password
267 --bindir=/path/to/binaries Usually /usr/bin
268 --libexecdir=/path/to/libexecdir Usually /usr/libexec/merlin
277 nagios_cfg
=$
(get_arg
"$1")
284 dest_dir
=$
(get_arg
"$1")
291 db_name
=$
(get_arg
"$1")
298 db_type
=$
(get_arg
"$1")
305 db_user
=$
(get_arg
"$1")
312 db_pass
=$
(get_arg
"$1")
319 db_root_user
=$
(get_arg
"$1")
326 db_root_pass
=$
(get_arg
"$1")
336 install=$
(get_arg
"$1")
343 root_path
=$
(get_arg
"$1")
350 libexecdir
=$
(get_arg
"$1")
357 bindir
=$
(get_arg
"$1")
367 echo "Illegal argument. I have no idea what to make of '$1'"
374 if [ "$db_pass" = "generate" ]; then
375 db_pass
=$
(dd if=/dev
/random bs
=32 count
=1 |
sha1sum |
sed -n '$s/\([0-9a-f]*\).*/\1/p')
378 for c
in $
(echo "$install" |
sed 's/,/ /g'); do
380 files|db|config|init|apps
) ;;
382 echo "I don't know how to install component $c"
383 echo "You may only pass one or more of 'db,files,config' to --install"
384 echo "and you must pass one of them if you use --install"
392 Type (--db-type): $db_type
393 Name (--db-name): $db_name
396 Nagios config file (--nagios-cfg): $nagios_cfg
397 Destination directory (--dest-dir): $dest_dir
398 libexecdir (--libexecdir): $libexecdir
399 bindir (--bindir): $bindir
400 Base root (--root): $root_path
402 Installing the following components: $install
405 case $
(ask
"Does this look ok? [Y/n]" "ynYN" y
) in
406 n|N
) echo "Aborting installation"; exit 1;;
410 for i
in db config files apps
; do
411 echo "$install" |
grep -q $i && components
="$i"
413 if ! test "$components"; then
414 echo "### No components selected to install."
415 echo "### You must pass one or more of 'db', 'config' and 'files'"
416 echo "### to the --install argument"
424 if echo "$install" |
grep -q 'files'; then
425 install_files || abort
"Failed to install files."
427 if echo "$install" |
grep -q 'init'; then
428 install_init || abort
"Failed to install merlind init script"
430 if echo "$install" |
grep -q 'db'; then
431 db_setup || abort
"Failed to setup database."
433 if echo "$install" |
grep -q 'config'; then
434 modify_nagios_cfg || abort
"Failed to modify Nagios config."
436 if echo "$install" |
grep -q 'apps'; then
437 install_apps || abort
"Failed to install apps"
441 say
"Installation successfully completed"
443 say
"You will need to restart Nagios and start Merlind for changes to take effect"