]> granicus.if.org Git - icinga2/blob - tools/migrate-hosts.cmake
Docs: Add cli commands
[icinga2] / tools / migrate-hosts.cmake
1 #!/bin/sh
2 # Moves configuration files from /etc/icinga2/conf.d/hosts
3 # to /etc/icinga2/repository.d
4
5 icinga2bin=@CMAKE_INSTALL_FULL_SBINDIR@/icinga2
6 sysconfdir=`$icinga2bin variable get --current SysconfDir`
7
8 if [ -z "$sysconfdir" ]; then
9     echo "Could not determine SysconfDir"
10     exit 1
11 fi
12
13 if [ ! -d $sysconfdir/icinga2/conf.d/hosts ]; then
14     exit 0
15 fi
16
17 mkdir -p $sysconfdir/icinga2/repository.d/hosts
18
19 host_count=0
20 service_count=0
21
22 for hostFile in $sysconfdir/icinga2/conf.d/hosts/*.conf; do
23     if [ ! -e $hostFile ]; then
24         continue
25     fi
26
27     host_count=$(($host_count + 1))
28
29     host=`basename $hostFile .conf`
30
31     if [ "x$host" = "xlocalhost" ]; then
32         target="`hostname -f`"
33     else
34         target=$host
35     fi
36
37     if [ ! -e $sysconfdir/icinga2/repository.d/hosts/$target.conf ]; then
38         mv $sysconfdir/icinga2/conf.d/hosts/$host.conf $sysconfdir/icinga2/repository.d/hosts/$target.conf
39         sed "s/localhost/$target/g" $sysconfdir/icinga2/repository.d/hosts/$target.conf > $sysconfdir/icinga2/repository.d/hosts/$target.conf.tmp
40         mv $sysconfdir/icinga2/repository.d/hosts/$target.conf.tmp $sysconfdir/icinga2/repository.d/hosts/$target.conf
41     else
42         rm -f $sysconfdir/icinga2/conf.d/hosts/$host.conf
43     fi
44
45     if [ -d $sysconfdir/icinga2/conf.d/hosts/$host ]; then
46         service_count=$(($service_count + 1))
47
48         if [ ! -e $sysconfdir/icinga2/repository.d/hosts/$target ]; then
49             mv $sysconfdir/icinga2/conf.d/hosts/$host $sysconfdir/icinga2/repository.d/hosts/$target
50             for file in $sysconfdir/icinga2/repository.d/hosts/$target/*.conf; do
51                 if [ ! -e $file ]; then
52                     break
53                 fi
54
55                 sed "s/localhost/$target/g" $file > $file.tmp
56                 mv $file.tmp $file
57             done
58         else
59             rm -rf $sysconfdir/icinga2/conf.d/hosts/$host
60         fi
61     fi
62 done
63
64 cat >$sysconfdir/icinga2/conf.d/hosts/README <<TEXT
65 What happened to my configuration files?
66 ========================================
67
68 Your host and service configuration files were moved to the $sysconfdir/icinga2/repository.d directory.
69
70 This allows you to manipulate those files using the "icinga2 repository" CLI commands.
71
72 Here are a few commands you might want to try:
73
74 # icinga2 repository host list
75
76 # icinga2 repository service list
77
78 # icinga2 repository --help
79 TEXT
80
81 echo "Migrated $host_count host(s) and $service_count service(s)."
82
83 exit 0