Nagios Plugin for checking a couple of things on APC SmartUPS

Below is the code I wrote recently for checking a few things on an APC SmartUPS using SNMP.  The check can be used for checking three things currently: amount of battery time remaining, battery temperature, and temperature from a connected sensor.  The script also converts the temperature from Celsius to Fahrenheit.  Many apologies for my terrible code structure.  I am not really a developer.  I just occasionally have to write Nagios plugins and have been trying to do them in ruby instead of bash lately to help teach myself that language.

Also, presuming you are using RHEL/CentOS/SL, you need to do install the following package first.

yum -y install rubygem-trollop

Copy/paste the code below into a file, add to your nagios plugins or libexec directory and add execute permissions. If you are using a different distro like Debian/Ubuntu you may need to update the path for the ruby executable and use apt to install the ruby trollop gem.

require 'trollop'

def temp_conversion(a)
  return 9 * a.to_i / 5 + 32

opts = Trollop::options do
  opt :hostname, "The hostname or IP of the APC UPS", :type=> :string, :short => "-H"
  opt :community, "The SNMP Community string: Defaults to public", :type => :string, :default => "public", :short=> "-C"
  opt :vers, "The SNMP Version: Defaults to 2c", :type => :string, :default => "2c", :short=> "-V"
  opt :check, "The type of APC UPS Check to run: Valid options are battery_time_left,battery_temp,sensor_temp", :type => :string, :short=> "-x"
  opt :warning, "The WARNING threshold for the check", :type => :integer, :short=> "-w"
  opt :critical, "The WARNING threshold for the check", :type => :integer, :short=> "-c"

case opts.check
  when "battery_time"
    battery_time = `snmpwalk #{opts.hostname} -v #{opts.vers} -c #{} -On . | tr -d ' ' | cut -d ":" -f 2`
    if battery_time.empty?
      puts "UNKNOWN: #{opts.hostname} is unreachable!"
      exit 3
    if battery_time.to_i < opts.critical
      puts "CRITICAL: There is #{battery_time.chomp} minutes left before the power goes out!|BatteryTime=#{battery_time.chomp};;;;"
      exit 2
    elsif battery_time.to_i < opts.warning
      puts "WARNING: There is #{battery_time.chomp} minutes left before the power goes out!|BatteryTime=#{battery_time.chomp};;;;"
      exit 1
      puts "OK: There is #{battery_time.chomp} minutes left before the power goes out.|BatteryTime=#{battery_time.chomp};;;;"
      exit 0
  when "battery_temp"
    battery_temp = `snmpwalk #{opts.hostname} -v #{opts.vers} -c #{} -On . | tr -d ' ' | cut -d ":" -f 2`
    if battery_temp.empty?
      puts "UNKNOWN: #{opts.hostname} is unreachable!"
      exit 3
    battery_temp = temp_conversion(battery_temp.to_i)
    if battery_temp > opts.critical
      puts "CRITICAL: The UPS battery is #{battery_temp} degrees Fahrenheit!|BatteryTemp=#{battery_temp};;;;"
      exit 2
    elsif battery_temp > opts.warning
      puts "WARNING: The UPS battery is #{battery_temp} degrees Fahrenheit!|BatteryTemp=#{battery_temp};;;;"
      exit 1
      puts "OK: The UPS battery is #{battery_temp} degrees Fahrenheit.|BatteryTemp=#{battery_temp};;;;"
      exit 0
  when "sensor_temp"
    sensor_temp = `snmpwalk #{opts.hostname} -v #{opts.vers} -c #{} -On . | tr -d ' ' | cut -d ":" -f 2`
    if sensor_temp.empty?
      puts "UNKNOWN: #{opts.hostname} is unreachable!"
      exit 3
    sensor_temp = temp_conversion(sensor_temp.to_i)
    if sensor_temp > opts.critical
      puts "CRITICAL: The UPS external sensor is #{sensor_temp} degrees Fahrenheit!|BatteryTemp=#{sensor_temp};;;;"
      exit 2
    elsif sensor_temp > opts.warning
      puts "WARNING: The UPS external sensor is #{sensor_temp} degrees Fahrenheit!|BatteryTemp=#{sensor_temp};;;;"
      exit 1
      puts "OK: The UPS external sensor is #{sensor_temp} degrees Fahrenheit.|BatteryTemp=#{sensor_temp};;;;"
      exit 0
    puts "Valid check options are: battery_time, battery_temp, sensor_temp"
