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.

#!/usr/bin/ruby
require 'trollop'

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

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"
end

case opts.check
  when "battery_time"
    battery_time = `snmpwalk #{opts.hostname} -v #{opts.vers} -c #{opts.community} -On .1.3.6.1.2.1.33.1.2.3.0 | tr -d ' ' | cut -d ":" -f 2`
    if battery_time.empty?
      puts "UNKNOWN: #{opts.hostname} is unreachable!"
      exit 3
    end
    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
    else
      puts "OK: There is #{battery_time.chomp} minutes left before the power goes out.|BatteryTime=#{battery_time.chomp};;;;"
      exit 0
    end
  when "battery_temp"
    battery_temp = `snmpwalk #{opts.hostname} -v #{opts.vers} -c #{opts.community} -On .1.3.6.1.2.1.33.1.2.7.0 | tr -d ' ' | cut -d ":" -f 2`
    if battery_temp.empty?
      puts "UNKNOWN: #{opts.hostname} is unreachable!"
      exit 3
    end
    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
    else
      puts "OK: The UPS battery is #{battery_temp} degrees Fahrenheit.|BatteryTemp=#{battery_temp};;;;"
      exit 0
    end
  when "sensor_temp"
    sensor_temp = `snmpwalk #{opts.hostname} -v #{opts.vers} -c #{opts.community} -On .1.3.6.1.4.1.318.1.1.10.2.3.2.1.4 | tr -d ' ' | cut -d ":" -f 2`
    if sensor_temp.empty?
      puts "UNKNOWN: #{opts.hostname} is unreachable!"
      exit 3
    end
    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
    else
      puts "OK: The UPS external sensor is #{sensor_temp} degrees Fahrenheit.|BatteryTemp=#{sensor_temp};;;;"
      exit 0
    end
  else
    puts "Valid check options are: battery_time, battery_temp, sensor_temp"
end
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.

#!/bin/bash
IFACE=eth0
SEC=5
while getopts ":i:s:" opt; do
 case $opt in
 i)
 IFACE=$OPTARG
 if [ ! -f /sys/class/net/$IFACE/statistics/rx_bytes ]; then
 echo "Interface $IFACE does not exist!"
 exit 1
 fi
 ;;
 s)
 SEC=$OPTARG
 ;;
 \?)
 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
 ;;
 esac
done
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`
RXBytes3="$(((RXBytes2-RXBytes1)/SEC))"
TXBytes3="$(((TXBytes2-TXBytes1)/SEC))"
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.

[Unit]
Description=Don't Starve Together Server
After=network.target
[Service]
ExecStart=/home/steam/dontstarve/bin/dontstarve_dedicated_server_nullrenderer
GuessMainPID=no
Type=simple
User=steam
Group=steam
WorkingDirectory=/home/steam/dontstarve/bin
[Install]
WantedBy=multi-user.target

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:

#!/bin/bash
systemctl stop dontstarve
su - steam -c "/home/steam/updatedontstarve.sh"
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/bash
/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.

http://repos.servertoast.com/repos/coins/el/6/

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 http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-19.noarch.rpm
sudo rpm -Uvh http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-19.noarch.rpm

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: http://developer.amd.com/tools-and-sdks/heterogeneous-computing/amd-accelerated-parallel-processing-app-sdk/downloads/

Then use the below commands to install it.

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

The installer now creates files in /etc/ld.so.conf.d 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: http://developer.amd.com/tools-and-sdks/graphics-development/display-library-adl-sdk/

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

wget http://ck.kolivas.org/apps/cgminer/3.7/cgminer-3.7.2.tar.bz2
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
make
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.

[hdhomerun]
name=HDHomeRun - $basearch
baseurl=http://repos.servertoast.com/repos/hdhomerun/fedora/19/
failovermethod=priority
enabled=1
gpgcheck=0

And the SRPMS are here:

[hdhomerun-src]
name=HDHomeRun Source RPMS - $basearch
baseurl=http://repos.servertoast.com/repos/hdhomerun/fedora/SRPMS/
failovermethod=priority
enabled=1
gpgcheck=0
Posted in Uncategorized | 1 Comment