Archive for the ‘Technology’ Category

Sending HTML Email from the Linux Command Line

Posted 131 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 |
+----------+----------+----------+

Reconfiguring Network Interfaces in CentOS/RHEL Systems Cloned with vCenter

Posted 189 days ago by Alex in Hardware, Randomness, Technology

While cloning CentOS VMs in our environment, I ran into a problem where eth0 wouldn’t start up. When trying to start the networking service, the following error popped up:

Bringing up interface eth0: Device eth0 does not seem to be preset, delaying initialization.      [FAILED]

The reason this error occurs is because networking adapters in cloned VMs are assigned unique MAC addresses, so they don’t conflict with the parent VM. During OS installation, the installer detects the network adapter and udev configures the mapping between the device eth0 and the MAC address. When the MAC address changes udev thinks the device is missing.

To fix this, we need to update udev’s mapping rules to point the eth0 definition to the device with the correct MAC address. Open the file /etc/udev/rules.d/70-persistent-net.rules. You should see something similar to what is below:

# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100f (e1000) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:9c:00:16", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100f (e1000) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:9c:00:18", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

As you can see there are two PCI ethernet adapters present. The original one from the parent VM (MAC: 9c:00:16) and the new one from the current VM (MAC: 9c:00:18). To fix the issue you need to change the eth0 device definition to have the correct MAC address (9c:00:18) and remove the eth1 device. Your resulting file will look like so:

# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100f (e1000) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:50:56:9c:00:18", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

You’ll also want to update the /etc/sysconfig/network-scripts/ifcfg-eth0 file to reflect the correct MAC address. Then, after a quick system restart your eth0 adapter will be back up.

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

Posted 203 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 223 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

Enable Remote Disk Browsing on a Mac

Posted 265 days ago by Alex in Hardware, Software, Technology

I recently bought a blu-ray drive for my PC to be able to watch movies and burn blu-ray disks easily. Sadly, Apple doesn’t offer a Mac with a blu-ray drive nor have I found a blu-ray internal drive for Macbook Pros.

One thing that is available for Macs is called ‘DVD or CD Sharing’, also called ‘Remote Disk’. You can find the default instructions on how to set it up over at Apple’s support article. The instructions include how to install the DVD or CD Sharing client on a Windows PC for sharing to a Mac.

What isn’t included in those instructions is the fact that by default, only Macbook Airs and Mac Minis support Remote Disk out of the box. I guess this is because they are the only two Mac models that don’t have optical drives.

Once DVD or CD Sharing has been enabled on a remote system (and the firewalls between the two systems correctly configured), the following two lines will activate the ‘Remote Disk’ option in the Finder window. Run them in a terminal window, then restart your computer.

defaults write com.apple.NetworkBrowser EnableODiskBrowsing -bool true
defaults write com.apple.NetworkBrowser ODSSupported -bool true

After boot, you should see the following options in your Finder.
Screenshot of Finder with Remote Disk devices
Note: My PC has two disk drives shared in the above picture.

Augeas Lens for Modifying Munin Nodes

Posted 284 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