Renaming Apache Log Locations

Published on Sunday, January 25, 2009

I realized a few of my log files were growing unusually large, and even worse, logrotate was skipping them. I took a look in logrotate.d and straight away realized why: I had created silly names for the log file. logrotate look for .log files, but I had specified mine as .log -- e.g. kelvinism_access_log. I was as familiar with logrotate when I set up the domains, so set forth to get them in the rotation.

Firstly, I had to rename the actual log files. So, to rename kelvinism_access_log to kelvinism_access.log, a one-liner:

for x in *_log; do mv $x `basename $x _log`.log; done;


Next, I needed to rename the log location inside each of the Apache config files. While a one-liner might be possible, I used the following tiny script:

#!/bin/sh

for x in *
do
sed 's/\_log/\.log/' $x > /tmp/tmpfile.tmp
mv /tmp/tmpfile.tmp $x
done

Beginning Scripting ESXi

Published on Tuesday, January 6, 2009

I'm not impressed too often with much software, especially the closed source kind. I find a leaning preference to all things FOSS. If I had a million dollars, I'd likely spend all day contributing to all the projects I wish I had time to contribute to. Regardless, there are a select few closed-source products that I believe are truly excellent. I mean, the type of software where you aren't asking "I wish this could do this" and start asking "I wonder what else this can do."

While I've played around with most types of virtualization out there (OpenVZ, Xen, V-Server, qemu...), I've really found a soft spot for VMWare.

Don't get me wrong, if I was going to host a heap of Linux web servers I would absolutely use Xen, but for a heterogeneous environment, I haven't used anything as easy as VMWare's products. Not that I judge a product by how easy it is to use, not by a long shot, but ease of use sure makes judging other factors easier.

Regardless, this isn't a post trumpeting VMWare. I just realized tonight that some of the VMs I have running don't need to be except for certain hours of the day, or if condition A is true. The first example is my backup mail server; I really don't need it even powered on unless my main server is down. The second example is my Server 2003 instance, which has VI3 on it; I don't need this running unless I'm asleep. One of the most useful resources I've seen for the vmrun command is over at VirtualTopia -- loaded with examples.

Turn off via time



On my "monitoring" instance, which is always up, I've decided to install the script that controls my VM. I've opted to use a soft shutdown.


192.168.0.10 = ESXi box

datastore1 = name of datastore that hosts VMs

#!/bin/sh

vmrun -t esx -h https://192.168.0.10/sdk -u root -p root_password stop "[datastore1] Server 2003 R2/Server 2003 R2.vmx" soft


I have that saved in a file called stop_2003.sh in /opt/vmware/bin; make sure it isn't world readable. I also have a start_2003.sh:

#!/bin/sh

vmrun -t esx -h https://192.168.0.10/sdk -u root -p root_password start "[datastore1] Server 2003 R2/Server 2003 R2.vmx"



Next, edit root's crontab (crontab -e):

# m h  dom mon dow   command
0 8 * * * /opt/vmware/bin/start_2003.sh
0 23 * * * /opt/vmware/bin/stop_2003.sh


The conditional task is a tad bit more tricky, but just a tad. Ping won't do, since the mailserver could go down itself, so install nmap. Create a script:

#!/bin/bash

if nmap -p25 -PN -sT -oG - mail.kelvinism.com | grep 'Ports:.*/open/' >/dev/null ; then
echo `time` >> mailserver.log
else
/opt/vmware/bin/start_mail.sh
fi


And sticking with our theme, start_mail.sh:

#!/bin/sh

vmrun -t esx -h https://192.168.0.10/sdk -u root -p root_password start "[datastore1] Mail Server/Mail Server.vmx"


This of course changes the crontab entry to:

# m h  dom mon dow   command
0 8 * * * /opt/vmware/bin/start_2003.sh
0 23 * * * /opt/vmware/bin/stop_2003.sh
*/5 * * * * /opt/vmware/bin/detect_port.sh


So, that's it. detect_port.sh is lacking any type of error detection or redundancy - if one packet/scan is dropped, the mail server will turn on. I'll re-work this at some point, but it works for now.

Update: Vmware has also released a decent blog entry about using vmrun: on their blog.