lib/ceilometer
e263c82e
 #!/bin/bash
 #
93361643
 # lib/ceilometer
6d04fd7b
 # Install and start **Ceilometer** service
 
8d137034
 # To enable a minimal set of Ceilometer services, add the following to localrc:
cb961597
 #
1c402286
 #   enable_service ceilometer-acompute ceilometer-acentral ceilometer-anotification ceilometer-collector ceilometer-api
8d137034
 #
 # To ensure Ceilometer alarming services are enabled also, further add to the localrc:
cb961597
 #
19eed744
 #   enable_service ceilometer-alarm-notifier ceilometer-alarm-evaluator
31d31855
 
93361643
 # Dependencies:
6a5aa7c6
 #
93361643
 # - functions
4a2b1c66
 # - OS_AUTH_URL for auth in api
ad80eadb
 # - DEST set to the destination directory
4a2b1c66
 # - SERVICE_PASSWORD, SERVICE_TENANT_NAME for auth in api
91b8d13e
 # - STACK_USER service user
93361643
 
 # stack.sh
 # ---------
6a5aa7c6
 # - install_ceilometer
 # - configure_ceilometer
 # - init_ceilometer
 # - start_ceilometer
 # - stop_ceilometer
 # - cleanup_ceilometer
93361643
 
7903b795
 # Save trace setting
 XTRACE=$(set +o | grep xtrace)
 set +o xtrace
93361643
 
 
 # Defaults
 # --------
 
6d04fd7b
 # Set up default directories
e08ab104
 GITDIR["python-ceilometerclient"]=$DEST/python-ceilometerclient
5cb19069
 
93361643
 CEILOMETER_DIR=$DEST/ceilometer
6d04fd7b
 CEILOMETER_CONF_DIR=/etc/ceilometer
 CEILOMETER_CONF=$CEILOMETER_CONF_DIR/ceilometer.conf
 CEILOMETER_API_LOG_DIR=/var/log/ceilometer-api
8c54849d
 CEILOMETER_AUTH_CACHE_DIR=${CEILOMETER_AUTH_CACHE_DIR:-/var/cache/ceilometer}
ae6fb18f
 CEILOMETER_WSGI_DIR=${CEILOMETER_WSGI_DIR:-/var/www/ceilometer}
6d04fd7b
 
9fbeeddc
 # Support potential entry-points console scripts
9778b3cb
 CEILOMETER_BIN_DIR=$(get_python_exec_prefix)
93361643
 
9778b3cb
 # Set up database backend
69f74574
 CEILOMETER_BACKEND=${CEILOMETER_BACKEND:-mysql}
cc6b4435
 
fa5ccfff
 # Ceilometer connection info.
 CEILOMETER_SERVICE_PROTOCOL=http
 CEILOMETER_SERVICE_HOST=$SERVICE_HOST
 CEILOMETER_SERVICE_PORT=${CEILOMETER_SERVICE_PORT:-8777}
ae6fb18f
 CEILOMETER_USE_MOD_WSGI=$(trueorfalse False $CEILOMETER_USE_MOD_WSGI)
4237f590
 
46e1abaf
 # To enable OSprofiler change value of this variable to "notifications,profiler"
 CEILOMETER_NOTIFICATION_TOPICS=${CEILOMETER_NOTIFICATION_TOPICS:-notifications}
 
4237f590
 # Tell Tempest this project is present
 TEMPEST_SERVICES+=,ceilometer
 
fa5ccfff
 
cc6b4435
 # Functions
 # ---------
e4fa7213
 
 # Test if any Ceilometer services are enabled
 # is_ceilometer_enabled
 function is_ceilometer_enabled {
     [[ ,${ENABLED_SERVICES} =~ ,"ceilometer-" ]] && return 0
     return 1
 }
 
fa5ccfff
 # create_ceilometer_accounts() - Set up common required ceilometer accounts
67bc8e8a
 #
42a59c2b
 # Project              User         Roles
 # ------------------------------------------------------------------
 # SERVICE_TENANT_NAME  ceilometer   admin
 # SERVICE_TENANT_NAME  ceilometer   ResellerAdmin (if Swift is enabled)
