File setup-monitoring.init of Package monitoring-appliance

#! /bin/bash
# Copyright (c) 2013 M. Reichmuth
# All rights reserved.
#
# Author: M. Reichmuth, 2013
#
# /etc/init.d/setup-monitoring

### BEGIN INIT INFO
# Provides:          setup-monitoring
# Required-Start:    $ALL
# Required-Stop:     $null
# Default-Start:     2 3 5
# Default-Stop:      0 1 2 6
# Short-Description: Setup log & monitoring appliance
# Description:       This script setup the log & monitoring
#	appliance and removes itself
### END INIT INFO

# Check for existence of needed config file and read it
LOGMON_CFG=/etc/sysconfig/setup-monitoring
test -r $LOGMON_CFG || { echo "$LOGMON_CFG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

# Read config
. $LOGMON_CFG

# Load the rc.status script for this service.
. /etc/rc.status

# Reset status of this service
rc_reset

# Input function
function ask_yes_or_no() {
    read -p "$1 ([y]es or [N]o): "
    case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
        y|yes) echo "yes" ;;
         n|no) echo "no" ;;
            *) echo $(ask_yes_or_no "Wrong input, try again") ;;
    esac
}
function ask_password() {
  unset password
  prompt=$1
  while IFS= read -p "$prompt" -r -s -n 1 char
  do
    if [[ $char == $'\0' ]]
    then
        break
    fi
    prompt='*'
    password+="$char"
  done
  echo $password
}
function rc_status2() {
  echo -en "\033[1A"
  rc_status $1 $2 $3 $4 $5
}


### START SETUP
  echo "START SETUP MONITORING APPLIANCE AT $(date)" > $LOG_FILE

  # Disable IPV6
  if [[ "yes" == $(ask_yes_or_no "Do you wanna disable IPv6 support?") ]]
  then
    echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf 2>>$LOG_FILE
	rc_status2 -v
  else
	rc_status2 -s   
  fi
  
  # Get default password
  DFT_PASS=$(ask_password "Please enter a default password for all accounts: ")
  DFT_USER="admin"
  echo

  # Configure mysql
  if [[ "yes" == $(ask_yes_or_no "Do you wanna configure the mysql database?") ]]
  then
    #mysql_install_db
    chkconfig mysql on >> $LOG_FILE 2>&1 &&
    sed '/\[mysqld\]/ a\
bind-address    = localhost' /etc/my.cnf > /tmp/my.cnf.new 2>>$LOG_FILE &&
    mv /tmp/my.cnf.new /etc/my.cnf >> $LOG_FILE 2>&1 &&
    rcmysql start >> $LOG_FILE 2>&1 &&
    /usr/bin/mysqladmin -u root password $DFT_PASS >> $LOG_FILE 2>&1
    rc_status2 -v
  else
    rc_status2 -s
  fi


  # Configure RabbitMQ
  if [[ "yes" == $(ask_yes_or_no "Do you wanna configure rabbitmq?") ]]
  then
    chkconfig rabbitmq-server on >> $LOG_FILE 2>&1 &&
    rabbitmq-plugins enable rabbitmq_management >> $LOG_FILE 2>&1 &&
    rabbitmq-plugins enable rabbitmq_management_visualiser >> $LOG_FILE 2>&1 &&
    cat > /etc/rabbitmq/rabbitmq.config<<EOF
   [
    {mnesia, [{dump_log_write_threshold, 1000}]},
    {kernel,[{inet_dist_use_interface,{127,0,0,1}}]},
    {rabbit, [{tcp_listeners, [{"127.0.0.1", 5672}]}]},
    {rabbitmq_management,  [ {http_log_dir,   "/var/log/rabbit-mgmt"} ] },
    {rabbitmq_management_agent, [ {force_fine_statistics, true} ] },
    {rabbitmq_mochiweb, [ {listeners, [{mgmt, [{port, 55672},
                                               {ip, "127.0.0.1"}]}]},
                          {default_listener, [{port, 60000} ] } ] }
   ].
