#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/utilities/cl_community_name.sh 1.3.2.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2001,2015 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)25  1.3.2.2  src/43haes/usr/sbin/cluster/utilities/cl_community_name.sh, hacmp.utils, 61haes_r720, 1539B_hacmp720 9/10/15 13:17:22

###############################################################################
#
#  Name:  cl_community_name
#
#  Return the SNMP community name, called when using the snmpinfo
#  command and starting clinfo.
#
# Arguments:    none
#
#  Returns:     0
#
###############################################################################

PROGNAME=${0##*/}
PATH="/usr/bin:/usr/sbin"
if [[ $VERBOSE_LOGGING == "high" ]]
then
    set -x
    version='1.3.2.2'
fi

export LC_ALL=C

#
# AIX 520, 530 and beyond have many different versions
# of snmpd, so checking for specific names like V3
# snmpdv3ne and snmpdv3e means future versions of AIX
# may break this logic, leading to very difficult
# problems to debug.
#
# To avoid this, an attempt is made to extract the
# snmpd.conf file directly from the snmpd binary,
# following any symlinks which may be in place.
#

integer count=0
SNMPD_FILES=""
SNMPCONF=""
strings /usr/sbin/snmpd | egrep '/etc/snmpd.*conf' | sort -u | while read file
do
    #
    : Obtain a list of valid SNMPD files from the snmpd binary
    #
    if [[ -s $file ]]
    then
	SNMPD_FILES="$SNMPD_FILES $file"
    fi
done

#
: Determine the file that was last changed by the user, use this file
: as the version to search for the community string
#
if [[ -n $SNMPD_FILES ]]; 
then
    SNMPCONF=$(ls -t $SNMPD_FILES | head -n 1)
    count=1
else
    count=0
fi

#
: In all current versions of snmpd, there is only one .conf file
: embedded in the executable. If some future snmpd executable
: changes this rule, catch this case by returning an error in
: the community name. Also catch missing .conf files, which should
: never happen.
#
: Invalid community names are returned because the error checking
: by scripts/executables using cl_community_name are missing in
: many cases. Having an invalid community name show up, will show
: clearly what the problem is so it can be fixed.
#

if (( $count < 1 ))  
then
    echo "-c cmname-error-missing-conf-file"
    exit 1
elif (( $count > 1 )) 
then
    echo "-c cmname-error-multiple-conf-files-$count"
    exit 1
fi

#
: pick up community entries, excluding private and system
: and any comment lines which exist in the file
#
cat $SNMPCONF | grep -v '^#' | grep -i '^community' | grep -ivw '\ private' | grep -ivw 'system' | \
    while read skip community_name rest
    do
	break
    done

if [[ -n ${community_name} ]]
then
    echo "-c ${community_name}"
else
    echo "-c public" # default it to public if we cannot find the name
fi
exit 0
