Client networking configuration using ARD

Recently all of my clients were set to ethernet duplex/speed of 100/full. We upgraded all of the switches and they are setup to auto negotiate. So I have have to change all of my client’s ethernet configuration to automatic. Here is a UNIX command that I sent through ARD to update all of my clients on the fly.

networksetup -setMedia en0 autoselect

Refer to the networksetup man pages for many other useful networking commands. In the terminal do:
man networksetup

ARD reports no serial number

For certain machines when running reports in Apple Remote Desktop it will return no computer serial number. To get around this you can grep the system_profiler:

system_profiler | grep -i “serial number (system)”

Kill all httpd processes

Recently the web service on a Snow Leopard Server was extremely laggy, taking up to 3 minutes to load a page. To solve this I killed all of the httpd processes with the following command:

kill -9 `ps -ef | grep [h]ttp | awk ‘NR>1 {print $2}’`

Better Understanding Virtual OS X Servers

Have you ever thought about going virtual yet? Why or why not? I will admit the word virtual has kind of scared me, but why? Am I afraid that I need everything running on a physical Apple server hard drive? Or maybe I just don’t trust the software or myself to run a virtual environment properly? These are all good concerns but I am diving in head first and want to hear your feedback.

Why go virtual?

My main reason is that I am currently supporting one setup of 3 Intel Apple Xserves; production, testing, and development. They are all pretty much exact copies of themselves being used for Web and MySQL services. Recently I upgraded the machines to OSXS 10.5 and then my clients wanted me to role everything back to OSXS 10.4 due to a few bugs. I’m not exactly sure why these bugs were never caught on the testing and development servers since they were exact copies!? Anyway instead of having to swap drives every weekend, my plan is to go virtual. This way I can take a snapshot of my VM (Virtual Machine) and then roll back whenever my clients want. All from the comfort of my own home. ;)

What Virtual Management Application should I use?

Well so far I have tested Parallels Server for Mac (PSFM) and VMware Fusion 3. PSFM is supposedly an enterprise software where as VMWF3 is specifically for desktop use only but will run Snow Leopard Server. Now I don’t really see very many differences after testing and I’m not exactly sure why PSFM is considered enterprise software. However, with that being said I do think that PSFM is a little better. I’m not going to go into these differences since VMWF3 is specifically built for desktop use, but I figured I would compare the 2 anyway. PSFM will set you back $1248.75, but for what it’s worth I think that is a great price. I haven’t found anything else to try yet so far, but please share if you know of something better?

Understanding Virtual Management?

It is very simple to understand, a server that runs the virtual management software is called the host. This server’s virtual management software will run virtual guests (or extra operating systems) on the same server. My initial concern was DNS and how it was handled but the virtual management software takes care of this seamlessly for you, either with a NAT or bridge setup through your host machine.

Apache Benchmarking on Virtual OS X Server

The following shows you how to setup and run some files for Apache Benchmarking. I give you the code, command, and results for each file test. The file code I supplied is pretty generic but you can adjust it to better test your environment. I simply wanted to get baseline results using a Virtual Server. The best way to get a standardized time is to run each test multiple times and use the best time. I also watched the CPU load during my testing which I included. Enjoy!

Environment:

OS X Server 10.5.8 Virtual Server setup w/3GHz & 1GB 667MHz DRAM

Physical Server 2×3GHz Quad-Core Intel Xeon 4GB 800 MHz DDR2 FB-DIMM

Initial Load Average: 14:05 up 20:36, 2 users, load averages: 0.22 0.05 0.02

Post Load Average: 16:39 up 23:10, 2 users, load averages: 1.89 1.32 0.64


HTML test:

CODE

<html>

<head>

<title>Webserver test</title>

</head>

<body>

This is a webserver test page.

</body>

</html>

ENDCODE

Basic HTML test results of 1000 requests:

localhost:~ username$ ab -n 1000 -c 5 http://mysite/benchmarking/testpage.html

Time taken for tests: 0.793 seconds

I also watched the CPU load as each test was run:

Performance results: httpd processes averaged 1.5% CPU load


PERL TEST:

CODE

#!/usr/bin/perl

$command=`perl -v`;

$title = “Perl Version”;

print “Content-type: text/html\n\n”;

print “<html><head><title>$title</title></head>\n<body>\n\n”;

print ”

<h1>$title</h1>

\n”;

print $command;

print “\n\n</body></html>”;

ENDCODE

PERL TEST results of 1000 requests:

localhost:~ username$ ab -n 1000 -c 5 http://mysite/benchmarking/perltest.pl

Time taken for tests: 0.724 seconds

Performance results: httpd processes averaged 6% CPU load


PHP/MYSQL TEST

CODE

<html>

<head><title>Php+MySQL</title></head>

<body>

<?php

$link = mysql_connect(“localhost”, “user”, “pass”);

mysql_select_db(“test”);

$query = “SELECT * FROM benchmarking WHERE int_field=’5′”;

$result = mysql_query($query);

while ($row = mysql_fetch_array($result))

