Skip to content

Instantly share code, notes, and snippets.

@janlauber
Created February 10, 2022 10:35
Show Gist options
  • Save janlauber/825e8fb18cc59249ddbadfb311ab69c8 to your computer and use it in GitHub Desktop.
Save janlauber/825e8fb18cc59249ddbadfb311ab69c8 to your computer and use it in GitHub Desktop.
This script is used to set the PRTG config with a defined SNMP community.
#!/bin/bash
# PRTG SNMP script
# Author: Jan Lauber
# Version: 1.0
# Date: 2018-02-12
# License: MIT
#
# Description:
# This script is used to set the PRTG config with a defined SNMP community.
#
# Usage:
# sudo ./prtg_snmp.sh <community> <network/mask>
#
# Example:
# sudo ./prtg_snmp.sh public 10.10.0.0/16
#
# Check if the script is run as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Check if argument 1 and 2 are set
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: sudo ./prtg_snmp.sh <community> <network/mask>"
exit 1
fi
# Check if snmpd exists
if [ ! -f /etc/snmp/snmpd.conf ]; then
echo "snmpd does not exist" 1>&2
# get linux distribution
if [ -f /etc/debian_version ]; then
echo "This is a Debian based system"
echo "Installing snmpd"
apt-get install snmpd
elif [ -f /etc/redhat-release ]; then
echo "This is a Redhat based system"
echo "Installing snmpd"
yum install snmpd
elif [ -f /etc/SuSE-release ]; then
echo "This is a SuSE based system"
echo "Installing snmpd"
zypper install snmpd
elif [ -f /etc/arch-release ]; then
echo "This is an Arch based system"
echo "Installing snmpd"
pacman -S snmpd
elif [ -f /etc/gentoo-release ]; then
echo "This is a Gentoo based system"
echo "Installing snmpd"
emerge net-snmp
else
echo "This is an unknown system"
echo "Please install snmpd manually"
exit 1
fi
fi
# SNMP config path
SNMPCONF="/etc/snmp/snmpd.conf"
echo "SNMP config path: $SNMPCONF"
echo "=========================================================="
echo "Setting SNMP community, please wait..."
echo "=========================================================="
# Get the community
COMMUNITY=$1
NETWORK=$2
sudo cat << 'EOF' > $SNMPCONF
###########################################################################
#
# snmpd.conf
# An example configuration file for configuring the Net-SNMP agent ('snmpd')
# See snmpd.conf(5) man page for details
#
###########################################################################
sysLocation Sitting on the Dock of the Bay
sysContact Me support@bscg.ch
sysServices 72
master agentx
agentAddress udp:161
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1
rocommunity $COMMUNITY $NETWORK
rouser authPrivUser authpriv -V systemonly
EOF
echo "=========================================================="
echo "SNMP community set to $COMMUNITY on $NETWORK"
echo "=========================================================="
# Check if snmpd is enabled
if [ -f /etc/default/snmpd ]; then
echo "snmpd is enabled"
else
echo "snmpd is not enabled"
fi
# Restart snmpd
echo "Restarting snmpd"
service snmpd restart
# Check if snmpd is running
if [ -f /etc/init.d/snmpd ]; then
echo "snmpd is running"
else
echo "snmpd is not running"
fi
# Check if snmpd is enabled
if [ -f /etc/default/snmpd ]; then
echo "snmpd is enabled"
else
echo "snmpd is not enabled"
fi
# exit script
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment