diff --git a/snmp/conf.sls b/snmp/conf.sls index b560d08..30f546c 100644 --- a/snmp/conf.sls +++ b/snmp/conf.sls @@ -4,6 +4,14 @@ include: - snmp + +/usr/local/bin/distro: + file.managed: + - user: root + - group: root + - mode: 750 + - source: salt://snmp/files/distro + snmp_conf: file.managed: - name: {{ snmp.config }} diff --git a/snmp/default.sls b/snmp/default.sls index e48de2d..7455008 100644 --- a/snmp/default.sls +++ b/snmp/default.sls @@ -13,3 +13,4 @@ default_snmpd: - mode: 644 - watch_in: - service: {{ snmp.service }} + diff --git a/snmp/files/distro b/snmp/files/distro new file mode 100644 index 0000000..cd9e814 --- /dev/null +++ b/snmp/files/distro @@ -0,0 +1,139 @@ +#!/usr/bin/env bash +# Detects which OS and if it is Linux then it will detect which Linux Distribution. + +OS=`uname -s` +REV=`uname -r` +MACH=`uname -m` + +if [ "${OS}" = "SunOS" ] ; then + OS=Solaris + ARCH=`uname -p` + OSSTR="${OS} ${REV}(${ARCH} `uname -v`)" + +elif [ "${OS}" = "AIX" ] ; then + OSSTR="${OS} `oslevel` (`oslevel -r`)" + +elif [ "${OS}" = "Linux" ] ; then + KERNEL=`uname -r` + + if [ -f /etc/fedora-release ]; then + DIST=$(cat /etc/fedora-release | awk '{print $1}') + REV=`cat /etc/fedora-release | sed s/.*release\ // | sed s/\ .*//` + + elif [ -f /etc/redhat-release ] ; then + DIST=$(cat /etc/redhat-release | awk '{print $1}') + if [ "${DIST}" = "CentOS" ]; then + DIST="CentOS" + elif [ "${DIST}" = "CloudLinux" ]; then + DIST="CloudLinux" + elif [ "${DIST}" = "Mandriva" ]; then + DIST="Mandriva" + PSEUDONAME=`cat /etc/mandriva-release | sed s/.*\(// | sed s/\)//` + REV=`cat /etc/mandriva-release | sed s/.*release\ // | sed s/\ .*//` + elif [ -f /etc/oracle-release ]; then + DIST="Oracle" + elif [ -f /etc/rockstor-release ]; then + DIST="Rockstor" + else + DIST="RedHat" + fi + + PSEUDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//` + REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//` + + elif [ -f /etc/mandrake-release ] ; then + DIST='Mandrake' + PSEUDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//` + REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//` + + elif [ -f /etc/devuan_version ] ; then + DIST="Devuan `cat /etc/devuan_version`" + REV="" + + elif [ -f /etc/debian_version ] ; then + DIST="Debian `cat /etc/debian_version`" + REV="" + if [ -f /usr/bin/lsb_release ] ; then + ID=`lsb_release -i | awk -F ':' '{print $2}' | sed 's/ //g'` + fi + if [ "${ID}" = "Raspbian" ] ; then + DIST="Raspbian `cat /etc/debian_version`" + fi + if [ -f /usr/bin/pveversion ]; then + DIST="${DIST}/PVE `/usr/bin/pveversion | cut -d '/' -f 2`" + fi + + elif [ -f /etc/gentoo-release ] ; then + DIST="Gentoo" + REV=$(tr -d '[[:alpha:]]' /dev/null 2>&1 + fi + fi + + if [ -n "${REV}" ] + then + OSSTR="${DIST} ${REV}" + else + OSSTR="${DIST}" + fi + +elif [ "${OS}" = "Darwin" ] ; then + if [ -f /usr/bin/sw_vers ] ; then + OSSTR=`/usr/bin/sw_vers|grep -v Build|sed 's/^.*:.//'| tr "\n" ' '` + fi + +elif [ "${OS}" = "FreeBSD" ] ; then + DIST=$(cat /etc/version | cut -d'-' -f 1) + if [ "${DIST}" = "FreeNAS" ]; then + OSSTR=`cat /etc/version | cut -d' ' -f 1` + else + OSSTR=`/usr/bin/uname -mior` + fi +fi + +echo ${OSSTR} diff --git a/snmp/files/snmpd.conf b/snmp/files/snmpd.conf index 5275494..fb3bd2a 100644 --- a/snmp/files/snmpd.conf +++ b/snmp/files/snmpd.conf @@ -197,6 +197,13 @@ syscontact "{{ conf.get('syscontact', 'Root (add saltstack pill # system.sysServices.0 = 72 +#Distro Detection +extend .1.3.6.1.4.1.2021.7890.1 distro /usr/local/bin/distro +#Hardware Detection (uncomment to enable) +extend .1.3.6.1.4.1.2021.7890.2 hardware '/bin/cat /sys/devices/virtual/dmi/id/product_name' +extend .1.3.6.1.4.1.2021.7890.3 manufacturer '/bin/cat /sys/devices/virtual/dmi/id/sys_vendor' +extend .1.3.6.1.4.1.2021.7890.4 serial '/bin/cat /sys/devices/virtual/dmi/id/product_serial' + ############################################################################### # Logging # diff --git a/snmp/map.jinja b/snmp/map.jinja index 873a64f..ddc4d04 100644 --- a/snmp/map.jinja +++ b/snmp/map.jinja @@ -50,6 +50,9 @@ FreeBSD: pkg: net-snmp pkgutils: net-snmp rootgroup: wheel +Windows: + pkg: SNMP-Service + pkgutils: SNMP-WMI-Provider {% endload %} {% load_yaml as rhel_specific %} @@ -74,3 +77,32 @@ FreeBSD: {% if user_override %} {% do snmp.update(user_override) %} {% endif %} + +{% set Sources = [] %} +{% for rocommunity in salt['pillar.get']('snmp:conf:rocommunities', '') %} + Community: {{ rocommunity }} + {% set source = salt['pillar.get']('snmp:conf:rocommunities:'+ rocommunity +':source', '') %} + {% if source.__class__ in (().__class__, [].__class__) %} + {% for i in source %} + {% set WinManager = Sources.append(i) %} + {% endfor %} + {% elif source != '' %} + {% set WinManager = Sources.append(source) %} + {% else %} + {% endif %} +{% endfor %} + +{% for rwcommunity in salt['pillar.get']('snmp:conf:rwcommunities', '') %} + Community: {{ rwcommunity }} + {% set source = salt['pillar.get']('snmp:conf:rwcommunities:'+ rwcommunity +':source', '') %} + {% if source.__class__ in (().__class__, [].__class__) %} + {% for i in source %} + {% set WinManager = Sources.append(i) %} + {% endfor %} + {% elif source != '' %} + {% set WinManager = Sources.append(source) %} + {% else %} + {% endif %} +{% endfor %} + +{% do snmp.update({'Sources': Sources}) %} diff --git a/snmp/windows.sls b/snmp/windows.sls new file mode 100644 index 0000000..6968f70 --- /dev/null +++ b/snmp/windows.sls @@ -0,0 +1,64 @@ +{% from "snmp/map.jinja" import snmp with context %} +{% set mushStamp = salt['grains.get']('osfinger', 'NA') %} + +{% if mushStamp in ('Windows-2016Server', 'Windows-2012ServerR2', 'Windows-2012Server') %} +Remote Server Admin: + win_servermanager.installed: + - force: True + - recurse: True + - name: RSAT + +Core Package: + win_servermanager.installed: + - force: True + - recurse: True + - name: {{ snmp.pkg }} + +WMI Poller Package: + win_servermanager.installed: + - force: True + - recurse: True + - name: {{ snmp.pkgutils }} +{% elif mushStamp in ('Windows-10') %} +Core Package: + cmd.run: + - name: 'dism.exe /online /enable-feature /featurename:"SNMP" /featurename:"WMISnmpProvider"' +{% else %} +Core Package: + cmd.run: + - name: 'servermanagercmd -install {{ snmp.pkg }} -allSubFeatures' +{% endif %} + +sysLocation: + cmd.run: + - name: 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysLocation /t REG_SZ /d "{{ salt['pillar.get']('snmp:conf:location', 'Unknown (add saltstack pillar)') }}" /f' + +sysContact: + cmd.run: + - name: 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysContact /t REG_SZ /d "{{ salt['pillar.get']('snmp:conf:syscontact', 'Root (add saltstack pillar)') }}" /f' + +{% for manager in snmp.Sources %} +HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers{{ loop.index }}: + cmd.run: + - name: 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v {{ loop.index }} /t REG_SZ /d observium.erickson.is /f' +{% endfor %} + +{%- for rwcommunity in salt['pillar.get']('snmp:conf:rwcommunities', '') %} +HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities: + cmd.run: + - name: 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v {{ rwcommunity }} /t REG_DWORD /d 10 /f' +{%- endfor -%} + +{%- for rocommunity in salt['pillar.get']('snmp:conf:rocommunities', '') %} +HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities: + cmd.run: + - name: 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v {{ rocommunity }} /t REG_DWORD /d 4 /f' +{% endfor -%} + +snmp: + service.running: + - reload: True + - watch: + - cmd: HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/SNMP/Parameters/ValidCommunities + - cmd: syslocation + - cmd: sysContact