Apache server unstable detection and the related server(mysql, apache) restart automatically

By Chun Kang - Last updated: Monday, March 19, 2012

I am using CentOS with php, and mysql for web service, and sometimes my server is dead by unknown reason so I optimize mysql database and restart web server manually.

Following is the first my /etc/cron.hourly/optimze process for seamless service. But I found some error when following function is called.

# Programmed 2012 by Chun Kang (Super Coder, kurapa@kurapa.com)
# useless log file deletion
rm -rf /var/log/*.2
rm -rf /var/log/*.3
rm -rf /var/log/*.4
rm -rf /var/log/*.5

rm -rf /var/log/httpd/*.2
rm -rf /var/log/httpd/*.3
rm -rf /var/log/httpd/*.4
rm -rf /var/log/httpd/*.5

# server instance restart
/etc/init.d/httpd restart
/etc/init.d/mysqld restart

# database optimization
mysqlcheck --auto-repair -u<id> -p<password> --all-databases

In order to optimize database, you need to change , and for suitable to your server.

So I found another method to remove unnecessary restart for daemon as following.

# Programmed 2012 by Chun Kang (Super Coder, kurapa@kurapa.com)
# useless log file deletion
rm -rf /var/log/*.2
rm -rf /var/log/*.3
rm -rf /var/log/*.4
rm -rf /var/log/*.5

rm -rf /var/log/httpd/*.2
rm -rf /var/log/httpd/*.3
rm -rf /var/log/httpd/*.4
rm -rf /var/log/httpd/*.5

curl http://kurapa.com > /tmp/_foo_com_check.html
RESULT_SIZE=($( stat -c %s /tmp/_foo_com_check.html))

if [ $RESULT_SIZE -gt 1024 ]; then
  # server status is normal so nothing should do
  echo "server_agnt: nothing should do";
else
  # instance restart
  /etc/init.d/mysqld restart
  /etc/init.d/httpd restart

  # database optimization
  mysqlcheck --auto-repair -u<id> -p<password> --all-databases
fi
unlink /tmp/_foo_com_check.html
Filed in PHP • Tags: , , ,

How to optimize MySQL automatically in CentOS Linux

By Chun Kang - Last updated: Tuesday, March 6, 2012

I am using CentOS Linux for service purpose. Main reason to use CentOS is cheaper hosting for private purpose. Actually MySQL requires periodical optimization process in my experiences, and I use /etc/cron.hourly usually for those kind of bath processing.

I made ‘koptimize’ file with permission 0755.

 % vi /etc/cron.hourly/koptimize

Please type as below with database ID, and Password can perform optimization. It may require administrator permission or something similar with that.

# database optimization
mysqlcheck --auto-repair -u(Root ID) -p(Root Password) --all-databases

After storing above file, you need to change file permission for automatic execution as below.

chmod 755 /etc/cron.hourly/koptimize

Eventually koptimize file will be executed hourly.

Filed in SQL • Tags: , , , , , , ,

Word counting source program based on MapReduce framework

By Chun Kang - Last updated: Tuesday, February 28, 2012

The MapReduce framework operates exclusively on <key, value> pairs, that is, the framework views the input to the job as a set of <key, value> paris and produces a set of <key, value> pairs as the output of the job, conceivably of different types.

Below is the simple application that counts the number of occurences of each word in a given input set. In addition, this works with a local-standalone, pseudo-distributed or fully-distributed Hadoop installation.

package org.myorg;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {

  public static class Map extends MapReduceBase implements Mapper {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, OutputCollector output, Reporter reporter) throws IOException {
      String line = value.toString();
      StringTokenizer tokenizer = new StringTokenizer(line);
      while (tokenizer.hasMoreTokens()) {
        word.set(tokenizer.nextToken());
        output.collect(word, one);
      }
    }
  }

  public static class Reduce extends MapReduceBase implements Reducer {
    public void reduce(Text key, Iterator values, OutputCollector output, Reporter reporter) throws IOException {
      int sum = 0;
      while (values.hasNext()) {
        sum += values.next().get();
      }
      output.collect(key, new IntWritable(sum));
    }
  }

  public static void main(String[] args) throws Exception {
    JobConf conf = new JobConf(WordCount.class);
    conf.setJobName("wordcount");

    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(IntWritable.class);

    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);

    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    JobClient.runJob(conf);
  }
}

 

Usage

Assuming HADOO_HOME is the root of the installation and HADOOP_VERSION is the Hadoop version installed, compile WordCount.java and create a jar:

$ mkdir wordcount_classes
$ javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar -d wordcount_classes WordCount.java
$ jar -cvf /usr/joe/wordcount.jar -C wordcount_classes/

Assuming that:

Sample text-files as input:

$ bin/hadoop dfs -ls /usr/joe/wordcount/input/
/usr/joe/wordcount/input/file01
/usr/joe/wordcount/input/file02

$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file01
Hello World Bye World

$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file02
Hello Hadoop Goodbye Hadoop

Run the application:

$ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount /usr/joe/wordcount/input /usr/joe/wordcount/output

Output:

$ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2

Applications can specify a comma separated list of paths which would be present in the current working directory of the task using the option -files. The -libjars option allows applications to add jars to the classpaths of the maps and reduces. The option -archives allows them to pass comma separated list of archives as arguments. These archives are unarchived and a link with name of the archive is created in the current working directory of tasks. More details about the command line options are available at Commands Guide.

Running wordcount example with -libjars, -files and -archives:
hadoop jar hadoop-examples.jar wordcount -files cachefile.txt -libjars mylib.jar -archives myarchive.zip input output Here, myarchive.zip will be placed and unzipped into a directory by the name “myarchive.zip”.

Users can specify a different symbolic name for files and archives passed through -files and -archives option, using #.

For example, hadoop jar hadoop-examples.jar wordcount -files dir1/dict.txt#dict1,dir2/dict.txt#dict2 -archives mytar.tgz#tgzdir input output Here, the files dir1/dict.txt and dir2/dict.txt can be accessed by tasks using the symbolic names dict1 and dict2 respectively. The archive mytar.tgz will be placed and unarchived into a directory by the name “tgzdir”.

Reference
http://hadoop.apache.org/common/docs/current/mapred_tutorial.html#Inputs+and+Outputs

Filed in Java • Tags: , , , , ,

How To Check If Your Printer Is Connected Using C#

By Chun Kang - Last updated: Monday, February 27, 2012

This code will test whether a printer is connected to your system or not. It works with USB and network printers (haven’t tested LPT printers yet).
Some printers (like my HP DeskJet 930c) is detected online even though I press the OFF button (as long as the power adaptor is switched ON). This is true,as the printer is able to switch on itself when the print queue is not empty.

But if I switch off the power adaptor,then the printer cannot switch on itself,and Windows automatically adjusts the printer setting by checking the “Use Printer Offline” in the Printer Control Panel. Same case with network printers. When I plug my network cable,Windows automatically unchecks the “Use Printer Offline”,and when I unplug it,Windows rechecks the “Use Printer Offline”.
This is what my code detects,whether “Use Printer Offline” is actually checked or not.

using System;
using System.Management;
namespace kurapa.com
{
 class PrinterOffline
 {
  [STAThread]
  static void Main(string[] args)
  {
   // Set management scope
   ManagementScope scope = new ManagementScope(@"\root\cimv2");
   scope.Connect();

   // Select Printers from WMI Object Collections
   ManagementObjectSearcher searcher = new
    ManagementObjectSearcher("SELECT * FROM Win32_Printer");

   string printerName = "";
   foreach (ManagementObject printer in searcher.Get())
   {
    printerName = printer["Name"].ToString().ToLower();
    if (printerName.Equals(@"hp deskjet 930c"))
    {
     Console.WriteLine("Printer = "   printer["Name"]);
     if (printer["WorkOffline"].ToString().ToLower().Equals("true"))
     {
      // printer is offline by user
      Console.WriteLine("Your PnP printer is not connected.");
     }
     else
     {
      // printer is not offline
       Console.WriteLine("Your PnP printer is connected.");
     }
    }
   }
  }
 }
}
Filed in C# • Tags: , , , ,

Shake your browser window by Javascript

By Chun Kang - Last updated: Monday, February 27, 2012

Below code is the example can shake your browser window by javascript

<!–-
  This is developed by nepali guys
  for Further information please email us at
  ha.p@mailcity.com
  shivascm@hotmail.com
-–>

<script langauge=”JavaScript”>
function shakewindow()
{
  var x=10
  if (document.all||document.layers)
  {
    for (i=0;i,i<20;i++)
    {
      window.moveBy(0,x)
      window.moveBy(x,0)
      window.moveBy(0,-x)
      window.moveBy(-x,0)
    }
  }
}
</script>
<form>
<input type=”button” onClick=”shakewindow()” value=”Shake Window”>
</form>
Filed in HTML, Javascript • Tags: ,

FNC v1.02 release – The file created date time information is available for those cameras providing incorrect EXIF data

By Chun Kang - Last updated: Monday, October 10, 2011

Today following feature is added for Sony HX5V digial camera user.

- User can set file created date time as file name. This feature is useful to Sony HX5V digital camera user that the camera store inaccurate date time information as EXIF in digital photo file sometimes.

The above feature won’t be necessary for the usual digial camera users, but if you think the date time information is incorrect and the file created date time is much better accurate, please set above red rectangulared area for applying correct information to generate file name.

1407837879.zip

Filed in Software • Tags: , , , , ,

How to set AUTO_INCREMENT ID in mySQL ?

By Chun Kang - Last updated: Friday, July 1, 2011

In case that you need to set AUTO_INCREMENT ID manually, you can set it as below:


ALTER TABLE tbl AUTO_INCREMENT = 1000

Filed in PHP • Tags: ,

How to create private URL on S3/CloudFront with time-limit ?

By Chun Kang - Last updated: Thursday, May 26, 2011

For security reason, many companies may require one time temporary or private URL with time-limit. Some people call this as private or signed URL. Below URL is based on PHP and contains S3 signed URL generation function and CloudFront signed URL generation function.

The implementation method both S3 and CloudFront is totally different. Please refer below.

<?
// below is for generating temporary URL based on AWS S3, and CloudFront.
//
// Programmed 2011 by Kurapa Chunun Kang (kurapa@kurapa.com)

// reference:
//    https://forums.aws.amazon.com/thread.jspa?messageID=112693

function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY, $aws_s3_secret_key, $expire_seconds)
{
 $expires = time()+$expire_seconds;

  // s3 signed url creation
  $string_to_sign = “GET\n\n\n{$expires}\n/”.str_replace(“.s3.amazonaws.com”,”", $bucket).”/$resource”;
 $signature = urlencode(base64_encode((hash_hmac(“sha1″, utf8_encode($string_to_sign), $aws_s3_secret_key, TRUE))));

 $authentication_params = “AWSAccessKeyId=”.$AWS_S3_KEY;
 $authentication_params.= “&Expires={$expires}”;
 $authentication_params.= “&Signature={$signature}”;

  return $link = “http://{$bucket}/{$resource}?{$authentication_params}”;
}

function get_cloudfront_signed_url($cloudfront, $resource, $expires, $key)
{
  // you can get your private key at below url
  //    https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=access-key
  //    it must be stored at server side in order to create signed URL.
  //
  // after log in, change tab to key-pairs for cloudfront url signing

  $priv_key = file_get_contents(‘aws.pem’);
  $pkeyid = openssl_get_privatekey($priv_key);
  $policy_str = ‘{“Statement”:[{"Resource":"'.$resource.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$expires.'}}}]}’;
  $policy_str = trim( preg_replace( ‘/\s+/’, ”, $policy_str ) );
  $res = openssl_sign($policy_str, $signature, $pkeyid, OPENSSL_ALGO_SHA1);
  $signature_base64 = (base64_encode($signature));

  $repl = array(‘+’ => ‘-’,'=’ => ‘_’,'/’ => ‘~’);
  $signature_base64 = strtr($signature_base64,$repl);

  $url = “http://{$cloudfront}/{$resource}?Expires={$expires}&Signature={$signature_base64}&Key-Pair-Id={$key}”;

  return $url;
}

$aws_access_key_id = “your_aws_access_key_id”;
$aws_s3_secret = “your_aws_s3_secret”;
$aws_cloudfront_key_pair_id= “your_aws_cloudfront_pub_key”;

echo “<a href=” . get_s3_signed_url( “blahblah.s3.amazonaws.com”, “test.jpg”, $aws_access_key_id, $aws_s3_secret, 30) . ” target=_blank>s3</a><br>”;
echo “<a href=” . get_cloudfront_signed_url( “blahblah.cloudfront.net”, “test.jpg”, 30, $aws_cloudfront_key_pair_id) . ” target=_blank>cloudfront</a><br>”;

?>

In addition, you need to install open ssl software on your server.

Filed in PHP • Tags: , , , , , , , ,

The Delphi function to get My Videos folder

By Chun Kang - Last updated: Friday, April 29, 2011

In order to get My Videos folder, you need to call SHGetSpecialFolderPath(). But I recommend you to use below wrapper for easy control.

function GetSpecialFolderPath(Folder: Integer; CanCreate: Boolean): string;
// Gets path of special system folders
//
// Call this routine as follows:
// GetSpecialFolderPath (CSIDL_PERSONAL, false)
//        returns folder as result
//
var
   FilePath: array [0..255] of char;

begin
 SHGetSpecialFolderPath(0, @FilePath[0], FOLDER, CanCreate);
 Result := FilePath;
end;

What you want is simply ‘My Videos’ folder, you can get it as below way.

m_directory.Text := GetSpecialFolderPath(CSIDL_MYVIDEO, True);

In case that you need to get other special folders like My Musics, My Pictures, or something like that, below information will be helpful for you.

  CSIDL_DESKTOP                       = $0000; { <desktop> }
  CSIDL_INTERNET                      = $0001; { Internet Explorer (icon on desktop) }
  CSIDL_PROGRAMS                      = $0002; { Start Menu\Programs }
  CSIDL_CONTROLS                      = $0003; { My Computer\Control Panel }
  CSIDL_PRINTERS                      = $0004; { My Computer\Printers }
  CSIDL_PERSONAL                      = $0005; { My Documents.  This is equivalent to CSIDL_MYDOCUMENTS in XP and above }
  CSIDL_FAVORITES                     = $0006; { <user name>\Favorites }
  CSIDL_STARTUP                       = $0007; { Start Menu\Programs\Startup }
  CSIDL_RECENT                        = $0008; { <user name>\Recent }
  CSIDL_SENDTO                        = $0009; { <user name>\SendTo }
  CSIDL_BITBUCKET                     = $000a; { <desktop>\Recycle Bin }
  CSIDL_STARTMENU                     = $000b; { <user name>\Start Menu }
  CSIDL_MYDOCUMENTS                   = $000c; { logical “My Documents” desktop icon }
  CSIDL_MYMUSIC                       = $000d; { “My Music” folder }
  CSIDL_MYVIDEO                       = $000e; { “My Video” folder }
  CSIDL_DESKTOPDIRECTORY              = $0010; { <user name>\Desktop }
  CSIDL_DRIVES                        = $0011; { My Computer }
  CSIDL_NETWORK                       = $0012; { Network Neighborhood (My Network Places) }
  CSIDL_NETHOOD                       = $0013; { <user name>\nethood }
  CSIDL_FONTS                         = $0014; { windows\fonts }
  CSIDL_TEMPLATES                     = $0015;
  CSIDL_COMMON_STARTMENU              = $0016; { All Users\Start Menu }
  CSIDL_COMMON_PROGRAMS               = $0017; { All Users\Start Menu\Programs }
  CSIDL_COMMON_STARTUP                = $0018; { All Users\Startup }
  CSIDL_COMMON_DESKTOPDIRECTORY       = $0019; { All Users\Desktop }
  CSIDL_APPDATA                       = $001a; { <user name>\Application Data }
  CSIDL_PRINTHOOD                     = $001b; { <user name>\PrintHood }
  CSIDL_LOCAL_APPDATA                 = $001c; { <user name>\Local Settings\Application Data (non roaming) }
  CSIDL_ALTSTARTUP                    = $001d; { non localized startup }
  CSIDL_COMMON_ALTSTARTUP             = $001e; { non localized common startup }
  CSIDL_COMMON_FAVORITES              = $001f;
  CSIDL_INTERNET_CACHE                = $0020;
  CSIDL_COOKIES                       = $0021;
  CSIDL_HISTORY                       = $0022;
  CSIDL_COMMON_APPDATA                = $0023; { All Users\Application Data }
  CSIDL_WINDOWS                       = $0024; { GetWindowsDirectory() }
  CSIDL_SYSTEM                        = $0025; { GetSystemDirectory() }
  CSIDL_PROGRAM_FILES                 = $0026; { C:\Program Files }
  CSIDL_MYPICTURES                    = $0027; { C:\Program Files\My Pictures }
  CSIDL_PROFILE                       = $0028; { USERPROFILE }
  CSIDL_SYSTEMX86                     = $0029; { x86 system directory on RISC }
  CSIDL_PROGRAM_FILESX86              = $002a; { x86 C:\Program Files on RISC }
  CSIDL_PROGRAM_FILES_COMMON          = $002b; { C:\Program Files\Common }
  CSIDL_PROGRAM_FILES_COMMONX86       = $002c; { x86 C:\Program Files\Common on RISC }
  CSIDL_COMMON_TEMPLATES              = $002d; { All Users\Templates }
  CSIDL_COMMON_DOCUMENTS              = $002e; { All Users\Documents }
  CSIDL_COMMON_ADMINTOOLS             = $002f; { All Users\Start Menu\Programs\Administrative Tools }
  CSIDL_ADMINTOOLS                    = $0030; { <user name>\Start Menu\Programs\Administrative Tools }
  CSIDL_CONNECTIONS                   = $0031; { Network and Dial-up Connections }
  CSIDL_COMMON_MUSIC                  = $0035; { All Users\My Music }
  CSIDL_COMMON_PICTURES               = $0036; { All Users\My Pictures }
  CSIDL_COMMON_VIDEO                  = $0037; { All Users\My Video }
  CSIDL_RESOURCES                     = $0038; { Resource Directory }
  CSIDL_RESOURCES_LOCALIZED           = $0039; { Localized Resource Directory }
  CSIDL_COMMON_OEM_LINKS              = $003a; { Links to All Users OEM specific apps }
  CSIDL_CDBURN_AREA                   = $003b; { USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning }
  CSIDL_COMPUTERSNEARME               = $003d; { Computers Near Me (computered from Workgroup membership) }
  CSIDL_PROFILES                      = $003e;
Filed in Delphi • Tags: , , , , , , ,

Get Application Data Path on Delphi

By Chun Kang - Last updated: Friday, April 29, 2011

In case of getting application data path on delphi like C:\Documents and Settings\[username]\Application Data\’, you can use following function.

uses SysUtils;

Edit2.Text := GetEnvironmentVariable(‘TEMP’);

Filed in Delphi • Tags: ,