RSSAll Entries in the "Development" Category

Health check script Linux

Bellow I will present script checking Load Average on a Linux server which will send report if it becomes to high.
It is using Linux command ‘uptime’ which is pulling out server’s uptime as well as its Load Average:

In my script bellow I am using not the last minute load average, but the last five minutes (the second) one.
If you are not aware you can interpret a load average of “1.63, 0.70, 7.89” on a single-CPU system as:

– during the last minute, the system was overloaded by 63% on average (1.63 runnable processes, so that 0.73 processes had to wait for a turn for a single CPU system on average).

– during the last 5 minutes, the CPU was idling 30% of the time on average.

– during the last 15 minutes, the system was overloaded 698% on average (7.89 runnable processes, so that 6.98 processes had to wait for a turn for a single CPU system on average).

I have chosen the five minute interval as sending mails every minute is too aggressive in case of a server load. Also it could be something too short and handled by the server without notification.

* Note that some settings may need tuning because of changed/different command output

Once the script is ready you can set it as Cron. job Mine is set to check every 5th minute:

Tags

Timestamp in PHP

In this tutorial I will give you example how to add timestamp to file name in PHP .
This is useful when you create similar files like database backups, regular reports, or whatever you are saving in PHP with timestamp in the file name.

Bellow is an example that creates one CSV file with timestamp in the file name:

The part that sets the timestamp in PHP is:

The file created using this method will be like this: testFile.04-10-2011.csv – certainly with different date.
You can modify the PHP date function and use different formats. For example if you need only the current time for timestamp use:

The file name with this timestamp will be like: testFile.06:18:08.csv

For full timestamp including year-month-day-hour-minute-seconds change the code to:

My file appeared with timestamp: testFile.2011-10-04_06:18:08.csv

Certainly you can use different separators for the timestamp, also there are more options available which can be found at PHP date function page.

Tags

Incorrect integer value WATCHDOG_ERR

This tutorial is related to issue with Drupal after upgrade, module change or module activation. The error is similar to this one:

“PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘WATCHDOG_ERR’ for column ‘severity’ at row 1: INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => The specified file %file could not be copied to %destination. [:db_insert_placeholder_3] => a:2:{s:5:”%file”;s:22:”temporary://filefCLBmE”;s:12:”%destination”;s:56:”public://ctools/css/818a9e4e3a8a0543c79825df2bf73444.css”;} [:db_insert_placeholder_4] => WATCHDOG_ERR [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://www.domain-name.com/ [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => 84.22.6.242 [:db_insert_placeholder_9] => 1317299034 ) in dblog_watchdog() (line 157 of /var/www/vhosts/www.domain-name.com/httpdocs/modules/dblog/dblog.module).”

After some search and investigation the issue appeared to be in a file called ‘file.inc’ located inside ‘includes’ folder. All the rest variables were WATCHDOG_ERROR, but there was one WATCHDOG_ERR which seams to be not declared anywhere.

The output was :

One of the watchdog lines is not like the rest, and instead of WATCHDOG_ERROR is WATCHDOG_ERR:

So use your preferred editor and just change the variable to WATCHDOG_ERROR and save the file.
Then the issue should be resolved.

PHP relocation error

This will be quick tutorial about how to fix PHP relocation error like this one: php: symbol zlibVersion , version libmysqlclient_16 not defined in in file libmysqlclient.so.16 with link time reference. Instead php: relocation error: php: symbol zlibVersion the error could be php: relocation error: php: symbol crc32.

This is an example and the issue could happen with any shared library not only with libmysqlclient.so .

As per the error message the library version is not matching so we have to check the shared library dependencies. This can be done with tool called ‘ldd’ which prints the shared library dependencies. In this case we will check PHP for “libmysqlclient” so the command should be like this:

Or if for some reason the PHP binary is located elsewhere:

The output should be similar to this one:

Let’s check the file:

In my case this is a symbolic link to the library in the same folder:

Now let’s check if this file exist in another place.

My output is:

So there are two libraries, and now we have to check where is the missing symbol – for example zlibVersion. For that will use ‘nm’ command lists symbols from object files. Let’s try the current linked file:

The output is:

So it seams there are no symbols in this library.

With the other file:

The output is:

Which seams better.

Now I will just remote the current link:

And will create a new one with the library that contains the symbol:

This should resolve the case, and the php: symbol zlibVersion , version libmysqlclient_16 not defined in in file libmysqlclient.so.16 with link time reference error was solved.

Tags

Host multiple domains on one hosting – cheating the hosting companies.

I barely remember my first site and how it was created, but I still remember the filling when I saw the price for the webhosting I was searching for. Then I was a student and if I wanted to see my website live, I had to pay one third of my monthly budget. That was before more than ten years and the webhosting was pretty expensive than now.

Nowadays the hosting prices are way to low, but if you are familiar with webhosting and understand the services that some hosting companies provide – you probably know that even price of couple of bugs is too much for what you actually get. I am not going to fail into detailed discussion about hosting companies, plans etc.- so if you are at the point where you do not have much to spend on webhosting, and in the same time you want to start building websites and host them cheep you may use the method described bellow.

Lets consider the following case:
You have bought webhosting which include something like this:
One domain hosted, 160GB drive space, MySQL database, PHP, any-other-marketing-bla-bla . (For Windows hosting users, please read Redirect domain alias to a particular folder in Windows)

Certainly the main point here is that you have pretty much space, PHP and MySQL which allows you to do almost everything, and .. just one domain hosted. Here I have to say that the whole freetutorialssubmit.com website is not more than 600MB and if I decide to host it on such hosting, the rest of the 160GB space will remain unused. You may decide to use the rest of the space as a storage, but in most cases it just remain like that.

With the PHP script bellow you can host multiple domains on one hosting even it the hosting company allows only one domain to be hosted, which will allow you to use the rest of the hosting space for different websites.

Before that I have to mention that all domain you want hosted in this hosting should be set like CNAME for the main domain. The reason is that when a domain is set as a A CNAME record or Canonical Name record in the Domain Name System (DNS) – this specifies that the domain name is an alias of the other domain. This is rather simple and can be done in with the company where the domain is registered for free. (If not try GoDaddy ;)

Once a domain is set as CNAME it will do exactly the same as the first domain. Here comes my script which basically check as which domain the browser request is done and redirects to a particular folder where the according domain files are set. The script should be in the main index.php file, otherwise it will not be opened and will not redirect accordingly.

Ok, it may looks confusing, but what is actually happening is when someone lands on your hosting, his browser opens the main index file. The script checks ‘HTTP_HOST’ global PHP variable which contains actually via which domain the server was reached and then redirects to its index (you can see it in the example as index2.php and index1.php ), or to a particular folder. If there is no match the visitor’s browser will stay in the same file and will continue with the main domain code.

Well that’s it … and yes, it is legal to host multiple domains on one hosting using this way!

301 redirect checker script

This tutorial contains 301 redirect checker script which I have created after spending more than half an hour searching the Internet for something suitable I can use for my needs. While I was searching I found only separate web pages with fields where you can check one – two URLs or heavy applications that can be installed, and yet not covering my needs. Certainly, there were paid 301 redirect checker solutions which can be used to check 301 Permanent Redirect, but I am not sure whether it does worth to buy such. The purpose of this checker in most cases is only to see if a particular pages redirect permanent covering the SEO (Search Engine Optimization) requirements.

In my case part of the OnlineHowTo.net is moving over Free Tutorials Submit dot com, and from SEO perspective, the tutorials moved have to be with 301 redirect to their new URLs.

I decided to write it as Linux bash 301 redirect checker script because it can be run on almost any Linux machine with curl installed and easily added to the Crontab. I believe the script is rather easy to be understood and used, even by not so experienced administrators.

What the redirect script is doing is to gather the URLs you want to check from an external file (in my case: /opt/scripts/301URLs.txt ), where every URL is placed on a separate line like this:

I chose this way as most of the get-all-urls-from-domain applications provide the listing like this, but certainly you can choose (and then modify a little bit my script) different format.
Then using simple bash loop it reads every line one by one, processing it with CURL extracting the headers (curl -I option).
Every header contains information similar to:

Once we have this information we can easily egrep for the 301 Moved Permanently part and if it exist, the 301 redirect checker script continues with the next URL. If the redirect is something different it writes down the URL into the /tmp/301report.txt file.

Finally the script checks whether the /tmp/301report.txt file exist – it will be created only if there is even one different from 301 Moved Permanently redirect – and will send it via e-mail to the address you have set in the EMAIL variable. If there are no such file, the script will end with no report.

Here is the whole script which you can freely use and modify as per your needs:

 

To be honest, I am using a little bit modified 301 redirect checker script, which is also checking whether the redirected URL is the one I need, but it is more complicated and may confuse you if you have to modify it.
Also, more elegant solution will be if you decide to use array, but this redirect checker script fits my needs perfect.
Also I have put sleep command as if the number of the URLs is too big, it may lead to server abuse and blocking the IP you are checking the redirect from. If you do not need the script to wait 5 seconds for every URL, just remove that line.

Hope this scrip will cover your needs as well. Your comments and suggestions are welcome.

Tags

Convert Word document to BBcode

To be honest I will provide you with a macro script for MS Word, which is not written by me, and it was taken from the phpBB community Word to BBcode Macro. This Visual Basic BBCode converter is really useful when you have CMS which is using BBCode, but in the same time does not provide enough options for easy management. Then every text you write, must be gone trough and where you have bold, italic, bolditalic, URL, , must be surrounded with the correct BB code. This is rather annoying especially when you want to mark frequently different keyword for your SEO.

The Word2BBCode-Converter is written on June 2, 2006 by Matthew Kruer,as some parts are adapted from \”Word2Wiki-Converter\”. Major improvements were made by Gunter Schmidt.
The BBCode converter works with MS Word XP,2000 and above. The current license is GPL, which means that you are free to use and modify, but you have to keep the credits and do not sell.

Tags

Error while connect to network services in PHP

Error when you connect to network service is another runtime error in PHP.
When you connect other machines using a network we can imagine how the other hardware fails. Or not just the hardware, there are a lot of devices and services that are not under your control. That is why you really need to carefully check the return value of functions that attempt to interact with a network service.

Let me give one example – if we call a function like that:

Call to undefined function and wrong parameters in function.

In this tutorial we will discuss runtime errors in PHP caused by functions.
If your script call to undefined function or the function does exist, but the number of parameters is wrong you will receive an error.

It is easy to call a function that does not exist.
You can call function that does not exist in the current script but might exist elsewhere. If your code contains a call to a nonexistent function, such as wrong_name_function(); or missing_function(); – you will receive an error message from the parser similar to this:

Fatal error: Call to undefined function: wrong_name_function()
in /home/public_html/file.php on line 3

Also, if you call a function that exists but call it with an incorrect number of parameters,
you will receive a warning.

The function two_strings() requires two strings – for example bottle and size of the bottle ‘“ and you run the function just like this:

Runtime errors in PHP

In a previous tutorial we discussed PHP syntax errors description , and now we will talk about Runtime errors in PHP.

Sometimes Runtime errors are harder to detect. Runtime errors are not caused only by the contents of your script. They can rely on interactions between your scripts and other events or conditions.
For example the next construction: