Friday, August 30, 2013

G-mail Search Query for All Unread Mail except Spam

I use G-Mail, and plenty of people do too.  Why not? It's easily one of the best "free" e-mail service out there.  I often have my mail sorted into labels, with some of the stuff skipping the Inbox.  Why? I also use the account on my phone, and I don't want to be alerted for every bill or other automated notifications that come in for me.  So, in my filters, they skip the Inbox and that way I don't get bothered.   At the end of the day, I have a routine where I review all of the unread e-mail.  Using the search query - "label:unread" will get you every unread mail.. but that also includes everything in Spam.  What to do? The answer is really simple-  use the query:

in:(all -spam) label:unread 

That will get you every mail, except spam.  The ( ) operator acts as an OR selector.  You could, for example, do "subject:angry (bird pig)" to search for all subjects with angry and either pig or bird.

If you want to check out more advanced search queries that you can use in your G-Mail search.. read this page.

Thursday, August 1, 2013

Re-indenting a script in VIM

I use vim daily on all of the systems I manage.  It's a critical tool for me; I can't stand emacs nor can I stand nano.  Whatever.

All of my scripts are perfectly formatted; I swear by this.  Four spaces for tabs.  Opening and closing braces are where they need to be.   I take pride in this.  But, every once in a while, you're going to run into a script or two that wasn't written by you- and you need this formatted proper. 

One note, this only works for scripts that VIM recognizes- so these obscure scripts won't get reformatted properly. 

When you open up the file (`vim some_cool_script.pl`), while in command mode (not edit mode)- type gg=G.  That's without hitting the ":" first.   Literally,  hit 'g', then 'g' again, followed by '=' and *SHIFT*+'g'.  

It has saved me quite a bit of headaches, especially from people who insist tab should be two literal spaces.  /smh

Granted, you can map this to a fn key.  E.g. in .vimrc, have an entry somehwhere on a line showing (this is untested):

map <F5> mzgg=G`z<CR>

But, as I said, on over 50 machines so I'm not about to maintain 50 .vimrc files.  :)  Sometimes it's simpler to just remember the shortcut.


Starting vmtoolsd as a service on Red Hat / CentOS

If you're like me;  you may manage virtual servers within vSphere.. Linux ones.  Red Hat ones, in particular, but this applies to CentOS as well.

A long, long time ago, in a galaxy far away, the vmware-tools setup procedure installed the necessary init script for you.  Lately though, for new images that I've been building - those init scripts aren't getting installed by the vmware tools installation package.  So they don't start up on reboot.  VMware based backups failed; clock were going askew, you name it.   I need that daemon started on reboot.

Without a SysV init script handy, I had to roll my own.. and this is the result;  despite having worked with Linux for well over 15 years, setting up SysV init scripts remain somewhat of a black art.  The ones on our older system were more complicated than we needed.  I was aiming for something simpler and portable.

With RHEL 7, the rumor mills are abuzz with systemd so that may change. But, I'm a practical system administrator, and it isn't here yet...  Yet.

Anyways, here it is;  place this script @ /etc/rc.d/init.d as 'vmtoolsd' then run `chkconfig vmtoolsd on` and `service vmtoolsd start` (if you change the script name, then change chkconfig and service commands accordingly):

#!/bin/bash
#
#   vmtoolsd          Start/stop the vmware tools daemon
#
# chkconfig:  2345 90 60
# description: vmtoolsd is a daemon that starts up.  for some reason, it
#              doesn't include a sysv init startup file in the latest release.
#              so i have to write this
#

### BEGIN INIT INFO
# Provides: vmtoolsd
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 2345
# Default-Stop: 90
# Short-Description: Run vmware tools daemon
# Description:  Yadda yadda.
### END INIT INFO

RETVAL=0
prog="vmtoolsd"
exec="/usr/sbin/vmtoolsd"
lockfile="/var/lock/subsys/vmtoolsd"

# Source function library
. /etc/rc.d/init.d/functions

start() {
    # Ensure no one has access
    if [ $UID -ne 0 ]; then
        echo "User has insufficient privileges."
        exit 4
    fi

    [ -x $exec ] || exit 5

    echo -n $"Starting $prog: "
    daemon $prog --background=/var/run/vmtoolsd.pid
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile

}

stop() {
    if [ $UID -ne 0 ]; then
        echo "User has insufficient privileges."
        exit 4
    fi

    echo -n $"Stopping $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec
        RETVAL=3
    else
        failure $"Stopping $prog"
    fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec -HUP
    else
        failure $"Reloading $prog"
    fi
    retval=$?
    echo
}

rh_status() {
    status -p /var/run/vmtoolsd.pid $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}



case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 2
esac
exit $?