#!/bin/bash
#
# (c) Copyright 2011-2016 Hewlett Packard Enterprise Development Company, L.P.
#
# See "man chkconfig" for information on next two lines (Red Hat only)
# chkconfig: - 90 1
# description: BMC AgentX proxy. 
#
#
# Following lines are in conformance with LSB 1.2 spec
### BEGIN INIT INFO
# Provides:            ahslog
# Default-Start:       2 3 4 5
# Required-Start:      $network
# Required-Stop:       $network $syslog
# Default-Stop:        0 1 6
# Description:         starts AHS Logger 
# Short-Description:   AHS logger daemon helper
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME="Active Health Service Logger"
SNAME="ahslog"
AHSLOG_OPTIONS=""

if [ -f /etc/sysconfig/ahslog ]; then
  . /etc/sysconfig/ahslog
fi
if [ -f "/etc/default/ahslog" ]; then
  . /etc/default/ahslog
fi

case "$1" in
   start)
      lsmod|grep -q hpilo
      if [ $? -ne 0 ]; then 
         modprobe hpilo
      fi
      if [ "$ALLOW_CORE" = "y" ]; then 
        mkdir -p /var/log/cores/ahslog
        echo "/var/log/cores/%e/%p-%s-%t.core" > /proc/sys/kernel/core_pattern
        ulimit -c unlimited
      fi

      if [ -f /sys/module/hpilo/parameters/max_ccb ]; then
          let MAXCCBS=`cat /sys/module/hpilo/parameters/max_ccb`-2
      else
          let MAXCCBS=6
      fi
      let ILODEVS=`lsof +d /dev/hpilo|grep hpilo|wc -l`
      if [ $ILODEVS -ge $MAXCCBS ]; then
         echo "Running $SNAME will exhaust all available iLO devices." 
         echo "Please stop hp-health, hp-snmp-agents or other consumers of iLO devices."
         exit 1
      fi

      pid=`pidof $SNAME`
      if [ -n "$pid" ]; then
         echo "$SNAME is already running.  Please stop $SNAME or use 'restart'"
         exit 1
      else
        /sbin/$SNAME $AHSLOG_OPTIONS; RC=$?
      fi
      if [ "$RC" -eq "0" ]; then
         [ -d /var/lock/subsys ] && touch /var/lock/subsys/ahslog
      fi
      exit $RC
   ;;
   stop)
      RC=0;
      killall $SNAME > /dev/null 2>&1; RC=$?
      if [ "$RC" -eq "1" ]; then
         [ -d /var/lock/subsys ] && rm -rf /var/lock/subsys/ahslog
         exit 0
      else
        sleep 3
        killall $SNAME > /dev/null 2>&1; RC=$?
        if [ "$RC" -eq "1" ]; then
           [ -d /var/lock/subsys ] && rm -rf /var/lock/subsys/ahslog
           exit 0
        else
          sleep 3
          killall -9 $SNAME > /dev/null 2>&1
            [ -d /var/lock/subsys ] && rm -rf /var/lock/subsys/ahslog
          exit 0
        fi

      fi    
      $SNAME -S  > /dev/null 2>&1
   ;;
   reload)
   ;;
   restart)
      $0 stop
      sleep 5
      $0 start
   ;;
   status)
      pid=`pidof $SNAME`
      if [ -n "$pid" ]; then
          echo "$SNAME is running..."
      else
          echo "$SNAME is stopped..."
      fi
   ;;
   *)
     echo "Usage: /etc/init.d/ahslog {start|stop|restart|status}"
     exit 1
esac

exit 0 