Posted in Uncategorized | Leave a comment

Microsoft Office sucks (with opening URLs)

Ran into a really bizarre situation today with loading URLs from inside a Word document.  Microsoft Office apparently does some stupid junk with pre-loading the URLs inside Office and will follow URLs straight to logout page instead of passing it to the browser untouched like a normal application would.  I managed to fix this in Apache by basically telling MS Office to have an HTTP 200 code anytime it attempts to access the server.  Below is the configuration block for that.

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*ms.*office.*$
RewriteRule .* - [R=200,L]
Posted in Uncategorized | Leave a comment

Nagios plugin to monitor bandwidth statistics

Below is a little plugin I wrote today for Nagios to monitor bandwidth usage on an interface.  It’s pretty basic and there are probably much better ways to do this, but this doesn’t require installing any extra packages on most systems.

while getopts ":i:s:" opt; do
 case $opt in
 if [ ! -f /sys/class/net/$IFACE/statistics/rx_bytes ]; then
 echo "Interface $IFACE does not exist!"
 exit 1
 echo "Invalid option: -$OPTARG"
 echo "Valid Options are -i to choose an interface and -s to specify number of seconds to average bandwidth over."
 echo "Interface will default to eth0 if not specified and seconds will default to 5 if not specified" >&2
 exit 1
 echo "Option -$OPTARG requires an argument." >&2
 exit 1
RXBytes1=`cat /sys/class/net/$IFACE/statistics/rx_bytes`
TXBytes1=`cat /sys/class/net/$IFACE/statistics/tx_bytes`
sleep $SEC
RXBytes2=`cat /sys/class/net/$IFACE/statistics/rx_bytes`
TXBytes2=`cat /sys/class/net/$IFACE/statistics/tx_bytes`
echo 'OK - Bandwidth Statistics only|RX='$RXBytes3'Bytes;;;;|TX='$TXBytes3'Bytes;;;;'
Posted in Uncategorized | Leave a comment

Don’t Starve Together Dedicated Server on Linux stuff

Recently, I have been playing a game called Don’t Starve Together with my wife.  I wanted to play with setting up a dedicated server and followed the very well made guide at Don’t Starve Together Game Wikia Dedicated Server Setup

But at the end of setting things up, I didn’t like that it was being run using screen instead of a proper managed process that would start on a reboot.  Since I run Cent7 on my server, it was actually super easy to get this going with systemd.  I have messed with setting up init scripts before, but systemd made things way easier to get going.  Below is a paste of the systemd unit file I came up with.

Description=Don't Starve Together Server

Most of the file is pretty self explanatory, but the main things I had to change was the Type from fork to simple, and I had to set the WorkingDirectory to where the binary was installed.  For some reason, it fails to start if you don’t set that.  The last thing I had to do was like a “systemctl enable dontstarve“.  You may or may not have to also do a “systemctl daemon-reload“.  I know I had to several times when I was making edits to the file to get it just right.

Lastly, I made a script to update it nightly and added it into cron.  Below is that script:

systemctl stop dontstarve
su - steam -c "/home/steam/"
systemctl start dontstarve

And then I made one more script to send an email everyday with a list of the users that connected to the service that day.  Again, this was made super simple due to journalctl.  I know a lot of people are down on that part of systemctl, but I found it a bit easier to get what I wanted vs having to pull it from a normal syslog file.

/bin/journalctl --since today | grep joined | mailx -s "Today's Don't Starve Together players" root@localhost
Posted in Uncategorized | Tagged | Leave a comment

CGMiner for ButterflyLabs or ASIC USB Erupter RPMs for RHEL6(and clones)