{

echo $row[0], “”, $row[1], $row[2], “”, $row[3], $row[4], “”, $row[5], “”, $row[6], “”, $row[7], “<p>”;

}

mysql_close($link);

?>

</body>

</html>

ENDCODE

PHP/MYSQL TEST results of 1000 requests:

localhost:~ username$ ab -n 1000 -c 5 http://mysite/benchmarking/mysqltest.php

Time taken for tests: 5.149 seconds

Performance results:

httpd processes averaged 10% CPU load

mysqld process averaged 25% CPU load

10.6 serveradmin not connecting fix port 311

1. Delete all servermgr preferences in /Library/Preferences/

com.servermgr_info.plist

com.servermgr_notification.plist

com.servermgrd.plist

2. Unload servermgrd process in the terminal:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.servermgrd.plist

3. Copy /Library/Preferences from a working 10.6 server to the same location. You can also get this from an OS X S 10.6 install disc in /Library/System/Preferences if you don’t have another build to snag it from.

com.servermgr_info.plist

com.servermgr_notification.plist

com.servermgrd.plist

4. Load servermgrd process in the terminal:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.servermgrd.plist

MySQL slow query log parser

I couldn’t find a mysql slow query log parser any where on the web that I could utilize, so I wrote one in bash.  This script will parse a MySQL Slow Query Log with the following parameters:

–current mysql slow query log path –query time

Here is a CLI example:

path_to_this_mysql_slow_query_parser.sh /path_to_your/mysql-sloq-query-log 30

This will print all slow queries that took 30 seconds or longer ~/Desktop/MSQP_ouput.txt

