Add Scroogle to your search area in Firefox 2.0 Install the 'Scroogle Scraper' search plugin.

class profiler
{
public $start_time;
public $debugMode;
function start($debugMode=false)
{
        $t = microtime(true);
        $this->start_time=$t;
        $this->debugMode=$debugMode;
}
 
function stamp($stampName)
{
        $t = microtime(true);
        $stampTime=$t-$this->start_time;
        if ($debugMode)
        {
                echo("## $stampName completed in $stampTime seconds ##");
        }
        $this->start_time=$t;
}
}
$profile = new profiler;
$profile->start();
 
code code code
 
$profile->stamp('some descriptive text here');
 
more code code code
 
$profile->stamp('more  descriptive text here');

developers have sandboxes with code checked out from svn. they develop code there. once it’s good, they check code in and then update the staging server with that new code. it’s tested there by QA. once QA signs off, then those specific files are pushed out to the production servers by the release manager by running the pshfiles.sh script. the script pushes to one or all servers, emails the dev team, logs the push, checks the servers afterwards for health and clears the zend cache.

[deployer@staging ~]$ cat pshfiles.sh
#!/bin/bash
if [ "$1" == '' ]; then
   echo "Usage: pshfiles.sh [file_name_with_files_in_it] [server_name]"
else
SERVER=$2
if [ "$SERVER" == "all" ]; then
SERVERLIST=( web1 web2 web3 )
else
SERVERLIST=( $SERVER )
fi
 
for SERVER in ${SERVERLIST[@]}
do
   while read line; do
      echo "psh $line $SERVER"
      pshonenocache $line $SERVER
   done < $1
done
 
FILESPUSHED=`cat $1`
sendEmail.pl -s smtp.example.com -f deployer@example.com -m "these files were pushed: \n $FILESPUSHED" -xu deployer@example.com -t devteam@example.com -xp password -u "Code pushed to $2"
 
echo "Clear Zend cache?"
read ZEND
if [ "$ZEND" == "y" ] || [ "$ZEND" == "Y" ]; then
/usr/bin/clroptcache $2
fi
 
fi

where pshonenocache is:

[deployer@staging ~]# cat /usr/bin/pshonenocache
#!/bin/bash
TODAY=`date '+%Y-%m-%d %H:%M'`
if [ "$1" == '' ]; then
   echo -n "Which File?"
   read -e FILENAME
else
   FILENAME=$1
fi
 
if [ "$2" == '' ]; then
   echo -n "Which Server? (web1,web2,web3,all) : "
   read -e SERVER
else
   SERVER=$2
fi
 
if [ "$SERVER" == "all" ]; then
SERVERLIST=( web1 web2 web3 )
else
SERVERLIST=( $SERVER )
fi
 
for SERVER in ${SERVERLIST[@]}
do
        if [ -e $FILENAME ]; then
                echo -e "$TODAY: Company SOURCE Update ONE FILE: \tscp $FILENAME deployer@$SERVER:$FILENAME" | tee -a ~/updateLogs/$SERVER-updates.log
                scp $FILENAME deployer@$SERVER:$FILENAME
        fi
 
done
/usr/bin/checkwebservers

where checkwebservers is:

[deployer@staging~]$ cat /usr/bin/checkwebservers
#!/bin/bash
echo -n "Checking Web Servers after PSH: "
SERVERSTATUS=`wget -qO- http://example.com/checkServersStatus.html|grep "All is Well"|wc -l`
DETAILEDSTATUS=`wget -qO- http://example.com/checkServersStatus.html`
 
if [ "$SERVERSTATUS" != "1" ]; then
        echo "CRITICAL - one of the web servers isn't well: $DETAILEDSTATUS"
else
        echo "OK - The webservers look good"
fi

and clroptcache is

[deployer@staging ~]# cat /usr/bin/clroptcache
#!/bin/bash
 
if [ "$1" == '' ]; then
   echo -n "Which Server?( web1,web2,web3,all) : "
   read -e SERVER
else
   SERVER=$1
fi
 
if [ "$SERVER" == "all" ]; then
SERVERLIST=( web1 web2 web3 )
else
SERVERLIST=( $SERVER )
fi
 
for SERVER in ${SERVERLIST[@]}
do
        echo -e "Clearing Zend Optimizer Cache on $SERVER \n"
        if [ "$SERVER" == "web1" ]; then
                wget -qO- http://10.10.10.1/cacheclear.php | cat
        fi
        if [ "$SERVER" == "web2" ]; then
                wget -qO- http://10.10.10.2/cacheclear.php | cat
        fi
        if [ "$SERVER" == "web3" ]; then
                wget -qO- http://10.10.10.3/cacheclear.php | cat
        fi
done

sometimes (rarely) you might want to push ALL files. then you use the psh script:

[deployer@server ~]# cat /usr/bin/psh
#!/bin/bash
TODAY=`date '+%Y-%m-%d %H:%M'`
if [ "$1" == '' ]; then
   echo -n "Which Server? (web1,web2,web3,all) : "
   read -e SERVER
else
   SERVER=$1
fi
 
echo "run PHP Lint check first? (on files modified during last X days?)"
read -e LINT
if [ "$LINT" == "y" ] || [ "$LINT" == "Y" ]; then
echo "how many days?"
read DAYS
cd /classes && find -name "*.php" -mtime -$DAYS -exec /usr/local/zend/bin/php -l {} \;
fi
 
if [ "$SERVER" == "all" ]; then
SERVERLIST=( web1 web2 web3 )
else
SERVERLIST=( $SERVER )
fi
 
for SERVER in ${SERVERLIST[@]}
do
echo -e "$TODAY: Company SOURCE Update:" | tee -a ~/updateLogs/$SERVER-updates.log
echo "Updating $SERVER..." | tee -a ~/updateLogs/$SERVER-updates.log
echo "Command DRY Run:" | tee -a ~/updateLogs/$SERVER-updates.log
rsync -avczne ssh /classes/ $SERVER:/classes/ --links --exclude-from "/home/deployer/rsync_exclude.txt" | tee -a ~/updateLogs/$SERVER-updates.log
rsync -avczne ssh /var/www/ $SERVER:/var/www/ --links --exclude-from "/home/deployer/rsync_exclude.txt" | tee -a ~/updateLogs/$SERVER-updates.log
echo -n "Press Y to Accept This Change : "
read dec
if [ "$dec" == "Y" ]; then
        rsync -avcze ssh /classes/ $SERVER:/classes/ --links --exclude-from "/home/deployer/rsync_exclude.txt" | tee -a ~/updateLogs/$SERVER-updates.log
        rsync -avcze ssh /var/www/ $SERVER:/var/www/ --links --exclude-from "/home/deployer/rsync_exclude.txt" | tee -a ~/updateLogs/$SERVER-updates.log
 
else
   echo 'Command NOT Accepted' | tee -a ~/updateLogs/$SERVER-updates.log
fi
 
echo "Done!" | tee -a ~/updateLogs/$SERVER-updates.log
 
done
 
echo "Clear Zend cache?"
read ZEND
if [ "$ZEND" == "y" ] || [ "$ZEND" == "Y" ]; then
/usr/bin/clroptcache $1
fi
 
/usr/bin/checkwebservers

what’s next? well, it would be nice to be able to revert pushes quickly. so perhaps taking a backup right before would be handy. otherwise, you have to rollback the svn commit and repush.

We have 10 distributors for a tire.  Each one has a different price:
 
   1. 100
   2. 120
   3. 99
   4. 140
   5. 210
   6. 40
   7. 104
   8. 102
   9. 103
  10. 150
 
We need to, in SQL hopefully, exclude the higher and lower prices automatically and get an average of the closest prices.

Here’s a quick answer:

identify outliers (1 standard deviation from the mean):
 
mysql> SELECT tires.price, (tires.price - agro.mean) / agro.dev AS num_devs  FROM tires CROSS JOIN (     SELECT AVG(price) AS mean, STDDEV(price) AS dev      FROM tires ) agro WHERE ABS( tires.price - agro.mean ) / agro.dev > 1 ORDER BY tires.price
    -> ;
+-------+------------------+
| price | num_devs         |
+-------+------------------+
|    40 | -1.8407371215298 |
|   210 |  2.2338111943565 |
+-------+------------------+
2 rows in set (0.00 sec)
 
mysql>
 
strip outliers:
mysql> select t.price from tires t left join (SELECT tires.id, tires.price FROM tires CROSS JOIN (     SELECT AVG(price) AS mean, STDDEV(price) AS dev      FROM tires ) agro WHERE ABS( tires.price - agro.mean ) / agro.dev > 1 ORDER BY tires.price) o on t.price=o.price where o.price is null;
+-------+
| price |
+-------+
|   100 |
|   120 |
|    99 |
|   140 |
|   104 |
|   102 |
|   103 |
|   150 |
+-------+
8 rows in set (0.00 sec)
 
get your stats, excluding those outliers:
mysql> select avg(t.price), stddev(t.price) from tires t left join (SELECT tires.id, tires.price FROM tires CROSS JOIN (     SELECT AVG(price) AS mean, STDDEV(price) AS dev      FROM tires ) agro WHERE ABS( tires.price - agro.mean ) / agro.dev > 1 ORDER BY tires.price) o on t.price=o.price where o.price is null;
+--------------+-----------------+
| avg(t.price) | stddev(t.price) |
+--------------+-----------------+
|       114.75 | 18.673175948403 |
+--------------+-----------------+
1 row in set (0.00 sec)
 
mysql>

see for reference: http://www.sitecrafting.com/blog/stats-in-mysql-pt-outliers/

yum install pptp-linux
yum install pptp
vi /etc/ppp/chap-secrets
vi /etc/ppp/peers/myvpnname
vi /etc/ppp/ip-up

where:

[root@server local]# cat /etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
VPNUSERNAMEHERE PPTP VPNPASSWORDHERE *

and

[root@server local]# cat /etc/ppp/peers/myvpnname
pty "pptp VPNIPADDRESS --nolaunchpppd"
name VPNUSERNAMEHERE
remotename PPTP
require-mppe-128
file /etc/ppp/options.pptp
ipparam myvpnname

and

[root@server local]# cat /etc/ppp/ip-up
PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH
 
LOGDEVICE=$6
REALDEVICE=$1
 
[ -f /etc/sysconfig/network-
scripts/ifcfg-${LOGDEVICE} ] && /etc/sysconfig/network-scripts/ifup-post --realdevice ${REALDEVICE} ifcfg-${LOGDEVICE}
 
/etc/ppp/ip-up.ipv6to4 ${LOGDEVICE}
 
[ -x /etc/ppp/ip-up.local ] && /etc/ppp/ip-up.local "$@"
 
exit 0

and then run it!

pppd call myvpnname
ip route add 10.0.0.0/8 dev ppp0

the goals of our weekly code reviews:

1. Find and eliminate code defects
2. Communication – understand what others are working on
3. Responsibility – reduce code hacks, develop and enforce conventions
4. Discussion on methodology
5. Security best practices (XSS, SQL injection, etc.)
6. Conventions/code style (function and variable naming conventions, etc.), e.g.:
1. http://pear.php.net/manual/en/standards.php
2. http://framework.zend.com/manual/en/coding-standard.html
7. SQL best practices- only return the columns of data you need, etc.
8. REGEX best practices
9. OO best practices
10. Javascript best practices

Here is a PHP Code Review Checklist

1. No deprecated functions in the code
2. There should be an explanation for any code that is commented out. “Dead Code” should be removed. If it is a temporary hack, it should be identified as such.
3. Check if code has file/class/function level comments. Describe each function, method, and class in one or two sentences at the top of its definition.
4. Check that each function is doing only a single thing. That is never function named createCustomer should delete the existing customer and create it again
5. A method should not be larger than 40 lines of code. The basic idea is always a function should be viewable in single screen with out scrolling.
6. Always try to initialize the variable before using that in a function.
7. No public class attributes.
8. Always try to use constants in the left hand side of the comparison. That is instead of doing if ($variable == 2) always use if (2 == $variable) because this will help to identify the errors in the earlier stage of development even if we miss and “=” from that statement
9. Always try using single quote ( ‘ ) when working with the php string because it has a minor performance improvement when compared to the double quotes ( ” ). The difference come from the factor that ( ” ) can be used to build a string with variables inside it.
10. In the case of a system crash never ever put up the error information that exposes the internal behavior of the system.
11. No magic numbers. There should be no magic numbers like 6,10 etc… any numbers like this should be define as a constant. And Constants should be well commented about the purpose.
12. Always try to have unit test for the new piece of code. In ideal condition we should have 100% unit test coverage
13. Does the code have an impact on size, speed, or memory use? Can it be optimized?
14. Optimizations may often make code harder to read and more likely to contain bugs. Such optimizations should be avoided unless a need has been identified. Has the code been profiled? Check if any over optimization has led to functionality disappearing.
15. Are function parameters explicitly verified in the code?
16. Check for code paths that can cause infinite loops
17. As a reviewer, you should understand the code. If you don’t, the review may not be complete, or the code may not be well commented.

srcs:

http://joseantony.com/2010/12/04/php-code-review-checklist/

http://charlesconradvaz.wordpress.com/2006/02/16/code-review-checklist-2/

here’s a pre-commit script which does PHP lint checks on checked in files. I didn’t write it, I’m posting it here for reference

#!/bin/bash
 
REPOS="$1"
TXN="$2"
 
# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null || exit 1
 
PHP="/usr/bin/php"
SVNLOOK="/usr/bin/svnlook"
AWK="/bin/awk"
GREP="/bin/egrep"
SED="/bin/sed"
 
CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | $GREP "^[U|A]" | $AWK '{print $2}' | $GREP \\.php$`
 
for FILE in $CHANGED
do
        MESSAGE=`$SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | $PHP -l`
        if [ $? -ne 0 ]
        then
                echo 1>&2
                echo "***********************************" 1>&2
                echo "PHP error in: $FILE:" 1>&2
                echo `echo "$MESSAGE" | $SED "s| -| $FILE|g"` 1>&2
                echo "***********************************" 1>&2
                exit 1
        fi
done
 
# All checks passed, so allow the commit.
exit 0

here is a post-commit script that calls a PHP file which emails out the diff using PEAR’s SMTP class

#!/bin/sh
echo "post-commit"
REPOS="$1"
REV="$2"
/usr/bin/php /var/svn/repos/hooks/commit-email.php $REPOS $REV

and here is that php commit-email script

#!/usr/bin/php
<?php
 require_once "Mail.php";
 
 $host = "smtp.example.com";
 $username = "svntest@example.com";
 $password = "svntest";
 
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 
$repository = isset($argv[1]) ? $argv[1] : '';
$revision = isset($argv[2]) ? $argv[2] : '';
 
$author = exec('/usr/bin/svnlook author ' . $repository);
$changed = exec('/usr/bin/svnlook changed ' . $repository);
$log = exec('svnlook log ' . $repository);
 
$diff = '';
$fp = popen('svnlook diff ' . $repository, "r");
while(!feof($fp)) {
  $diff .= fread($fp, 1024);
  flush();
}
pclose($fp);
 
$project_name = 'Project Name';
 
$body = "$author has just committed revision $revision in $project_name and said $log.
The following files have changed :
$changed
The changelog is given below
$diff";
 
$to = 'devteam@example.com';
 
$subject = "svn commit: $repository $revision";
 
$headers = array ('From' => 'noreply@example.com',
   'To' => $to,
   'Subject' => $subject);
 
 $mail = $smtp->send($to, $headers, $body);
 
 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent! $body</p>");
  }

you can pipe the output of a crontab cron to the mail command to get its output, for example:

0 6 * * *  /usr/bin/php script.php | /bin/mail -s `daily_order_validation` spencer@example.com

Sends an email to spencer every day.

But what if you only want NONBLANK emails? You could do something like:

data=$(/usr/bin/php script.php); [[ -n "$data" ]] && mail -s 'only non blank email' spencer@example.com<<< "$data"

what if you only want an email if the script output has the word ERROR in it?

data=$(/usr/bin/php script.php); [[ $data == *ERROR* ]] && mail -s 'only non ERROR email' spencer@example.com<<< "$data"

src: http://unix.stackexchange.com/questions/13326/how-to-pipe-output-from-one-process-to-another-but-only-execute-if-the-first-has/13337#13337
and http://unix.stackexchange.com/questions/13326/how-to-pipe-output-from-one-process-to-another-but-only-execute-if-the-first-has

#!/bin/bash
 
HOSTNAME=`hostname`
LOADAVG1=`/usr/bin/top -b -n 1|head -n 1|awk '{print $12}'`
LOADAVG1=`echo "$LOADAVG1"|sed 's/\(.*\)./\1/'`
 
LOADAVG5=`/usr/bin/top -b -n 1|head -n 1|awk '{print $13}'`
LOADAVG5=`echo "$LOADAVG5"|sed 's/\(.*\)./\1/'`
 
LOADAVG10=`/usr/bin/top -b -n 1|head -n 1|awk '{print $14}'`
 
PROCESSES=`/usr/bin/top -b -n 1|head -n 2|tail -n 1|awk '{print $2}'`
MEMUSED=`/usr/bin/top -b -n 1|head -n 4|tail -n 1|awk '{print $4}'`
SWAP=`/usr/bin/top -b -n 1|head -n 5|tail -n 1|awk '{print $2","$4}'`
NOW=`date '+%Y-%m-%d %H:%M:%S'`
 
echo "$NOW,$HOSTNAME,$LOADAVG1,$LOADAVG5,$LOADAVG10,$PROCESSES,$MEMUSED,$SWAP"

then make it executable

chmod +x systemstats.sh

and then in crontab:

* * * * * /root/systemstats.sh >>/root/systemstats.log

wget http://aws-catalog-download-files.s3.amazonaws.com/AmazonSES-2011-07-21.zip
unzip AmazonSES-2011-07-21.zip
vi aws-credentials
export AWS_CREDENTIALS_FILE=aws-credentials
./ses-verify-email-address.pl -k aws-credentials -v test@example.com
./ses-verify-email-address.pl -k aws-credentials -l
./ses-get-stats.pl -q
echo "test email">msgbody
ses-send-email.pl -k aws-credentials -s "Testing amazon SES" -f verified@example.com destination@example.com < msgbody

here you need to request production access so you can send to non-verified emails.

you can also setup sendmail to send through amazon, but if you’re using php’s mail function, be sure to set the return path to a verified email via the secret fifth parameter:

cat testphpmail.php
<?php
$to      = 'destination@example.com';
$subject = 'test php mail';
$message = 'hello';
$headers = 'From: verified@example.com' . "\r\n" .
    'Reply-To: verified@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
 
print(mail($to, $subject, $message, $headers,"-fverified@example.com" ));
?>

of course, it’s trivial to send mail directly from php:
first, include their one file (ses.php) and then:

$m = new SimpleEmailServiceMessage();
$m->addTo($email);
$m->setFrom('verified@example.com');
$m->setSubject($subject);
$m->setMessageFromString($body);
$con->sendEmail($m);

here is that script: http://sourceforge.net/projects/php-aws-ses/

Next Page »

Send to a friend * Print this page * Join the club * Talk with my robot * Advertise here * Search this Site * Donate * Link to me


Web hosting by Utah Hub *  Powered by CreativeTap *  In association with Segomo
Unless otherwise noted, Copyright 2004-2006, Ryan Byrd. All Rights Reserved.
Ryan Byrd dot net -- probably the coolest site in Utah