Files
SNMP/install_snmp.sh
2026-01-24 02:08:49 -08:00

76 lines
1.3 KiB
Bash

#!/bin/bash
# ===== CONFIG =====
COMMUNITY="PathCore"
LOCATION="Server Room"
CONTACT="sysadmin@pathcore.internal"
ALLOWED_IP="10.10.0.36/32" # CHANGE THIS for security
# ==================
set -e
if [[ $EUID -ne 0 ]]; then
echo "Please run as root"
exit 1
fi
echo "Detecting OS..."
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_FAMILY=$ID
else
echo "Cannot detect OS"
exit 1
fi
install_snmp_debian() {
apt update -y
apt install -y snmp snmpd
}
install_snmp_rhel() {
dnf install -y net-snmp net-snmp-utils
}
case "$OS_FAMILY" in
ubuntu|debian)
echo "Detected Debian-based OS"
install_snmp_debian
;;
rhel|centos|rocky|almalinux|fedora)
echo "Detected RHEL-based OS"
install_snmp_rhel
;;
*)
echo "Unsupported OS: $OS_FAMILY"
exit 1
;;
esac
echo "Backing up SNMP config..."
SNMP_CONF="/etc/snmp/snmpd.conf"
cp $SNMP_CONF ${SNMP_CONF}.bak.$(date +%F-%T)
echo "Configuring SNMP..."
cat <<EOF > $SNMP_CONF
agentAddress udp:161
rocommunity $COMMUNITY $ALLOWED_IP
sysLocation $LOCATION
sysContact $CONTACT
# Basic monitoring
extend uptime /usr/bin/uptime
extend df /bin/df -h
EOF
echo "Starting and enabling SNMP..."
systemctl restart snmpd
systemctl enable snmpd
echo
echo "SNMP setup complete ✅"
echo "Test with:"
echo "snmpwalk -v2c -c $COMMUNITY localhost system"