Linux tips & techniques for developers and system administrators.


6 views

How to email the output of a cronjob to different addresses

By jbayer - Last updated: Thursday, May 10, 2012

Cron is a wonderful tool on a Linux/Unix box.  Normally, when a job runs and some output is generated, it is emailed to the owner of the job.  You can alter this by added the following line to the crontab file:

MAILTO=address@domain.com

However, if you have a number of jobs which are run by a single user, and you want the output from each job to go to a different address, this mechanism falls down.

The following is a simple way to control where the output is emailed to.  All you have to do is source the redirect file, then call the startredirect function specifying the email address(s), subject and if desired, error subject with whatever you want.

The script redirects stdout and stderr to a file, runs the code, and then emails the file to the specified address at the end.  The file is removed by the trap code when the script exits.

#!/bin/bash
. /usr/local/bin/redirect
startredirect "jonathan.bayer@achieve3000.com" "Test subject" "error test subject"
echo "test"
echo "test error" >&2
endredirect

Put the following into:  /usr/local/bin/redirect, the only permissions it needs is read:

#
# startredirect
#
# parameters:
#
# Email address
# Email subject
# Error email subject (optional)
#
function startredirect {
 if [ "$1" = "" -o "$2" = "" ]; then
 echo "No email and/or subject specified, not redirecting output"
 return
 fi
 MAILTO=$1
 SUBJECT=$2
 ERRSUBJECT=$3
fout=/tmp/$$fout$$.txt
 fouterr=$fout
[ "$ERRSUBJECT" != "" ] && fout=/tmp/$$fouterr$$.txt
################
 # Set a trap here for normal exit signals.
 # It says that no matter what signal you exit with, be it normal (0), or some
 # of the abnormal ones, remove the output files in /tmp.
 # If it's a really abnormal one, like an invalid memory ref that causes a
 # SIGSEGV (11) it will not clean up, because presumably you would want to
 # check it out. But 1 2 3 and 15 are all generated intentionally by users.
 ################
 trap endredirect 0 1 2 3 15
# Capture current stout and stderr
 exec 3>&1
 exec 4>&2
# Redirect stdout and stderr to output file
 exec 1>$fout
 [ "$fout" = "$fouterr" ] && exec 2>>$fout
 [ "$fout" != "$fouterr" ] && exec 2>$fouterr
}
#
# endredirect
#
# No parameters
#
function endredirect {
 # Restore stdout and stderr
 exec 1>&3
 exec 2>&4
# Send the mail, a second one if fouterr is different than fout
 [ -s $fout ] && cat $fout | mail $MAILTO -s "$SUBJECT"
 [ "$fout" != "$fouterr" -a -s $fouterr ] && cat $fouterr | mail $MAILTO -s "$ERRSUBJECT"
 rm -f $fout $fouterr >/dev/null 2>&1
}
Filed in Administration, Bash, Programming
24 views

Perl script to add/remove Maintenance

By jbayer - Last updated: Tuesday, May 1, 2012

I found a perl script to add/remove a single host to maintenance in Zabbix.  I needed more, the ability to add multiple hosts to a single maintenance, as well as an entire group.

The script I started with can be found at:

http://www.zabbix.org/wiki/Perl_script_to_add/remove_Maintenance

The attached file is my modified version.  The changes I made are:

  1. Change the curl cmd to a variable. This so if you need to go to a secure site, you can add whatever options you need to access it
  2. Added -t option to specify maintenance type without data collection
  3. Added -g option to specify a group instead of or in addition to a host
  4. Added ability to specify multiple hosts and groups
  5. Converted from the Getopt::Std to Getopt::Long
  6. Added long options –host, –group, –help, –remove

  maint.tar.gz (2.9 KiB, 5 hits)

Filed in Administration, Perl, Zabbix
5 views

Ogre

By jbayer - Last updated: Monday, April 30, 2012

An old favorite of mine, a game called Ogre, which was first issued in 1975, is being reissued in a new release.

It is being funded using the Kickstart web site, and already has exceeded it’s goals by 1500%.

Ogre is a tactical ground combat game set in the late 21st century. In 2085 A.D., armored warfare is faster and deadlier than ever. Hovercraft, tanks and infantry slug it out with tactical nukes. But the most feared weapon of all needs no human guidance. It’s the giant cybernetic tank called the Ogre.

Click on the image below to go to the kickstarter page, and if you like, you can also become a supporter.

Kickogre

Filed in Games
28 views

Etckeeper

By jbayer - Last updated: Tuesday, April 10, 2012

We’ve been using etckeeper lately to back up our /etc directories with a version control system..  I was dissatisfied with it because it was only designed to backup /etc or a specific directory.  It did not have the capability to backup multiple directories, such as

/etc, /usr/local/etc, /var/bind

I’ve been corresponding with the author, Joey Hess, and just sent him a patch which adds the ability to backup multiple directories.  I’ve included the patch below so you can apply it yourself.

This is against version 0.62, which is the current version as of this date.

Once applied, you need to edit the /etc/etckeeper/etckeeper.conf, uncomment and edit the ETCKEEPER_DIR line to specify whatever directories you wish to backup.  Then run “etckeeper init” to initialize the version control directories.


#-------------------- etckeeper.conf patch ------------------------------
*** etckeeper.conf.orig 2012-03-13 15:03:42.000000000 -0400
--- etckeeper.conf 2012-04-10 15:54:12.000000000 -0400
***************
*** 4,9 ****
--- 4,18 ----
 #VCS="bzr"
 #VCS="darcs"
+ # List of directories which etckeeper will be preserving. Do NOT use
+ for /home backups, etc # These are examples of what can be done. If
+ nothing specified, then /etc is # used as a default # Overridden by
+ the -d option # ETCKEEPER_DIR="/etc /var/bind"
+ # ETCKEEPER_DIR="/etc /usr/local/etc"
+ # ETCKEEPER_DIR="/etc /usr/local"
+ # ETCKEEPER_DIR="/etc"
+
 # Options passed to git commit when run by etckeeper.
 GIT_COMMIT_OPTIONS=""
#-------------------- /usr/bin/etckeeper patch -------------------------------
*** etckeeper.orig 2012-02-17 21:52:50.000000000 -0500
--- etckeeper 2012-04-10 15:46:55.000000000 -0400
***************
*** 84,104 ****
 fi
 fi
! if [ -z "$ETCKEEPER_DIR" ]; then
! ETCKEEPER_DIR=/etc
! fi
! cd "$ETCKEEPER_DIR"
! export ETCKEEPER_DIR
! lsscripts() {
! perl -e '
! $dir=shift;
! print join "\n", grep { ! -d $_ && -x $_ }
! grep /^\Q$dir\/\E[-a-zA-Z0-9]+$/,
! glob "$dir/*";
! ' "$1"
! }
! for script in $(lsscripts "$ETCKEEPER_CONF_DIR/$command.d"); do
! "$script" "$@"
 done
--- 84,109 ----
 fi
 fi
! KEEPDIRS="$ETCKEEPER_DIR"
! for keepdir in $KEEPDIRS; do
! ETCKEEPER_DIR=$keepdir
!
! if [ -z "$ETCKEEPER_DIR" ]; then
! ETCKEEPER_DIR=/etc
! fi
! cd "$ETCKEEPER_DIR"
! export ETCKEEPER_DIR
! lsscripts() {
! perl -e '
! $dir=shift;
! print join "\n", grep { ! -d $_ && -x $_ }
! grep /^\Q$dir\/\E[-a-zA-Z0-9]+$/,
! glob "$dir/*";
! ' "$1"
! }
! for script in $(lsscripts "$ETCKEEPER_CONF_DIR/$command.d"); do
! "$script" "$@"
! done
 done
# --------------------------- end of patch ------------------------------------------
Filed in Administration, Open Source
194 views

Installing the new Zabbix 2.0.0rc2

By jbayer - Last updated: Tuesday, April 10, 2012

I’ve taken my script for an automatic installation of Zabbix and modified it to install the 2.0.0rc2.

It is designed to work with any RHEL5+, CentOS 5+, Scientific Linux 5+ or higher systems.

It is best to start with a minimal install. It will download and install all necessary packages.

I’ve developed a directory structure for my zabbix installs, which this uses:

/etc/zabbix – main config directory
/etc/zabbix/alert.d – Alert files go here
/etc/zabbix/cron.d – Cron files go here
/etc/zabbix/externalscripts – External scripts go here
/etc/zabbix/zabbix_agentd.d – zabbix_agentd.conf includes this directory

