Skip to content

Commit

Permalink
Update plugin to 2020.10.04 to add memory info and Settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
kubedzero committed Oct 4, 2020
1 parent 347d46e commit 2e29c33
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 16 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]:/tmp/`
* Run a remote SSH command on macOS to build the package: `ssh -t [email protected] "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`
Expand All @@ -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
Expand Down
Binary file removed packages/libnl-1.1.4-x86_64-1.txz
Binary file not shown.
Binary file removed packages/net-snmp-5.8-x86_64-5.txz
Binary file not shown.
Binary file removed packages/unraid-snmp-2020.09.20-x86_64-1.txz
Binary file not shown.
Binary file added packages/unraid-snmp-2020.10.04-x86_64-1.txz
Binary file not shown.
14 changes: 12 additions & 2 deletions snmp.plg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!ENTITY author "kubedzero">
<!ENTITY gitbranch "master">

<!ENTITY pluginver "2020.09.20">
<!ENTITY pluginver "2020.10.04">
<!ENTITY minosver "6.7.0">

<!ENTITY githuburl "https://raw.githubusercontent.com/&author;/unraid-snmp/&gitbranch;">
Expand All @@ -26,7 +26,7 @@
<!ENTITY perlpkgmd5 "6993da990e2decbae18e92c5bf615dbe">

<!ENTITY pluginpkg "unraid-snmp-&pluginver;-x86_64-1.txz">
<!ENTITY pluginpkgmd5 "5b8d1827657b1590a3afe298ed43bf68">
<!ENTITY pluginpkgmd5 "849240df53b7853f02b3f12886472cc3">
]>

<PLUGIN name="&name;"
Expand All @@ -40,6 +40,16 @@
<CHANGES>
##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
Expand Down
5 changes: 4 additions & 1 deletion source/install/doinst.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 29 additions & 7 deletions source/usr/local/emhttp/plugins/snmp/disk_temps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"* ]]
Expand Down
40 changes: 40 additions & 0 deletions source/usr/local/emhttp/plugins/snmp/mem_info.sh
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions source/usr/local/emhttp/plugins/snmp/snmp.page
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Menu="Utilities"
Icon="snmp.png"
Title="SNMP"
Tag="cogs"
---

<!-- Menu is in what section of Unraid the page shows up. -->
<!-- "Utilities" puts this page in Settings > User Utilities -->
<!-- "Tools" puts this page in the Tools menu -->
<!-- Icon can be the png in the /usr/local/emhttp/plugins/snmp dir -->
<!-- Title is the name showing up on the Settings page / in the header -->
<!-- Tag can be any Font Awesome icon name https://fontawesome.com/icons -->


<?
// Dynamix provided function to parse /boot/config/plugins/snmp/snmp.cfg
// Sample line from .cfg: SETTINGNAME="value";
$cfg = parse_plugin_cfg('snmp');
?>

<!-- Creates the settings form, which can POST updates to the server -->
<!-- Configures reads/writes to the file /boot/config/plugins/snmp/snmp.cfg -->
<form markdown="1" method="POST" action="/update.php" target="progressFrame">
<input type="hidden" name="#file" value="snmp/snmp.cfg">

<!-- Adds a new category to the Form -->
<!-- The colon space moves it in the same line to the right -->
<!-- Set the name to be used for storage in the file, plus the ID for javascript reference -->
Enable Unsafe Temperature Checking:
: <select name="UNSAFETEMP" id="unsafeTemp">
<!-- Create options for this category. It will try to grab the config value first, -->
<!-- and then fall back to the first option for visibility -->
<!-- 0/1 are the values written to the .cfg while No/Yes are shown in the UI -->
<?=mk_option($cfg['UNSAFETEMP'], "0", "No") ?>
<?=mk_option($cfg['UNSAFETEMP'], "1", "Yes") ?>
</select>

<!-- Popup help section that can be accessed by clicking on the category name -->
> 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.

<!-- Create a button titled "Default" in the UI -->
<!-- It calls setDefaults javascript function when clicked and then submits the form after-->
<input type="submit" value="Default" onClick="setDefaults(this.form)">
<!-- Create a button titled "Apply" that submits the form -->
: <input type="submit" value="Apply">
<!-- Button titled "Done" that calls done() and returns to the page before without submission -->
<input type="button" value="Done" onclick="done()">
</form>


<!-- Javascript block where we can define various JS code -->
<!-- Create a function called setDefaults that resets the form values -->
<!-- It finds form values by their ID -->
<script type="text/javascript">
function setDefaults(form) {
form.unsafeTemp.value = "0";
}
</script>
1 change: 1 addition & 0 deletions source/usr/local/emhttp/plugins/snmp/snmpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2e29c33

Please sign in to comment.