Shutdown Daemon

June 10th, 2011 robin Posted in Ironman, Perl | 3 Comments »

A typical problem with a home server/NAS used by several people is that someone always turns it on, but who turns it off?  It stays on for days, and the electricity meter whirs happily as the kilowatts get unnecessarily burned…

A better strategy is to let the server shut itself down when it is not being used… intelligently… and this is how I made it happen:

How to determine if the server is not being used

This depends a lot on your setup.  In my case I have a Linux NAS with 6 disks.  One disk (/dev/sda) is the operating system (Linux) disk, and the other 5 are in a (software) raid array serving the shared filesystem which I and my flatmates and I use.  This makes it pretty easy to see if someone is using the system because if none of the 5 raid disks are active, then no one can be using them.  You can see if they are active with hdparm.

A brief introduction to hdparm

hdparm is a really handy linux utility for setting and reading hard disk parameters/status.  To set the spindown time of a disk to 20 minutes:

hdparm -S 240 /dev/sdb

To make your settings persistent, put them into /etc/hdparm.conf

/dev/sdb {
spindown_time = 240
}

To see the current state (active or not) of a disk:

hdparm -C /dev/sdb

Look up the hdparm documentation for all options.

How to shut down the server…

For this we need a daemon which monitors the hard drive status, and initiates a shutdown when the desired… Introducing: Shutdown Daemon (SDD).

This is a script I put together to do exactly what I described above.  For my setup I have used the following configuration:

---
log_level: INFO
log_file: /var/log/sdd.log
sleep_before_run: 60
monitor:
 hdparm:
 loop_sleep: 60
 trigger_time: 3600
 disks:
  - /dev/sdb
  - /dev/sdc
  - /dev/sdd
  - /dev/sde
  - /dev/sdf

This means it waits one minute before starting monitoring, checks the disk status every minute, and will wait for an hour of spun-down disk status before actually shutting down the server (unless one of the disks becomes active again within that time).

Installing SDD

Clone sdd from the github repository:

git clone git@github.com:robin13/sdd.git

Make it:

cd sdd
perl Makefile.PL
make
make test
make install

Copy the init.d script and config, and update the rc.d entries:

cp examples/etc/sdd.conf /etc/sdd.conf
vim /etc/sdd.conf
cp examples/init.d/sdd /etc/init.d/sdd
update-rc.d sdd defaults
/etc/init.d/sdd start

Saving power

In my case, just configuring the spindown on the hard drives reduces the power consumption of the NAS server from 44 to 28Watt.  Now factor in all those times where the server was forgotten and left on for days… the power saving potential is fantastic!

What other monitors could be useful?

Of course spindown time of the disks isn’t the only condition which could be useful for shutting down your server.  Here’s some others I’ve thought of:

  • hard drive temperature too high
  • No user logged in with ssh (users)
  • No locks on samba file shares (smbstatus -L)

If you have any ideas, feel free to fork the project on github and implement a Monitor – I’m looking forward to seeing many more monitors! 🙂


Varnish::CLI

March 13th, 2011 robin Posted in Ironman, Perl | Comments Off on Varnish::CLI

Varnish is the cutting edge in reverse proxy for speeding up web services and reducing load on your web application server.  It has a pretty neat CLI for managing a running Varnish instance, but being an interactive telnet interface, it is not so easy to script for…

Some examples why you might want to interface to the Varnish CLI with a script:

  • Automatic reload of configuration file when it changes
  • Purge the cache for a specific website when changes are made (new blog entry, new CSS, …)

And so I wrote up a Perl interface to the Varnish CLI (git repo here).  The module is Moosey, and requires Net::Telnet and Digest::SHA to run.

It’s pretty easy to use – here’s a snippet with which you can purge your cache:

use Varnish::CLI;
my $v = Varnish::CLI->new();
$v->send( "url.purge .*" );
$v->close();

Have fun, and keep your web services zipping with Varnish! 🙂


FSpot::DbTool

February 20th, 2011 robin Posted in Ironman, Perl | 1 Comment »

Introducing FSpot::DbTool!

F-Spot is a pretty neat photo management application which comes with the Gnome desktop.  I have been using it for a few years to manage my photos, and by now I have over 23,000 photos in it!  It has lots of handy features: tagging, versions, dates… but there are a few things it doesn’t do so well: moving files from one location to another (not all your files, just a sub-section), compacting the database, and I’m sure some more will come to mind!  The database it uses is SQLite – the perfect application for this kind of database!

I’m getting into Moose (a post-modern object system for Perl 5), so this weekend I put together a neat interface to the FSpot database: FSpot::DbTool.  It comes with a command line tool fspot-tool.

Usage:
 fspot-tool.pl [options]

Options:
 --action
    move_dir
      Requires: from, to Optional: merge, rename
    replace_path
      Requires: from, to
    find_lost
      Requires: path find_orphans
    clean_trailing_slashes
    tag_no_description
    backup_db
    check_db
    compact_db

 --from   Path from
 --to     Path to
 --path   It's a path!
 --merge  Do a merge
 --rename Rename files if necessary

Actions:
 move_dir:
 Move pictures from one directory to another. Does the move on the
 filesystem, and the corresponding move of the path in the FSpot database
 afterwards

 replace_path:
 Useful if you have already moved files in your filesystem (e.g. moved
 your entire photo folder to a new location. Will replace all instances
 of a path with a new path

 e.g.

 from = /home/clarkero/my_pictures
 to   = /home/rcl/pictures/processed

 clean_trailing_slashes:
 Clean trailing slashes off the end of paths in the database

 find_lost:
 Find files which exist in the filesystem, but do not exist in the
 database

 find_orphans:
 Find orphaned files (files which exist in in the database, but not on
 the filesystem)

 tag_no_description:
 Tag all photos which do not yet have a description

 check_db:
 Confirms you have the correct database version for this script to work
 with

 backup_db:
 Make a backup of the database This is carried out automatically for
 move_dir, replace_path and compact_db actions.

 compact_db:
 Carries out the SQLite compact function (VACUUM) on the database.

By using Moose::Roles I’ve paved the way for easily adding additional tools for the database – just add them into the lib/FSpot/DbTools directory!

I hope you have fun with FSpot, and find a use for the DbTool.