#!/bin/sh 

################################################################################
#
#    5639-VC4 and 5639-VW1
#
#    \xc2\xa9 Copyright Inspur Corp. 2006, 2016 All Rights Reserved.
#
#    The source code for this program is not published or
#    otherwise divested of its trade secrets, irrespective of
#    what has been deposited with the U.S. Copyright Office.
#
################################################################################

export SVCAGENT_DATA=/data/cimom
export SVCAGENT_HOME=/compass/cimom

. $SVCAGENT_HOME/config/envConf
 

if [ $# -ne 1 ]
then
    echo "Required log level is missing. The available log level is 'default_level', 'mid_level' or 'max_level'";
    echo "1" >&2;
    exit 1;
else
    if [ $1 == "default_level" ]
    then
        level="Default";
    elif  [ $1 == "mid_level" ]
    then
        level="DebugMid";
    elif  [ $1 == "max_level" ]
    then
        level="DebugMax";
    else
        echo "Invalid log level. The available log levels is 'default_level', 'mid_level' or 'max_level'";
        echo "1" >&2;
        exit 1;
    fi  
fi 

if [ $1 == "max_level" ]
then
    if [[ ! -f /dumps/cimom/cimMemUse. && ! -f /dumps/cimom/providerAudit. ]]
    then
        if [ ! -r /dumps/cimom/cimMemUse.* ]
        then
            if [ -r /dumps/cimom/providerAudit.* ]
            then
                export SVC_NODEID=$(ls -ft /dumps/cimom/providerAudit.* | head -1 | cut -d. -f2)
            fi     
        else
            export SVC_NODEID=$(ls -ft /dumps/cimom/cimMemUse.* | head -1 | cut -d. -f2)
        fi
    fi
    if [ ! $SVC_NODEID ]
    then
       cimconfig -s traceFilePath=/dumps/cimom/PegasusTrace.log -c 
    else       
       if [[ -f /dumps/cimom/PegasusTrace.$SVC_NODEID && ! -w /dumps/cimom/PegasusTrace.$SVC_NODEID ]]
       then
            cimconfig -s traceFilePath=/dumps/cimom/PegasusTrace.log -c
       else
            cimconfig -s traceFilePath=/dumps/cimom/PegasusTrace.$SVC_NODEID -c
       fi
    fi    
    cimconfig -s traceComponents=ALL -c
    cimconfig -s traceLevel=5 -c
#Default value is 1G, set to 64M
    cimconfig -s traceFileSizeKBytes=655360 -c
    cimconfig -s enableAuditLog=true -c
else
    cimconfig -s enableAuditLog=false -c
    cimconfig -s traceLevel=0 -c
fi 

retCode=`cimcli -n root/inspur --timeout 60 im  'InspurMCS_ProviderConfiguration.InstanceId="InspurMCS:MCSCIMProvider"' ApplyTracingConfiguration  LogLevel=$level`;

succeed="Return Value= 0";

if [ "$retCode" != "$succeed" ]
then
    echo "1" >&2;
    exit 1;
else
    echo "0" >&2;
    exit 0;
fi    
