#!/bin/bash

#the postscript to enable snmpd in ONIE compatible switches

if ! cat /etc/os-release |grep -i  '^NAME=[ "]*Cumulus Linux[ "]*$' >/dev/null 2>&1 ; then
    echo "This script is only supported on Cumulus OS in ONIE switch"
    exit 2
fi

#define conf file
snmp_conf="/etc/snmp/snmpd.conf"
if [ ! -f  "$snmp_conf" ]; then
    echo "/etc/snmp/snmpd.conf doesn't not exist"
    exit -1
fi

[ -f ${snmp_conf}.orig ] || cp $snmp_conf ${snmp_conf}.orig

#get snmp attribute
#NOTE: the length of SNMP Password has to be min=8
xCATSettingsSTART="xCAT settings START"
xCATSettingsEND="xCAT settings END"
xCATSettingsOID="xCAT customized"
xCATSettingsInfo="Entries between the START and END lines will be replaced each time by running enablesnmp"
snmpversion=$SNMPVERSION
snmpuser=$SNMPUSER
snmppwd=$SNMPPASSWD
snmppriv=$SNMPPRIV
snmpauth=$SNMPAUTH
snmpc=$SNMPC
community="public"

#Get cumulus orig conf file
grep "$xCATSettingsOID" $snmp_conf 2>&1 1> /dev/null
if [ $? -eq 0 ]; then
    cp ${snmp_conf}.cumulus $snmp_conf
fi

grep "$xCATSettingsSTART" $snmp_conf 2>&1 1> /dev/null
if [ $? -eq 0 ]; then
    #remove the previous snmp rule generated by xCAT
    sed -i "/$xCATSettingsSTART/,/$xCATSettingsEND/ d" $snmp_conf
else
    sed -i "/^\s*agentAddress/s/^/#/" $snmp_conf
    sed -i '/agentAddress udp\:161\,udp6\:\[\:\:1\]\:161/s/^#//' $snmp_conf
    sed -i "/view\s*systemonly\s*included\s*.1.3.6.1.2.1.17/s/^#//" $snmp_conf
    sed -i "/pass_persist\s*.1.3.6.1.2.1.17\s*\/usr\/share\/snmp\/bridge_pp.py/s/^#//" $snmp_conf
fi


# Mark the start of xCAT section
echo "# $xCATSettingsSTART" >> $snmp_conf
echo "# $xCATSettingsInfo" >> $snmp_conf

if [[ "$snmpversion" =~ "3" ]]; then
    #set up snmp version 3
    if [ -n "$snmpuser" ] && [ -n "$snmpauth" ] && [ -n "$snmppwd" ]; then
        len=${#snmppwd}
        if [ $len -lt 8 ]; then
            echo "SNMP v3 specification requires password to have a minimum of 8 characters"
            echo "# $xCATSettingsEND" >> $snmp_conf
            exit -1
        fi
        snmpauth=`echo $snmpauth | tr '[a-z]' '[A-Z]'`
        if [ -n "$snmppriv" ]; then
            snmppriv=`echo $snmppriv | tr '[a-z]' '[A-Z]'`
            echo "createUser $snmpuser $snmpauth $snmppwd $snmppriv $snmppwd" >> $snmp_conf
            echo "rwuser $snmpuser" >> $snmp_conf
        else
            echo "createUser $snmpuser $snmpauth $snmppwd" >> $snmp_conf
            echo "rouser $snmpuser" >> $snmp_conf
        fi
    else
        echo "Please define user/password/auth for SNMP v3 specification"
        echo "# $xCATSettingsEND" >> $snmp_conf
        exit -1
    fi
elif [ -n "$snmppwd" ]; then
    community=$snmppwd
elif [ -n "$snmpc" ]; then
    community=$snmpc
fi

echo "rocommunity $community default" >> $snmp_conf
echo "rocommunity $community default -V systemonly" >> $snmp_conf

echo "# $xCATSettingsEND" >> $snmp_conf

#create snmpd restart conf file
mkdir -p /etc/systemd/system/snmpd.service.d
restart_conf="/etc/systemd/system/snmpd.service.d/restart.conf"
echo "[Service]" > $restart_conf
echo "Restart=always" >> $restart_conf
echo "RestartSec=60" >> $restart_conf

systemctl enable snmpd
systemctl restart snmpd