67bc8e8a
 function create_ceilometer_accounts {
fa5ccfff
 
16ef9760
     local service_tenant=$(openstack project list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
     local admin_role=$(openstack role list | awk "/ admin / { print \$2 }")
fa5ccfff
 
     # Ceilometer
     if [[ "$ENABLED_SERVICES" =~ "ceilometer-api" ]]; then
16ef9760
         local ceilometer_user=$(get_or_create_user "ceilometer" \
             "$SERVICE_PASSWORD" $service_tenant)
         get_or_add_user_role $admin_role $ceilometer_user $service_tenant
0abde393
 
fa5ccfff
         if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
16ef9760
             local ceilometer_service=$(get_or_create_service "ceilometer" \
0abde393
                 "metering" "OpenStack Telemetry Service")
16ef9760
             get_or_create_endpoint $ceilometer_service \
0abde393
                 "$REGION_NAME" \
                 "$CEILOMETER_SERVICE_PROTOCOL://$CEILOMETER_SERVICE_HOST:$CEILOMETER_SERVICE_PORT/" \
                 "$CEILOMETER_SERVICE_PROTOCOL://$CEILOMETER_SERVICE_HOST:$CEILOMETER_SERVICE_PORT/" \
                 "$CEILOMETER_SERVICE_PROTOCOL://$CEILOMETER_SERVICE_HOST:$CEILOMETER_SERVICE_PORT/"
fa5ccfff
         fi
42a59c2b
         if is_service_enabled swift; then
             # Ceilometer needs ResellerAdmin role to access swift account stats.
0abde393
             get_or_add_user_role "ResellerAdmin" "ceilometer" $SERVICE_TENANT_NAME
42a59c2b
         fi
fa5ccfff
     fi
 }
 
cc6b4435
 
ae6fb18f
 # _cleanup_keystone_apache_wsgi() - Remove wsgi files, disable and remove apache vhost file
 function _cleanup_ceilometer_apache_wsgi {
     sudo rm -f $CEILOMETER_WSGI_DIR/*
     sudo rm -f $(apache_site_config_for ceilometer)
 }
 
93361643
 # cleanup_ceilometer() - Remove residual data files, anything left over from previous
 # runs that a clean run would need to clean up
aee18c74
 function cleanup_ceilometer {
936284b0
     if [ "$CEILOMETER_BACKEND" != 'mysql' ] && [ "$CEILOMETER_BACKEND" != 'postgresql' ] ; then
         mongo ceilometer --eval "db.dropDatabase();"
     fi
ae6fb18f
     if [ "$CEILOMETER_USE_MOD_WSGI" == "True" ]; then
         _cleanup_ceilometer_apache_wsgi
     fi
 }
 
 function _config_ceilometer_apache_wsgi {
     sudo mkdir -p $CEILOMETER_WSGI_DIR
 
     local ceilometer_apache_conf=$(apache_site_config_for ceilometer)
     local apache_version=$(get_apache_version)
 
     # copy proxy vhost and wsgi file
     sudo cp $CEILOMETER_DIR/ceilometer/api/app.wsgi $CEILOMETER_WSGI_DIR/app
 
     sudo cp $FILES/apache-ceilometer.template $ceilometer_apache_conf
     sudo sed -e "
         s|%PORT%|$CEILOMETER_SERVICE_PORT|g;
         s|%APACHE_NAME%|$APACHE_NAME|g;
         s|%WSGIAPP%|$CEILOMETER_WSGI_DIR/app|g;
         s|%USER%|$STACK_USER|g
     " -i $ceilometer_apache_conf
93361643
 }
 
 # configure_ceilometer() - Set config files, create data dirs, etc
aee18c74
 function configure_ceilometer {
c5259b4a
     [ ! -d $CEILOMETER_CONF_DIR ] && sudo mkdir -m 755 -p $CEILOMETER_CONF_DIR
e578effb
     sudo chown $STACK_USER $CEILOMETER_CONF_DIR
31d31855
 
c5259b4a
     [ ! -d $CEILOMETER_API_LOG_DIR ] &&  sudo mkdir -m 755 -p $CEILOMETER_API_LOG_DIR
e578effb
     sudo chown $STACK_USER $CEILOMETER_API_LOG_DIR
93361643
 
8c11f561
     iniset_rpc_backend ceilometer $CEILOMETER_CONF DEFAULT
d36268af
 
46e1abaf
     iniset $CEILOMETER_CONF DEFAULT notification_topics "$CEILOMETER_NOTIFICATION_TOPICS"
4b3e4e52
     iniset $CEILOMETER_CONF DEFAULT verbose True
423d7901
     iniset $CEILOMETER_CONF DEFAULT debug "$ENABLE_DEBUG_LOG_LEVEL"
53a5f42e
 
8abd8598
     if [[ -n "$CEILOMETER_COORDINATION_URL" ]]; then
         iniset $CEILOMETER_CONF coordination backend_url $CEILOMETER_COORDINATION_URL
         iniset $CEILOMETER_CONF compute workload_partitioning True
     fi
 
4a2b1c66
     # Install the policy file for the API server
     cp $CEILOMETER_DIR/etc/ceilometer/policy.json $CEILOMETER_CONF_DIR
     iniset $CEILOMETER_CONF DEFAULT policy_file $CEILOMETER_CONF_DIR/policy.json
 
95053cf4
     cp $CEILOMETER_DIR/etc/ceilometer/pipeline.yaml $CEILOMETER_CONF_DIR
     cp $CEILOMETER_DIR/etc/ceilometer/api_paste.ini $CEILOMETER_CONF_DIR
     cp $CEILOMETER_DIR/etc/ceilometer/event_definitions.yaml $CEILOMETER_CONF_DIR
 
8ceb794c
     if [ "$CEILOMETER_PIPELINE_INTERVAL" ]; then
         sed -i "s/interval:.*/interval: ${CEILOMETER_PIPELINE_INTERVAL}/" $CEILOMETER_CONF_DIR/pipeline.yaml
     fi
 
14246ac1
     # the compute and central agents need these credentials in order to
1af4afb0
     # call out to other services' public APIs
     # the alarm evaluator needs these options to call ceilometer APIs
     iniset $CEILOMETER_CONF service_credentials os_username ceilometer
     iniset $CEILOMETER_CONF service_credentials os_password $SERVICE_PASSWORD
     iniset $CEILOMETER_CONF service_credentials os_tenant_name $SERVICE_TENANT_NAME
14246ac1
 
0595237e
     configure_auth_token_middleware $CEILOMETER_CONF ceilometer $CEILOMETER_AUTH_CACHE_DIR
4a2b1c66
 
246d9bbd
     if [ "$CEILOMETER_BACKEND" = 'mysql' ] || [ "$CEILOMETER_BACKEND" = 'postgresql' ] ; then
48bbfe93
         iniset $CEILOMETER_CONF database alarm_connection $(database_connection_url ceilometer)
         iniset $CEILOMETER_CONF database event_connection $(database_connection_url ceilometer)
         iniset $CEILOMETER_CONF database metering_connection $(database_connection_url ceilometer)
05bd7b80
         iniset $CEILOMETER_CONF DEFAULT collector_workers $API_WORKERS
9778b3cb
     else
48bbfe93
         iniset $CEILOMETER_CONF database alarm_connection mongodb://localhost:27017/ceilometer
         iniset $CEILOMETER_CONF database event_connection mongodb://localhost:27017/ceilometer
         iniset $CEILOMETER_CONF database metering_connection mongodb://localhost:27017/ceilometer
9778b3cb
         configure_mongodb
         cleanup_ceilometer
     fi
846609b6
 
     if [[ "$VIRT_DRIVER" = 'vsphere' ]]; then
         iniset $CEILOMETER_CONF DEFAULT hypervisor_inspector vsphere
         iniset $CEILOMETER_CONF vmware host_ip "$VMWAREAPI_IP"
         iniset $CEILOMETER_CONF vmware host_username "$VMWAREAPI_USER"
         iniset $CEILOMETER_CONF vmware host_password "$VMWAREAPI_PASSWORD"
     fi
ae6fb18f
 
     if [ "$CEILOMETER_USE_MOD_WSGI" == "True" ]; then
         iniset $CEILOMETER_CONF api pecan_debug "False"
         _config_ceilometer_apache_wsgi
     fi
93361643
 }
 
aee18c74
 function configure_mongodb {
936284b0
     # server package is the same on all
     local packages=mongodb-server
 
     if is_fedora; then
         # mongodb client + python bindings
         packages="${packages} mongodb pymongo"
     else
         packages="${packages} python-pymongo"
     fi
 
     install_package ${packages}
 
cc6b4435
     if is_fedora; then
285c75e7
         # ensure smallfiles selected to minimize freespace requirements
         sudo sed -i '/--smallfiles/!s/OPTIONS=\"/OPTIONS=\"--smallfiles /' /etc/sysconfig/mongod
 
         restart_service mongod
     fi
936284b0
 
     # give mongodb time to start-up
     sleep 5
285c75e7
 }
 
8c54849d
 # init_ceilometer() - Initialize etc.
aee18c74
 function init_ceilometer {
8c54849d
     # Create cache dir
     sudo mkdir -p $CEILOMETER_AUTH_CACHE_DIR
91b8d13e
     sudo chown $STACK_USER $CEILOMETER_AUTH_CACHE_DIR
8c54849d
     rm -f $CEILOMETER_AUTH_CACHE_DIR/*
9778b3cb
 
c921a95f
     if is_service_enabled mysql postgresql; then
         if [ "$CEILOMETER_BACKEND" = 'mysql' ] || [ "$CEILOMETER_BACKEND" = 'postgresql' ] ; then
             recreate_database ceilometer utf8
             $CEILOMETER_BIN_DIR/ceilometer-dbsync
         fi
9778b3cb
     fi
8c54849d
 }
 
2112743b
 # install_redis() - Install the redis server.
 function install_redis {
     if is_ubuntu; then
         install_package redis-server
     else
         # This will fail (correctly) where a redis package is unavailable
         install_package redis
     fi
 
     restart_service redis
 }
 
93361643
 # install_ceilometer() - Collect source and prepare
aee18c74
 function install_ceilometer {
93361643
     git_clone $CEILOMETER_REPO $CEILOMETER_DIR $CEILOMETER_BRANCH
13344bd9
     setup_develop $CEILOMETER_DIR
 
8abd8598
     if echo $CEILOMETER_COORDINATION_URL | grep -q '^memcached:'; then
         install_package memcached
2112743b
     elif echo $CEILOMETER_COORDINATION_URL | grep -q '^redis:'; then
         install_redis
8abd8598
     fi
93361643
 }
 
e583d9b8
 # install_ceilometerclient() - Collect source and prepare
aee18c74
 function install_ceilometerclient {
e08ab104
     if use_library_from_git "python-ceilometerclient"; then
         git_clone_by_name "python-ceilometerclient"
         setup_dev_lib "python-ceilometerclient"
         sudo install -D -m 0644 -o $STACK_USER {${GITDIR["python-ceilometerclient"]}/tools/,/etc/bash_completion.d/}ceilometer.bash_completion
5cb19069
     fi
e583d9b8
 }
 
93361643
 # start_ceilometer() - Start running processes, including screen
aee18c74
 function start_ceilometer {
2f27a0ed
     run_process ceilometer-acentral "ceilometer-agent-central --config-file $CEILOMETER_CONF"
     run_process ceilometer-anotification "ceilometer-agent-notification --config-file $CEILOMETER_CONF"
     run_process ceilometer-collector "ceilometer-collector --config-file $CEILOMETER_CONF"
ae6fb18f
 
     if [[ "$CEILOMETER_USE_MOD_WSGI" == "False" ]]; then
         run_process ceilometer-api "ceilometer-api -d -v --log-dir=$CEILOMETER_API_LOG_DIR --config-file $CEILOMETER_CONF"
     else
         enable_apache_site ceilometer
         restart_apache_server
         tail_log ceilometer /var/log/$APACHE_NAME/ceilometer.log
         tail_log ceilometer-api /var/log/$APACHE_NAME/ceilometer_access.log
     fi
 
4922bfa8
 
     # Start the compute agent last to allow time for the collector to
     # fully wake up and connect to the message bus. See bug #1355809
fc1b7786
     if [[ "$VIRT_DRIVER" = 'libvirt' ]]; then
bdeea69d
         run_process ceilometer-acompute "ceilometer-agent-compute --config-file $CEILOMETER_CONF" $LIBVIRT_GROUP
fc1b7786
     fi
846609b6
     if [[ "$VIRT_DRIVER" = 'vsphere' ]]; then
2f27a0ed
         run_process ceilometer-acompute "ceilometer-agent-compute --config-file $CEILOMETER_CONF"
846609b6
     fi
1ed64cbb
 
7083b822
     # only die on API if it was actually intended to be turned on
b9a70350
     if is_service_enabled ceilometer-api; then
7083b822
         echo "Waiting for ceilometer-api to start..."
         if ! timeout $SERVICE_TIMEOUT sh -c "while ! curl --noproxy '*' -s http://localhost:8777/v2/ >/dev/null; do sleep 1; done"; then
             die $LINENO "ceilometer-api did not start"
         fi
1ed64cbb
     fi
 
2f27a0ed
     run_process ceilometer-alarm-notifier "ceilometer-alarm-notifier --config-file $CEILOMETER_CONF"
     run_process ceilometer-alarm-evaluator "ceilometer-alarm-evaluator --config-file $CEILOMETER_CONF"
93361643
 }
7903b795
 
699a29f7
 # stop_ceilometer() - Stop running processes
aee18c74
 function stop_ceilometer {
ae6fb18f
     if [ "$CEILOMETER_USE_MOD_WSGI" == "True" ]; then
         disable_apache_site ceilometer
         restart_apache_server
     fi
699a29f7
     # Kill the ceilometer screen windows
1c402286
     for serv in ceilometer-acompute ceilometer-acentral ceilometer-anotification ceilometer-collector ceilometer-api ceilometer-alarm-notifier ceilometer-alarm-evaluator; do
2f27a0ed
         stop_process $serv
699a29f7
     done
 }
 
cc6b4435
 
7903b795
 # Restore xtrace
 $XTRACE
584d90ec
 
6a5aa7c6
 # Tell emacs to use shell-script-mode
 ## Local variables:
 ## mode: shell-script
 ## End: