diff --git a/README.md b/README.md index 1a78633..ed8b114 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,13 @@ The key to creating these Slackware packages is to use `makepkg` which is provid -* Get the source code from macOS onto Unraid with `scp -r ~/GitHub/kubedzero/unraid-snmp/source root@unraid.local:/tmp/packageSource` - * recursively copy all source files to the Unraid server with IP `unraid.local` - * replace `unraid.local` with `192.168.1.10` or whatever your server's IP is +* Get the source code from macOS onto Unraid with `scp -r ~/GitHub/kubedzero/unraid-snmp/source root@unraid:/tmp/packageSource` + * recursively copy all source files to the Unraid server with IP `unraid` + * replace `unraid` with `192.168.1.10` or whatever your server's IP is * `scp` will automatically create the `packageSource` directory and drop the sub-contents into it. So if there was a file on macOS `~/GitHub/kubedzero/unraid-snmp/source/install/doinst.sh` it would be copied to `/tmp/packageSource/install/doinst.sh`. -* Copy the `createpackage.sh` script as well: `scp ~/GitHub/kubedzero/unraid-snmp/createpackage.sh root@unraid.local:/tmp/` -* Run a remote SSH command on macOS to build the package: `ssh -t root@unraid.local "cd /tmp/ && bash /tmp/createpackage.sh 2020.09.19 /tmp/packageSource/"` + * NOTE: If `/tmp/packageSource` already existed before running the command, the `./source` directory is created within. So if there was a file on macOS `~/GitHub/kubedzero/unraid-snmp/source/install/doinst.sh` it would be copied to `/tmp/packageSource/source/install/doinst.sh`. +* Copy the `createpackage.sh` script as well: `scp ~/GitHub/kubedzero/unraid-snmp/createpackage.sh root@unraid:/tmp/` +* Run a remote SSH command on macOS to build the package: `ssh -t root@unraid "cd /tmp/ && bash /tmp/createpackage.sh 2020.09.19 /tmp/packageSource/"` * The `-t` command executes everything in the double quotes on the Unraid server * The command first establishes a location by moving into the `/tmp/` directory * It then calls `bash /tmp/createpackage.sh` because Unraid changed to not allow direct execution, aka just executing `/tmp/createpackage.sh` @@ -64,7 +65,7 @@ The key to creating these Slackware packages is to use `makepkg` which is provid * The `makepkg` command bundles everything in the directory from where it was called into the package, so in preparation, the script moves to the source directory (provided as the second argument) * The `makepkg` command is invoked and the package is created (outside the source directory, as required by the tool) * The MD5 of the created package is computed and printed for convenience -* Now we need to copy the compiled package back to macOS, where our Git repository lives. `scp "root@unraid.local:/tmp/*.txz" ~/GitHub/kubedzero/unraid-snmp/packages` +* Now we need to copy the compiled package back to macOS, where our Git repository lives. `scp "root@unraid:/tmp/*.txz" ~/GitHub/kubedzero/unraid-snmp/packages` * This copies any `.txz` file in `/tmp/` so it doesn't have to be updated for version bumps, but `*.txz` can just as easily be replaced with the full name `unraid-snmp-2020.09.19-x86_64-1.txz` if desired * Now we need to update the MD5 listed in the `snmp.plg` file for the `unraid-snmp.txz` package we copied over. I do this manually, using the printout from the `createpackage.sh` script. A sample MD5 is `09655c2ee9391e64ff7584b2893b5454` * Now update the plugin version in the `snmp.plg` file if it hasn't already been done, commit the code and package changes, and push to GitHub diff --git a/packages/libnl-1.1.4-x86_64-1.txz b/packages/libnl-1.1.4-x86_64-1.txz deleted file mode 100644 index 4d2d29a..0000000 Binary files a/packages/libnl-1.1.4-x86_64-1.txz and /dev/null differ diff --git a/packages/net-snmp-5.8-x86_64-5.txz b/packages/net-snmp-5.8-x86_64-5.txz deleted file mode 100644 index 3b02ea7..0000000 Binary files a/packages/net-snmp-5.8-x86_64-5.txz and /dev/null differ diff --git a/packages/unraid-snmp-2020.09.20-x86_64-1.txz b/packages/unraid-snmp-2020.09.20-x86_64-1.txz deleted file mode 100644 index cdd1ad3..0000000 Binary files a/packages/unraid-snmp-2020.09.20-x86_64-1.txz and /dev/null differ diff --git a/packages/unraid-snmp-2020.10.04-x86_64-1.txz b/packages/unraid-snmp-2020.10.04-x86_64-1.txz new file mode 100644 index 0000000..2c0128b Binary files /dev/null and b/packages/unraid-snmp-2020.10.04-x86_64-1.txz differ diff --git a/snmp.plg b/snmp.plg index 2e634c5..a3e597e 100644 --- a/snmp.plg +++ b/snmp.plg @@ -5,7 +5,7 @@ - + @@ -26,7 +26,7 @@ - + ]> ##SNMP +###2020.10.04 +- Added `mem_info.sh` to print memory values in bytes + - Exposed in SNMP as `meminfo` + - Current memory values exported are MemTotal, MemFree, MemAvailable, Cached, Active, Inactive, Committed_AS +- Added Settings page to disable `--no-check standby` when getting disk temps + - Used in Unraid setups where disks are always reported to be in Standby + - Used in Unraid setups where disks don't need spinning up to get temperature + - State is stored between reboots in `/boot/config/plugins/snmp/snmp.cfg` + - The script `/usr/local/emhttp/plugins/snmp/disk_temps.sh` reads this config to change behavior + ###2020.09.20 - Bugfix: SMB and NFS permission denied errors after plugin install - Unused scripts were present in `unraid-snmp-2020.09.19-x86_64-1.txz` and somehow interrupted server access diff --git a/source/install/doinst.sh b/source/install/doinst.sh old mode 100644 new mode 100755 index 7b90f0f..e8c0f5e --- a/source/install/doinst.sh +++ b/source/install/doinst.sh @@ -15,19 +15,22 @@ echo "Set shell script executable permissions" chmod a+x cpu_mhz.sh chmod a+x disk_free_space.sh chmod a+x disk_temps.sh +chmod a+x mem_info.sh chmod a+x share_free_space.sh echo "Set read only permissions for other files" chmod a+r snmpd.conf +chmod a+r snmp.page chmod a+r snmp.png chmod a+r README.md +echo "Checking if /etc/rc.d/rc.snmpd exists before editing" # Only run modifications if snmpd startup/shutdown file exists # https://linuxize.com/post/bash-check-if-file-exists/ if [[ -f /etc/rc.d/rc.snmpd ]]; then echo "Stop SNMP daemon if it is currently running" - bash /etc/rc.d/rc.snmpd stop + bash /etc/rc.d/rc.snmpd stop 2>&1 echo "Replace default snmpd.conf with our own, backing up the original" # NOTE: Use cp, not mv. Plugin 2020.04.01 and earlier use the .conf diff --git a/source/usr/local/emhttp/plugins/snmp/disk_temps.sh b/source/usr/local/emhttp/plugins/snmp/disk_temps.sh index c454b14..a6db332 100755 --- a/source/usr/local/emhttp/plugins/snmp/disk_temps.sh +++ b/source/usr/local/emhttp/plugins/snmp/disk_temps.sh @@ -7,13 +7,15 @@ set -euo pipefail # Define the working directory in which files will live -working_dir=/tmp/plugins/snmp/ +working_dir="/tmp/plugins/snmp/" # Define the file that will hold disk temperatures -cache_file_name=disk_temps.txt +cache_file_name="disk_temps.txt" # Define the lock file that will prevent multiple simultaneous writes -cache_lock_name=disk_temps.lock +cache_lock_name="disk_temps.lock" # Define the log output for errors -cache_log_name=disk_temps.log +cache_log_name="disk_temps.log" +# Define the file that the SNMP Settings page updates +config_file="/boot/config/plugins/snmp/snmp.cfg" # Update file name variables with full paths cache_file_full_path="$working_dir$cache_file_name" @@ -64,6 +66,26 @@ function update_cache_file { exit 1 fi + # source will load the lines of the .cfg as variables + # Exit flag is 1 when file missing, so provide an OR true to offer an exit 0 + # 2> /dev/null hides STDERR and sends it to /dev/null instead + source "$config_file" 2> /dev/null || true + + # Check if the variable sourced from the .cfg is equal to 1/enabled + # Provide default value if variable is unset, to avoid breaking `set` + # https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash + if [[ "${UNSAFETEMP:-}" -eq "1" ]] + then + echo "Disk temp retrieval may wake disks from STANDBY" + # Set the command-modifying var we'll use later to an empty string + # NOTE: WD disks need to be spun up for attributes to show + # NOTE: Disks may be forced out of STANDBY to report attributes + standby_check="" + else + # Call smartctl with --nocheck standby to exit early if power mode is STANDBY + standby_check="--nocheck standby" + fi + # Remove the cache file before we start writing to it rm -f "$cache_file_full_path" @@ -92,10 +114,10 @@ function update_cache_file { disk_name=$(echo "$model_line" | sed 's/.*=//') # Call smartctl and attempt to get the attributes via -A - # Call with --nocheck standby to exit early if power mode is STANDBY # Exit flag is 2 when in standby, so provide an OR true to offer an exit 0 - # NOTE: WD disks need to be spun up for attributes to show - smartctl_output=$(smartctl --nocheck standby -A "$dev_path") || true + # standby_check variable should either be an empty string (no modification) + # or standby_check might be --nocheck standby to prevent disk spinup + smartctl_output=$(smartctl $standby_check -A "$dev_path") || true # Check if the disk is reported to be in standby mode if [[ $smartctl_output == *"Device is in STANDBY mode"* ]] diff --git a/source/usr/local/emhttp/plugins/snmp/mem_info.sh b/source/usr/local/emhttp/plugins/snmp/mem_info.sh new file mode 100755 index 0000000..abbf1f5 --- /dev/null +++ b/source/usr/local/emhttp/plugins/snmp/mem_info.sh @@ -0,0 +1,40 @@ +#!/usr/bin/bash + +# This uses /proc/meminfo to grab various memory values for SNMP + +# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ +set -euo pipefail + +# Given arguments, retrieve and format for output to SNMP +# $1 is proc_mem_name_grep, $2 is friendly_name +getValFromMemInfo () { + + # Using the passed in argument, try to get the line from /proc/meminfo + mem_value=$(grep "$1" /proc/meminfo) + + # Skip outputting if grep pattern did not yield exactly one result + if [[ ! "$(echo $mem_value | wc -l)" -eq "1" ]] + then + return + fi + + # Comparing to free --kibi, kibibytes is confirmed in /proc/meminfo + mem_value_kibi=$(echo "$mem_value" | awk '{print $2}') + # Change to bytes, avoid kilobyte (1000 bytes) kibibyte (1024 bytes) misuse + mem_value_bytes=$((mem_value_kibi * 1024)) + # Use the friendly name and byte value as output + echo "$2: $mem_value_bytes" +} + +# Call the function, $1 is the grep pattern and $2 is the SNMP output name +# https://access.redhat.com/solutions/406773 describes different values +getValFromMemInfo "MemTotal:" "MemTotal" +getValFromMemInfo "MemFree:" "MemFree" +getValFromMemInfo "MemAvailable:" "MemAvailable" +# Force matching at the beginning of the line +getValFromMemInfo "^Cached:" "Cached" +getValFromMemInfo "Active:" "Active" +getValFromMemInfo "Inactive:" "Inactive" +getValFromMemInfo "Committed_AS:" "Committed_AS" + +exit 0 \ No newline at end of file diff --git a/source/usr/local/emhttp/plugins/snmp/snmp.page b/source/usr/local/emhttp/plugins/snmp/snmp.page new file mode 100644 index 0000000..cf075b4 --- /dev/null +++ b/source/usr/local/emhttp/plugins/snmp/snmp.page @@ -0,0 +1,58 @@ +Menu="Utilities" +Icon="snmp.png" +Title="SNMP" +Tag="cogs" +--- + + + + + + + + + + + + + +
+ + + + + +Enable Unsafe Temperature Checking: +: + + +> By default, this is disabled and SNMP only checks the disk temperature if the disk reports itself to be out of STANDBY mode. Some systems' disks always report STANDBY mode, preventing SNMP from ever fetching disk temperature. Enable this setting to use a less safe method of temperature fetching that may wake the disks from STANDBY in some systems. + + + + + +: + + +
+ + + + + + diff --git a/source/usr/local/emhttp/plugins/snmp/snmpd.conf b/source/usr/local/emhttp/plugins/snmp/snmpd.conf index 36ff81b..3094d23 100644 --- a/source/usr/local/emhttp/plugins/snmp/snmpd.conf +++ b/source/usr/local/emhttp/plugins/snmp/snmpd.conf @@ -34,4 +34,5 @@ disk /mnt/cache extend cpumhz /usr/local/emhttp/plugins/snmp/cpu_mhz.sh extend diskfree /usr/local/emhttp/plugins/snmp/disk_free_space.sh extend disktemp /usr/local/emhttp/plugins/snmp/disk_temps.sh +extend meminfo /usr/local/emhttp/plugins/snmp/mem_info.sh extend sharefree /usr/local/emhttp/plugins/snmp/share_free_space.sh