Archive for the ‘Code’ Category

Sending HTML Email from the Linux Command Line

Posted 41 days ago by Alex in Code, Randomness, Technology

Just a little tip that I found useful for a project I was working on today. I had to send the results of a MySQL query via email. Since the results are printed in a table, the formatting in the email had to use a monospace font. The easiest way to use a monospace font in an email is to enable HTML and wrap everything in a <pre> tag.

#!/bin/bash
mailto=email@awesome.tld
subject="The awesome subject line of your email message"
(
  echo "Subject: $subject"
  echo "MIME-Version: 1.0"
  echo "Content-Type: text/html"
  echo "Content-Disposition: inline"
  echo "<html><body><pre>"
  mysql db -uawesome -pawesomer --table < some-sql-to-run.sql
  echo "</pre></body></html>"
) | /usr/sbin/sendmail $mailto

The output looks like this:

To: email@email.tld
From: system@awesome.tld
Subject: The awesome subject line of your email message
+----------+----------+----------+
| Column 1 | Column 2 | Column 3 |
+----------+----------+----------+
| Apples   |        2 |    14.00 |
| Oranges  |        2 |   0.5000 |
+----------+----------+----------+

MySQL “duplicate entry for ’127′ for key 1″ in Rails

Posted 113 days ago by Alex in Code, Technology

After building a lightly used rails application for internal use at the company I work for, I got a notification that there was an error in the application. Someone was trying to add a new record and it was failing (500 ISE). I checked the logs and found the following:

ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '127' for key 1: INSERT INTO...

It turns out that this problem is triggered when the id column is of type TINYINT which has a maximum signed value of 127. This of course causes the above error to occur when record 128 is entered.

I was able to resolve this by changing the column type to INT using the MySQL client.

ALTER TABLE employees MODIFY id INTEGER NOT NULL AUTO_INCREMENT;

Disable nouveau drivers in Fedora 15

Posted 133 days ago by Alex in Code, Software, Technology

Edit: This also works in Fedora 16. During the upgrade I had to boot into single user mode to switch to run level 3 and reinstall the latest nVidia drivers. Then switch back to run level 5 and reboot.

When trying to install the latest nVidia drivers on my Fedora 15 workstation, I kept getting errors about the default nouveau video driver being loaded into the kernel. The nVidia installer creates a modprobe config file that is supposed to prevent that module from being loaded but it doesn’t work fully.

To really disable the nouveau driver, you need to edit the grub config file and add the following to the end of the kernel init line:

rdblacklist=nouveau nouveau.modeset=0

For example, your resulting grub.conf file will look like this:

title Fedora (2.6.40.6-0.fc15.x86_64)
        root (hd0,1)
        kernel /vmlinuz-2.6.40.6-0.fc15.x86_64 ro root=/dev/mapper/vg_cline-lv_root noiswmd LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us rhgb vga=794 quiet rdblacklist=nouveau nouveau.modeset=0
        initrd /initramfs-2.6.40.6-0.fc15.x86_64.img

Augeas Lens for Modifying Munin Nodes

Posted 195 days ago by Alex in Code, Software, Technology

Augeas (from EPEL) doesn’t come with a default lens for the munin-node.conf files that control Munin Nodes. So I whipped one together.

(* Munin Node module for Augeas *)

module MuninNode =
  autoload xfm

  let record =
    let value = store /[^ \t\n]+([ \t]+[^ \t\n]+)*/ in
      [ key Rx.word . Sep.space . value . Util.eol ]

  let lns = (record | Util.comment | Util.empty) *

  let filter = incl "/etc/munin/munin-node.conf" . Util.stdexcl

  let xfm = transform lns filter

List last modified file in all subdirectories

Posted 456 days ago by Alex in Code, Randomness, Software

I don’t know when someone would use this little snippet, but I think it’s useful.

It will open every directory in /home and retrieve the last modified file in all subdirectories under that directory. Then it will print out the file’s modification time and path. This might be useful for a system administrator who wants to see when the last time a user modified a file in their home directory.

for i in `find /home -maxdepth 1 -mindepth 1 -type d`; do
  cd $i
  find `pwd` -type f -printf "%TY-%Tm-%Td %TT %p\n" | sort | tail -n 1
done

To run it, save the code as ‘directory-age-check.sh’ and run:

 sudo ./directory-age-check.sh | sort 

Image Rotator Script

Posted 1130 days ago by Alex in Code

<?
// Image Rotator Script
// By Alex Cline (http://alexcline.net)
// Displays the next image in the array based on a cookie which stores the previous image
// If the cookie doesn't exist, the first image is shown.
// User must have cookies enabled for this to work.

// Create the array of images.  This must contain the names of all the images.
$imgArray = array("image1.jpg", "image2.gif", "image.png");
// Path to where your images are stored
$hostDir = "http://www.somedomain.com/path/to/images/"; // Don't forget that ending '/'

$imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
$imgToShow = 0; // We'll default to always show the first image.

// Check to see if the cookie has already been created.
if ($_COOKIE['imagerotate'] != "") {
  // The cookie existed
  $imgToShow = $_COOKIE['imagerotate'];
  if($imgToShow + 1 >= $imgArraySize) {
    // The image we were supposed to show next would have been out of bounds in the array.
    $imgToShow = 0;  // We'll show the first image
  }
  else  // The image to be shown is not out of bounds.
    $imgToShow++;  // Increment the image counter
}

// Write the new cookie with the new image we are showing
setcookie ("imagerotate", $imgToShow, time()+1000);

//Print the image from the directory.
echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";
?>