I created some RPMs for CGMiner on RHEL6(CentOS and Scientific also) for CGMiner that one is compiled for the ButterflyLabs ASIC and one for the USB ASIC Erupter(Icarus).  I have tested both of these as working.  You can grab the RPMS at the below URL.  I can build Fedora RPMS if people are interested.

Posted in Uncategorized | Leave a comment

Compiling CGMiner on Fedora 19 with AMD Catalyst Drivers

Below is a guide to get cgminer compiled and working on Fedora 19 with scrypt using the AMD Catalyst and OpenCL drivers.

First, you will need to enable the RPMFusion Repo for the AMD Catalyst drivers.

sudo rpm -Uvh
sudo rpm -Uvh

Then, you will need to yum install the AMD Catalyst driver and some other needed libraries.

sudo yum -y install curl-devel automake gcc ncurses-devel kmod-catalyst

You should reboot after installing the AMD binary drivers and then run the following command if you have multiple amd cards to get them into the Xorg config.

sudo DISPLAY=:0 aticonfig -f --initial --adapter=all

You will want to reload X or reboot after running that command to load the changes.

Grab the AMD-APP-SDK from here:

Then use the below commands to install it.

tar -xvzf AMD-APP-SDK-v2.9-lnx64.tgz
sudo ./

The installer now creates files in /etc/ to load the libraries.  I think you may be able to trigger this with the ldconfig command, but a reboot definitely reloads it.

Grab the latest AMD-ADL-SDK from here:

Then in the same directory where you downloaded the ADL_SDK zip file, run the below commands:

