Updates from May, 2011 Toggle Comment Threads | Keyboard Shortcuts

  • levin 2:28 am on May 1, 2011 Permalink | Reply
    Tags: , memory, monitoring   

    3 Step to keep track OS memory usage by process 

    Tutorial

    So many people asking how to keep track on the OS memory usage by process, as you know a bit about Physical Memory, Virtual Memory (VSZ), Resident Set Size (RSS), Shared Memory, Paging Memory…etc, counting RSS to determine memory usage is very common way and easier to understand for dummies like me, although it’s not a precise figure while you’re ignoring shared memory, fs cache..etc.
    But anyway this tutorial is starting point to help you monitor the process RSS usage and keep tracking the usage that allows to identify peak/min.

    Alright, you need to create one database or table, and one script and one cron job.

    Step 1 – Create a database for storing stat data

    CREATE DATABASE sysstat DEFAULT CHARACTER SET latin1;
    GRANT INSERT, UPDATE, DELETE, SELECT on sysstat.* to sysstat@'localhost' identified by 'passw0rd';
    USE sysstat;
    CREATE TABLE 'memory' (  'id' int(11) NOT NULL auto_increment,  'date' datetime NOT NULL,  'memory' int(20) NOT NULL,  'process' varchar(300) NOT NULL,  PRIMARY KEY  ('id')) ENGINE=MyISAM AUTO_INCREMENT=21794 DEFAULT CHARSET=latin1;
    

    Step 2 – Script

    Usage:

    To show all memory usage by process

    ./memory_stat.sh usage
    
    [root@vm1 sbin]# ./memory_stat.sh usage
    2011-05-01 01:55:39 16576 /usr/lib/courier-imap/bin/couriertls
    2011-05-01 01:55:39 35576 /usr/lib/courier-imap/bin/imapd
    2011-05-01 01:55:39 38832 /usr/sbin/httpd.worker
    2011-05-01 01:55:39 73100 MailScanner:
    2011-05-01 01:55:39 125632 clamd
    2011-05-01 01:55:39 208964 /usr/libexec/mysqld
    2011-05-01 01:55:39 429720 /usr/bin/php-cgi
    

    To record into database

    ./memory_stat.sh record
    

    To retrieve the usage report

    ./memory_stat.sh report {process} {date - optional}
    
    [root@vm1 sbin]# ./memory_stat.sh report php "2011-05-01 01"
    php usage report on vm1.dreamerworks.net
    date	memory
    2011-05-01 00:00:01	471588
    2011-05-01 00:05:02	468772
    2011-05-01 00:10:02	470788
    2011-05-01 00:15:01	471392
    2011-05-01 00:20:01	472140
    2011-05-01 00:25:01	469016
    2011-05-01 00:30:02	469016
    2011-05-01 00:35:01	469016
    2011-05-01 00:40:01	472376
    

    To housekeep the database

    ./memory_stat.sh housekeep
    

    memory_stat.sh

    #!/bin/bash
    
    process=`ps aux|awk '{print $11}'|sort|uniq|grep -v -e grep -e awk -e ps -e sort -e uniq`
    date=`date +%Y-%m-%d\ %H:%M:%S`
    database="sysstat"
    dbuser="sysstat"
    password="passw0rd"
    housekeepday="30"
    
    usage(){
    for PS in $process; do
    
    echo "$date `ps aux | grep -- $PS |grep -v grep | awk '{sum +=$6}; END {print sum}'` $PS"
    
    done
    }
    
    record() {
    usage > /tmp/usage.log
    mysql -u$dbuser -p$password <<EOF
    use $database;
    `while read date time memory process;
    do
    echo "insert into memory (date,memory,process) values ('$date $time','$memory','$process');"
    done < /tmp/usage.log
    `
    EOF
    
    rm -f /tmp/usage.log
    
    }
    
    housekeep() {
    mysql -u$dbuser -p$password <<EOF
    use $database;
    delete from memory where date < CURRENT_DATE - $housekeepday;
    EOF
    }
    
    report() {
    if [ x$1 = "x" ];then
            echo "Usage: $0 [process]"
            exit
    fi
    process=$1
    datetime_arg="AND date like '$2%'"
    echo "$process usage report on `hostname`"
    mysql -u$dbuser -p$password <<EOF
    use $database;
    `
    echo "select date, memory from memory where process like '%$1%' $datetime_arg order by date";
    `
    EOF
    
    }
    
    case $1 in
    	usage)
    	usage | sort -k 3n
    	;;
            housekeep)
            housekeep
            ;;
            record)
            record
            ;;
    	report)
    	report $2 $3
    	;;
            *)
            echo "Usage: $1 (usage|record|housekeep|report [process] [date])"
            ;;
    esac
    

    Step 3 – Cron Job

    crontab -e
    0,5,10,15,20,25,30,35,40,45,50,55 * * * *       /usr/local/sbin/memory_stat.sh record
    

    That’s it

    At least, you may tell a bit more about which process is huger one and then you’ll able to narrow down for investigation.

     
  • levin 8:16 pm on April 22, 2011 Permalink | Reply
    Tags: ,   

    Rename Post format string in WordPress theme 

    Code

    Because post format string is hard coded in post.php, so you can’t change them in filter way. To rename the post format string and display in a theme, you can add the following function in function.php.

    function post_format_nicename() {
    		global $post;
    		$slug = str_replace('post-format-', '', get_post_format($post->ID));
    		if(empty($slug)) {
    				$slug = 'standard';
    		}
    		$source = array('Standard', 'Status');
    		$replacement = array(__('Blog Post', 'textdomain'),__('Status Update','textdomain'));
    		$strings = str_replace($source, $replacement, get_post_format_strings());
            $string = $strings[$slug];
    		return $string;
    }
    

    To display the mod-ed post formant name, call this function

    
    echo post_format_nicename();
    
     
  • levin 9:08 pm on April 15, 2011 Permalink | Reply
    Tags: ,   

    How to filter “Standard” Post format in WordPress 

    Post format

    As you know Post Formats is a theme feature introduced with Version 3.1. A Post Format is a piece of meta information that can be used by a theme to customize its presentation of a post. The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. There are new template tag to play with post_format as below

    WordPress Codex

    However, when you try to implement post_format, usually you’ll try to sort out the posts for each formats. For example, show all images format.

    <a href="<?php echo get_post_format_link('image');?>">
    Show Image Posts
    </a>
    

    The above code do present a link taking you to post_fomart archives page, pretty easy right? So… how about display the standand post? Unfortunately, there is no out of box function to do that. Let’s work it out.

    functions.php

    Setup theme support in functions.php, I put the post formats in vars, because we need to reused them in some place within the theme. Additionally, I created another function to register query variable to handle new query for standard post format.

    /* Register Query Variables*/
    add_filter('query_vars', 'my_queryvars' );
    function my_queryvars( $qvars )
    {
    $qvars[] = 'filter';
    return $qvars;
    }
    /* Post formats support */
    $my_post_formats = array( 'status','aside','image', 'gallery', 'link', 'video', 'audio' );
    foreach ($my_post_formats as $shortname) {
    	$my_post_formats_longname[] = 'post-format-'.$shortname;
    }
    add_theme_support( 'post-formats', $my_post_formats );
    

    The filter links

    <a href="<?php echo add_query_arg( 'filter','standard',remove_query_arg('post_format'));?>"><?php echo get_post_format_string('');?></a>
    <?php global $my_post_formats; foreach ($my_post_formats as $postformat) { ?>
    	| <a href="<?php echo get_post_format_link($postformat);?>"><?php echo get_post_format_string(str_replace('post-format-', '', $postformat) ); ?></a>
    <?php } ?>
    

    the loop

    Put the following code before the loop. This is a new taxonomy query since WordPress 3.0, it allows you to setup own query by taxonomy terms. And you may notice that I’ve put “NOT IN” operator to filter out standard posts. It is because “standard” post do not have post-format- taxonomy. The first condition to determine the from the $_GET request.

    <?php if(get_query_var('filter') == "standard") { 	global $my_post_formats_longname; 	$args = array( 			'tax_query' =--> array(
    			array( 'taxonomy' => 'post_format',
    				  'field' => 'slug',
    				  'terms' => $my_post_formats_longname,
    				  'operator' => 'NOT IN'
    				  )
    			)
    	);
    	query_posts( $args );
    }
    ?>
    

    That’s it

    Btw, I hope there have a better solution in future WordPress release. Thanks for reading!

     
  • levin 6:36 pm on March 30, 2011 Permalink | Reply  

    Data Protector: Object is a mounted filesystem => not restored. 

    Unexpected mounted filesystems detected when restoring a disk imageWhen restoring a disk image, you get a message that the disk image being restoredis a mounted filesystem and will not be restored:

    Object is a mounted filesystem => not restored.

    This happens when an application on the disk image leaves some patterns on thedisk image. The patterns confuse the system call that verifies whether the filesystemon the disk image is mounted or not, so the system call reports that there is a mountedfilesystem on the disk image.

    Solution

    1. Try zero out the device

    dd if=/dev/zero of=/dev/r[logical volume] bs=8k
    

    2. For AIX, lslv [logical volume] , verify it doesn’t contain DS_LVZ, otherwise, recreate it without -T O
    3. For AIX with IBM SAN Volume Controller, run

    lquerypr -ph  /dev/vpathX
    

    4. For AIX, try not varyon the volume group in concurrent mode.

     

    http://www.google.com/url?sa=t&source=web&cd=2&ved=0CBwQFjAB&url=http%3A%2F%2Fbizsupport.austin.hp.com%2Fbc%2Fdocs%2Fsupport%2FSupportManual%2Fc01631245%2Fc01631245.pdf&rct=j&q=%22Object%20is%20a%20mounted%20filesystem%20%3D%3E%20not%20restored.%20%22&ei=ywWTTbuzJsaPcdWJpYkH&usg=AFQjCNGYnyf6RUAjGFa41m188gLmkq-Nqw&cad=rja

     
  • levin 7:18 pm on March 29, 2011 Permalink | Reply
    Tags: ,   

    Submitting WordPress post from frontend without a plugin 

    Screen Shot

    Screen Shot

    About

    This tutorial will guide you to create your own custom post composing form to enable fast blogging with your favorite theme without adding a plugin. I’m looking for any alternative to not porting P2, however…. none.,  finally that’s the reason i wrote it, and It was also inspired by the original development from http://wpshout.com/wordpress-submit-posts-from-frontend/.

    Why

    I found the code from http://wpshout.com/wordpress-submit-posts-from-frontend/ , but it doesn’t  handle file attachment, indeed most people and I was demand on image uploads for status update,  photo blogging.. whatever . But to get working with build-in WordPress media upload function require a lots of code work,  and if you prefer a fully functional media library, you could base on P2 theme instead.

    How it work

    1. It create a post form to allow  the post content inserted by wp_insert_post.
    2. following the post_id, it attach the uploaded files to the post.
    3. Set the first ordered image to the post “featured images”
    4. Lastly, you call get_all_thumbnails before the_content() in order to retrieve the uploaded files.

    Limitation

    The critical limitation is lack of media features, unlike build-in media library, you can customize your post images and place it on your desired position. Therefore the images only able to retrieve by the_post_thumbnail() for single image or get_all_thumbnails() for multiple images.

    (More …)

     
  • levin 11:08 pm on February 19, 2011 Permalink | Reply
    Tags: , rman   

    Getting ORA-01180 error during database restoration 

    Why

    Having restore database from rman but got a ORA-01180 error, it happen often because your backup file source directory is different then original backup path.

    Let’s try

    Restore pfile and recreate all necessary directory eg: archive log, admin, a/b/c/udump, datafile… every directory you specified in the pfile

    Copy your backup pfile to %ORACLE_HOME%\dbs\

    sqlplus / as sysdba
    sqlplus> startup nomount pfile='%ORACLE_HOME%\dbs\initORAINST.ora'
    

    Restore controlfile

    rman target /
    RMAN> restore controlfile from 'D:\path\to\controlfile.bak'
    

    Restore database

    RMAN> alter database mount;
    RMAN> restore database;
    

    Then you got a similar message as below, because originally your backup path is somewhere different then your current backup file source path.

    creating datafile fno=1 name=D:\ORADATA\ORAINST\SYSTEM01.DBF
    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure of restore command at 02/18/2011 16:55:06
    ORA-01180: can not create datafile 1
    ORA-01110: data file 1: 'D:\ORADATA\ORAINST\SYSTEM01.DBF'
    

    Try this to re-catalog your backup files then re-run the restore

    rman> catalog start with 'D:\backup\source'
     
  • levin 5:05 pm on January 14, 2011 Permalink | Reply
    Tags:   

    MySQL tuning for dummies 

    Introduction

    There is so many people asking how to improve their mysql instance, and also so many replies that increase key_buffer, sort_buffer, cache size… etc… which believe will lighten up the performance. However, fellows never notice those parameters was actually efficient to their setup.

    So here you are MySqlTuner,
    (More …)

     
    • Singularity 11:30 pm on January 21, 2011 Permalink | Reply

      how do you figure this is for “dummies”?!

    • Florin 3:20 am on January 25, 2011 Permalink | Reply

      “./mysqltuner.pl” didn’t worked for me .
      I made it work with “pearl mysqltuner.pl”

      • levin 3:29 am on January 25, 2011 Permalink | Reply

        try “chmod +x mysqltuner.pl” first, then you can run it without specifying “perl”

  • levin 7:05 pm on January 12, 2011 Permalink | Reply
    Tags: csv, xml   

    Convert XML to CSV using xml2 

    What you need

    • Red Hat Linux 4, 5
    • gcc
    • libxml2
    • libxml2-devel
    • xml2

    Compiling xml2

    1. Extract xml2 tar ball

     tar zxvf ./xml2-0.4.tar.gz

    2. Modify compiling option in “configure” file

    search line between 3428-3470, replace “libxml” with “libxml-2.0″

    for example:

    From

    [bash]
        { (echo "$as_me:$LINENO: \$PKG_CONFIG --exists --print-errors \"libxml\"") >&5
      ($PKG_CONFIG --exists --print-errors "libxml") 2>&5
      ac_status=$?
      echo "$as_me:$LINENO: \$? = $ac_status" >&5
      (exit $ac_status); }; then
      pkg_cv_XML_CFLAGS=`$PKG_CONFIG --cflags "libxml" 2>/dev/null`
    

    To

        { (echo "$as_me:$LINENO: \$PKG_CONFIG --exists --print-errors \"libxml-2.0\"") >&5
      ($PKG_CONFIG --exists --print-errors "libxml-2.0") 2>&5
      ac_status=$?
      echo "$as_me:$LINENO: \$? = $ac_status" >&5
      (exit $ac_status); }; then
      pkg_cv_XML_CFLAGS=`$PKG_CONFIG --cflags "libxml-2.0" 2>/dev/null`
    

    3. Run configure

     ./configure 

    4. Modify the “Makefile”

    Change the “XML_CFALGS” var to
    (More …)

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel