Archive for the ‘ Php ’ Category

Cloudflare updater script

Today I’m going to bring to you a simple Php script to update a DNS entry for a domain that is under Cloudflare; this way Cloudflare can became a sort of Dynamic DNS service like No-IP or DynDNS, with the difference that you can use your own domain/subdomain (for example home.example.org). Unlike other services Cloudflare has a free plan and also a nice set of API that allows you to easily change ip (and other things).

If you need an explanation of what is a Dynamic DNS I’ll leave here the wikipedia page ( https://en.wikipedia.org/wiki/Dynamic_DNS ) but to make things short it’s a way to access a machine (a server, NAS or simply a Raspberry Pi) connected to the Internet without knowing the IP, and this IP changes over time so you can’t just add it do a static DNS entry.

After you created an account on Cloudflare and added you domain (I’m skipping this part because their tutorials are way better 😛 ) the first thing we need to do is to obtain an API key. First step, from the top menu click on the email and click on “My Setting”.

Cattura

 

Then scroll all the way down until you see the API Key section and click on “View API key”.

Cattura2

Finally a popup will show, remember that the API key is like a password so it should never be known by other people (from the previous page you can regenerate another one if needed).Cattura3

Now that we have an API key we can start modifing the script:

  • line 8: you can change the default timezone (here’s a list of supported timezones http://php.net/manual/en/timezones.php)
  • line 11: $apiKey this is the key we just obtained
  • line 12: $myDomain the domain that we want to update (for example: example.com); this domain needs to be configured on Cloudflare panel
  • line 13: $emailAddress the email address of your account
  • line 14: $ddnsAddresses an array that contains the subdomains that needs to be updated with the current ip of the machine that is executing the script (for example home.example.com)
  • line 17: a simple webservice that returns the current ip of the machine without any markup (feel free to use the default one)
  • line 20: the url of the API endpoint, this should not be changed unless Cloudflare changes it
  • line 23: the path of the log where you can find the updates of the IP (needs to be writable so give 777 or other permissions)
  • line 24: the path of the error log (needs to be writable so give 777 or other permissions)

This script needs only Php to be installed on the server/nas/whatever and should be added in crontab to execute every few minutes (for example every five minutes). The script automatically checks if the IP has changed and if there’s no connection so there shouldn’t any problems with API usage.

Feel free to contact me on my email or by posting a comment in the box below if you encounter any problems, I’ll try to respond to all the comments/emails.

Server uptime monitoring using UptimeRobot

Today I’m going to present you a simple script that uses the UptimeRobot APIs; UptimeRobot is an awesome service that, for free, allows you to check the uptime of your servers (max 50) with a check based on a webpage or an open listening port on the server with a minimum delay between the checks of 5 minutes (or higher). It also supports webhooks, basically when one of your server goes offline UptimeRobot makes a call to url. I use this feature to send an SMS to my telephone using a paid SMS Gateway (unsupported by UptimeRobot). For more infomation visit their website: UptimeRobot.com

Anyway, I created this script to present some statistics on the servers monitored such as the uptime in the last days, the response time during the last 24 hours and more important than everything else the current status of the server (Online or not).

The script is configurable, you can:

  • select which server to add: UptimeRobots supports two types of API keys, a private one that allows access to all the monitors inside your account and a public key that allows access to the statistics of a specific monitor; it’s up to you to choose which key to use, but keep in mind that you can’t add the same monitor two times;
  • choose if you want to see the response time of the last 24 hours and how precisely you want to see it, take in mind that by enabling this feature the loading time of the page will be higher (no problems with 4-5 monitors); the precision is indicated in minutes, for example 30 minutes mean that in the graph there will be the response time every 30 minutes, I prefer 120 minutes to avoid spikes in the graph;
  • regarding the uptime statistics, select the period used to calculate the uptime ratio, by default is last day, last week and last month: 1, 7 and 30 days; you can add other values such as ‘1-7-30-60′.

You can see the script in action here Status page. As you can see the script is fully responsive, if it works in every browser at every resolution 😛
I left some comments in the script so you should be able to customize it (like I did to add the munin graphs and Laura Bodewig, the anime girl in the background).

Now to the download links, remember to edit the config file. Feel free to contact me if anything goes wrong.

I also used these awesome libraries:

PHP-Zend 1: create an ImageController

During the development of a website I encountered the following problem: how to load some images located outside the document root into a ZF1 view? Obviously we could move these images into the document root but just to be sure we decided to leave them outside (these images are created with an external script), and in that place it’s impossible to get them with the standard approach ( <img src="relative/path"> ).

Now I found, thanks to a hint from a senior colleague, a way to overcome this problem: create a Zend Framework (we used the version 1.12.3 in this project) controller that has only to load the image and create a response with only the image within. By using a ZF controller you’ll be able to check if the user has the permission to open the image with a few queries and by using the current user session.

Now the only thing to do is to call this controller with a parameter: in the project I used the id of the image because the path is saved into a mysql table, but obviously you can use what you need.

Here comes the self-explanatory snippet of this controller:

 

Now when you want to load this image you only have to write a code like this:

return top