tar -xvjf cgminer-3.7.2.tar.bz2
unzip -j ADL_SDK_*.zip include/*.h -d cgminer-3.7.2/ADL_SDK/
cd cgminer-3.7.2
./configure --enable-scrypt --enable-opencl
sudo make install

Now, you should have cgminer installed and ready to run using your AMD videocard.  Run the below command to test that it sees your videocard and everything.

sudo /usr/local/bin/cgminer -n

If you are running this over SSH, you may need to export the display as part of the command.

DISPLAY=:0 sudo /usr/local/bin/cgminer -n
Posted in Uncategorized | Tagged , , , , , | 55 Comments

HDHomerun on Fedora 19

Yay, the good folks making Fedora have finally included the DVB modules in the kernel-devel package.  This makes things a lot easier for crafting RPMs since I no longer have to download the full kernel source and compile against it.

Anyways, I have created a yum repo for both the binary and source RPMS for these.  I would love for somebody to test these out and let me know if things work or not for them.  I created newer hdhomerun and hdhomerun-devel packages than what is already in the Fedora repos.  They are created from today’s CVS.

name=HDHomeRun - $basearch

And the SRPMS are here:

name=HDHomeRun Source RPMS - $basearch
Posted in Uncategorized | 1 Comment

Building a repository for CryptoCoin Mining

My hosting company got pwned and lost my entire VM about a week after I just happened to reinstall the desktop that I built all the RPMs on, so I lost my RPMS, Spec files, and SRPMS.  I might try to rebuild everything in a couple of weeks after Fedora 19 comes out…

I’m working on building a repository so that people can get wallets and mining going easily on Fedora 18 and eventually RHEL/Cent/SL.  Right now, I am mostly working on building the wallets for all the coins(both the daemon and the qt clients).  Here is a link to the site that has a list of what I was working from.  I am building all of them from the git repos with the date of the clone as the version number in the RPM.  Below is a list of the ones I have completed.  Please post any additional coins that you would like to see RPMS built for.

  • bbqcoin
  • bitbar
  • bitcoin
  • elacoin
  • FeatherCoin
  • franko
  • gldcoin
  • litecoin
  • nibble
  • Powercoin
  • ppcoin
  • terracoin
  • Worldcoin

Anyways, here is what you need to dump into /etc/yum.repos.d/coins.repo to install any of these wallets.  If anybody wants, I can put the scripts and spec files out in the repo to look at as well.


name=Crypto Coins - $basearch


name=Crypto Coins - $basearch


name=Crypto Coins - $basearch


name=Crypto Coins - $basearch
Posted in Uncategorized | Leave a comment

CGMiner on Fedora 18 with Nvidia GPU

First, you need to install the Nvidia binary drivers from RPMFusion.  The below commands should handle that:

sudo rpm -Uvh
sudo rpm -Uvh
sudo yum -y install kmod-nvidia xorg-x11-drv-nvidia-devel

Then, you need to download and install the CUDA software from

Install with the following command:

chmod ugo+x
sudo ./ -override compiler

Accept the license, answer n to installing the driver, y to the Toolkit, and y to Samples, I left installation paths at default.

Create a file /etc/ that contains the below two lines:


Download the latest source for cgminer and compile with the following command below:

CFLAGS=-I/usr/local/cuda/include LDFLAGS=-L/usr/lib64/nvidia ./configure --enable-cpumining && make && sudo make install

That should get you to the point of having cgminer compiled for Nvidia on Fedora 18.  Feel free to post comments/corrections.

Posted in Uncategorized | 1 Comment

Setting up dvbhdhomerun on Fedora 17

As always, this guide assumes SELinux is off and that you have installed all updates and rebooted into the latest kernel.  I recently got a couple of HDHomerun devices.  I set an HDHomerun Dual awhile back with TVHeadend.  I just got a Prime and am setting it up with MythTV.  This is a guide to what I had to do to get it going.  It’s based off the guide here.  First, you have to install some pre-requisites to build the dvbhdhomerun drivers.

yum -y install asciidoc binutils-devel cmake elfutils-devel gcc-c++ hdhomerun hdhomerun-devel kernel-devel newt-devel python-devel perl-ExtUtils-Embed redhat-rpm-config rpm-build zlib-devel xmlto hmaccalc bison pciutils-devel

Then, you should grab the source code from here.  Next, run the following commands to download the kernel source and install it.  Replace the kernel/linux directories and filenames to match the ones that you have.

yumdownloader --source kernel
rpm -ivh kernel-$(uname -r).src.rpm
cd ~/rpmbuild
rpmbuild -bp --target $(arch) --rmsource --rmspec SPECS/kernel.spec
cd BUILD/kernel-3.6.fc17/linux-3.6.9-2.fc17.x86_64
cp configs/kernel-3.6.9-x86_64.config .config

Now, in the directory that you are in,do a uname -a and copy the second dash to the end, then open the Makefile with vi or another editor and paste into the EXTRAVERSION line.  My paste was “-2.fc17.x86_64”.  Then do the next commands, changing the kernel to match your version.

make oldconfig
cp /usr/src/kernels/3.6.9-2.fc17.x86_64/Module.symvers .
make prepare scripts

Now, change back to the directory where you downloaded the dvbhdhomerun source package and run the following commands to unbundle it.

tar -xvzf dvbhdhomerun_0.0.11.tar.gz
cd dvbhdhomerun-0.0.11/kernel/

Then, edit the Makefile in that directory with vi or whatever you prefer and change the KERNEL_DIR line to ~/rpmbuild/BUILD/kernel-3.6.fc17/linux-3.6.9-2.fc17.x86_64/ changing the kernel and linux versions to match your systems.  Then run this to get to the next directory you need.

cd ../userhdhomerun/

Then edit CMakeLists.txt with vi or whatever and change SET(LIBHDHOMERUN_PATH /usr/lib/libhdhomerun) to SET(LIBHDHOMERUN_PATH /usr/include/hdhomerun) 

Now, do the following to compile everything.

cd ../kernel/ && make && make install
cp ../debian/dvbhdhomerun-utils.udev /etc/udev/rules.d/
systemctl restart udev.service
modprobe dvb_hdhomerun
cd ../userhdhomerun/
make run

After the last command, you should see some lines that begin with Registered tuner, id from kernel: 0 name: that will indicate that the service found your HDHomerun devices.  You can CTRL+C to stop this. Run the next line to install it.

make install

That should have all you need to get going.  You will need whatever user you want to run the service as to be a member of the video group, ie mythtv user.  You can start the daemon with the command line if you have a mythtv user.

/usr/local/bin/userhdhomerun -f -u mythtv
Posted in Uncategorized | 21 Comments