|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +### BEGIN INIT INFO |
| 4 | +# Provides: cloud usage |
| 5 | +# Required-Start: $network $local_fs |
| 6 | +# Required-Stop: $network $local_fs |
| 7 | +# Default-Start: 3 4 5 |
| 8 | +# Default-Stop: 0 1 2 6 |
| 9 | +# Short-Description: Start/stop Apache CloudStack Usage Monitor |
| 10 | +# Description: This scripts Starts/Stops the Apache CloudStack Usage Monitor |
| 11 | +## The CloudStack Usage Monitor is a part of the Apache CloudStack project and is used |
| 12 | +## for storing usage statistics from instances. |
| 13 | +## JSVC (Java daemonizing) is used for starting and stopping the usage monitor. |
| 14 | +### END INIT INFO |
| 15 | + |
| 16 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 17 | +# or more contributor license agreements. See the NOTICE file |
| 18 | +# distributed with this work for additional information |
| 19 | +# regarding copyright ownership. The ASF licenses this file |
| 20 | +# to you under the Apache License, Version 2.0 (the |
| 21 | +# "License"); you may not use this file except in compliance |
| 22 | +# with the License. You may obtain a copy of the License at |
| 23 | +# |
| 24 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | +# |
| 26 | +# Unless required by applicable law or agreed to in writing, |
| 27 | +# software distributed under the License is distributed on an |
| 28 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 29 | +# KIND, either express or implied. See the License for the |
| 30 | +# specific language governing permissions and limitations |
| 31 | +# under the License. |
| 32 | + |
| 33 | +. /etc/rc.d/init.d/functions |
| 34 | + |
| 35 | +SHORTNAME="cloud-usage" |
| 36 | +PIDFILE=/var/run/"$SHORTNAME".pid |
| 37 | +LOCKFILE=/var/lock/subsys/"$SHORTNAME" |
| 38 | +LOGFILE=/var/log/cloud/usage/usage.log |
| 39 | +PROGNAME="CloudStack Usage Monitor" |
| 40 | +CLASS="com.cloud.usage.UsageServer" |
| 41 | +PROG="jsvc" |
| 42 | +DAEMON="/usr/bin/jsvc" |
| 43 | +USER=cloud |
| 44 | + |
| 45 | +unset OPTIONS |
| 46 | +[ -r /etc/sysconfig/default/"$SHORTNAME" ] && source /etc/sysconfig/default/"$SHORTNAME" |
| 47 | + |
| 48 | +# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not defined in $DEFAULT) |
| 49 | +JDK_DIRS="/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-openjdk-i386 /usr/lib/jvm/java-6-openjdk-amd64 /usr/lib/jvm/java-6-sun /usr/lib/jvm/jre-1.6.0 /usr/lib/j2sdk1.5-sun /usr/lib/jre-openjdk" |
| 50 | + |
| 51 | +for jdir in $JDK_DIRS; do |
| 52 | + if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then |
| 53 | + JAVA_HOME="$jdir" |
| 54 | + fi |
| 55 | +done |
| 56 | +export JAVA_HOME |
| 57 | + |
| 58 | +SCP="" |
| 59 | +DCP="" |
| 60 | +UCP=`ls /usr/share/cloud/usage/java/* | tr '\n' ':'` |
| 61 | +JCP="/usr/share/java/commons-daemon.jar" |
| 62 | + |
| 63 | +# We need to append the JSVC daemon JAR to the classpath |
| 64 | +# AgentShell implements the JSVC daemon methods |
| 65 | +export CLASSPATH="$SCP:$DCP:$UCP:$JCP:/etc/sysconfig |
| 66 | +
|
| 67 | +start() { |
| 68 | + if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then |
| 69 | + echo "$PROGNAME apparently already running" |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | +
|
| 73 | + if hostname --fqdn >/dev/null 2>&1 ; then |
| 74 | + true |
| 75 | + else |
| 76 | + echo "The host name does not resolve properly to an IP address. Cannot start $PROGNAME" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +
|
| 80 | + echo -n "Starting $PROGNAME" "$SHORTNAME" |
| 81 | +
|
| 82 | + if daemon --pidfile $PIDFILE $DAEMON -cp "$CLASSPATH" -pidfile "$PIDFILE" -user "$USER" -errfile SYSLOG -Dpid=$$ $CLASS |
| 83 | + RETVAL=$? |
| 84 | + then |
| 85 | + rc=0 |
| 86 | + sleep 1 |
| 87 | + if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then |
| 88 | + failure |
| 89 | + rc=1 |
| 90 | + fi |
| 91 | + else |
| 92 | + rc=1 |
| 93 | + fi |
| 94 | +
|
| 95 | + if [ $rc -eq 0 ]; then |
| 96 | + success |
| 97 | + else |
| 98 | + failure |
| 99 | + rm -f "$PIDFILE" |
| 100 | + fi |
| 101 | + echo |
| 102 | +} |
| 103 | +
|
| 104 | +stop() { |
| 105 | + echo -n "Stopping $PROGNAME" "$SHORTNAME" |
| 106 | + killproc -p $PIDFILE $DAEMON |
| 107 | + if [ "$?" -eq 0 ]; then |
| 108 | + success |
| 109 | + else |
| 110 | + failure |
| 111 | + fi |
| 112 | + rm -f "$PIDFILE" |
| 113 | + echo |
| 114 | +} |
| 115 | +
|
| 116 | +case "$1" in |
| 117 | + start) |
| 118 | + start |
| 119 | + ;; |
| 120 | + stop) |
| 121 | + stop |
| 122 | + ;; |
| 123 | + status) |
| 124 | + status -p $PIDFILE $SHORTNAME |
| 125 | + RETVAL=$? |
| 126 | + ;; |
| 127 | + restart | force-reload) |
| 128 | + stop |
| 129 | + sleep 3 |
| 130 | + start |
| 131 | + ;; |
| 132 | + *) |
| 133 | + echo "Usage: $0 {start|stop|restart|force-reload|status}" |
| 134 | + RETVAL=3 |
| 135 | +esac |
| 136 | +
|
| 137 | +exit $RETVAL |
| 138 | +
|
0 commit comments