From 798藝術區, Beijing, long lens camera

Wah, I guess that’s better than my own camera.. I should say picture are really nice, but so complex to use :(

Large objective
Huge lens

From a coffee shop in 798 (Beijing)

Just take a break in 798藝術區, really interesting place, full of artist communities, art expositions,..

Coffee shop 798

Coffee shop 798

From 竹林小餐, Sichuan restaurant in Beijing

Open from 10am to 4am, in Sanlitun Area, this restaurant provide a large and various range of spicy food ! The restaurant is very nice, with an old decoration.. just the music (techno?) isn’t really a good choice.

竹林小餐

竹林小餐

Monitor ping and packet lose with MRTG

The ping and packet lose can be a good indicator for your server, so we will add it to MRTG, to have some very new graphs. First of all, we will create the bash script file. This script will parse the output of ping. You can change the destination of the ping with the ADDR variable. Then just save it (don’t forgot to make it executable with “chmod +x nameoffile“):

#!/bin/sh
ADDR="61.148.60.42"
PING="/bin/ping"
DATA=`$PING -c10 -s500 $ADDR -q `
LOSS=`echo $DATA | awk '{print $18 }' | tr -d %`
echo $LOSS
if [ $LOSS = 100 ];
then echo 0
else
echo $DATA | awk -F/ '{print $5 }'
fi

Second point, we should add the graph to our MRTG configuration file (/etc/mrtg.conf). You can eventualy change the “MaxBytes” option and the “XSize/YSize” to change the size of the graph:

Title[localhost.ping]: RTT to 61.148.60.42 (Beijing, China)
PageTop[localhost.ping]: Round Trip Time

Target[localhost.ping]: `/home/mycado/mrtg_ping.sh`
MaxBytes[localhost.ping]: 2000
Options[localhost.ping]: growright,unknaszero,nopercent,gauge
ShortLegend[localhost.ping]:
LegendI[localhost.ping]: Pkt lose %
LegendO[localhost.ping]: RTT (avg)
YLegend[localhost.ping]: RTT (ms)
XSize[localhost.ping]: 600
YSize[localhost.ping]: 150

Then we just need to recreate the index.html with indexmaker and reload MRTG with:

r34xx:/# indexmaker /etc/mrtg.cfg --columns=1 --output /var/www/mrtg/index.html
r34xx:/#  mrtg

If you have some warning, ignore it, and start MRTG again, after 2 or 3 times it should be fine.

RTT with MRTG

RTT with MRTG

Basic use of crontab

Crontab is a job scheduler based on time, you can for example launch a bash script every 2 hours or everyday at the same hour. We will just need to write one command like “* 20 * * * /home/moi/myScript”.

The first fifth arguments specify when cron should execute the script, and the 6th is the command itself:

  • 1st argument: Minutes
  • 2nd argument: Hours
  • 3rd argument: Day of the month
  • 4th argument: Month
  • 5th argument: Day of the week

For example, this command will be executed every 5 minutes:

5 * * * * /home/test/myscript

Also, this script will be executed every 1st day of the month:

* * 1 * * /home/test/myscript

To add this job to our crontab, it’s easy, just need to use the editor, with the -e argument:

crontab -e

If you want to list your crontab you can use the -l argument, and the -r argument to remove one:

crontab -l
crontab -r

By default, every cron jobs sends an email to the user account. If you don’t want, you just should add the following command at the end of the cron job:

>/dev/null 2>&1

Don’t forget to sometime check your crontab list, don’t keep some useless job ;)

Mid-autumn festival moon cakes (月餅) !

For the little story, the Chinese Emperor should offer sacrifices to the sun in spring and the moon in autumn. The 15th day of the 8th lunar month is the day called “Mid-Autumn”. The night on the 15th of the 8th lunar month is also called “Night of the Moon”. Under the Song Dynasty (420), the day was officially declared for Mid-Autumn Festival (中秋節).

Traditional mooncakes have an imprint on top consisting of the Chinese characters for “longevity” or “harmony” as well as the name of the bakery and the filling in the moon cake.

The Mid-Autumn Festival is held on the 15th day of the eighth month in the Chinese calendar, so this year, it’s the 22 September.

Moon cakes

Moon cakes

Avoid error when inserting data in MySQL with PHP

If you need to insert data from the “outside”, like from an user or from a file, you can have problems with some special characters. The basic example is when an user add a comment to your page. These characters are \x00, \n, \r, \, , and \x1a.

We can have some security issues (SQL injection):

<?php
$user = "admin";
$pass = "' OR ''='";

$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";
mysql_query($query);
?>

So the query will be:

SELECT * FROM users WHERE user='admin' AND pass='' OR ''=''

To avoid that, we have a function named mysql_real_escape_string, which will add a “\” before these special characters:

$user = mysql_real_escape_string("admin");
$pass = mysql_real_escape_string("' OR ''='");

The query will be:

SELECT * FROM users WHERE user='admin' AND pass='\' OR \'\'=\''

And when you need to use these data, just use the stripslashes to un-quote the text:

$var = stripslashes("mum\'s home");

Simple.. ;-)

« Previous PageNext Page »