Linux tips & techniques for developers and system administrators.


954 views

Monitoring Memcached servers

By jbayer - Last updated: Wednesday, August 15, 2012

If you are using memcached, you may want to know if it is running or not, and if it isn’t, to get it started.

The attached script will do that.  Run it as on the server which is running the memcached instance(s), as root, from cron, once a minute, and it will check.

In our case, we run multiple memcached instances on a single server.  We run memcached on ports 11211 and 11212.  The 11212 is used by PHP for session management.  The 11211 is used by our application to cache commonly used information.

An example cron entry for this would be:

 

* * * * * /usr/local/bin/memcached_server_status.sh

You need to set a few parameters at the top to customize it for your configuration.

The script can monitor memcached instances on both the local and remote systems.

The script was written and tested on Redhat and CentOS systems.  For any others, the only line you might have to change is the service restart line, around line 78.

Updated 10/25/2012

Added internal loop & sleep, so that multiple checks can be done in a single minute

  memcached_server_status.tar.gz (1.7 KiB, 0 hits)

Filed in Bash, Open Source
2,096 views

Updated Zabbix install script 1.8 (for Zabbix 2.0.1)

By jbayer - Last updated: Thursday, July 12, 2012

Here is the latest update to my zabbix installation script.  It now installs Zabbix 2.0.1, along with everything else necessary.

I noticed that when doing an upgrade, that the config files aren’t always updated properly.  In some cases, old files get deleted.  This usually happens when upgrading from an older set of RPMs where the config files are in different RPMs than the current.

To protect against this, the script will back up the zabbix directories before perform the upgrade.

This bug has been fixed in the current script, however, if you are upgrading from previous versions, it may happen since the config files were in the wrong RPMs.

Please note that if you downloaded this on 7/12, it had a bug.  The current version has been fixed.

Filed in Administration, Zabbix
1,183 views

Disk layout for Zabbix server

By jbayer - Last updated: Monday, June 25, 2012

If you are using my script to install Zabbix, and don’t intend to add anything else to the Zabbix server, then you can partition your disk as follows:

Disk Partition Disk Space
/boot> 500 meg
/ 5 gig
swap 2x memory (ie.  if 1 gig memory, then 2 gig)
/var all remaining disk space

Generally I don’t like having data on the root partition, so I put it in /var instead.  This has worked pretty well for me in my installations.

Filed in Zabbix
1,039 views

Count files in all subdirectories, by directory

By jbayer - Last updated: Friday, June 22, 2012

This is a quickie. I had a need to see how many files were in each directory (not including files in directories underneath)

This little script will do just that, and save the output in a file called “b.csv” (I was lazy).  The file will be formatted as a comma separated csv, which you can load into any spreadsheet:

#!/bin/bash
dir=`pwd`
 [ $# -ne 0 ] && dir=$1
 echo $dir
#echo "ls -1 $1 | wc -l" >b.sh
 echo "find . -maxdepth 1 -type f -print | wc -l" >b.sh
 chmod +x b.sh
 find $dir -type d -print -exec ./b.sh {} \; | awk '{printf("%s%s", $0, (NR%2 ? "," : "\n"))}' >b.csv

 

Filed in Bash, Programming
500 views

CF-Engine 3 Installation

By jbayer - Last updated: Friday, June 22, 2012

We are starting to use CF-Engine 3.  I didn’t find any RPMs for CentOS that I liked, so I whipped up the attached script to download, compile and install CF-engine 3 for me.

This was developed and tested on a CentOS 6.2 system.  It should work on RHEL6, SL6, and probably all the RHEL5 based variants as well.

A quick note about the file.  Once you download it, uncompress it with gzip:

gzip -d cfengine.sh.gz

and just run the script.  This file is something called a “shar” file, which is a way to put files into text.  It’s been slightly modified to run the install script after extracting the included files.

Create a new directory for this, and run it from there, as root.

Udated 7/23/2012, now properly handles both Redhat/CentOS/Scientific Linux 5 & 6 releases

Also can now initialize a local install as a non-policy server

  cfengine.sh.gz (11.7 KiB, 317 hits)

Filed in Administration
6,079 views

Zabbix 2.0.0 Install Script

By jbayer - Last updated: Friday, May 25, 2012

So Zabbix 2.0.0 was recently released.  It has a lot of improvements, and I’ve updated my script to install the new version.

Download onto a bare-bones CentOS, Scientific Linux or RHEL install and run it.

The script is also now capable of building and installing the Zabbix Proxy.  One thing, the proxy requires a database, and unless you build it on the proxy system, you will have to manually install the database; I don’t have that in the script yet.

Filed in Bash, Zabbix
1,016 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
1,078 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, 437 hits)

Filed in Administration, Perl, Zabbix
428 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
1,178 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
asd