Skip to content

Commit

Permalink
Update plugin to 2021.05.21 to add UI-customizable SNMP config file
Browse files Browse the repository at this point in the history
  • Loading branch information
kubedzero committed May 22, 2021
1 parent 46196aa commit 14cf6e8
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

.DS_Store
.idea
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ The key to creating these Slackware packages is to use `makepkg` which is provid
* https://github.com/dmacias72/unRAID-NerdPack/blob/master/README.md
* A modified version of `pkg_build.sh` https://github.com/dmacias72/unRAID-NerdPack/blob/master/source/mkpkg
* Structuring of the source directories, specifically the install script, were found at https://slackwiki.com/Doinst.sh
* Other plugins' styles of uninstalling. The first two remove all traces of the plugin from the /boot persistent USB storage, while the latter two only remove some traces. This could allow configs to stay behind, so a reinstall of the plugin would reuse the leftover config from the previous install. I'm tending to stick with the former approach, as I worry leaving behind remnants could lead to unexpected behavior
* https://github.com/jbrodriguez/unbalance/blob/master/plugin/template.plg#L88
* https://github.com/dlandon/open.files/blob/master/open.files.plg#L163
* https://github.com/dmacias72/unRAID-NerdPack/blob/master/plugin/NerdPack.plg#L292
- https://github.com/StevenDTX/unRAID-open-vm-tools/blob/master/openVMTools_compiled.plg#L275

Binary file added packages/unraid-snmp-2021.05.21-x86_64-1.txz
Binary file not shown.
13 changes: 10 additions & 3 deletions snmp.plg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!ENTITY author "kubedzero">
<!ENTITY gitbranch "main">

<!ENTITY pluginver "2020.12.20">
<!ENTITY pluginver "2021.05.21">
<!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 "946e75bfef443c03b04cef44f63ed3d6">
<!ENTITY pluginpkgmd5 "6afea4c03bb4f0a9db8d49e2450bd40d">
]>

<PLUGIN name="&name;"
Expand All @@ -40,6 +40,13 @@
<CHANGES>
## SNMP

### 2021.05.21
- Added customizable snmpd.conf to the SNMP Settings page. Thanks @Mattie112 for the contribution!
- Users can define a custom SNMP config at `/boot/config/plugins/snmp/snmpd.conf` that will automatically be picked up at boot/install time
- Tweaked the install script to avoid an early exit if the bundled snmpd.conf was missing
- Adjusted PLG uninstall process to properly remove `/etc/rc.d/rc.snmpd*`
- SNMP HDD/CPU/RAM/etc scripts now use `/bin/bash` as `/usr/bin/bash` just symlinks to it anyway

### 2020.12.20
- Add Dirty Memory reporting.

Expand Down Expand Up @@ -273,7 +280,7 @@ do
[ -z "$line" ] &amp;&amp; continue

echo "Deleting $line"
# rm -rf "/path/to/prefix*" will not work due to quotes, wrap in bash exec
# rm -rf "/path/to/prefix*" * expansion fails due to quotes, wrap in bash exec
bash -c "rm -rf $line"
done &lt; &lt;(printf '%s\n' "$dirs_files_to_remove")

Expand Down
25 changes: 20 additions & 5 deletions source/install/doinst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euo pipefail

directory="/usr/local/emhttp/plugins/snmp/"
directory="/usr/local/emhttp/plugins/snmp"
echo "Set permissions and move into dir $directory"
chmod a+r "$directory"
cd "$directory"
Expand All @@ -32,10 +32,25 @@ if [[ -f /etc/rc.d/rc.snmpd ]]; then
echo "Stop SNMP daemon if it is currently running"
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
# under /usr/local and updating will fail if SNMP can't start.
cp --backup /usr/local/emhttp/plugins/snmp/snmpd.conf /etc/snmp/snmpd.conf

# Check if a UI/user-defined snmpd.conf exists on the USB disk,
# copying that over the default snmpd.conf. Otherwise use the
# bundled, Unraid-customized snmpd.conf unzipped as part of the .txz.
# NOTE: Plugin 2020.04.01 and earlier use the .conf under
# /usr/local/emhttp/plugins/, so leave it by using cp instead of mv.
custom_snmpd="/boot/config/plugins/snmp/snmpd.conf"
original_snmpd="/etc/snmp/snmpd.conf"
plugin_default_snmpd="$directory/snmpd.conf"
if [[ -f $custom_snmpd ]]; then
echo "Using the user-defined config $custom_snmpd, backing up the original"
cp --backup "$custom_snmpd" "$original_snmpd"
elif [[ -f $plugin_default_snmpd ]]; then
echo "Using the Unraid default config $plugin_default_snmpd, backing up the original"
cp --backup "$plugin_default_snmpd" "$original_snmpd"
else
echo "Could not find user-defined or Unraid-customized snmpd.conf! Using default $original_snmpd"
fi