#!/bin/bash
# Galen Sprague 2009
# This script will parse a MySQL Slow Query Log with the following parameters.
# “–current mysql slow query log path –query time”
# CLI example:
# path_to_this_mysql_slow_query_parser.sh /path_to_your/mysql-sloq-query-log 30
# This would print all slow queries that took 30 seconds and higher to ~/Desktop/MSQP_ouput.txt
#count each line of the mysql slow query log
let COUNTER=0
#counter for each slow query
let SLOWQUERYCOUNTER=0
#read each line of the mysql slow query log
cat $1 | while read LINE
do
# Count each LINE
let COUNTER=COUNTER+1
#While reading each line see if we are on a line that contians “# Time: “
if [[ $LINE == *'# Time: '* ]]
then
ACTUALTIMEQUERYWASRUN=$LINE
TIMEALREADYPRINTED=’false’
fi
#While reading each line see if we are on a line that contians “# Query_time: “
if [[ $LINE == *'# Query_time: '* ]]
then
# Get the query_time number on this line
# Example: “# Query_time: 0  Lock_time: 0  Rows_sent: 1  Rows_examined: 85102″
# by parsing every thing from the right of “time:”
LINE=${LINE#*time:}
# and by parsing everything from the left of “Lock” giving use the query time number
LINE=${LINE%% Lock*}
if (( $LINE >= $2 ))
then
# GETLINESTART = Go back a line from the query time to collect the user data
let GETLINESTART=COUNTER-1
# For CLI ouput
let SLOWQUERYCOUNTER=SLOWQUERYCOUNTER+1
# To show progress in CLI
echo $SLOWQUERYCOUNTER”. High Query_time on line: ” $COUNTER ” printing to file…”
# print the time for these queries only once
if [[ $TIMEALREADYPRINTED == 'false' ]]
then
# echo time the query was run
echo $ACTUALTIMEQUERYWASRUN >> ~/Desktop/MSQP_output.txt
TIMEALREADYPRINTED=’true’
fi
#Print Query info
sed -n -e “$GETLINESTART,/;/p” $1 >> ~/Desktop/MSQP_output.txt
#if there is a use database statement first then add the following select statement
let USEDBLINE=COUNTER+1
CHECKFORUSEDB=`sed -n -e “$USEDBLINE p” $1`
if [[ $CHECKFORUSEDB == *'use '* ]]
then
echo “We are in the USEDB if…”
let SELECTLINE=USEDBLINE+1
sed -n -e “$SELECTLINE p” $1 >> ~/Desktop/MSQP_output.txt
let SELECTLINE=0
fi
let USEDBLINE=0
# echo line feed to separate queries
echo >&2 >> ~/Desktop/MSQP_output.txt
fi
fi
done

#!/bin/bash

#count each line of the mysql slow query log

let COUNTER=0

#counter for each slow query

let SLOWQUERYCOUNTER=0

#read each line of the mysql slow query log

cat $1 | while read LINE

do

# Count each LINE

let COUNTER=COUNTER+1

#While reading each line see if we are on a line that contians “# Time: “

if [[ $LINE == *'# Time: '* ]]

then

ACTUALTIMEQUERYWASRUN=$LINE

TIMEALREADYPRINTED=’false’

fi

#While reading each line see if we are on a line that contians “# Query_time: “

if [[ $LINE == *'# Query_time: '* ]]

then

# Get the query_time number on this line

# Example: “# Query_time: 0  Lock_time: 0  Rows_sent: 1  Rows_examined: 85102″

# by parsing every thing from the right of “time:”

LINE=${LINE#*time:}

# and by parsing everything from the left of “Lock” giving use the query time number

LINE=${LINE%% Lock*}

if (( $LINE >= $2 ))

then

# GETLINESTART = Go back a line from the query time to collect the user data

let GETLINESTART=COUNTER-1

# For CLI ouput

let SLOWQUERYCOUNTER=SLOWQUERYCOUNTER+1

# To show progress in CLI

echo $SLOWQUERYCOUNTER”. High Query_time on line: ” $COUNTER ” printing to file…”

# print the time for these queries only once

if [[ $TIMEALREADYPRINTED == 'false' ]]

then

# echo time the query was run

echo $ACTUALTIMEQUERYWASRUN >> ~/Desktop/MSQP_output.txt

TIMEALREADYPRINTED=’true’

fi

#Print Query info

sed -n -e “$GETLINESTART,/;/p” $1 >> ~/Desktop/MSQP_output.txt

#if there is a use database statement first then add the following select statement

let USEDBLINE=COUNTER+1

CHECKFORUSEDB=`sed -n -e “$USEDBLINE p” $1`

if [[ $CHECKFORUSEDB == *'use '* ]]

then

#echo “We are in the USEDB if…”

let SELECTLINE=USEDBLINE+1

#sed -n -e “$SELECTLINE p” $1 >> ~/Desktop/MSQP_output.txt

GETSELECTSTATEMENT=`sed -n -e “$SELECTLINE p” $1`

if [[ $GETSELECTSTATEMENT == 'SELECT ' ]]

then

#there are multiple lines for this select statement

sed -n -e “$SELECTLINE,/;/p” $1 >> ~/Desktop/MSQP_output.txt

else

#there is just one line

sed -n -e “$SELECTLINE p” $1 >> ~/Desktop/MSQP_output.txt

fi

let SELECTLINE=0

fi

let USEDBLINE=0

# echo line feed to separate queries

echo >&2 >> ~/Desktop/MSQP_output.txt

fi

fi

done

Enjoy! ;)

Snow Leopard Software Update Server

Are you having problems getting the Snow Leopard Software Update Server to work, don’t worry you are not alone? There are plenty of issues posted on the web and plenty of hacked fixes that just didn’t work for me. But luckily you came here instead and I have the fix for you! Apple doesn’t really give you a good tutorial for setting this up at all and if you are not really experienced with OS X Server you might have some trouble getting Software Update Server running properly.

First turn on Software Update in Server Admin.

Set the settings the way you want, they are pretty self explanatory. However, leave the path and port settings as is.

Next allow the server to download all of the updates before doing anything else. If you have a slow connection this could take quite some time, maybe even a day or two. Refer to the logs in Server Admin/Software Update to see when all of the downloads have finished.

Now in Server Admin under Web Services, create a site using port 8088 and change the Web Folder path to: /var/db/swupd/html

Note: Wikis and blogs will be enabled automatically, so you may want to disable them on this port which you can do by selecting your site in Server Admin/Web and then select the Web Services tab and uncheck anything you don’t want enabled.

Enable the Server Admin/Web service.

Now in Workgroup Manager set your client’s or group’s path that you want to have managed for Software Updates to: http://serverName:8088/index.sucatalog

If your client is currently logged in, they will need to logout and back in to see any software updates that you have enable for them to download.

UNIX check CPU usage for a process

This script will check to see if a certain process is utilizing CPU. You can then set up a Lingon or launchd job to run this script whenever you want to be notified or to kick off some other process.

#!/bin/bash

OUTPUT=`ps alux | grep yourProcess | grep -v grep | awk ‘{print $5}’`

if [ $OUTPUT -gt 31 ]

then

mail -s “The Process is Running” your@email.com

fi


Notes: OUTPUT is searching all processes and getting yourProcess while ignoring the actual grep command for your process and then printing or setting the variable OUTPUT to the 5th column which is the CPU usage. When the CPU usage is at 31 the process is running but idle (not utilizing any CPU). So when the process is greater than 31 then the process is utilizing the CPU. In my example above when my process starting running and is no longer idle I will then receive an email. There are other ways to obtain the CPU usage with the “ps” command, but then you need to deal with decimals. The example above is easier to code and to understand.


Important: The command above uses the mail command which uses postfix (open source mail transfer agent) in OS X. If wish to use this and your provider is blocking this then you will need to uncomment and enter your providers relayhost in /etc/postfix/main.cf

Unix at command

The Unix “at” command can be very helpful to run a process at a specific time.

For instance, say you want to be emailed a list of files or folders from a job folder. You can run the following command to kick the job off at 1pm:

ls -l /Volumes/Macintosh\ HD/myJobFolder | mail -s ‘JobFolder List’ yourEmail@blah.com | at 13:00

Important: The command above uses the mail command which uses postfix (open source mail transfer agent) in OS X. If wish to use this and your provider is blocking this then you will need to uncomment and enter your providers relayhost in /etc/postfix/main.cf