EOF
    echo "RABBITMQ_NODENAME=rabbit@localhost" >> /etc/sysconfig/rabbitmq-server 2>>$LOG_FILE &&
    echo "RABBITMQ_NODE_IP_ADDRESS=127.0.0.1" >> /etc/sysconfig/rabbitmq-server 2>>$LOG_FILE &&
    echo "ERL_EPMD_ADDRESS=127.0.0.1" >> /etc/sysconfig/rabbitmq-server 2>>$LOG_FILE &&
    rcrabbitmq-server start >> $LOG_FILE 2>&1 &&
    rabbitmqctl -n rabbit@localhost add_user $DFT_USER $DFT_PASS >> $LOG_FILE 2>&1 &&
    rabbitmqctl -n rabbit@localhost set_user_tags $DFT_USER administrator >> $LOG_FILE 2>&1 &&
    rabbitmqctl -n rabbit@localhost set_permissions -p "/" $DFT_USER ".*" ".*" ".*" >> $LOG_FILE 2>&1 &&
    rabbitmqctl -n rabbit@localhost set_user_tags guest >> $LOG_FILE 2>&1 &&
    cat > /etc/apache2/conf.d/rabbitmq.conf<<EOF
  <IfDefine RABBITMQ>
    ProxyPreserveHost on
    ProxyPass /rabbitmq http://localhost:55672/
    ProxyPassReverse /rabbitmq http://localhost:55672/
  </IfDefine>
EOF
    a2enflag RABBITMQ >> $LOG_FILE 2>&1 &&
    a2enmod proxy >> $LOG_FILE 2>&1 &&
    a2enmod proxy_http >> $LOG_FILE 2>&1
    rc_status2 -v
  else
    rc_status2 -s
  fi
  
  # Configure icinga & nconf
  if [[ "yes" == $(ask_yes_or_no "Do you wanna configure icinga & nconf?") ]]
  then
    echo "event_broker_options=-1" >> /etc/icinga/icinga.cfg 2>>$LOG_FILE &&
    echo "broker_module=/usr/lib/icinga/eventhandler/neb2amqp.o name=localhost host=localhost port=5672 virtual_host=/ exchange_name=icinga connector=icinga userid=guest password=guest" >> /etc/icinga/icinga.cfg 2>>$LOG_FILE &&
    chkconfig icinga on  >> $LOG_FILE 2>&1 &&
    rcicinga start  >> $LOG_FILE 2>&1 &&
    a2enflag ICINGA  >> $LOG_FILE 2>&1 &&
 
    cat > /etc/apache2/conf.d/nconf.conf<<EOF
  <IfDefine NCONF>
    Alias /nconf "/srv/www/nconf"
    <Directory "/srv/www/nconf">
      Options None
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>
  </IfDefine>
EOF
    a2enflag NCONF  >> $LOG_FILE 2>&1 &&
    rmdir /srv/www/nconf/config  >> $LOG_FILE 2>&1 &&
    mv /srv/www/nconf/config.orig /srv/www/nconf/config  >> $LOG_FILE 2>&1 &&
    mysqladmin --password=$DFT_PASS create nconf  >> $LOG_FILE 2>&1 &&
    mysql --password=$DFT_PASS mysql -e "CREATE USER 'nconfuser'@'localhost' IDENTIFIED BY '$DFT_PASS';"  >> $LOG_FILE 2>&1 &&
    mysql --password=$DFT_PASS mysql -e "GRANT ALL PRIVILEGES ON nconf.* TO 'nconfuser'@'localhost';"  >> $LOG_FILE 2>&1 &&
    mysqladmin --password=$DFT_PASS flush-privileges  >> $LOG_FILE 2>&1 &&
    mysql -u nconfuser --password=$DFT_PASS nconf < /srv/www/nconf/INSTALL/create_database.sql  >> $LOG_FILE 2>&1 &&
    rpm -e nconf-installation >> $LOG_FILE 2>&1 &&
    ln -s /usr/sbin/icinga /srv/www/nconf/bin/nagios >> $LOG_FILE 2>&1 &&
    cat > /srv/www/nconf/config/nconf.php<<EOF >> $LOG_FILE 2>&1 &&