# Define the additional flags we want to add into the SNMP daemon startup
# Spaces at end of string to separate from other flags
Expand Down
2 changes: 1 addition & 1 deletion source/usr/local/emhttp/plugins/snmp/cpu_mhz.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/bash

# This uses lscpu to find the speed of the processor in Megahertz

Expand Down
2 changes: 1 addition & 1 deletion source/usr/local/emhttp/plugins/snmp/disk_free_space.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/bash

# This uses df to find the free space of physical disks

Expand Down
2 changes: 1 addition & 1 deletion source/usr/local/emhttp/plugins/snmp/disk_temps.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/bash

# Scan installed disks for their standby state and temperature
# Use a 5 minute TTL cache file for increased performance when calling rapidly
Expand Down
2 changes: 1 addition & 1 deletion source/usr/local/emhttp/plugins/snmp/mem_info.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/bash

# This uses /proc/meminfo to grab various memory values for SNMP

Expand Down
8 changes: 8 additions & 0 deletions source/usr/local/emhttp/plugins/snmp/run_before_save.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
if (!empty($_REQUEST['SNMPDCONF'])) {
// Copy the text box content into the live config file used by SNMP
file_put_contents('/etc/snmp/snmpd.conf', $_REQUEST['SNMPDCONF']);
// Also write to USB so package installation can use it after reboot/update
file_put_contents('/boot/config/plugins/snmp/snmpd.conf', $_REQUEST['SNMPDCONF']);
}
?>
2 changes: 1 addition & 1 deletion source/usr/local/emhttp/plugins/snmp/share_free_space.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/bash

# This uses the Unraid-provided shares.ini to print out free KB per share

Expand Down
57 changes: 47 additions & 10 deletions source/usr/local/emhttp/plugins/snmp/snmp.page
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,73 @@ 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 -->
<?PHP
/* https://github.com/kubedzero/unraid-snmp
*
* Above params:
* Menu is the section of Unraid in which 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
*
* Below: PHP and Javascript to customize the SNMP settings page in Unraid
*/
?>


<!-- This block of PHP runs on each page load -->
<?
// Dynamix provided function to parse /boot/config/plugins/snmp/snmp.cfg
// Declared in /usr/local/emhttp/plugins/dynamix/include/Wrappers.php
// Sample line from .cfg: SETTINGNAME="value";
$cfg = parse_plugin_cfg('snmp');

// Load the live snmpd.conf into a variable
$snmpd_conf = file_get_contents('/etc/snmp/snmpd.conf');
// Stolen from SMBExtras.page, standardize newlines
$snmpd_conf = preg_replace(["/\r\n/","/\r/"],"\n",$snmpd_conf);

?>

<!-- Creates the settings form, which can POST updates to the server -->
<!-- Configures reads/writes to the file /boot/config/plugins/snmp/snmp.cfg -->
<!-- update.php is found at /usr/local/emhttp/update.php -->
<form markdown="1" method="POST" action="/update.php" target="progressFrame">
<!-- Stolen from update.php. Parameters with empty strings are omitted from being written to the file -->
<input type="hidden" name="#cleanup" value="true">
<input type="hidden" name="#file" value="snmp/snmp.cfg">
<!-- PHP to execute after exiting this file but before handing off to update.php -->
<input type="hidden" name="#include" value="/plugins/snmp/run_before_save.php">
<!-- auto restart snmp after saving -->
<input type="hidden" name="#command" value="../../../usr/bin/bash"> <!-- Unraid appends this to /usr/local/emhttp/ -->
<input type="hidden" name="#arg[0]" value="/etc/rc.d/rc.snmpd">
<input type="hidden" name="#arg[1]" value="restart">

<!-- 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 -->
<!-- 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.

<!-- Stolen from SMBExtras.page, format the text entry field and safely print smnpd.conf -->
<!-- https://stackoverflow.com/questions/3318132 -->
Custom snmpd.conf contents:
: <textarea spellcheck="false" rows="<?=substr_count($snmpd_conf,"\n")+1?>" maxlength="2048" name="SNMPDCONF" style="resize:none;font-family:bitstream;width:40%"><?php echo htmlspecialchars($snmpd_conf); ?></textarea>

<!-- Popup help section that can be accessed by clicking on the category name -->
> This text will be copied to /etc/snmp/snmpd.conf and also backed up to USB. SNMP will also be restarted after clicking Apply or Default

<!-- 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)">
Expand All @@ -53,6 +86,10 @@ Enable Unsafe Temperature Checking:
<!-- It finds form values by their ID -->
<script type="text/javascript">
function setDefaults(form) {
form.unsafeTemp.value = "0";
form.unsafeTemp.value = "0";
form.SNMPDCONF.value = `
<? $snmpd_conf_default = file_get_contents('/usr/local/emhttp/plugins/snmp/snmpd.conf');
echo $snmpd_conf_default; ?>
`;
}
</script>

0 comments on commit 14cf6e8

Please sign in to comment.