#!/bin/bash
#
# Copyright (C) Metaswitch Networks 2016
# If license terms are provided to you in a COPYING file in the root directory
# of the source code repository by which you are accessing this code, then
# the license outlined in that COPYING file applies to your use.
# Otherwise no rights are granted except for those provided to you by
# Metaswitch Networks in a separate written agreement.
#

# This script allows editing of the snmpd configuration file to modify the community and target.
# It takes two parameters
COMMUNITY=$1
TARGET=$2

if [[ $# -ne 2 ]];
then
   echo "Error - invalid set of parameters"
   echo "Usage:"
   echo "  cw-set_snmp_community [community] [target]"
   echo ""
   echo " [community] is the SNMP community name"
   echo " [target] is a hostname, IPv4 address or subnet (represented "
   echo "either as IP/MASK or IP/BITS) where IP is the network prefix"
   echo ""
   echo "e.g. cw-set_snmp_community clearwater 1.2.0.0/255.255.0.0"
   echo " or  cw-set_snmp_community clearwater 1.2.0.0/16"
   echo ""
   exit 1
fi

# Community strings can be any combination of letters, numbers dashes, underscores or dots.
if ! [[ $COMMUNITY =~ ^[0-9A-Za-z_.\-]*$ ]];
then
  echo Error - invalid characters in community parameter.
  exit 1
fi

# Check valid IPv4 address/MASK (dots, numbers and forward slash), or hostname
# (alphanumeric, dots or dashes)
if ! [[ $TARGET =~ ^[0-9A-Za-z/.\-]*$ ]];
then
  echo Error - invalid characters in target parameter
   exit 1
fi

SNMPD_CONF=/etc/snmp/snmpd.conf
TMP_FILE=$(mktemp $SNMPD_CONF.XXXXX)
sed "s#\(rocommunity \).*\( -V clearwater$\)#\1$COMMUNITY $TARGET\2#g" $SNMPD_CONF >"$TMP_FILE"
mv "$TMP_FILE" $SNMPD_CONF

echo Configuration updated
service snmpd restart