<?php
define('NCONFDIR', '/srv/www/nconf');
define('OS_LOGO_PATH', "img/logos");
define('NAGIOS_BIN', '/srv/www/nconf/bin/icinga');
define('CHECK_UPDATE', 1);
define('TEMPLATE_DIR', 'nconf_fresh');
define("DEBUG_MODE",     0); # [0|1]
define("DEBUG_GENERATE", 3); # [1=ERROR|2=WARN|3=INFO|4=DEBUG|5=TRACE]
define("DB_NO_WRITES",   0); # [0|1] Experimental, use with CAUTION!
define('REDIRECTING_DELAY', "1");
define('ALLOW_DEPLOYMENT', 1);
$STATIC_CONFIG = array("static_cfg");
define('CHECK_STATIC_SYNTAX', 1);
$SUPERADMIN_GROUPS = array ("+admins");
$ONCALL_GROUPS = array ();
define('OVERVIEW_QUANTITY_STANDARD', "25");
define('SELECT_VALUE_SEPARATOR', "::");
define('PASSWD_ENC', "clear");
define('PASSWD_DISPLAY', 0);
define('PASSWD_HIDDEN_STRING', "********");
?>
EOF
    cat > /srv/www/nconf/config/mysql.php<<EOF
<?php

define('DBHOST', 'localhost');
define('DBNAME', 'nconf');
define('DBUSER', 'nconfuser');
define('DBPASS', '$DFT_PASS');
?>
EOF
    cat > /srv/www/nconf/config/deployment.ini<<EOF
[extract config]
type        = local
source_file = "/srv/www/nconf/output/NagiosConfig.tgz"
target_file = "/tmp/"
action      = extract

[copy collector config]
type        = local
source_file = "/tmp/Default_collector/"
target_file = "/etc/icinga/conf.d/nconf/collector/"
action      = copy

[copy global config]
type        = local
source_file = "/tmp/global/"
target_file = "/etc/icinga/conf.d/nconf/global/"
action      = copy
reload_command = "sudo /etc/init.d/icinga reload"
EOF
    mkdir /etc/icinga/conf.d/nconf >> $LOG_FILE 2>&1 &&
    chown wwwrun /etc/icinga/conf.d/nconf >> $LOG_FILE 2>&1 &&
    rm -r /etc/icinga/objects >> $LOG_FILE 2>&1 &&
    sed 's/^cfg_file=\/etc\/icinga\/objects/#cfg_file=\/etc\/icinga\/objects/' /etc/icinga/icinga.cfg > /tmp/icinga.cfg 2>>$LOG_FILE &&
    mv /tmp/icinga.cfg /etc/icinga/icinga.cfg >> $LOG_FILE 2>&1 &&
    echo "wwwrun ALL = (root) NOPASSWD: /etc/init.d/icinga reload" >> /etc/sudoers 2>>$LOG_FILE
    rc_status2 -v
  else
    rc_status2 -s
  fi

  # Configure apache2
  if [[ "yes" == $(ask_yes_or_no "Do you wanna configure apache?") ]]
  then
    chkconfig apache2 on >> $LOG_FILE 2>&1
    rc_status2 -v
  else
    rc_status2 -s
  fi
  
  # Configure check_mk & multisite

  # Configure graphite & graphsky
  
  # Configure kibana & elasticsearch
  if [[ "yes" == $(ask_yes_or_no "Do you wanna configure kibana?") ]]
  then
    chkconfig kibana on >> $LOG_FILE 2>&1 &&
#  cat > /etc/apache2/conf.d/kibana.conf<<EOF
#  <IfDefine KIBANA>
#    ProxyPreserveHost on
#    ProxyPass /kibana http://localhost:5601/
#    ProxyPassReverse /kibana http://localhost:5601/
#  </IfDefine>
#EOF
    a2enflag KIBANA >> $LOG_FILE 2>&1 &&
    a2enmod proxy >> $LOG_FILE 2>&1 &&
    a2enmod proxy_http >> $LOG_FILE 2>&1 &&
    /etc/init.d/kibana start >> $LOG_FILE 2>&1
    rc_status2 -v
  else
    rc_status2 -s
  fi  
  
  # Configure logstash
  #chkconfig logstash on

  # Configure JasperReport

  # NSCA
  # collectd
  # gmond
  # syslog-ng
  # RabbitMQ extern
  
  echo "Monitoring appliance successfully configured!" >> $LOG_FILE 2>&1
  echo
  echo "Monitoring appliance successfully configured!" >> $LOG_FILE 2>&1
  echo
  echo "END SETUP MONITORING APPLIANCE AT $(date)" >> $LOG_FILE
### END SETUP

# Remove myself
insserv setup-monitoring -r
rpm -e monitoring-appliance

rc_exit
openSUSE Build Service is sponsored by