I hope this helps someone, and would be interested in hearing about any comments.

install_zabbix-2.0.0rc2.tar

Filed in Administration, Open Source, Zabbix
61 views

Installing Zabbix on Redhat/CentOS – v. 1.5

By jbayer - Last updated: Monday, April 9, 2012

Another update.  Changes below.

  1. Updated Zabbix version to 1.8.11
  2. Added install of php-xml and php-xmlrpc, needed for template import
  3. Added better Apache config, inspired by http://zabbixzone.com/zabbix/making-your-frontend-faster/
  4. Updated URL for epel repo
  5. Make default mysql conf file my-large.conf
  6. Added option to use Percona server
  7. Modified zabbix_agentd.conf to include files from zabbix_agentd.d
  8. Changed locations of log files, all files (both client & server) now go in /var/log/zabbix
  9. Added modified init scripts which properly wait for prog to exit before restarting

  install_zabbix.zip (14.8 KiB, 0 hits)

Filed in Administration, Open Source, Zabbix
9 views

Patching Checkinstall to save options between runs

By jbayer - Last updated: Thursday, April 5, 2012

I had a problem where I was working on an existing, installed package for our systems here.  I needed to have the new RPMs be the same as the old ones, except for the release number being incremented.  Checkinstall, while a great program, doesn’t do anything to save options, so you have to re-enter them every time you run it (or create a script to run it).

The attached patch is applied against checkinstall 1.6.2, and adds an automatic method to save all the options in a file called .checkinstall_opts in the directory of the package you are creating.

  checkinstall.zip (1.1 KiB, 2 hits)

Filed in Building packages, Open Source
49 views

Yum and RPM install history

By jbayer - Last updated: Friday, March 30, 2012

Sometimes I need to find out when something was installed.  The following two commands will show the history for yum and rpm, respectively

To get a history of all the yum installs, look at:

/var/log/yum.log

 

Rpm package installation history

If you need when the packages installed in Red Hat or Fedora

rpm -qa –queryformat ‘%{NAME}:\t %{INSTALLTIME:date}\n’

Use the following to get the list of packages sorted by install date/time:

rpm -qa –queryformat ‘%{installtime} (%{installtime:date}) %{name}\n’ | sort -n

Filed in Administration
92 views

Installing latest virt-manager on RHEL6/CentOS6

By jbayer - Last updated: Monday, March 5, 2012

The virt-manager which comes with RHEL6/CentOS6 is somewhat old, being version 0.8.6, which was released on 1/14/2011.  The attached script will take a basic system and download/install all necessary RPMs, and then download and install the latest virt-manager, version 0.9.1 released 1/31/2012

There are a lot of nice improvements which make this a very worthwhile upgrade.

 

  kvm_on_6.tar.gz (1.2 KiB, 0 hits)

Filed in Administration, Virtulization
246 views

Using Partitioning on Zabbix DB tables to improve performance

By jbayer - Last updated: Monday, March 5, 2012

This script will partition your zabbix database to improve the efficiency. It will also create stored procedures to do the necessary housekeeping, and create a cronjob to do this on a daily basis. This script was inspired by the following:

http://zabbixzone.com/zabbix/partitioning-tables/

While the basic SQL is from the above page, this script both creates the necessary SQL for the desired tables, and can create new partitions as the time goes on assuming that the cronjob has been properly entered.

Use all information and scripts at your own risk. Make backups!

 

  partitiontables.zip (4.0 KiB, 88 hits)

 

Usage:

./partitiontables.sh [-h host][-u user][-p password][-d min_days][-y startyear]
 -h host database host
 -u user db user
 -p password user password
 -d min_days Minimum number of days of history to keep
 -m min_months Minimum number of months to keep trends
 -y startyear First year to set up with partitions

After running this script, don’t forget to disable housekeeping if you didn’t have the script disable it, and add the following cronjob:

Change the file /etc/zabbix/zabbix_server.conf as follows to disable the housekeeping:

### Option: DisableHousekeeping
 # If set to 1, disables housekeeping.
 #
 # Mandatory: no
 # Range: 0-1
 ################### Uncomment and change the following line to 1 in
 ################### Then restart the zabbix server
 DisableHousekeeping=1

Cron job

0 0 * * * /etc/zabbix/cron.d/housekeeping.sh

 

Filed in Administration, Database, Zabbix